4 * @file src/Core/Theme.php
7 namespace Friendica\Core;
9 use Friendica\Core\System;
11 require_once 'boot.php';
14 * Some functions to handle themes
19 * @brief Parse theme comment in search of theme infos.
24 * * Description: My Cool Theme
26 * * Author: John <profile url>
27 * * Maintainer: Jane <profile url>
30 * @param string $theme the name of the theme
33 public static function getInfo($theme)
42 'experimental' => file_exists("view/theme/$theme/experimental"),
43 'unsupported' => file_exists("view/theme/$theme/unsupported")
46 if (!is_file("view/theme/$theme/theme.php")) {
51 $stamp1 = microtime(true);
52 $theme_file = file_get_contents("view/theme/$theme/theme.php");
53 $a->save_timestamp($stamp1, "file");
55 $result = preg_match("|/\*.*\*/|msU", $theme_file, $matches);
58 $comment_lines = explode("\n", $matches[0]);
59 foreach ($comment_lines as $comment_line) {
60 $comment_line = trim($comment_line, "\t\n\r */");
61 if ($comment_line != "") {
62 list($key, $value) = array_map("trim", explode(":", $comment_line, 2));
63 $key = strtolower($key);
64 if ($key == "author") {
65 $result = preg_match("|([^<]+)<([^>]+)>|", $value, $matches);
67 $info['author'][] = ['name' => $matches[1], 'link' => $matches[2]];
69 $info['author'][] = ['name' => $value];
71 } elseif ($key == "maintainer") {
72 $result = preg_match("|([^<]+)<([^>]+)>|", $value, $matches);
74 $info['maintainer'][] = ['name' => $matches[1], 'link' => $matches[2]];
76 $info['maintainer'][] = ['name' => $value];
78 } elseif (array_key_exists($key, $info)) {
88 * @brief Returns the theme's screenshot.
90 * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
92 * @param sring $theme The name of the theme
95 public static function getScreenshot($theme)
97 $exts = ['.png', '.jpg'];
98 foreach ($exts as $ext) {
99 if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
100 return(System::baseUrl() . '/view/theme/' . $theme . '/screenshot' . $ext);
103 return(System::baseUrl() . '/images/blank.png');
106 // install and uninstall theme
107 public static function uninstall($theme)
109 logger("Addons: uninstalling theme " . $theme);
111 include_once "view/theme/$theme/theme.php";
112 if (function_exists("{$theme}_uninstall")) {
113 $func = "{$theme}_uninstall";
118 public static function install($theme)
120 // silently fail if theme was removed
122 if (!file_exists("view/theme/$theme/theme.php")) {
126 logger("Addons: installing theme $theme");
128 include_once "view/theme/$theme/theme.php";
130 if (function_exists("{$theme}_install")) {
131 $func = "{$theme}_install";
135 logger("Addons: FAILED installing theme $theme");
141 * @brief Get the full path to relevant theme files by filename
143 * This function search in the theme directory (and if not present in global theme directory)
144 * if there is a directory with the file extension and for a file with the given
147 * @param string $file Filename
148 * @param string $root Full root path
149 * @return string Path to the file or empty string if the file isn't found
151 public static function getPathForFile($file, $root = '')
153 $file = basename($file);
155 // Make sure $root ends with a slash / if it's not blank
156 if ($root !== '' && $root[strlen($root) - 1] !== '/') {
159 $theme_info = get_app()->theme_info;
160 if (is_array($theme_info) && array_key_exists('extends', $theme_info)) {
161 $parent = $theme_info['extends'];
165 $theme = get_app()->getCurrentTheme();
167 $ext = substr($file, strrpos($file, '.') + 1);
169 "{$root}view/theme/$thname/$ext/$file",
170 "{$root}view/theme/$parent/$ext/$file",
171 "{$root}view/$ext/$file",
173 foreach ($paths as $p) {
174 // strpos() is faster than strstr when checking if one string is in another (http://php.net/manual/en/function.strstr.php)
175 if (strpos($p, 'NOPATH') !== false) {
177 } elseif (file_exists($p)) {
185 * @brief Return relative path to theme stylesheet file
187 * Provide a sane default if nothing is chosen or the specified theme does not exist.
189 * @param string $theme Theme name
193 public static function getStylesheetPath($theme)
197 $opts = (($a->profile_uid) ? '?f=&puid=' . $a->profile_uid : '');
198 if (file_exists('view/theme/' . $theme . '/style.php')) {
199 return 'view/theme/' . $theme . '/style.pcss' . $opts;
202 return 'view/theme/' . $theme . '/style.css';