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