]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Theme.php
Merge pull request #6381 from MrPetovan/bug/2800-fix-accordion-ios
[friendica.git] / src / Core / Theme.php
index f08d48efbc31a5a4f9262f55988fbd906b8c7ee4..1524c29bacfe2a8cd878f51ad01e2944d88839d5 100644 (file)
-<?php\r
-/**\r
- * @file src/Core/Theme.php\r
- */\r
-namespace Friendica\Core;\r
-\r
-use Friendica\App;\r
-use Friendica\Core\Config;\r
-use Friendica\Core\System;\r
-use Friendica\Database\DBM;\r
-\r
-/**\r
- * Some functions to handle themes\r
- */\r
-class Theme\r
-{\r
-    /**\r
-     * @brief Parse theme comment in search of theme infos.\r
-     *\r
-     * like\r
-     * \code\r
-     * ..* Name: My Theme\r
-     *   * Description: My Cool Theme\r
-     * . * Version: 1.2.3\r
-     *   * Author: John <profile url>\r
-     *   * Maintainer: Jane <profile url>\r
-     *   *\r
-     * \endcode\r
-     * @param string $theme the name of the theme\r
-     * @return array\r
-     */\r
-\r
-    function get_theme_info($theme) {\r
-        $info=[\r
-            'name' => $theme,\r
-            'description' => "",\r
-            'author' => [],\r
-            'maintainer' => [],\r
-            'version' => "",\r
-            'credits' => "",\r
-            'experimental' => false,\r
-            'unsupported' => false\r
-        ];\r
-\r
-        if (file_exists("view/theme/$theme/experimental"))\r
-            $info['experimental'] = true;\r
-        if (file_exists("view/theme/$theme/unsupported"))\r
-            $info['unsupported'] = true;\r
-\r
-        if (!is_file("view/theme/$theme/theme.php")) return $info;\r
-\r
-        $a = get_app();\r
-        $stamp1 = microtime(true);\r
-        $f = file_get_contents("view/theme/$theme/theme.php");\r
-        $a->save_timestamp($stamp1, "file");\r
-\r
-        $r = preg_match("|/\*.*\*/|msU", $f, $m);\r
-\r
-        if ($r) {\r
-            $ll = explode("\n", $m[0]);\r
-            foreach ( $ll as $l ) {\r
-                $l = trim($l,"\t\n\r */");\r
-                if ($l != "") {\r
-                    list($k,$v) = array_map("trim", explode(":",$l,2));\r
-                    $k= strtolower($k);\r
-                    if ($k == "author") {\r
-\r
-                        $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);\r
-                        if ($r) {\r
-                            $info['author'][] = ['name'=>$m[1], 'link'=>$m[2]];\r
-                        } else {\r
-                            $info['author'][] = ['name'=>$v];\r
-                        }\r
-                    } elseif ($k == "maintainer") {\r
-                        $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);\r
-                        if ($r) {\r
-                            $info['maintainer'][] = ['name'=>$m[1], 'link'=>$m[2]];\r
-                        } else {\r
-                            $info['maintainer'][] = ['name'=>$v];\r
-                        }\r
-                    } else {\r
-                        if (array_key_exists($k,$info)) {\r
-                            $info[$k]=$v;\r
-                        }\r
-                    }\r
-\r
-                }\r
-            }\r
-\r
-        }\r
-        return $info;\r
-    }\r
-\r
-    /**\r
-     * @brief Returns the theme's screenshot.\r
-     *\r
-     * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].\r
-     *\r
-     * @param sring $theme The name of the theme\r
-     * @return string\r
-     */\r
-    function get_theme_screenshot($theme) {\r
-        $exts = ['.png','.jpg'];\r
-        foreach ($exts as $ext) {\r
-            if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {\r
-                return(System::baseUrl() . '/view/theme/' . $theme . '/screenshot' . $ext);\r
-            }\r
-        }\r
-        return(System::baseUrl() . '/images/blank.png');\r
-    }\r
-\r
-    // install and uninstall theme\r
-    function uninstall_theme($theme) {\r
-        logger("Addons: uninstalling theme " . $theme);\r
-\r
-        include_once("view/theme/$theme/theme.php");\r
-        if (function_exists("{$theme}_uninstall")) {\r
-            $func = "{$theme}_uninstall";\r
-            $func();\r
-        }\r
-    }\r
-\r
-    function install_theme($theme) {\r
-        // silently fail if theme was removed\r
-\r
-        if (! file_exists("view/theme/$theme/theme.php")) {\r
-            return false;\r
-        }\r
-\r
-        logger("Addons: installing theme $theme");\r
-\r
-        include_once("view/theme/$theme/theme.php");\r
-\r
-        if (function_exists("{$theme}_install")) {\r
-            $func = "{$theme}_install";\r
-            $func();\r
-            return true;\r
-        } else {\r
-            logger("Addons: FAILED installing theme $theme");\r
-            return false;\r
-        }\r
-\r
-    }\r
-\r
-    /**\r
-     * @brief Get the full path to relevant theme files by filename\r
-     *\r
-     * This function search in the theme directory (and if not present in global theme directory)\r
-     * if there is a directory with the file extension and  for a file with the given\r
-     * filename.\r
-     *\r
-     * @param string $file Filename\r
-     * @param string $root Full root path\r
-     * @return string Path to the file or empty string if the file isn't found\r
-     */\r
-    function theme_include($file, $root = '') {\r
-        $file = basename($file);\r
-\r
-        // Make sure $root ends with a slash / if it's not blank\r
-        if ($root !== '' && $root[strlen($root)-1] !== '/') {\r
-            $root = $root . '/';\r
-        }\r
-        $theme_info = get_app()->theme_info;\r
-        if (is_array($theme_info) && array_key_exists('extends',$theme_info)) {\r
-            $parent = $theme_info['extends'];\r
-        } else {\r
-            $parent = 'NOPATH';\r
-        }\r
-        $theme = current_theme();\r
-        $thname = $theme;\r
-        $ext = substr($file,strrpos($file,'.')+1);\r
-        $paths = [\r
-            "{$root}view/theme/$thname/$ext/$file",\r
-            "{$root}view/theme/$parent/$ext/$file",\r
-            "{$root}view/$ext/$file",\r
-        ];\r
-        foreach ($paths as $p) {\r
-            // strpos() is faster than strstr when checking if one string is in another (http://php.net/manual/en/function.strstr.php)\r
-            if (strpos($p,'NOPATH') !== false) {\r
-                continue;\r
-            } elseif (file_exists($p)) {\r
-                return $p;\r
-            }\r
-        }\r
-        return '';\r
-    }\r
-}\r
+<?php
+
+/**
+ * @file src/Core/Theme.php
+ */
+
+namespace Friendica\Core;
+
+use Friendica\Core\Logger;
+use Friendica\Core\System;
+
+/**
+ * Some functions to handle themes
+ */
+class Theme
+{
+       /**
+        * @brief Parse theme comment in search of theme infos.
+        *
+        * like
+        * \code
+        * ..* Name: My Theme
+        *   * Description: My Cool Theme
+        * . * Version: 1.2.3
+        *   * Author: John <profile url>
+        *   * Maintainer: Jane <profile url>
+        *   *
+        * \endcode
+        * @param string $theme the name of the theme
+        * @return array
+        */
+       public static function getInfo($theme)
+       {
+               $info = [
+                       'name' => $theme,
+                       'description' => "",
+                       'author' => [],
+                       'maintainer' => [],
+                       'version' => "",
+                       'credits' => "",
+                       'experimental' => file_exists("view/theme/$theme/experimental"),
+                       'unsupported' => file_exists("view/theme/$theme/unsupported")
+               ];
+
+               if (!is_file("view/theme/$theme/theme.php")) {
+                       return $info;
+               }
+
+               $a = get_app();
+               $stamp1 = microtime(true);
+               $theme_file = file_get_contents("view/theme/$theme/theme.php");
+               $a->saveTimestamp($stamp1, "file");
+
+               $result = preg_match("|/\*.*\*/|msU", $theme_file, $matches);
+
+               if ($result) {
+                       $comment_lines = explode("\n", $matches[0]);
+                       foreach ($comment_lines as $comment_line) {
+                               $comment_line = trim($comment_line, "\t\n\r */");
+                               if ($comment_line != "") {
+                                       list($key, $value) = array_map("trim", explode(":", $comment_line, 2));
+                                       $key = strtolower($key);
+                                       if ($key == "author") {
+                                               $result = preg_match("|([^<]+)<([^>]+)>|", $value, $matches);
+                                               if ($result) {
+                                                       $info['author'][] = ['name' => $matches[1], 'link' => $matches[2]];
+                                               } else {
+                                                       $info['author'][] = ['name' => $value];
+                                               }
+                                       } elseif ($key == "maintainer") {
+                                               $result = preg_match("|([^<]+)<([^>]+)>|", $value, $matches);
+                                               if ($result) {
+                                                       $info['maintainer'][] = ['name' => $matches[1], 'link' => $matches[2]];
+                                               } else {
+                                                       $info['maintainer'][] = ['name' => $value];
+                                               }
+                                       } elseif (array_key_exists($key, $info)) {
+                                               $info[$key] = $value;
+                                       }
+                               }
+                       }
+               }
+               return $info;
+       }
+
+       /**
+        * @brief Returns the theme's screenshot.
+        *
+        * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
+        *
+        * @param sring $theme The name of the theme
+        * @return string
+        */
+       public static function getScreenshot($theme)
+       {
+               $exts = ['.png', '.jpg'];
+               foreach ($exts as $ext) {
+                       if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
+                               return(System::baseUrl() . '/view/theme/' . $theme . '/screenshot' . $ext);
+                       }
+               }
+               return(System::baseUrl() . '/images/blank.png');
+       }
+
+       // install and uninstall theme
+       public static function uninstall($theme)
+       {
+               Logger::log("Addons: uninstalling theme " . $theme);
+
+               include_once "view/theme/$theme/theme.php";
+               if (function_exists("{$theme}_uninstall")) {
+                       $func = "{$theme}_uninstall";
+                       $func();
+               }
+       }
+
+       public static function install($theme)
+       {
+               // silently fail if theme was removed
+
+               if (!file_exists("view/theme/$theme/theme.php")) {
+                       return false;
+               }
+
+               Logger::log("Addons: installing theme $theme");
+
+               include_once "view/theme/$theme/theme.php";
+
+               if (function_exists("{$theme}_install")) {
+                       $func = "{$theme}_install";
+                       $func();
+                       return true;
+               } else {
+                       Logger::log("Addons: FAILED installing theme $theme");
+                       return false;
+               }
+       }
+
+       /**
+        * @brief Get the full path to relevant theme files by filename
+        *
+        * This function search in the theme directory (and if not present in global theme directory)
+        * if there is a directory with the file extension and  for a file with the given
+        * filename.
+        *
+        * @param string $file Filename
+        * @param string $root Full root path
+        * @return string Path to the file or empty string if the file isn't found
+        */
+       public static function getPathForFile($file, $root = '')
+       {
+               $file = basename($file);
+
+               // Make sure $root ends with a slash / if it's not blank
+               if ($root !== '' && $root[strlen($root) - 1] !== '/') {
+                       $root = $root . '/';
+               }
+               $theme_info = get_app()->theme_info;
+               if (is_array($theme_info) && array_key_exists('extends', $theme_info)) {
+                       $parent = $theme_info['extends'];
+               } else {
+                       $parent = 'NOPATH';
+               }
+               $theme = get_app()->getCurrentTheme();
+               $thname = $theme;
+               $ext = substr($file, strrpos($file, '.') + 1);
+               $paths = [
+                       "{$root}view/theme/$thname/$ext/$file",
+                       "{$root}view/theme/$parent/$ext/$file",
+                       "{$root}view/$ext/$file",
+               ];
+               foreach ($paths as $p) {
+                       // strpos() is faster than strstr when checking if one string is in another (http://php.net/manual/en/function.strstr.php)
+                       if (strpos($p, 'NOPATH') !== false) {
+                               continue;
+                       } elseif (file_exists($p)) {
+                               return $p;
+                       }
+               }
+               return '';
+       }
+
+       /**
+        * @brief Return relative path to theme stylesheet file
+        *
+        * Provide a sane default if nothing is chosen or the specified theme does not exist.
+        *
+        * @param string $theme Theme name
+        *
+        * @return string
+        */
+       public static function getStylesheetPath($theme)
+       {
+               $a = get_app();
+
+               $opts = (($a->profile_uid) ? '?f=&puid=' . $a->profile_uid : '');
+               if (file_exists('view/theme/' . $theme . '/style.php')) {
+                       return 'view/theme/' . $theme . '/style.pcss' . $opts;
+               }
+
+               return 'view/theme/' . $theme . '/style.css';
+       }
+}