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