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