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")) {
77 $stamp1 = microtime(true);
78 $theme_file = file_get_contents("view/theme/$theme/theme.php");
79 $a->getProfiler()->saveTimestamp($stamp1, "file", System::callstack());
81 $result = preg_match("|/\*.*\*/|msU", $theme_file, $matches);
84 $comment_lines = explode("\n", $matches[0]);
85 foreach ($comment_lines as $comment_line) {
86 $comment_line = trim($comment_line, "\t\n\r */");
87 if ($comment_line != "") {
88 list($key, $value) = array_map("trim", explode(":", $comment_line, 2));
89 $key = strtolower($key);
90 if ($key == "author") {
91 $result = preg_match("|([^<]+)<([^>]+)>|", $value, $matches);
93 $info['author'][] = ['name' => $matches[1], 'link' => $matches[2]];
95 $info['author'][] = ['name' => $value];
97 } elseif ($key == "maintainer") {
98 $result = preg_match("|([^<]+)<([^>]+)>|", $value, $matches);
100 $info['maintainer'][] = ['name' => $matches[1], 'link' => $matches[2]];
102 $info['maintainer'][] = ['name' => $value];
104 } elseif (array_key_exists($key, $info)) {
105 $info[$key] = $value;
114 * @brief Returns the theme's screenshot.
116 * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
118 * @param string $theme The name of the theme
120 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
122 public static function getScreenshot($theme)
124 $theme = Strings::sanitizeFilePathItem($theme);
126 $exts = ['.png', '.jpg'];
127 foreach ($exts as $ext) {
128 if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
129 return System::baseUrl() . '/view/theme/' . $theme . '/screenshot' . $ext;
132 return System::baseUrl() . '/images/blank.png';
135 public static function uninstall($theme)
137 $theme = Strings::sanitizeFilePathItem($theme);
139 // silently fail if theme was removed or if $theme is funky
140 if (file_exists("view/theme/$theme/theme.php")) {
141 include_once "view/theme/$theme/theme.php";
143 $func = "{$theme}_uninstall";
144 if (function_exists($func)) {
149 $allowed_themes = Theme::getAllowedList();
150 $key = array_search($theme, $allowed_themes);
151 if ($key !== false) {
152 unset($allowed_themes[$key]);
153 Theme::setAllowedList($allowed_themes);
157 public static function install($theme)
159 $theme = Strings::sanitizeFilePathItem($theme);
161 // silently fail if theme was removed or if $theme is funky
162 if (!file_exists("view/theme/$theme/theme.php")) {
167 include_once "view/theme/$theme/theme.php";
169 $func = "{$theme}_install";
170 if (function_exists($func)) {
174 $allowed_themes = Theme::getAllowedList();
175 $allowed_themes[] = $theme;
176 Theme::setAllowedList($allowed_themes);
179 } catch (\Exception $e) {
180 Logger::error('Theme installation failed', ['theme' => $theme, 'error' => $e->getMessage()]);
186 * @brief Get the full path to relevant theme files by filename
188 * This function searches in order in the current theme directory, in the current theme parent directory, and lastly
189 * in the base view/ folder.
191 * @param string $file Filename
192 * @return string Path to the file or empty string if the file isn't found
195 public static function getPathForFile($file)
199 $theme = $a->getCurrentTheme();
201 $parent = Strings::sanitizeFilePathItem($a->theme_info['extends'] ?? $theme);
204 "view/theme/$theme/$file",
205 "view/theme/$parent/$file",
209 foreach ($paths as $path) {
210 if (file_exists($path)) {
219 * @brief Return relative path to theme stylesheet file
221 * Provide a sane default if nothing is chosen or the specified theme does not exist.
223 * @param string $theme Theme name
227 public static function getStylesheetPath($theme)
229 $theme = Strings::sanitizeFilePathItem($theme);
231 if (!file_exists('view/theme/' . $theme . '/style.php')) {
232 return 'view/theme/' . $theme . '/style.css';
239 $puid = Profile::getThemeUid($a);
241 $query_params['puid'] = $puid;
244 return 'view/theme/' . $theme . '/style.pcss' . (!empty($query_params) ? '?' . http_build_query($query_params) : '');