3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Core;
25 use Friendica\Model\Profile;
26 use Friendica\Util\Strings;
28 require_once 'boot.php';
31 * Some functions to handle themes
35 public static function getAllowedList()
37 $allowed_themes_str = DI::config()->get('system', 'allowed_themes');
38 $allowed_themes_raw = explode(',', str_replace(' ', '', $allowed_themes_str));
40 if (count($allowed_themes_raw)) {
41 foreach ($allowed_themes_raw as $theme) {
42 $theme = Strings::sanitizeFilePathItem(trim($theme));
43 if (strlen($theme) && is_dir("view/theme/$theme")) {
44 $allowed_themes[] = $theme;
49 return array_unique($allowed_themes);
52 public static function setAllowedList(array $allowed_themes)
54 DI::config()->set('system', 'allowed_themes', implode(',', array_unique($allowed_themes)));
58 * Parse theme comment in search of theme infos.
63 * * Description: My Cool Theme
65 * * Author: John <profile url>
66 * * Maintainer: Jane <profile url>
69 * @param string $theme the name of the theme
72 public static function getInfo($theme)
74 $theme = Strings::sanitizeFilePathItem($theme);
83 'experimental' => file_exists("view/theme/$theme/experimental"),
84 'unsupported' => file_exists("view/theme/$theme/unsupported")
87 if (!is_file("view/theme/$theme/theme.php")) {
91 DI::profiler()->startRecording('file');
92 $theme_file = file_get_contents("view/theme/$theme/theme.php");
93 DI::profiler()->stopRecording();
95 $result = preg_match("|/\*.*\*/|msU", $theme_file, $matches);
98 $comment_lines = explode("\n", $matches[0]);
99 foreach ($comment_lines as $comment_line) {
100 $comment_line = trim($comment_line, "\t\n\r */");
101 if (strpos($comment_line, ':') !== false) {
102 list($key, $value) = array_map("trim", explode(":", $comment_line, 2));
103 $key = strtolower($key);
104 if ($key == "author") {
105 $result = preg_match("|([^<]+)<([^>]+)>|", $value, $matches);
107 $info['author'][] = ['name' => $matches[1], 'link' => $matches[2]];
109 $info['author'][] = ['name' => $value];
111 } elseif ($key == "maintainer") {
112 $result = preg_match("|([^<]+)<([^>]+)>|", $value, $matches);
114 $info['maintainer'][] = ['name' => $matches[1], 'link' => $matches[2]];
116 $info['maintainer'][] = ['name' => $value];
118 } elseif (array_key_exists($key, $info)) {
119 $info[$key] = $value;
128 * Returns the theme's screenshot.
130 * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
132 * @param string $theme The name of the theme
134 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
136 public static function getScreenshot($theme)
138 $theme = Strings::sanitizeFilePathItem($theme);
140 $exts = ['.png', '.jpg'];
141 foreach ($exts as $ext) {
142 if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
143 return DI::baseUrl() . '/view/theme/' . $theme . '/screenshot' . $ext;
146 return DI::baseUrl() . '/images/blank.png';
149 public static function uninstall($theme)
151 $theme = Strings::sanitizeFilePathItem($theme);
153 // silently fail if theme was removed or if $theme is funky
154 if (file_exists("view/theme/$theme/theme.php")) {
155 include_once "view/theme/$theme/theme.php";
157 $func = "{$theme}_uninstall";
158 if (function_exists($func)) {
162 Hook::delete(['file' => "view/theme/$theme/theme.php"]);
165 $allowed_themes = Theme::getAllowedList();
166 $key = array_search($theme, $allowed_themes);
167 if ($key !== false) {
168 unset($allowed_themes[$key]);
169 Theme::setAllowedList($allowed_themes);
173 public static function install($theme)
175 $theme = Strings::sanitizeFilePathItem($theme);
177 // silently fail if theme was removed or if $theme is funky
178 if (!file_exists("view/theme/$theme/theme.php")) {
183 include_once "view/theme/$theme/theme.php";
185 $func = "{$theme}_install";
186 if (function_exists($func)) {
190 $allowed_themes = Theme::getAllowedList();
191 $allowed_themes[] = $theme;
192 Theme::setAllowedList($allowed_themes);
195 } catch (\Exception $e) {
196 Logger::error('Theme installation failed', ['theme' => $theme, 'error' => $e->getMessage()]);
202 * Get the full path to relevant theme files by filename
204 * This function searches in order in the current theme directory, in the current theme parent directory, and lastly
205 * in the base view/ folder.
207 * @param string $file Filename
208 * @return string Path to the file or empty string if the file isn't found
211 public static function getPathForFile($file)
215 $theme = $a->getCurrentTheme();
217 $parent = Strings::sanitizeFilePathItem($a->getThemeInfoValue('extends', $theme));
220 "view/theme/$theme/$file",
221 "view/theme/$parent/$file",
225 foreach ($paths as $path) {
226 if (file_exists($path)) {
235 * Return relative path to theme stylesheet file
237 * Provide a sane default if nothing is chosen or the specified theme does not exist.
239 * @param string $theme Theme name
243 public static function getStylesheetPath($theme)
245 $theme = Strings::sanitizeFilePathItem($theme);
247 if (!file_exists('view/theme/' . $theme . '/style.php')) {
248 return 'view/theme/' . $theme . '/style.css';
255 $puid = Profile::getThemeUid($a);
257 $query_params['puid'] = $puid;
260 return 'view/theme/' . $theme . '/style.pcss' . (!empty($query_params) ? '?' . http_build_query($query_params) : '');
264 * Returns the path of the provided theme
267 * @return string|null
269 public static function getConfigFile($theme)
271 $theme = Strings::sanitizeFilePathItem($theme);
274 $base_theme = $a->getThemeInfoValue('extends') ?? '';
276 if (file_exists("view/theme/$theme/config.php")) {
277 return "view/theme/$theme/config.php";
279 if ($base_theme && file_exists("view/theme/$base_theme/config.php")) {
280 return "view/theme/$base_theme/config.php";
286 * Returns the background color of the provided theme if available.
288 * @param string $theme
289 * @param int|null $uid Current logged-in user id
290 * @return string|null
292 public static function getBackgroundColor(string $theme, $uid = null)
294 $theme = Strings::sanitizeFilePathItem($theme);
298 // silently fail if theme was removed or if $theme is funky
299 if (file_exists("view/theme/$theme/theme.php")) {
300 include_once "view/theme/$theme/theme.php";
302 $func = "{$theme}_get_background_color";
303 if (function_exists($func)) {
304 $return = $func($uid);
312 * Returns the theme color of the provided theme if available.
314 * @param string $theme
315 * @param int|null $uid Current logged-in user id
316 * @return string|null
318 public static function getThemeColor(string $theme, int $uid = null)
320 $theme = Strings::sanitizeFilePathItem($theme);
324 // silently fail if theme was removed or if $theme is funky
325 if (file_exists("view/theme/$theme/theme.php")) {
326 include_once "view/theme/$theme/theme.php";
328 $func = "{$theme}_get_theme_color";
329 if (function_exists($func)) {
330 $return = $func($uid);