]> git.mxchange.org Git - friendica.git/blob - include/plugin.php
Use short form array syntax everywhere
[friendica.git] / include / plugin.php
1 <?php
2 /**
3  * @file include/plugin.php
4  *
5  * @brief Some functions to handle addons and themes.
6  */
7
8 use Friendica\App;
9 use Friendica\Core\Config;
10 use Friendica\Core\System;
11 use Friendica\Database\DBM;
12
13 /**
14  * @brief uninstalls an addon.
15  *
16  * @param string $plugin name of the addon
17  * @return boolean
18  */
19 function uninstall_plugin($plugin) {
20         logger("Addons: uninstalling " . $plugin);
21         dba::delete('addon', ['name' => $plugin]);
22
23         @include_once('addon/' . $plugin . '/' . $plugin . '.php');
24         if (function_exists($plugin . '_uninstall')) {
25                 $func = $plugin . '_uninstall';
26                 $func();
27         }
28 }
29
30 /**
31  * @brief installs an addon.
32  *
33  * @param string $plugin name of the addon
34  * @return bool
35  */
36 function install_plugin($plugin) {
37         // silently fail if plugin was removed
38
39         if (!file_exists('addon/' . $plugin . '/' . $plugin . '.php')) {
40                 return false;
41         }
42         logger("Addons: installing " . $plugin);
43         $t = @filemtime('addon/' . $plugin . '/' . $plugin . '.php');
44         @include_once('addon/' . $plugin . '/' . $plugin . '.php');
45         if (function_exists($plugin . '_install')) {
46                 $func = $plugin . '_install';
47                 $func();
48
49                 $plugin_admin = (function_exists($plugin."_plugin_admin") ? 1 : 0);
50
51                 dba::insert('addon', ['name' => $plugin, 'installed' => true,
52                                         'timestamp' => $t, 'plugin_admin' => $plugin_admin]);
53
54                 // we can add the following with the previous SQL
55                 // once most site tables have been updated.
56                 // This way the system won't fall over dead during the update.
57
58                 if (file_exists('addon/' . $plugin . '/.hidden')) {
59                         dba::update('addon', ['hidden' => true], ['name' => $plugin]);
60                 }
61                 return true;
62         } else {
63                 logger("Addons: FAILED installing " . $plugin);
64                 return false;
65         }
66 }
67
68 // reload all updated plugins
69
70 function reload_plugins() {
71         $plugins = Config::get('system', 'addon');
72         if (strlen($plugins)) {
73
74                 $r = q("SELECT * FROM `addon` WHERE `installed` = 1");
75                 if (DBM::is_result($r)) {
76                         $installed = $r;
77                 } else {
78                         $installed = [];
79                 }
80
81                 $parr = explode(',',$plugins);
82
83                 if (count($parr)) {
84                         foreach ($parr as $pl) {
85
86                                 $pl = trim($pl);
87
88                                 $fname = 'addon/' . $pl . '/' . $pl . '.php';
89
90                                 if (file_exists($fname)) {
91                                         $t = @filemtime($fname);
92                                         foreach ($installed as $i) {
93                                                 if (($i['name'] == $pl) && ($i['timestamp'] != $t)) {
94                                                         logger('Reloading plugin: ' . $i['name']);
95                                                         @include_once($fname);
96
97                                                         if (function_exists($pl . '_uninstall')) {
98                                                                 $func = $pl . '_uninstall';
99                                                                 $func();
100                                                         }
101                                                         if (function_exists($pl . '_install')) {
102                                                                 $func = $pl . '_install';
103                                                                 $func();
104                                                         }
105                                                         dba::update('addon', ['timestamp' => $t], ['id' => $i['id']]);
106                                                 }
107                                         }
108                                 }
109                         }
110                 }
111         }
112
113 }
114
115 /**
116  * @brief check if addon is enabled
117  *
118  * @param string $plugin
119  * @return boolean
120  */
121 function plugin_enabled($plugin) {
122         return dba::exists('addon', ['installed' => true, 'name' => $plugin]);
123 }
124
125
126 /**
127  * @brief registers a hook.
128  *
129  * @param string $hook the name of the hook
130  * @param string $file the name of the file that hooks into
131  * @param string $function the name of the function that the hook will call
132  * @param int $priority A priority (defaults to 0)
133  * @return mixed|bool
134  */
135 function register_hook($hook, $file, $function, $priority=0) {
136         $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
137         $exists = dba::exists('hook', $condition);
138         if ($exists) {
139                 return true;
140         }
141
142         $r = dba::insert('hook', ['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]);
143
144         return $r;
145 }
146
147 /**
148  * @brief unregisters a hook.
149  *
150  * @param string $hook the name of the hook
151  * @param string $file the name of the file that hooks into
152  * @param string $function the name of the function that the hook called
153  * @return array
154  */
155 function unregister_hook($hook, $file, $function) {
156         $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
157         $r = dba::delete('hook', $condition);
158         return $r;
159 }
160
161
162 function load_hooks() {
163         $a = get_app();
164         $a->hooks = [];
165         $r = dba::select('hook', ['hook', 'file', 'function'], [], ['order' => ['priority' => 'desc', 'file']]);
166
167         while ($rr = dba::fetch($r)) {
168                 if (! array_key_exists($rr['hook'],$a->hooks)) {
169                         $a->hooks[$rr['hook']] = [];
170                 }
171                 $a->hooks[$rr['hook']][] = [$rr['file'],$rr['function']];
172         }
173         dba::close($r);
174 }
175
176 /**
177  * @brief Calls a hook.
178  *
179  * Use this function when you want to be able to allow a hook to manipulate
180  * the provided data.
181  *
182  * @param string $name of the hook to call
183  * @param string|array &$data to transmit to the callback handler
184  */
185 function call_hooks($name, &$data = null)
186 {
187         $a = get_app();
188
189         if (is_array($a->hooks) && array_key_exists($name, $a->hooks)) {
190                 foreach ($a->hooks[$name] as $hook) {
191                         call_single_hook($a, $name, $hook, $data);
192                 }
193         }
194 }
195
196 /**
197  * @brief Calls a single hook.
198  *
199  * @param string $name of the hook to call
200  * @param array $hook Hook data
201  * @param string|array &$data to transmit to the callback handler
202  */
203 function call_single_hook($a, $name, $hook, &$data = null) {
204         // Don't run a theme's hook if the user isn't using the theme
205         if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false)
206                 return;
207
208         @include_once($hook[0]);
209         if (function_exists($hook[1])) {
210                 $func = $hook[1];
211                 $func($a, $data);
212         } else {
213                 // remove orphan hooks
214                 $condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
215                 dba::delete('hook', $condition);
216         }
217 }
218
219 //check if an app_menu hook exist for plugin $name.
220 //Return true if the plugin is an app
221 function plugin_is_app($name) {
222         $a = get_app();
223
224         if (is_array($a->hooks) && (array_key_exists('app_menu',$a->hooks))) {
225                 foreach ($a->hooks['app_menu'] as $hook) {
226                         if ($hook[0] == 'addon/'.$name.'/'.$name.'.php')
227                                 return true;
228                 }
229         }
230
231         return false;
232 }
233
234 /**
235  * @brief Parse plugin comment in search of plugin infos.
236  *
237  * like
238  * \code
239  *...* Name: Plugin
240  *   * Description: A plugin which plugs in
241  * . * Version: 1.2.3
242  *   * Author: John <profile url>
243  *   * Author: Jane <email>
244  *   *
245  *  *\endcode
246  * @param string $plugin the name of the plugin
247  * @return array with the plugin information
248  */
249
250 function get_plugin_info($plugin) {
251
252         $a = get_app();
253
254         $info=[
255                 'name' => $plugin,
256                 'description' => "",
257                 'author' => [],
258                 'version' => "",
259                 'status' => ""
260         ];
261
262         if (!is_file("addon/$plugin/$plugin.php")) return $info;
263
264         $stamp1 = microtime(true);
265         $f = file_get_contents("addon/$plugin/$plugin.php");
266         $a->save_timestamp($stamp1, "file");
267
268         $r = preg_match("|/\*.*\*/|msU", $f, $m);
269
270         if ($r) {
271                 $ll = explode("\n", $m[0]);
272                 foreach ( $ll as $l ) {
273                         $l = trim($l,"\t\n\r */");
274                         if ($l != "") {
275                                 list($k,$v) = array_map("trim", explode(":",$l,2));
276                                 $k= strtolower($k);
277                                 if ($k == "author") {
278                                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
279                                         if ($r) {
280                                                 $info['author'][] = ['name'=>$m[1], 'link'=>$m[2]];
281                                         } else {
282                                                 $info['author'][] = ['name'=>$v];
283                                         }
284                                 } else {
285                                         if (array_key_exists($k,$info)) {
286                                                 $info[$k]=$v;
287                                         }
288                                 }
289
290                         }
291                 }
292
293         }
294         return $info;
295 }
296
297
298 /**
299  * @brief Parse theme comment in search of theme infos.
300  *
301  * like
302  * \code
303  * ..* Name: My Theme
304  *   * Description: My Cool Theme
305  * . * Version: 1.2.3
306  *   * Author: John <profile url>
307  *   * Maintainer: Jane <profile url>
308  *   *
309  * \endcode
310  * @param string $theme the name of the theme
311  * @return array
312  */
313
314 function get_theme_info($theme) {
315         $info=[
316                 'name' => $theme,
317                 'description' => "",
318                 'author' => [],
319                 'maintainer' => [],
320                 'version' => "",
321                 'credits' => "",
322                 'experimental' => false,
323                 'unsupported' => false
324         ];
325
326         if (file_exists("view/theme/$theme/experimental"))
327                 $info['experimental'] = true;
328         if (file_exists("view/theme/$theme/unsupported"))
329                 $info['unsupported'] = true;
330
331         if (!is_file("view/theme/$theme/theme.php")) return $info;
332
333         $a = get_app();
334         $stamp1 = microtime(true);
335         $f = file_get_contents("view/theme/$theme/theme.php");
336         $a->save_timestamp($stamp1, "file");
337
338         $r = preg_match("|/\*.*\*/|msU", $f, $m);
339
340         if ($r) {
341                 $ll = explode("\n", $m[0]);
342                 foreach ( $ll as $l ) {
343                         $l = trim($l,"\t\n\r */");
344                         if ($l != "") {
345                                 list($k,$v) = array_map("trim", explode(":",$l,2));
346                                 $k= strtolower($k);
347                                 if ($k == "author") {
348
349                                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
350                                         if ($r) {
351                                                 $info['author'][] = ['name'=>$m[1], 'link'=>$m[2]];
352                                         } else {
353                                                 $info['author'][] = ['name'=>$v];
354                                         }
355                                 } elseif ($k == "maintainer") {
356                                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
357                                         if ($r) {
358                                                 $info['maintainer'][] = ['name'=>$m[1], 'link'=>$m[2]];
359                                         } else {
360                                                 $info['maintainer'][] = ['name'=>$v];
361                                         }
362                                 } else {
363                                         if (array_key_exists($k,$info)) {
364                                                 $info[$k]=$v;
365                                         }
366                                 }
367
368                         }
369                 }
370
371         }
372         return $info;
373 }
374
375 /**
376  * @brief Returns the theme's screenshot.
377  *
378  * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
379  *
380  * @param sring $theme The name of the theme
381  * @return string
382  */
383 function get_theme_screenshot($theme) {
384         $exts = ['.png','.jpg'];
385         foreach ($exts as $ext) {
386                 if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
387                         return(System::baseUrl() . '/view/theme/' . $theme . '/screenshot' . $ext);
388                 }
389         }
390         return(System::baseUrl() . '/images/blank.png');
391 }
392
393 // install and uninstall theme
394 function uninstall_theme($theme) {
395         logger("Addons: uninstalling theme " . $theme);
396
397         include_once("view/theme/$theme/theme.php");
398         if (function_exists("{$theme}_uninstall")) {
399                 $func = "{$theme}_uninstall";
400                 $func();
401         }
402 }
403
404 function install_theme($theme) {
405         // silently fail if theme was removed
406
407         if (! file_exists("view/theme/$theme/theme.php")) {
408                 return false;
409         }
410
411         logger("Addons: installing theme $theme");
412
413         include_once("view/theme/$theme/theme.php");
414
415         if (function_exists("{$theme}_install")) {
416                 $func = "{$theme}_install";
417                 $func();
418                 return true;
419         } else {
420                 logger("Addons: FAILED installing theme $theme");
421                 return false;
422         }
423
424 }
425
426 /**
427  * @brief Get the full path to relevant theme files by filename
428  *
429  * This function search in the theme directory (and if not present in global theme directory)
430  * if there is a directory with the file extension and  for a file with the given
431  * filename.
432  *
433  * @param string $file Filename
434  * @param string $root Full root path
435  * @return string Path to the file or empty string if the file isn't found
436  */
437 function theme_include($file, $root = '') {
438         $file = basename($file);
439
440         // Make sure $root ends with a slash / if it's not blank
441         if ($root !== '' && $root[strlen($root)-1] !== '/') {
442                 $root = $root . '/';
443         }
444         $theme_info = get_app()->theme_info;
445         if (is_array($theme_info) && array_key_exists('extends',$theme_info)) {
446                 $parent = $theme_info['extends'];
447         } else {
448                 $parent = 'NOPATH';
449         }
450         $theme = current_theme();
451         $thname = $theme;
452         $ext = substr($file,strrpos($file,'.')+1);
453         $paths = [
454                 "{$root}view/theme/$thname/$ext/$file",
455                 "{$root}view/theme/$parent/$ext/$file",
456                 "{$root}view/$ext/$file",
457         ];
458         foreach ($paths as $p) {
459                 // strpos() is faster than strstr when checking if one string is in another (http://php.net/manual/en/function.strstr.php)
460                 if (strpos($p,'NOPATH') !== false) {
461                         continue;
462                 } elseif (file_exists($p)) {
463                         return $p;
464                 }
465         }
466         return '';
467 }