]> git.mxchange.org Git - friendica.git/blob - src/Core/Theme.php
37e673e404fd88a301357584a53a8f0ebed3b081
[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(',', 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 $allowed_themes;
35         }
36
37         public static function setAllowedList(array $allowed_themes)
38         {
39                 Config::set('system', 'allowed_themes', implode(',', $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                         return false;
181                 }
182         }
183
184         /**
185          * @brief Get the full path to relevant theme files by filename
186          *
187          * This function search in the theme directory (and if not present in global theme directory)
188          * if there is a directory with the file extension and  for a file with the given
189          * filename.
190          *
191          * @param string $file Filename
192          * @param string $root Full root path
193          * @return string Path to the file or empty string if the file isn't found
194          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
195          */
196         public static function getPathForFile($file, $root = '')
197         {
198                 $file = basename($file);
199
200                 // Make sure $root ends with a slash / if it's not blank
201                 if ($root !== '' && $root[strlen($root) - 1] !== '/') {
202                         $root = $root . '/';
203                 }
204                 $theme_info = \get_app()->theme_info;
205                 if (is_array($theme_info) && array_key_exists('extends', $theme_info)) {
206                         $parent = $theme_info['extends'];
207                 } else {
208                         $parent = 'NOPATH';
209                 }
210                 $theme = \get_app()->getCurrentTheme();
211                 $parent = Strings::sanitizeFilePathItem($parent);
212                 $ext = substr($file, strrpos($file, '.') + 1);
213                 $paths = [
214                         "{$root}view/theme/$theme/$ext/$file",
215                         "{$root}view/theme/$parent/$ext/$file",
216                         "{$root}view/$ext/$file",
217                 ];
218                 foreach ($paths as $p) {
219                         // strpos() is faster than strstr when checking if one string is in another (http://php.net/manual/en/function.strstr.php)
220                         if (strpos($p, 'NOPATH') !== false) {
221                                 continue;
222                         } elseif (file_exists($p)) {
223                                 return $p;
224                         }
225                 }
226                 return '';
227         }
228
229         /**
230          * @brief Return relative path to theme stylesheet file
231          *
232          * Provide a sane default if nothing is chosen or the specified theme does not exist.
233          *
234          * @param string $theme Theme name
235          *
236          * @return string
237          */
238         public static function getStylesheetPath($theme)
239         {
240                 $theme = Strings::sanitizeFilePathItem($theme);
241
242                 if (!file_exists('view/theme/' . $theme . '/style.php')) {
243                         return 'view/theme/' . $theme . '/style.css';
244                 }
245
246                 $a = BaseObject::getApp();
247
248                 $query_params = [];
249
250                 $puid = Profile::getThemeUid($a);
251                 if ($puid) {
252                         $query_params['puid'] = $puid;
253                 }
254
255                 return 'view/theme/' . $theme . '/style.pcss' . (!empty($query_params) ? '?' . http_build_query($query_params) : '');
256         }
257 }