3 * @file src/Core/Theme.php
5 namespace Friendica\Core;
7 use Friendica\Core\System;
9 require_once 'boot.php';
12 * Some functions to handle themes
17 * @brief Parse theme comment in search of theme infos.
22 * * Description: My Cool Theme
24 * * Author: John <profile url>
25 * * Maintainer: Jane <profile url>
28 * @param string $theme the name of the theme
32 public static function getInfo($theme)
41 'experimental' => false,
42 'unsupported' => false
45 if (file_exists("view/theme/$theme/experimental"))
46 $info['experimental'] = true;
47 if (file_exists("view/theme/$theme/unsupported"))
48 $info['unsupported'] = true;
50 if (!is_file("view/theme/$theme/theme.php")) return $info;
53 $stamp1 = microtime(true);
54 $f = file_get_contents("view/theme/$theme/theme.php");
55 $a->save_timestamp($stamp1, "file");
57 $r = preg_match("|/\*.*\*/|msU", $f, $m);
60 $ll = explode("\n", $m[0]);
61 foreach ( $ll as $l ) {
62 $l = trim($l,"\t\n\r */");
64 list($k, $v) = array_map("trim", explode(":", $l, 2));
68 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
70 $info['author'][] = ['name'=>$m[1], 'link'=>$m[2]];
72 $info['author'][] = ['name'=>$v];
74 } elseif ($k == "maintainer") {
75 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
77 $info['maintainer'][] = ['name'=>$m[1], 'link'=>$m[2]];
79 $info['maintainer'][] = ['name'=>$v];
82 if (array_key_exists($k, $info)) {
93 * @brief Returns the theme's screenshot.
95 * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
97 * @param sring $theme The name of the theme
100 public static function getScreenshot($theme)
102 $exts = ['.png','.jpg'];
103 foreach ($exts as $ext) {
104 if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
105 return(System::baseUrl() . '/view/theme/' . $theme . '/screenshot' . $ext);
108 return(System::baseUrl() . '/images/blank.png');
111 // install and uninstall theme
112 public static function uninstall($theme)
114 logger("Addons: uninstalling theme " . $theme);
116 include_once("view/theme/$theme/theme.php");
117 if (function_exists("{$theme}_uninstall")) {
118 $func = "{$theme}_uninstall";
123 public static function install($theme)
125 // silently fail if theme was removed
127 if (! file_exists("view/theme/$theme/theme.php")) {
131 logger("Addons: installing theme $theme");
133 include_once("view/theme/$theme/theme.php");
135 if (function_exists("{$theme}_install")) {
136 $func = "{$theme}_install";
140 logger("Addons: FAILED installing theme $theme");
147 * @brief Get the full path to relevant theme files by filename
149 * This function search in the theme directory (and if not present in global theme directory)
150 * if there is a directory with the file extension and for a file with the given
153 * @param string $file Filename
154 * @param string $root Full root path
155 * @return string Path to the file or empty string if the file isn't found
157 public static function getPathForFile($file, $root = '')
159 $file = basename($file);
161 // Make sure $root ends with a slash / if it's not blank
162 if ($root !== '' && $root[strlen($root)-1] !== '/') {
165 $theme_info = get_app()->theme_info;
166 if (is_array($theme_info) && array_key_exists('extends',$theme_info)) {
167 $parent = $theme_info['extends'];
171 $theme = current_theme();
173 $ext = substr($file,strrpos($file,'.')+1);
175 "{$root}view/theme/$thname/$ext/$file",
176 "{$root}view/theme/$parent/$ext/$file",
177 "{$root}view/$ext/$file",
179 foreach ($paths as $p) {
180 // strpos() is faster than strstr when checking if one string is in another (http://php.net/manual/en/function.strstr.php)
181 if (strpos($p,'NOPATH') !== false) {
183 } elseif (file_exists($p)) {