Add time parameter for iOS Safari to stylesheet URL
[friendica.git/.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\Core\Logger;
11 use Friendica\Core\System;
12
13 /**
14  * Some functions to handle themes
15  */
16 class Theme
17 {
18         /**
19          * @brief Parse theme comment in search of theme infos.
20          *
21          * like
22          * \code
23          * ..* Name: My Theme
24          *   * Description: My Cool Theme
25          * . * Version: 1.2.3
26          *   * Author: John <profile url>
27          *   * Maintainer: Jane <profile url>
28          *   *
29          * \endcode
30          * @param string $theme the name of the theme
31          * @return array
32          */
33         public static function getInfo($theme)
34         {
35                 $info = [
36                         'name' => $theme,
37                         'description' => "",
38                         'author' => [],
39                         'maintainer' => [],
40                         'version' => "",
41                         'credits' => "",
42                         'experimental' => file_exists("view/theme/$theme/experimental"),
43                         'unsupported' => file_exists("view/theme/$theme/unsupported")
44                 ];
45
46                 if (!is_file("view/theme/$theme/theme.php")) {
47                         return $info;
48                 }
49
50                 $a = \get_app();
51                 $stamp1 = microtime(true);
52                 $theme_file = file_get_contents("view/theme/$theme/theme.php");
53                 $a->saveTimestamp($stamp1, "file");
54
55                 $result = preg_match("|/\*.*\*/|msU", $theme_file, $matches);
56
57                 if ($result) {
58                         $comment_lines = explode("\n", $matches[0]);
59                         foreach ($comment_lines as $comment_line) {
60                                 $comment_line = trim($comment_line, "\t\n\r */");
61                                 if ($comment_line != "") {
62                                         list($key, $value) = array_map("trim", explode(":", $comment_line, 2));
63                                         $key = strtolower($key);
64                                         if ($key == "author") {
65                                                 $result = preg_match("|([^<]+)<([^>]+)>|", $value, $matches);
66                                                 if ($result) {
67                                                         $info['author'][] = ['name' => $matches[1], 'link' => $matches[2]];
68                                                 } else {
69                                                         $info['author'][] = ['name' => $value];
70                                                 }
71                                         } elseif ($key == "maintainer") {
72                                                 $result = preg_match("|([^<]+)<([^>]+)>|", $value, $matches);
73                                                 if ($result) {
74                                                         $info['maintainer'][] = ['name' => $matches[1], 'link' => $matches[2]];
75                                                 } else {
76                                                         $info['maintainer'][] = ['name' => $value];
77                                                 }
78                                         } elseif (array_key_exists($key, $info)) {
79                                                 $info[$key] = $value;
80                                         }
81                                 }
82                         }
83                 }
84                 return $info;
85         }
86
87         /**
88          * @brief Returns the theme's screenshot.
89          *
90          * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
91          *
92          * @param sring $theme The name of the theme
93          * @return string
94          */
95         public static function getScreenshot($theme)
96         {
97                 $exts = ['.png', '.jpg'];
98                 foreach ($exts as $ext) {
99                         if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
100                                 return(System::baseUrl() . '/view/theme/' . $theme . '/screenshot' . $ext);
101                         }
102                 }
103                 return(System::baseUrl() . '/images/blank.png');
104         }
105
106         // install and uninstall theme
107         public static function uninstall($theme)
108         {
109                 Logger::log("Addons: uninstalling theme " . $theme);
110
111                 include_once "view/theme/$theme/theme.php";
112                 if (function_exists("{$theme}_uninstall")) {
113                         $func = "{$theme}_uninstall";
114                         $func();
115                 }
116         }
117
118         public static function install($theme)
119         {
120                 // silently fail if theme was removed
121
122                 if (!file_exists("view/theme/$theme/theme.php")) {
123                         return false;
124                 }
125
126                 Logger::log("Addons: installing theme $theme");
127
128                 include_once "view/theme/$theme/theme.php";
129
130                 if (function_exists("{$theme}_install")) {
131                         $func = "{$theme}_install";
132                         $func();
133                         return true;
134                 } else {
135                         Logger::log("Addons: FAILED installing theme $theme");
136                         return false;
137                 }
138         }
139
140         /**
141          * @brief Get the full path to relevant theme files by filename
142          *
143          * This function search in the theme directory (and if not present in global theme directory)
144          * if there is a directory with the file extension and  for a file with the given
145          * filename.
146          *
147          * @param string $file Filename
148          * @param string $root Full root path
149          * @return string Path to the file or empty string if the file isn't found
150          */
151         public static function getPathForFile($file, $root = '')
152         {
153                 $file = basename($file);
154
155                 // Make sure $root ends with a slash / if it's not blank
156                 if ($root !== '' && $root[strlen($root) - 1] !== '/') {
157                         $root = $root . '/';
158                 }
159                 $theme_info = \get_app()->theme_info;
160                 if (is_array($theme_info) && array_key_exists('extends', $theme_info)) {
161                         $parent = $theme_info['extends'];
162                 } else {
163                         $parent = 'NOPATH';
164                 }
165                 $theme = \get_app()->getCurrentTheme();
166                 $thname = $theme;
167                 $ext = substr($file, strrpos($file, '.') + 1);
168                 $paths = [
169                         "{$root}view/theme/$thname/$ext/$file",
170                         "{$root}view/theme/$parent/$ext/$file",
171                         "{$root}view/$ext/$file",
172                 ];
173                 foreach ($paths as $p) {
174                         // strpos() is faster than strstr when checking if one string is in another (http://php.net/manual/en/function.strstr.php)
175                         if (strpos($p, 'NOPATH') !== false) {
176                                 continue;
177                         } elseif (file_exists($p)) {
178                                 return $p;
179                         }
180                 }
181                 return '';
182         }
183
184         /**
185          * @brief Return relative path to theme stylesheet file
186          *
187          * Provide a sane default if nothing is chosen or the specified theme does not exist.
188          *
189          * @param string $theme Theme name
190          *
191          * @return string
192          */
193         public static function getStylesheetPath($theme)
194         {
195                 $a = BaseObject::getApp();
196
197                 $query_params = [];
198
199                 // Workaround for iOS Safari not initially sending the cookie for static files
200                 if ($a->mobileDetect->isIos() && $a->mobileDetect->isSafari()) {
201                         $query_params['t'] = time();
202                 }
203
204                 if ($a->profile_uid) {
205                         $query_params['puid'] = $a->profile_uid;
206                 }
207
208
209                 if (file_exists('view/theme/' . $theme . '/style.php')) {
210                         return 'view/theme/' . $theme . '/style.pcss' . (!empty($query_params) ? '?' . http_build_query($query_params) : '');
211                 }
212
213                 return 'view/theme/' . $theme . '/style.css';
214         }
215 }