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