]> git.mxchange.org Git - friendica.git/blob - src/Core/Theme.php
Merge pull request #6955 from tobiasd/20190331-vier
[friendica.git] / src / Core / Theme.php
1 <?php
2
3 /**
4  * @file src/Core/Theme.php
5  */
6
7 namespace Friendica\Core;
8
9 use Friendica\BaseObject;
10 use Friendica\Model\Profile;
11 use Friendica\Util\Strings;
12
13 require_once 'boot.php';
14
15 /**
16  * Some functions to handle themes
17  */
18 class Theme
19 {
20         public static function getAllowedList()
21         {
22                 $allowed_themes_str = Config::get('system', 'allowed_themes');
23                 $allowed_themes_raw = explode(',', $allowed_themes_str);
24                 $allowed_themes = [];
25                 if (count($allowed_themes_raw)) {
26                         foreach ($allowed_themes_raw as $theme) {
27                                 $theme = Strings::sanitizeFilePathItem(trim($theme));
28                                 if (strlen($theme) && is_dir("view/theme/$theme")) {
29                                         $allowed_themes[] = $theme;
30                                 }
31                         }
32                 }
33
34                 return $allowed_themes;
35         }
36
37         /**
38          * @brief Parse theme comment in search of theme infos.
39          *
40          * like
41          * \code
42          * ..* Name: My Theme
43          *   * Description: My Cool Theme
44          * . * Version: 1.2.3
45          *   * Author: John <profile url>
46          *   * Maintainer: Jane <profile url>
47          *   *
48          * \endcode
49          * @param string $theme the name of the theme
50          * @return array
51          */
52         public static function getInfo($theme)
53         {
54                 $theme = Strings::sanitizeFilePathItem($theme);
55
56                 $info = [
57                         'name' => $theme,
58                         'description' => "",
59                         'author' => [],
60                         'maintainer' => [],
61                         'version' => "",
62                         'credits' => "",
63                         'experimental' => file_exists("view/theme/$theme/experimental"),
64                         'unsupported' => file_exists("view/theme/$theme/unsupported")
65                 ];
66
67                 if (!is_file("view/theme/$theme/theme.php")) {
68                         return $info;
69                 }
70
71                 $a = \get_app();
72                 $stamp1 = microtime(true);
73                 $theme_file = file_get_contents("view/theme/$theme/theme.php");
74                 $a->getProfiler()->saveTimestamp($stamp1, "file", System::callstack());
75
76                 $result = preg_match("|/\*.*\*/|msU", $theme_file, $matches);
77
78                 if ($result) {
79                         $comment_lines = explode("\n", $matches[0]);
80                         foreach ($comment_lines as $comment_line) {
81                                 $comment_line = trim($comment_line, "\t\n\r */");
82                                 if ($comment_line != "") {
83                                         list($key, $value) = array_map("trim", explode(":", $comment_line, 2));
84                                         $key = strtolower($key);
85                                         if ($key == "author") {
86                                                 $result = preg_match("|([^<]+)<([^>]+)>|", $value, $matches);
87                                                 if ($result) {
88                                                         $info['author'][] = ['name' => $matches[1], 'link' => $matches[2]];
89                                                 } else {
90                                                         $info['author'][] = ['name' => $value];
91                                                 }
92                                         } elseif ($key == "maintainer") {
93                                                 $result = preg_match("|([^<]+)<([^>]+)>|", $value, $matches);
94                                                 if ($result) {
95                                                         $info['maintainer'][] = ['name' => $matches[1], 'link' => $matches[2]];
96                                                 } else {
97                                                         $info['maintainer'][] = ['name' => $value];
98                                                 }
99                                         } elseif (array_key_exists($key, $info)) {
100                                                 $info[$key] = $value;
101                                         }
102                                 }
103                         }
104                 }
105                 return $info;
106         }
107
108         /**
109          * @brief Returns the theme's screenshot.
110          *
111          * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
112          *
113          * @param string $theme The name of the theme
114          * @return string
115          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
116          */
117         public static function getScreenshot($theme)
118         {
119                 $theme = Strings::sanitizeFilePathItem($theme);
120
121                 $exts = ['.png', '.jpg'];
122                 foreach ($exts as $ext) {
123                         if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
124                                 return System::baseUrl() . '/view/theme/' . $theme . '/screenshot' . $ext;
125                         }
126                 }
127                 return System::baseUrl() . '/images/blank.png';
128         }
129
130         public static function uninstall($theme)
131         {
132                 $theme = Strings::sanitizeFilePathItem($theme);
133
134                 // silently fail if theme was removed or if $theme is funky
135                 if (file_exists("view/theme/$theme/theme.php")) {
136                         Logger::log("Addons: uninstalling theme " . $theme);
137
138                         if (function_exists("{$theme}_uninstall")) {
139                                 $func = "{$theme}_uninstall";
140                                 $func();
141                         }
142                 }
143         }
144
145         public static function install($theme)
146         {
147                 $theme = Strings::sanitizeFilePathItem($theme);
148
149                 // silently fail if theme was removed or if $theme is funky
150                 if (!file_exists("view/theme/$theme/theme.php")) {
151                         return false;
152                 }
153
154                 Logger::log("Addons: installing theme $theme");
155
156                 include_once "view/theme/$theme/theme.php";
157
158                 if (function_exists("{$theme}_install")) {
159                         $func = "{$theme}_install";
160                         $func();
161                         return true;
162                 } else {
163                         Logger::log("Addons: FAILED installing theme $theme");
164                         return false;
165                 }
166         }
167
168         /**
169          * @brief Get the full path to relevant theme files by filename
170          *
171          * This function search in the theme directory (and if not present in global theme directory)
172          * if there is a directory with the file extension and  for a file with the given
173          * filename.
174          *
175          * @param string $file Filename
176          * @param string $root Full root path
177          * @return string Path to the file or empty string if the file isn't found
178          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
179          */
180         public static function getPathForFile($file, $root = '')
181         {
182                 $file = basename($file);
183
184                 // Make sure $root ends with a slash / if it's not blank
185                 if ($root !== '' && $root[strlen($root) - 1] !== '/') {
186                         $root = $root . '/';
187                 }
188                 $theme_info = \get_app()->theme_info;
189                 if (is_array($theme_info) && array_key_exists('extends', $theme_info)) {
190                         $parent = $theme_info['extends'];
191                 } else {
192                         $parent = 'NOPATH';
193                 }
194                 $theme = \get_app()->getCurrentTheme();
195                 $parent = Strings::sanitizeFilePathItem($parent);
196                 $ext = substr($file, strrpos($file, '.') + 1);
197                 $paths = [
198                         "{$root}view/theme/$theme/$ext/$file",
199                         "{$root}view/theme/$parent/$ext/$file",
200                         "{$root}view/$ext/$file",
201                 ];
202                 foreach ($paths as $p) {
203                         // strpos() is faster than strstr when checking if one string is in another (http://php.net/manual/en/function.strstr.php)
204                         if (strpos($p, 'NOPATH') !== false) {
205                                 continue;
206                         } elseif (file_exists($p)) {
207                                 return $p;
208                         }
209                 }
210                 return '';
211         }
212
213         /**
214          * @brief Return relative path to theme stylesheet file
215          *
216          * Provide a sane default if nothing is chosen or the specified theme does not exist.
217          *
218          * @param string $theme Theme name
219          *
220          * @return string
221          */
222         public static function getStylesheetPath($theme)
223         {
224                 $theme = Strings::sanitizeFilePathItem($theme);
225
226                 if (!file_exists('view/theme/' . $theme . '/style.php')) {
227                         return 'view/theme/' . $theme . '/style.css';
228                 }
229
230                 $a = BaseObject::getApp();
231
232                 $query_params = [];
233
234                 $puid = Profile::getThemeUid($a);
235                 if ($puid) {
236                         $query_params['puid'] = $puid;
237                 }
238
239                 return 'view/theme/' . $theme . '/style.pcss' . (!empty($query_params) ? '?' . http_build_query($query_params) : '');
240         }
241 }