]> git.mxchange.org Git - friendica.git/blob - src/Core/Theme.php
Move redundant System::baseUrl() to DI::baseUrl() calls
[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\DI;
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(',', str_replace(' ', '', $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 array_unique($allowed_themes);
35         }
36
37         public static function setAllowedList(array $allowed_themes)
38         {
39                 Config::set('system', 'allowed_themes', implode(',', array_unique($allowed_themes)));
40         }
41
42         /**
43          * @brief Parse theme comment in search of theme infos.
44          *
45          * like
46          * \code
47          * ..* Name: My Theme
48          *   * Description: My Cool Theme
49          * . * Version: 1.2.3
50          *   * Author: John <profile url>
51          *   * Maintainer: Jane <profile url>
52          *   *
53          * \endcode
54          * @param string $theme the name of the theme
55          * @return array
56          */
57         public static function getInfo($theme)
58         {
59                 $theme = Strings::sanitizeFilePathItem($theme);
60
61                 $info = [
62                         'name' => $theme,
63                         'description' => "",
64                         'author' => [],
65                         'maintainer' => [],
66                         'version' => "",
67                         'credits' => "",
68                         'experimental' => file_exists("view/theme/$theme/experimental"),
69                         'unsupported' => file_exists("view/theme/$theme/unsupported")
70                 ];
71
72                 if (!is_file("view/theme/$theme/theme.php")) {
73                         return $info;
74                 }
75
76                 $stamp1 = microtime(true);
77                 $theme_file = file_get_contents("view/theme/$theme/theme.php");
78                 DI::profiler()->saveTimestamp($stamp1, "file", System::callstack());
79
80                 $result = preg_match("|/\*.*\*/|msU", $theme_file, $matches);
81
82                 if ($result) {
83                         $comment_lines = explode("\n", $matches[0]);
84                         foreach ($comment_lines as $comment_line) {
85                                 $comment_line = trim($comment_line, "\t\n\r */");
86                                 if ($comment_line != "") {
87                                         list($key, $value) = array_map("trim", explode(":", $comment_line, 2));
88                                         $key = strtolower($key);
89                                         if ($key == "author") {
90                                                 $result = preg_match("|([^<]+)<([^>]+)>|", $value, $matches);
91                                                 if ($result) {
92                                                         $info['author'][] = ['name' => $matches[1], 'link' => $matches[2]];
93                                                 } else {
94                                                         $info['author'][] = ['name' => $value];
95                                                 }
96                                         } elseif ($key == "maintainer") {
97                                                 $result = preg_match("|([^<]+)<([^>]+)>|", $value, $matches);
98                                                 if ($result) {
99                                                         $info['maintainer'][] = ['name' => $matches[1], 'link' => $matches[2]];
100                                                 } else {
101                                                         $info['maintainer'][] = ['name' => $value];
102                                                 }
103                                         } elseif (array_key_exists($key, $info)) {
104                                                 $info[$key] = $value;
105                                         }
106                                 }
107                         }
108                 }
109                 return $info;
110         }
111
112         /**
113          * @brief Returns the theme's screenshot.
114          *
115          * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
116          *
117          * @param string $theme The name of the theme
118          * @return string
119          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
120          */
121         public static function getScreenshot($theme)
122         {
123                 $theme = Strings::sanitizeFilePathItem($theme);
124
125                 $exts = ['.png', '.jpg'];
126                 foreach ($exts as $ext) {
127                         if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
128                                 return DI::baseUrl() . '/view/theme/' . $theme . '/screenshot' . $ext;
129                         }
130                 }
131                 return DI::baseUrl() . '/images/blank.png';
132         }
133
134         public static function uninstall($theme)
135         {
136                 $theme = Strings::sanitizeFilePathItem($theme);
137
138                 // silently fail if theme was removed or if $theme is funky
139                 if (file_exists("view/theme/$theme/theme.php")) {
140                         include_once "view/theme/$theme/theme.php";
141
142                         $func = "{$theme}_uninstall";
143                         if (function_exists($func)) {
144                                 $func();
145                         }
146                 }
147
148                 $allowed_themes = Theme::getAllowedList();
149                 $key = array_search($theme, $allowed_themes);
150                 if ($key !== false) {
151                         unset($allowed_themes[$key]);
152                         Theme::setAllowedList($allowed_themes);
153                 }
154         }
155
156         public static function install($theme)
157         {
158                 $theme = Strings::sanitizeFilePathItem($theme);
159
160                 // silently fail if theme was removed or if $theme is funky
161                 if (!file_exists("view/theme/$theme/theme.php")) {
162                         return false;
163                 }
164
165                 try {
166                         include_once "view/theme/$theme/theme.php";
167
168                         $func = "{$theme}_install";
169                         if (function_exists($func)) {
170                                 $func();
171                         }
172
173                         $allowed_themes = Theme::getAllowedList();
174                         $allowed_themes[] = $theme;
175                         Theme::setAllowedList($allowed_themes);
176
177                         return true;
178                 } catch (\Exception $e) {
179                         Logger::error('Theme installation failed', ['theme' => $theme, 'error' => $e->getMessage()]);
180                         return false;
181                 }
182         }
183
184         /**
185          * @brief Get the full path to relevant theme files by filename
186          *
187          * This function searches in order in the current theme directory, in the current theme parent directory, and lastly
188          * in the base view/ folder.
189          *
190          * @param string $file Filename
191          * @return string Path to the file or empty string if the file isn't found
192          * @throws \Exception
193          */
194         public static function getPathForFile($file)
195         {
196                 $a = DI::app();
197
198                 $theme = $a->getCurrentTheme();
199
200                 $parent = Strings::sanitizeFilePathItem($a->theme_info['extends'] ?? $theme);
201
202                 $paths = [
203                         "view/theme/$theme/$file",
204                         "view/theme/$parent/$file",
205                         "view/$file",
206                 ];
207
208                 foreach ($paths as $path) {
209                         if (file_exists($path)) {
210                                 return $path;
211                         }
212                 }
213
214                 return '';
215         }
216
217         /**
218          * @brief Return relative path to theme stylesheet file
219          *
220          * Provide a sane default if nothing is chosen or the specified theme does not exist.
221          *
222          * @param string $theme Theme name
223          *
224          * @return string
225          */
226         public static function getStylesheetPath($theme)
227         {
228                 $theme = Strings::sanitizeFilePathItem($theme);
229
230                 if (!file_exists('view/theme/' . $theme . '/style.php')) {
231                         return 'view/theme/' . $theme . '/style.css';
232                 }
233
234                 $a = DI::app();
235
236                 $query_params = [];
237
238                 $puid = Profile::getThemeUid($a);
239                 if ($puid) {
240                         $query_params['puid'] = $puid;
241                 }
242
243                 return 'view/theme/' . $theme . '/style.pcss' . (!empty($query_params) ? '?' . http_build_query($query_params) : '');
244         }
245 }