4 * @file src/Core/Theme.php
7 namespace Friendica\Core;
9 use Friendica\BaseObject;
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(',', $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 $allowed_themes;
38 * @brief Parse theme comment in search of theme infos.
43 * * Description: My Cool Theme
45 * * Author: John <profile url>
46 * * Maintainer: Jane <profile url>
49 * @param string $theme the name of the theme
52 public static function getInfo($theme)
54 $theme = Strings::sanitizeFilePathItem($theme);
63 'experimental' => file_exists("view/theme/$theme/experimental"),
64 'unsupported' => file_exists("view/theme/$theme/unsupported")
67 if (!is_file("view/theme/$theme/theme.php")) {
72 $stamp1 = microtime(true);
73 $theme_file = file_get_contents("view/theme/$theme/theme.php");
74 $a->getProfiler()->saveTimestamp($stamp1, "file", System::callstack());
76 $result = preg_match("|/\*.*\*/|msU", $theme_file, $matches);
79 $comment_lines = explode("\n", $matches[0]);
80 foreach ($comment_lines as $comment_line) {
81 $comment_line = trim($comment_line, "\t\n\r */");
82 if ($comment_line != "") {
83 list($key, $value) = array_map("trim", explode(":", $comment_line, 2));
84 $key = strtolower($key);
85 if ($key == "author") {
86 $result = preg_match("|([^<]+)<([^>]+)>|", $value, $matches);
88 $info['author'][] = ['name' => $matches[1], 'link' => $matches[2]];
90 $info['author'][] = ['name' => $value];
92 } elseif ($key == "maintainer") {
93 $result = preg_match("|([^<]+)<([^>]+)>|", $value, $matches);
95 $info['maintainer'][] = ['name' => $matches[1], 'link' => $matches[2]];
97 $info['maintainer'][] = ['name' => $value];
99 } elseif (array_key_exists($key, $info)) {
100 $info[$key] = $value;
109 * @brief Returns the theme's screenshot.
111 * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
113 * @param string $theme The name of the theme
115 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
117 public static function getScreenshot($theme)
119 $theme = Strings::sanitizeFilePathItem($theme);
121 $exts = ['.png', '.jpg'];
122 foreach ($exts as $ext) {
123 if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
124 return System::baseUrl() . '/view/theme/' . $theme . '/screenshot' . $ext;
127 return System::baseUrl() . '/images/blank.png';
130 public static function uninstall($theme)
132 $theme = Strings::sanitizeFilePathItem($theme);
134 // silently fail if theme was removed or if $theme is funky
135 if (file_exists("view/theme/$theme/theme.php")) {
136 Logger::log("Addons: uninstalling theme " . $theme);
138 if (function_exists("{$theme}_uninstall")) {
139 $func = "{$theme}_uninstall";
145 public static function install($theme)
147 $theme = Strings::sanitizeFilePathItem($theme);
149 // silently fail if theme was removed or if $theme is funky
150 if (!file_exists("view/theme/$theme/theme.php")) {
154 Logger::log("Addons: installing theme $theme");
156 include_once "view/theme/$theme/theme.php";
158 if (function_exists("{$theme}_install")) {
159 $func = "{$theme}_install";
163 Logger::log("Addons: FAILED installing theme $theme");
169 * @brief Get the full path to relevant theme files by filename
171 * This function search in the theme directory (and if not present in global theme directory)
172 * if there is a directory with the file extension and for a file with the given
175 * @param string $file Filename
176 * @param string $root Full root path
177 * @return string Path to the file or empty string if the file isn't found
178 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
180 public static function getPathForFile($file, $root = '')
182 $file = basename($file);
184 // Make sure $root ends with a slash / if it's not blank
185 if ($root !== '' && $root[strlen($root) - 1] !== '/') {
188 $theme_info = \get_app()->theme_info;
189 if (is_array($theme_info) && array_key_exists('extends', $theme_info)) {
190 $parent = $theme_info['extends'];
194 $theme = \get_app()->getCurrentTheme();
195 $parent = Strings::sanitizeFilePathItem($parent);
196 $ext = substr($file, strrpos($file, '.') + 1);
198 "{$root}view/theme/$theme/$ext/$file",
199 "{$root}view/theme/$parent/$ext/$file",
200 "{$root}view/$ext/$file",
202 foreach ($paths as $p) {
203 // strpos() is faster than strstr when checking if one string is in another (http://php.net/manual/en/function.strstr.php)
204 if (strpos($p, 'NOPATH') !== false) {
206 } elseif (file_exists($p)) {
214 * @brief Return relative path to theme stylesheet file
216 * Provide a sane default if nothing is chosen or the specified theme does not exist.
218 * @param string $theme Theme name
222 public static function getStylesheetPath($theme)
224 $theme = Strings::sanitizeFilePathItem($theme);
226 if (!file_exists('view/theme/' . $theme . '/style.php')) {
227 return 'view/theme/' . $theme . '/style.css';
230 $a = BaseObject::getApp();
234 $puid = Profile::getThemeUid($a);
236 $query_params['puid'] = $puid;
239 return 'view/theme/' . $theme . '/style.pcss' . (!empty($query_params) ? '?' . http_build_query($query_params) : '');