]> git.mxchange.org Git - friendica.git/blob - src/Core/Theme.php
Review update
[friendica.git] / src / Core / Theme.php
1 <?php\r
2 /**\r
3  * @file src/Core/Theme.php\r
4  */\r
5 namespace Friendica\Core;\r
6 \r
7 use Friendica\Core\System;\r
8 \r
9 require_once 'boot.php';\r
10 \r
11 /**\r
12  * Some functions to handle themes\r
13  */\r
14 class Theme\r
15 {\r
16     /**\r
17      * @brief Parse theme comment in search of theme infos.\r
18      *\r
19      * like\r
20      * \code\r
21      * ..* Name: My Theme\r
22      *   * Description: My Cool Theme\r
23      * . * Version: 1.2.3\r
24      *   * Author: John <profile url>\r
25      *   * Maintainer: Jane <profile url>\r
26      *   *\r
27      * \endcode\r
28      * @param string $theme the name of the theme\r
29      * @return array\r
30      */\r
31 \r
32     public static function getInfo($theme)\r
33     {\r
34         $info=[\r
35             'name' => $theme,\r
36             'description' => "",\r
37             'author' => [],\r
38             'maintainer' => [],\r
39             'version' => "",\r
40             'credits' => "",\r
41             'experimental' => false,\r
42             'unsupported' => false\r
43         ];\r
44 \r
45         if (file_exists("view/theme/$theme/experimental"))\r
46             $info['experimental'] = true;\r
47         if (file_exists("view/theme/$theme/unsupported"))\r
48             $info['unsupported'] = true;\r
49 \r
50         if (!is_file("view/theme/$theme/theme.php")) return $info;\r
51 \r
52         $a = get_app();\r
53         $stamp1 = microtime(true);\r
54         $f = file_get_contents("view/theme/$theme/theme.php");\r
55         $a->save_timestamp($stamp1, "file");\r
56 \r
57         $r = preg_match("|/\*.*\*/|msU", $f, $m);\r
58 \r
59         if ($r) {\r
60             $ll = explode("\n", $m[0]);\r
61             foreach ( $ll as $l ) {\r
62                 $l = trim($l,"\t\n\r */");\r
63                 if ($l != "") {\r
64                     list($k, $v) = array_map("trim", explode(":", $l, 2));\r
65                     $k= strtolower($k);\r
66                     if ($k == "author") {\r
67 \r
68                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);\r
69                         if ($r) {\r
70                             $info['author'][] = ['name'=>$m[1], 'link'=>$m[2]];\r
71                         } else {\r
72                             $info['author'][] = ['name'=>$v];\r
73                         }\r
74                     } elseif ($k == "maintainer") {\r
75                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);\r
76                         if ($r) {\r
77                             $info['maintainer'][] = ['name'=>$m[1], 'link'=>$m[2]];\r
78                         } else {\r
79                             $info['maintainer'][] = ['name'=>$v];\r
80                         }\r
81                     } else {\r
82                         if (array_key_exists($k, $info)) {\r
83                             $info[$k] = $v;\r
84                         }\r
85                     }\r
86                 }\r
87             }\r
88         }\r
89         return $info;\r
90     }\r
91 \r
92     /**\r
93      * @brief Returns the theme's screenshot.\r
94      *\r
95      * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].\r
96      *\r
97      * @param sring $theme The name of the theme\r
98      * @return string\r
99      */\r
100     public static function getScreenshot($theme)\r
101     {\r
102         $exts = ['.png','.jpg'];\r
103         foreach ($exts as $ext) {\r
104             if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {\r
105                 return(System::baseUrl() . '/view/theme/' . $theme . '/screenshot' . $ext);\r
106             }\r
107         }\r
108         return(System::baseUrl() . '/images/blank.png');\r
109     }\r
110 \r
111     // install and uninstall theme\r
112     public static function uninstall($theme)\r
113     {\r
114         logger("Addons: uninstalling theme " . $theme);\r
115 \r
116         include_once("view/theme/$theme/theme.php");\r
117         if (function_exists("{$theme}_uninstall")) {\r
118             $func = "{$theme}_uninstall";\r
119             $func();\r
120         }\r
121     }\r
122 \r
123     public static function install($theme)\r
124     {\r
125         // silently fail if theme was removed\r
126 \r
127         if (! file_exists("view/theme/$theme/theme.php")) {\r
128             return false;\r
129         }\r
130 \r
131         logger("Addons: installing theme $theme");\r
132 \r
133         include_once("view/theme/$theme/theme.php");\r
134 \r
135         if (function_exists("{$theme}_install")) {\r
136             $func = "{$theme}_install";\r
137             $func();\r
138             return true;\r
139         } else {\r
140             logger("Addons: FAILED installing theme $theme");\r
141             return false;\r
142         }\r
143 \r
144     }\r
145 \r
146     /**\r
147      * @brief Get the full path to relevant theme files by filename\r
148      *\r
149      * This function search in the theme directory (and if not present in global theme directory)\r
150      * if there is a directory with the file extension and  for a file with the given\r
151      * filename.\r
152      *\r
153      * @param string $file Filename\r
154      * @param string $root Full root path\r
155      * @return string Path to the file or empty string if the file isn't found\r
156      */\r
157     public static function getPathForFile($file, $root = '')\r
158     {\r
159         $file = basename($file);\r
160 \r
161         // Make sure $root ends with a slash / if it's not blank\r
162         if ($root !== '' && $root[strlen($root)-1] !== '/') {\r
163             $root = $root . '/';\r
164         }\r
165         $theme_info = get_app()->theme_info;\r
166         if (is_array($theme_info) && array_key_exists('extends',$theme_info)) {\r
167             $parent = $theme_info['extends'];\r
168         } else {\r
169             $parent = 'NOPATH';\r
170         }\r
171         $theme = current_theme();\r
172         $thname = $theme;\r
173         $ext = substr($file,strrpos($file,'.')+1);\r
174         $paths = [\r
175             "{$root}view/theme/$thname/$ext/$file",\r
176             "{$root}view/theme/$parent/$ext/$file",\r
177             "{$root}view/$ext/$file",\r
178         ];\r
179         foreach ($paths as $p) {\r
180             // strpos() is faster than strstr when checking if one string is in another (http://php.net/manual/en/function.strstr.php)\r
181             if (strpos($p,'NOPATH') !== false) {\r
182                 continue;\r
183             } elseif (file_exists($p)) {\r
184                 return $p;\r
185             }\r
186         }\r
187         return '';\r
188     }\r
189 }\r