]> git.mxchange.org Git - friendica.git/blob - src/Core/Theme.php
Check the readability before acessing /proc/loadavg
[friendica.git] / src / Core / Theme.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Core;
23
24 use Friendica\DI;
25 use Friendica\Model\Profile;
26 use Friendica\Util\Strings;
27
28 require_once 'boot.php';
29
30 /**
31  * Some functions to handle themes
32  */
33 class Theme
34 {
35         public static function getAllowedList(): array
36         {
37                 $allowed_themes_str = DI::config()->get('system', 'allowed_themes');
38                 $allowed_themes_raw = explode(',', str_replace(' ', '', $allowed_themes_str));
39                 $allowed_themes = [];
40                 if (count($allowed_themes_raw)) {
41                         foreach ($allowed_themes_raw as $theme) {
42                                 $theme = Strings::sanitizeFilePathItem(trim($theme));
43                                 if (strlen($theme) && is_dir("view/theme/$theme")) {
44                                         $allowed_themes[] = $theme;
45                                 }
46                         }
47                 }
48
49                 return array_unique($allowed_themes);
50         }
51
52         public static function setAllowedList(array $allowed_themes)
53         {
54                 DI::config()->set('system', 'allowed_themes', implode(',', array_unique($allowed_themes)));
55         }
56
57         /**
58          * Parse theme comment in search of theme infos.
59          *
60          * like
61          * \code
62          * ..* Name: My Theme
63          *   * Description: My Cool Theme
64          * . * Version: 1.2.3
65          *   * Author: John <profile url>
66          *   * Maintainer: Jane <profile url>
67          *   *
68          * \endcode
69          * @param string $theme the name of the theme
70          * @return array
71          */
72         public static function getInfo(string $theme): array
73         {
74                 $theme = Strings::sanitizeFilePathItem($theme);
75
76                 $info = [
77                         'name' => $theme,
78                         'description' => "",
79                         'author' => [],
80                         'maintainer' => [],
81                         'version' => "",
82                         'credits' => "",
83                         'experimental' => file_exists("view/theme/$theme/experimental"),
84                         'unsupported' => file_exists("view/theme/$theme/unsupported")
85                 ];
86
87                 if (!is_file("view/theme/$theme/theme.php")) {
88                         return $info;
89                 }
90
91                 DI::profiler()->startRecording('file');
92                 $theme_file = file_get_contents("view/theme/$theme/theme.php");
93                 DI::profiler()->stopRecording();
94
95                 $result = preg_match("|/\*.*\*/|msU", $theme_file, $matches);
96
97                 if ($result) {
98                         $comment_lines = explode("\n", $matches[0]);
99                         foreach ($comment_lines as $comment_line) {
100                                 $comment_line = trim($comment_line, "\t\n\r */");
101                                 if (strpos($comment_line, ':') !== false) {
102                                         list($key, $value) = array_map("trim", explode(":", $comment_line, 2));
103                                         $key = strtolower($key);
104                                         if ($key == "author") {
105                                                 $result = preg_match("|([^<]+)<([^>]+)>|", $value, $matches);
106                                                 if ($result) {
107                                                         $info['author'][] = ['name' => $matches[1], 'link' => $matches[2]];
108                                                 } else {
109                                                         $info['author'][] = ['name' => $value];
110                                                 }
111                                         } elseif ($key == "maintainer") {
112                                                 $result = preg_match("|([^<]+)<([^>]+)>|", $value, $matches);
113                                                 if ($result) {
114                                                         $info['maintainer'][] = ['name' => $matches[1], 'link' => $matches[2]];
115                                                 } else {
116                                                         $info['maintainer'][] = ['name' => $value];
117                                                 }
118                                         } elseif (array_key_exists($key, $info)) {
119                                                 $info[$key] = $value;
120                                         }
121                                 }
122                         }
123                 }
124                 return $info;
125         }
126
127         /**
128          * Returns the theme's screenshot.
129          *
130          * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
131          *
132          * @param string $theme The name of the theme
133          * @return string
134          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
135          */
136         public static function getScreenshot(string $theme): string
137         {
138                 $theme = Strings::sanitizeFilePathItem($theme);
139
140                 $exts = ['.png', '.jpg'];
141                 foreach ($exts as $ext) {
142                         if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
143                                 return DI::baseUrl() . '/view/theme/' . $theme . '/screenshot' . $ext;
144                         }
145                 }
146                 return DI::baseUrl() . '/images/blank.png';
147         }
148
149         /**
150          * Uninstalls given theme name
151          *
152          * @param string $theme Name of theme
153          * @return bool true on success
154          */
155         public static function uninstall(string $theme)
156         {
157                 $theme = Strings::sanitizeFilePathItem($theme);
158
159                 // silently fail if theme was removed or if $theme is funky
160                 if (file_exists("view/theme/$theme/theme.php")) {
161                         include_once "view/theme/$theme/theme.php";
162
163                         $func = "{$theme}_uninstall";
164                         if (function_exists($func)) {
165                                 $func();
166                         }
167
168                         Hook::delete(['file' => "view/theme/$theme/theme.php"]);
169                 }
170
171                 $allowed_themes = Theme::getAllowedList();
172                 $key = array_search($theme, $allowed_themes);
173                 if ($key !== false) {
174                         unset($allowed_themes[$key]);
175                         Theme::setAllowedList($allowed_themes);
176                         return true;
177                 }
178                 return false;
179         }
180
181         /**
182          * Installs given theme name
183          *
184          * @param string $theme Name of theme
185          * @return bool true on success
186          */
187         public static function install(string $theme): bool
188         {
189                 $theme = Strings::sanitizeFilePathItem($theme);
190
191                 // silently fail if theme was removed or if $theme is funky
192                 if (!file_exists("view/theme/$theme/theme.php")) {
193                         return false;
194                 }
195
196                 try {
197                         include_once "view/theme/$theme/theme.php";
198
199                         $func = "{$theme}_install";
200                         if (function_exists($func)) {
201                                 $func();
202                         }
203
204                         $allowed_themes = Theme::getAllowedList();
205                         $allowed_themes[] = $theme;
206                         Theme::setAllowedList($allowed_themes);
207
208                         return true;
209                 } catch (\Exception $e) {
210                         Logger::error('Theme installation failed', ['theme' => $theme, 'error' => $e->getMessage()]);
211                         return false;
212                 }
213         }
214
215         /**
216          * Get the full path to relevant theme files by filename
217          *
218          * This function searches in order in the current theme directory, in the current theme parent directory, and lastly
219          * in the base view/ folder.
220          *
221          * @param string $file Filename
222          * @return string Path to the file or empty string if the file isn't found
223          * @throws \Exception
224          */
225         public static function getPathForFile(string $file): string
226         {
227                 $a = DI::app();
228
229                 $theme = $a->getCurrentTheme();
230
231                 $parent = Strings::sanitizeFilePathItem($a->getThemeInfoValue('extends', $theme));
232
233                 $paths = [
234                         "view/theme/$theme/$file",
235                         "view/theme/$parent/$file",
236                         "view/$file",
237                 ];
238
239                 foreach ($paths as $path) {
240                         if (file_exists($path)) {
241                                 return $path;
242                         }
243                 }
244
245                 return '';
246         }
247
248         /**
249          * Return relative path to theme stylesheet file
250          *
251          * Provide a sane default if nothing is chosen or the specified theme does not exist.
252          *
253          * @param string $theme Theme name
254          * @return string
255          */
256         public static function getStylesheetPath(string $theme): string
257         {
258                 $theme = Strings::sanitizeFilePathItem($theme);
259
260                 if (!file_exists('view/theme/' . $theme . '/style.php')) {
261                         return 'view/theme/' . $theme . '/style.css';
262                 }
263
264                 $a = DI::app();
265
266                 $query_params = [];
267
268                 $puid = Profile::getThemeUid($a);
269                 if ($puid) {
270                         $query_params['puid'] = $puid;
271                 }
272
273                 return 'view/theme/' . $theme . '/style.pcss' . (!empty($query_params) ? '?' . http_build_query($query_params) : '');
274         }
275
276         /**
277          * Returns the path of the provided theme
278          *
279          * @param string $theme Theme name
280          * @return string|null
281          */
282         public static function getConfigFile(string $theme)
283         {
284                 $theme = Strings::sanitizeFilePathItem($theme);
285
286                 $a = DI::app();
287                 $base_theme = $a->getThemeInfoValue('extends') ?? '';
288
289                 if (file_exists("view/theme/$theme/config.php")) {
290                         return "view/theme/$theme/config.php";
291                 }
292                 if ($base_theme && file_exists("view/theme/$base_theme/config.php")) {
293                         return "view/theme/$base_theme/config.php";
294                 }
295                 return null;
296         }
297         
298         /**
299          * Returns the background color of the provided theme if available.
300          *
301          * @param string   $theme Theme name
302          * @param int|null $uid   Current logged-in user id
303          * @return string|null
304          */
305         public static function getBackgroundColor(string $theme, int $uid = null)
306         {
307                 $theme = Strings::sanitizeFilePathItem($theme);
308
309                 $return = null;
310
311                 // silently fail if theme was removed or if $theme is funky
312                 if (file_exists("view/theme/$theme/theme.php")) {
313                         include_once "view/theme/$theme/theme.php";
314
315                         $func = "{$theme}_get_background_color";
316                         if (function_exists($func)) {
317                                 $return = $func($uid);
318                         }
319                 }
320
321                 return $return;
322         }
323
324         /**
325          * Returns the theme color of the provided theme if available.
326          *
327          * @param string   $theme
328          * @param int|null $uid   Current logged-in user id
329          * @return string|null
330          */
331         public static function getThemeColor(string $theme, int $uid = null)
332         {
333                 $theme = Strings::sanitizeFilePathItem($theme);
334
335                 $return = null;
336
337                 // silently fail if theme was removed or if $theme is funky
338                 if (file_exists("view/theme/$theme/theme.php")) {
339                         include_once "view/theme/$theme/theme.php";
340
341                         $func = "{$theme}_get_theme_color";
342                         if (function_exists($func)) {
343                                 $return = $func($uid);
344                         }
345                 }
346
347                 return $return;
348         }
349 }