]> git.mxchange.org Git - friendica.git/blob - src/Core/Theme.php
Replace html2plain calls by HTML::toPlaintext
[friendica.git] / src / Core / Theme.php
1 <?php
2 /**
3  * @file src/Core/Theme.php
4  */
5 namespace Friendica\Core;
6
7 use Friendica\Core\System;
8
9 require_once 'boot.php';
10
11 /**
12  * Some functions to handle themes
13  */
14 class Theme
15 {
16     /**
17      * @brief Parse theme comment in search of theme infos.
18      *
19      * like
20      * \code
21      * ..* Name: My Theme
22      *   * Description: My Cool Theme
23      * . * Version: 1.2.3
24      *   * Author: John <profile url>
25      *   * Maintainer: Jane <profile url>
26      *   *
27      * \endcode
28      * @param string $theme the name of the theme
29      * @return array
30      */
31
32     public static function getInfo($theme)
33     {
34         $info=[
35             'name' => $theme,
36             'description' => "",
37             'author' => [],
38             'maintainer' => [],
39             'version' => "",
40             'credits' => "",
41             'experimental' => false,
42             'unsupported' => false
43         ];
44
45         if (file_exists("view/theme/$theme/experimental"))
46             $info['experimental'] = true;
47         if (file_exists("view/theme/$theme/unsupported"))
48             $info['unsupported'] = true;
49
50         if (!is_file("view/theme/$theme/theme.php")) return $info;
51
52         $a = get_app();
53         $stamp1 = microtime(true);
54         $f = file_get_contents("view/theme/$theme/theme.php");
55         $a->save_timestamp($stamp1, "file");
56
57         $r = preg_match("|/\*.*\*/|msU", $f, $m);
58
59         if ($r) {
60             $ll = explode("\n", $m[0]);
61             foreach ( $ll as $l ) {
62                 $l = trim($l,"\t\n\r */");
63                 if ($l != "") {
64                     list($k, $v) = array_map("trim", explode(":", $l, 2));
65                     $k= strtolower($k);
66                     if ($k == "author") {
67
68                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
69                         if ($r) {
70                             $info['author'][] = ['name'=>$m[1], 'link'=>$m[2]];
71                         } else {
72                             $info['author'][] = ['name'=>$v];
73                         }
74                     } elseif ($k == "maintainer") {
75                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
76                         if ($r) {
77                             $info['maintainer'][] = ['name'=>$m[1], 'link'=>$m[2]];
78                         } else {
79                             $info['maintainer'][] = ['name'=>$v];
80                         }
81                     } else {
82                         if (array_key_exists($k, $info)) {
83                             $info[$k] = $v;
84                         }
85                     }
86                 }
87             }
88         }
89         return $info;
90     }
91
92     /**
93      * @brief Returns the theme's screenshot.
94      *
95      * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
96      *
97      * @param sring $theme The name of the theme
98      * @return string
99      */
100     public static function getScreenshot($theme)
101     {
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);
106             }
107         }
108         return(System::baseUrl() . '/images/blank.png');
109     }
110
111     // install and uninstall theme
112     public static function uninstall($theme)
113     {
114         logger("Addons: uninstalling theme " . $theme);
115
116         include_once("view/theme/$theme/theme.php");
117         if (function_exists("{$theme}_uninstall")) {
118             $func = "{$theme}_uninstall";
119             $func();
120         }
121     }
122
123     public static function install($theme)
124     {
125         // silently fail if theme was removed
126
127         if (! file_exists("view/theme/$theme/theme.php")) {
128             return false;
129         }
130
131         logger("Addons: installing theme $theme");
132
133         include_once("view/theme/$theme/theme.php");
134
135         if (function_exists("{$theme}_install")) {
136             $func = "{$theme}_install";
137             $func();
138             return true;
139         } else {
140             logger("Addons: FAILED installing theme $theme");
141             return false;
142         }
143
144     }
145
146     /**
147      * @brief Get the full path to relevant theme files by filename
148      *
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
151      * filename.
152      *
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
156      */
157     public static function getPathForFile($file, $root = '')
158     {
159         $file = basename($file);
160
161         // Make sure $root ends with a slash / if it's not blank
162         if ($root !== '' && $root[strlen($root)-1] !== '/') {
163             $root = $root . '/';
164         }
165         $theme_info = get_app()->theme_info;
166         if (is_array($theme_info) && array_key_exists('extends',$theme_info)) {
167             $parent = $theme_info['extends'];
168         } else {
169             $parent = 'NOPATH';
170         }
171         $theme = current_theme();
172         $thname = $theme;
173         $ext = substr($file,strrpos($file,'.')+1);
174         $paths = [
175             "{$root}view/theme/$thname/$ext/$file",
176             "{$root}view/theme/$parent/$ext/$file",
177             "{$root}view/$ext/$file",
178         ];
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) {
182                 continue;
183             } elseif (file_exists($p)) {
184                 return $p;
185             }
186         }
187         return '';
188     }
189 }