4 * @file src/Core/Theme.php
7 namespace Friendica\Core;
9 use Friendica\Core\Logger;
10 use Friendica\Core\System;
12 require_once 'boot.php';
15 * Some functions to handle themes
20 * @brief Parse theme comment in search of theme infos.
25 * * Description: My Cool Theme
27 * * Author: John <profile url>
28 * * Maintainer: Jane <profile url>
31 * @param string $theme the name of the theme
34 public static function getInfo($theme)
43 'experimental' => file_exists("view/theme/$theme/experimental"),
44 'unsupported' => file_exists("view/theme/$theme/unsupported")
47 if (!is_file("view/theme/$theme/theme.php")) {
52 $stamp1 = microtime(true);
53 $theme_file = file_get_contents("view/theme/$theme/theme.php");
54 $a->saveTimestamp($stamp1, "file");
56 $result = preg_match("|/\*.*\*/|msU", $theme_file, $matches);
59 $comment_lines = explode("\n", $matches[0]);
60 foreach ($comment_lines as $comment_line) {
61 $comment_line = trim($comment_line, "\t\n\r */");
62 if ($comment_line != "") {
63 list($key, $value) = array_map("trim", explode(":", $comment_line, 2));
64 $key = strtolower($key);
65 if ($key == "author") {
66 $result = preg_match("|([^<]+)<([^>]+)>|", $value, $matches);
68 $info['author'][] = ['name' => $matches[1], 'link' => $matches[2]];
70 $info['author'][] = ['name' => $value];
72 } elseif ($key == "maintainer") {
73 $result = preg_match("|([^<]+)<([^>]+)>|", $value, $matches);
75 $info['maintainer'][] = ['name' => $matches[1], 'link' => $matches[2]];
77 $info['maintainer'][] = ['name' => $value];
79 } elseif (array_key_exists($key, $info)) {
89 * @brief Returns the theme's screenshot.
91 * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
93 * @param sring $theme The name of the theme
96 public static function getScreenshot($theme)
98 $exts = ['.png', '.jpg'];
99 foreach ($exts as $ext) {
100 if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
101 return(System::baseUrl() . '/view/theme/' . $theme . '/screenshot' . $ext);
104 return(System::baseUrl() . '/images/blank.png');
107 // install and uninstall theme
108 public static function uninstall($theme)
110 Logger::log("Addons: uninstalling theme " . $theme);
112 include_once "view/theme/$theme/theme.php";
113 if (function_exists("{$theme}_uninstall")) {
114 $func = "{$theme}_uninstall";
119 public static function install($theme)
121 // silently fail if theme was removed
123 if (!file_exists("view/theme/$theme/theme.php")) {
127 Logger::log("Addons: installing theme $theme");
129 include_once "view/theme/$theme/theme.php";
131 if (function_exists("{$theme}_install")) {
132 $func = "{$theme}_install";
136 Logger::log("Addons: FAILED installing theme $theme");
142 * @brief Get the full path to relevant theme files by filename
144 * This function search in the theme directory (and if not present in global theme directory)
145 * if there is a directory with the file extension and for a file with the given
148 * @param string $file Filename
149 * @param string $root Full root path
150 * @return string Path to the file or empty string if the file isn't found
152 public static function getPathForFile($file, $root = '')
154 $file = basename($file);
156 // Make sure $root ends with a slash / if it's not blank
157 if ($root !== '' && $root[strlen($root) - 1] !== '/') {
160 $theme_info = get_app()->theme_info;
161 if (is_array($theme_info) && array_key_exists('extends', $theme_info)) {
162 $parent = $theme_info['extends'];
166 $theme = get_app()->getCurrentTheme();
168 $ext = substr($file, strrpos($file, '.') + 1);
170 "{$root}view/theme/$thname/$ext/$file",
171 "{$root}view/theme/$parent/$ext/$file",
172 "{$root}view/$ext/$file",
174 foreach ($paths as $p) {
175 // strpos() is faster than strstr when checking if one string is in another (http://php.net/manual/en/function.strstr.php)
176 if (strpos($p, 'NOPATH') !== false) {
178 } elseif (file_exists($p)) {
186 * @brief Return relative path to theme stylesheet file
188 * Provide a sane default if nothing is chosen or the specified theme does not exist.
190 * @param string $theme Theme name
194 public static function getStylesheetPath($theme)
198 $opts = (($a->profile_uid) ? '?f=&puid=' . $a->profile_uid : '');
199 if (file_exists('view/theme/' . $theme . '/style.php')) {
200 return 'view/theme/' . $theme . '/style.pcss' . $opts;
203 return 'view/theme/' . $theme . '/style.css';