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