3 * @copyright Copyright (C) 2010-2022, 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(): array
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(string $theme): array
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(string $theme): string
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';
150 * Uninstalls given theme name
152 * @param string $theme Name of theme
153 * @return bool true on success
155 public static function uninstall(string $theme)
157 $theme = Strings::sanitizeFilePathItem($theme);
159 // silently fail if theme was removed or if $theme is funky
160 if (file_exists("view/theme/$theme/theme.php")) {
161 include_once "view/theme/$theme/theme.php";
163 $func = "{$theme}_uninstall";
164 if (function_exists($func)) {
168 Hook::delete(['file' => "view/theme/$theme/theme.php"]);
171 $allowed_themes = Theme::getAllowedList();
172 $key = array_search($theme, $allowed_themes);
173 if ($key !== false) {
174 unset($allowed_themes[$key]);
175 Theme::setAllowedList($allowed_themes);
182 * Installs given theme name
184 * @param string $theme Name of theme
185 * @return bool true on success
187 public static function install(string $theme): bool
189 $theme = Strings::sanitizeFilePathItem($theme);
191 // silently fail if theme was removed or if $theme is funky
192 if (!file_exists("view/theme/$theme/theme.php")) {
197 include_once "view/theme/$theme/theme.php";
199 $func = "{$theme}_install";
200 if (function_exists($func)) {
204 $allowed_themes = Theme::getAllowedList();
205 $allowed_themes[] = $theme;
206 Theme::setAllowedList($allowed_themes);
209 } catch (\Exception $e) {
210 Logger::error('Theme installation failed', ['theme' => $theme, 'error' => $e->getMessage()]);
216 * Get the full path to relevant theme files by filename
218 * This function searches in order in the current theme directory, in the current theme parent directory, and lastly
219 * in the base view/ folder.
221 * @param string $file Filename
222 * @return string Path to the file or empty string if the file isn't found
225 public static function getPathForFile(string $file): string
229 $theme = $a->getCurrentTheme();
231 $parent = Strings::sanitizeFilePathItem($a->getThemeInfoValue('extends', $theme));
234 "view/theme/$theme/$file",
235 "view/theme/$parent/$file",
239 foreach ($paths as $path) {
240 if (file_exists($path)) {
249 * Return relative path to theme stylesheet file
251 * Provide a sane default if nothing is chosen or the specified theme does not exist.
253 * @param string $theme Theme name
256 public static function getStylesheetPath(string $theme): string
258 $theme = Strings::sanitizeFilePathItem($theme);
260 if (!file_exists('view/theme/' . $theme . '/style.php')) {
261 return 'view/theme/' . $theme . '/style.css';
268 $puid = Profile::getThemeUid($a);
270 $query_params['puid'] = $puid;
273 return 'view/theme/' . $theme . '/style.pcss' . (!empty($query_params) ? '?' . http_build_query($query_params) : '');
277 * Returns the path of the provided theme
279 * @param string $theme Theme name
280 * @return string|null
282 public static function getConfigFile(string $theme)
284 $theme = Strings::sanitizeFilePathItem($theme);
287 $base_theme = $a->getThemeInfoValue('extends') ?? '';
289 if (file_exists("view/theme/$theme/config.php")) {
290 return "view/theme/$theme/config.php";
292 if ($base_theme && file_exists("view/theme/$base_theme/config.php")) {
293 return "view/theme/$base_theme/config.php";
299 * Returns the background color of the provided theme if available.
301 * @param string $theme Theme name
302 * @param int|null $uid Current logged-in user id
303 * @return string|null
305 public static function getBackgroundColor(string $theme, int $uid = null)
307 $theme = Strings::sanitizeFilePathItem($theme);
311 // silently fail if theme was removed or if $theme is funky
312 if (file_exists("view/theme/$theme/theme.php")) {
313 include_once "view/theme/$theme/theme.php";
315 $func = "{$theme}_get_background_color";
316 if (function_exists($func)) {
317 $return = $func($uid);
325 * Returns the theme color of the provided theme if available.
327 * @param string $theme
328 * @param int|null $uid Current logged-in user id
329 * @return string|null
331 public static function getThemeColor(string $theme, int $uid = null)
333 $theme = Strings::sanitizeFilePathItem($theme);
337 // silently fail if theme was removed or if $theme is funky
338 if (file_exists("view/theme/$theme/theme.php")) {
339 include_once "view/theme/$theme/theme.php";
341 $func = "{$theme}_get_theme_color";
342 if (function_exists($func)) {
343 $return = $func($uid);