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