]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Theme.php
Don't spool relay deliveries
[friendica.git] / src / Core / Theme.php
index f08d48efbc31a5a4f9262f55988fbd906b8c7ee4..9133eae187a4d5505e54e1b439ecf4d981fd118c 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\System;
+
+require_once 'boot.php';
+
+/**
+ * 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' => false,
+            'unsupported' => false
+        ];
+
+        if (file_exists("view/theme/$theme/experimental"))
+            $info['experimental'] = true;
+        if (file_exists("view/theme/$theme/unsupported"))
+            $info['unsupported'] = true;
+
+        if (!is_file("view/theme/$theme/theme.php")) return $info;
+
+        $a = get_app();
+        $stamp1 = microtime(true);
+        $f = file_get_contents("view/theme/$theme/theme.php");
+        $a->save_timestamp($stamp1, "file");
+
+        $r = preg_match("|/\*.*\*/|msU", $f, $m);
+
+        if ($r) {
+            $ll = explode("\n", $m[0]);
+            foreach ( $ll as $l ) {
+                $l = trim($l,"\t\n\r */");
+                if ($l != "") {
+                    list($k, $v) = array_map("trim", explode(":", $l, 2));
+                    $k= strtolower($k);
+                    if ($k == "author") {
+
+                        $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
+                        if ($r) {
+                            $info['author'][] = ['name'=>$m[1], 'link'=>$m[2]];
+                        } else {
+                            $info['author'][] = ['name'=>$v];
+                        }
+                    } elseif ($k == "maintainer") {
+                        $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
+                        if ($r) {
+                            $info['maintainer'][] = ['name'=>$m[1], 'link'=>$m[2]];
+                        } else {
+                            $info['maintainer'][] = ['name'=>$v];
+                        }
+                    } else {
+                        if (array_key_exists($k, $info)) {
+                            $info[$k] = $v;
+                        }
+                    }
+                }
+            }
+        }
+        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("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("Addons: installing theme $theme");
+
+        include_once("view/theme/$theme/theme.php");
+
+        if (function_exists("{$theme}_install")) {
+            $func = "{$theme}_install";
+            $func();
+            return true;
+        } else {
+            logger("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 = current_theme();
+        $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 '';
+    }
+}