4 * @file src/Core/Theme.php
7 namespace Friendica\Core;
10 use Friendica\Model\Profile;
11 use Friendica\Util\Strings;
13 require_once 'boot.php';
16 * Some functions to handle themes
20 public static function getAllowedList()
22 $allowed_themes_str = Config::get('system', 'allowed_themes');
23 $allowed_themes_raw = explode(',', str_replace(' ', '', $allowed_themes_str));
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;
34 return array_unique($allowed_themes);
37 public static function setAllowedList(array $allowed_themes)
39 Config::set('system', 'allowed_themes', implode(',', array_unique($allowed_themes)));
43 * @brief Parse theme comment in search of theme infos.
48 * * Description: My Cool Theme
50 * * Author: John <profile url>
51 * * Maintainer: Jane <profile url>
54 * @param string $theme the name of the theme
57 public static function getInfo($theme)
59 $theme = Strings::sanitizeFilePathItem($theme);
68 'experimental' => file_exists("view/theme/$theme/experimental"),
69 'unsupported' => file_exists("view/theme/$theme/unsupported")
72 if (!is_file("view/theme/$theme/theme.php")) {
76 $stamp1 = microtime(true);
77 $theme_file = file_get_contents("view/theme/$theme/theme.php");
78 DI::profiler()->saveTimestamp($stamp1, "file", System::callstack());
80 $result = preg_match("|/\*.*\*/|msU", $theme_file, $matches);
83 $comment_lines = explode("\n", $matches[0]);
84 foreach ($comment_lines as $comment_line) {
85 $comment_line = trim($comment_line, "\t\n\r */");
86 if ($comment_line != "") {
87 list($key, $value) = array_map("trim", explode(":", $comment_line, 2));
88 $key = strtolower($key);
89 if ($key == "author") {
90 $result = preg_match("|([^<]+)<([^>]+)>|", $value, $matches);
92 $info['author'][] = ['name' => $matches[1], 'link' => $matches[2]];
94 $info['author'][] = ['name' => $value];
96 } elseif ($key == "maintainer") {
97 $result = preg_match("|([^<]+)<([^>]+)>|", $value, $matches);
99 $info['maintainer'][] = ['name' => $matches[1], 'link' => $matches[2]];
101 $info['maintainer'][] = ['name' => $value];
103 } elseif (array_key_exists($key, $info)) {
104 $info[$key] = $value;
113 * @brief Returns the theme's screenshot.
115 * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
117 * @param string $theme The name of the theme
119 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
121 public static function getScreenshot($theme)
123 $theme = Strings::sanitizeFilePathItem($theme);
125 $exts = ['.png', '.jpg'];
126 foreach ($exts as $ext) {
127 if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
128 return DI::baseUrl() . '/view/theme/' . $theme . '/screenshot' . $ext;
131 return DI::baseUrl() . '/images/blank.png';
134 public static function uninstall($theme)
136 $theme = Strings::sanitizeFilePathItem($theme);
138 // silently fail if theme was removed or if $theme is funky
139 if (file_exists("view/theme/$theme/theme.php")) {
140 include_once "view/theme/$theme/theme.php";
142 $func = "{$theme}_uninstall";
143 if (function_exists($func)) {
148 $allowed_themes = Theme::getAllowedList();
149 $key = array_search($theme, $allowed_themes);
150 if ($key !== false) {
151 unset($allowed_themes[$key]);
152 Theme::setAllowedList($allowed_themes);
156 public static function install($theme)
158 $theme = Strings::sanitizeFilePathItem($theme);
160 // silently fail if theme was removed or if $theme is funky
161 if (!file_exists("view/theme/$theme/theme.php")) {
166 include_once "view/theme/$theme/theme.php";
168 $func = "{$theme}_install";
169 if (function_exists($func)) {
173 $allowed_themes = Theme::getAllowedList();
174 $allowed_themes[] = $theme;
175 Theme::setAllowedList($allowed_themes);
178 } catch (\Exception $e) {
179 Logger::error('Theme installation failed', ['theme' => $theme, 'error' => $e->getMessage()]);
185 * @brief Get the full path to relevant theme files by filename
187 * This function searches in order in the current theme directory, in the current theme parent directory, and lastly
188 * in the base view/ folder.
190 * @param string $file Filename
191 * @return string Path to the file or empty string if the file isn't found
194 public static function getPathForFile($file)
198 $theme = $a->getCurrentTheme();
200 $parent = Strings::sanitizeFilePathItem($a->theme_info['extends'] ?? $theme);
203 "view/theme/$theme/$file",
204 "view/theme/$parent/$file",
208 foreach ($paths as $path) {
209 if (file_exists($path)) {
218 * @brief Return relative path to theme stylesheet file
220 * Provide a sane default if nothing is chosen or the specified theme does not exist.
222 * @param string $theme Theme name
226 public static function getStylesheetPath($theme)
228 $theme = Strings::sanitizeFilePathItem($theme);
230 if (!file_exists('view/theme/' . $theme . '/style.php')) {
231 return 'view/theme/' . $theme . '/style.css';
238 $puid = Profile::getThemeUid($a);
240 $query_params['puid'] = $puid;
243 return 'view/theme/' . $theme . '/style.pcss' . (!empty($query_params) ? '?' . http_build_query($query_params) : '');