]> git.mxchange.org Git - friendica.git/blob - include/plugin.php
Degrade priority step by step
[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
10 /**
11  * @brief uninstalls an addon.
12  *
13  * @param string $plugin name of the addon
14  * @return boolean
15  */
16 if (! function_exists('uninstall_plugin')){
17 function uninstall_plugin($plugin){
18         logger("Addons: uninstalling " . $plugin);
19         q("DELETE FROM `addon` WHERE `name` = '%s' ",
20                 dbesc($plugin)
21         );
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 if (! function_exists('install_plugin')){
37 function install_plugin($plugin) {
38         // silently fail if plugin was removed
39
40         if (! file_exists('addon/' . $plugin . '/' . $plugin . '.php'))
41                 return false;
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                 $r = q("INSERT INTO `addon` (`name`, `installed`, `timestamp`, `plugin_admin`) VALUES ( '%s', 1, %d , %d ) ",
52                         dbesc($plugin),
53                         intval($t),
54                         $plugin_admin
55                 );
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                         q("UPDATE `addon` SET `hidden` = 1 WHERE `name` = '%s'",
63                                 dbesc($plugin)
64                         );
65                 }
66                 return true;
67         }
68         else {
69                 logger("Addons: FAILED installing " . $plugin);
70                 return false;
71         }
72
73 }}
74
75 // reload all updated plugins
76
77 if (! function_exists('reload_plugins')) {
78 function reload_plugins() {
79         $plugins = get_config('system','addon');
80         if (strlen($plugins)) {
81
82                 $r = q("SELECT * FROM `addon` WHERE `installed` = 1");
83                 if (dbm::is_result($r))
84                         $installed = $r;
85                 else
86                         $installed = array();
87
88                 $parr = explode(',',$plugins);
89
90                 if (count($parr)) {
91                         foreach ($parr as $pl) {
92
93                                 $pl = trim($pl);
94
95                                 $fname = 'addon/' . $pl . '/' . $pl . '.php';
96
97                                 if (file_exists($fname)) {
98                                         $t = @filemtime($fname);
99                                         foreach ($installed as $i) {
100                                                 if (($i['name'] == $pl) && ($i['timestamp'] != $t)) {
101                                                         logger('Reloading plugin: ' . $i['name']);
102                                                         @include_once($fname);
103
104                                                         if (function_exists($pl . '_uninstall')) {
105                                                                 $func = $pl . '_uninstall';
106                                                                 $func();
107                                                         }
108                                                         if (function_exists($pl . '_install')) {
109                                                                 $func = $pl . '_install';
110                                                                 $func();
111                                                         }
112                                                         q("UPDATE `addon` SET `timestamp` = %d WHERE `id` = %d",
113                                                                 intval($t),
114                                                                 intval($i['id'])
115                                                         );
116                                                 }
117                                         }
118                                 }
119                         }
120                 }
121         }
122
123 }}
124
125 /**
126  * @brief check if addon is enabled
127  *
128  * @param string $plugin
129  * @return boolean
130  */
131 function plugin_enabled($plugin) {
132         $r = q("SELECT * FROM `addon` WHERE `installed` = 1 AND `name` = '%s'", $plugin);
133         return ((dbm::is_result($r)) && (count($r) > 0));
134 }
135
136
137 /**
138  * @brief registers a hook.
139  *
140  * @param string $hook the name of the hook
141  * @param string $file the name of the file that hooks into
142  * @param string $function the name of the function that the hook will call
143  * @param int $priority A priority (defaults to 0)
144  * @return mixed|bool
145  */
146 if (! function_exists('register_hook')) {
147 function register_hook($hook,$file,$function,$priority=0) {
148
149         $r = q("SELECT * FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' LIMIT 1",
150                 dbesc($hook),
151                 dbesc($file),
152                 dbesc($function)
153         );
154         if (dbm::is_result($r))
155                 return true;
156
157         $r = q("INSERT INTO `hook` (`hook`, `file`, `function`, `priority`) VALUES ( '%s', '%s', '%s', '%s' ) ",
158                 dbesc($hook),
159                 dbesc($file),
160                 dbesc($function),
161                 dbesc($priority)
162         );
163         return $r;
164 }}
165
166 /**
167  * @brief unregisters a hook.
168  *
169  * @param string $hook the name of the hook
170  * @param string $file the name of the file that hooks into
171  * @param string $function the name of the function that the hook called
172  * @return array
173  */
174 if (! function_exists('unregister_hook')) {
175 function unregister_hook($hook,$file,$function) {
176
177         $r = q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
178                 dbesc($hook),
179                 dbesc($file),
180                 dbesc($function)
181         );
182         return $r;
183 }}
184
185
186 if (! function_exists('load_hooks')) {
187 function load_hooks() {
188         $a = get_app();
189         $a->hooks = array();
190         $r = q("SELECT * FROM `hook` WHERE 1 ORDER BY `priority` DESC, `file`");
191
192         if (dbm::is_result($r)) {
193                 foreach ($r as $rr) {
194                         if (! array_key_exists($rr['hook'],$a->hooks))
195                                 $a->hooks[$rr['hook']] = array();
196                         $a->hooks[$rr['hook']][] = array($rr['file'],$rr['function']);
197                 }
198         }
199 }}
200
201 /**
202  * @brief Calls a hook.
203  *
204  * Use this function when you want to be able to allow a hook to manipulate
205  * the provided data.
206  *
207  * @param string $name of the hook to call
208  * @param string|array &$data to transmit to the callback handler
209  */
210 function call_hooks($name, &$data = null) {
211         $stamp1 = microtime(true);
212
213         $a = get_app();
214
215         if (is_array($a->hooks) && array_key_exists($name, $a->hooks))
216                 foreach ($a->hooks[$name] as $hook)
217                         call_single_hook($a, $name, $hook, $data);
218 }
219
220 /**
221  * @brief Calls a single hook.
222  *
223  * @param string $name of the hook to call
224  * @param array $hook Hook data
225  * @param string|array &$data to transmit to the callback handler
226  */
227 function call_single_hook($a, $name, $hook, &$data = null) {
228         // Don't run a theme's hook if the user isn't using the theme
229         if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false)
230                 return;
231
232         @include_once($hook[0]);
233         if (function_exists($hook[1])) {
234                 $func = $hook[1];
235                 $func($a, $data);
236         } else {
237                 // remove orphan hooks
238                 q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
239                         dbesc($name),
240                         dbesc($hook[0]),
241                         dbesc($hook[1])
242                 );
243         }
244 }
245
246 //check if an app_menu hook exist for plugin $name.
247 //Return true if the plugin is an app
248 if (! function_exists('plugin_is_app')) {
249 function plugin_is_app($name) {
250         $a = get_app();
251
252         if (is_array($a->hooks) && (array_key_exists('app_menu',$a->hooks))) {
253                 foreach ($a->hooks['app_menu'] as $hook) {
254                         if ($hook[0] == 'addon/'.$name.'/'.$name.'.php')
255                                 return true;
256                 }
257         }
258
259         return false;
260 }}
261
262 /**
263  * @brief Parse plugin comment in search of plugin infos.
264  *
265  * like
266  * \code
267  *...* Name: Plugin
268  *   * Description: A plugin which plugs in
269  * . * Version: 1.2.3
270  *   * Author: John <profile url>
271  *   * Author: Jane <email>
272  *   *
273  *  *\endcode
274  * @param string $plugin the name of the plugin
275  * @return array with the plugin information
276  */
277
278 if (! function_exists('get_plugin_info')){
279 function get_plugin_info($plugin){
280
281         $a = get_app();
282
283         $info=Array(
284                 'name' => $plugin,
285                 'description' => "",
286                 'author' => array(),
287                 'version' => "",
288                 'status' => ""
289         );
290
291         if (!is_file("addon/$plugin/$plugin.php")) return $info;
292
293         $stamp1 = microtime(true);
294         $f = file_get_contents("addon/$plugin/$plugin.php");
295         $a->save_timestamp($stamp1, "file");
296
297         $r = preg_match("|/\*.*\*/|msU", $f, $m);
298
299         if ($r){
300                 $ll = explode("\n", $m[0]);
301                 foreach ( $ll as $l ) {
302                         $l = trim($l,"\t\n\r */");
303                         if ($l!=""){
304                                 list($k,$v) = array_map("trim", explode(":",$l,2));
305                                 $k= strtolower($k);
306                                 if ($k=="author"){
307                                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
308                                         if ($r) {
309                                                 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
310                                         } else {
311                                                 $info['author'][] = array('name'=>$v);
312                                         }
313                                 } else {
314                                         if (array_key_exists($k,$info)){
315                                                 $info[$k]=$v;
316                                         }
317                                 }
318
319                         }
320                 }
321
322         }
323         return $info;
324 }}
325
326
327 /**
328  * @brief Parse theme comment in search of theme infos.
329  *
330  * like
331  * \code
332  * ..* Name: My Theme
333  *   * Description: My Cool Theme
334  * . * Version: 1.2.3
335  *   * Author: John <profile url>
336  *   * Maintainer: Jane <profile url>
337  *   *
338  * \endcode
339  * @param string $theme the name of the theme
340  * @return array
341  */
342
343 if (! function_exists('get_theme_info')){
344 function get_theme_info($theme){
345         $info=Array(
346                 'name' => $theme,
347                 'description' => "",
348                 'author' => array(),
349                 'maintainer' => array(),
350                 'version' => "",
351                 'credits' => "",
352                 'experimental' => false,
353                 'unsupported' => false
354         );
355
356         if (file_exists("view/theme/$theme/experimental"))
357                 $info['experimental'] = true;
358         if (file_exists("view/theme/$theme/unsupported"))
359                 $info['unsupported'] = true;
360
361         if (!is_file("view/theme/$theme/theme.php")) return $info;
362
363         $a = get_app();
364         $stamp1 = microtime(true);
365         $f = file_get_contents("view/theme/$theme/theme.php");
366         $a->save_timestamp($stamp1, "file");
367
368         $r = preg_match("|/\*.*\*/|msU", $f, $m);
369
370         if ($r){
371                 $ll = explode("\n", $m[0]);
372                 foreach ( $ll as $l ) {
373                         $l = trim($l,"\t\n\r */");
374                         if ($l!=""){
375                                 list($k,$v) = array_map("trim", explode(":",$l,2));
376                                 $k= strtolower($k);
377                                 if ($k=="author"){
378
379                                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
380                                         if ($r) {
381                                                 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
382                                         } else {
383                                                 $info['author'][] = array('name'=>$v);
384                                         }
385                                 }
386                                 elseif ($k=="maintainer"){
387                                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
388                                         if ($r) {
389                                                 $info['maintainer'][] = array('name'=>$m[1], 'link'=>$m[2]);
390                                         } else {
391                                                 $info['maintainer'][] = array('name'=>$v);
392                                         }
393                                 } else {
394                                         if (array_key_exists($k,$info)){
395                                                 $info[$k]=$v;
396                                         }
397                                 }
398
399                         }
400                 }
401
402         }
403         return $info;
404 }}
405
406 /**
407  * @brief Returns the theme's screenshot.
408  *
409  * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
410  *
411  * @param sring $theme The name of the theme
412  * @return string
413  */
414 function get_theme_screenshot($theme) {
415         $exts = array('.png','.jpg');
416         foreach ($exts as $ext) {
417                 if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
418                         return(App::get_baseurl() . '/view/theme/' . $theme . '/screenshot' . $ext);
419                 }
420         }
421         return(App::get_baseurl() . '/images/blank.png');
422 }
423
424 // install and uninstall theme
425 if (! function_exists('uninstall_theme')){
426 function uninstall_theme($theme){
427         logger("Addons: uninstalling theme " . $theme);
428
429         include_once("view/theme/$theme/theme.php");
430         if (function_exists("{$theme}_uninstall")) {
431                 $func = "{$theme}_uninstall";
432                 $func();
433         }
434 }}
435
436 if (! function_exists('install_theme')){
437 function install_theme($theme) {
438         // silently fail if theme was removed
439
440         if (! file_exists("view/theme/$theme/theme.php")) {
441                 return false;
442         }
443
444         logger("Addons: installing theme $theme");
445
446         include_once("view/theme/$theme/theme.php");
447
448         if (function_exists("{$theme}_install")) {
449                 $func = "{$theme}_install";
450                 $func();
451                 return true;
452         } else {
453                 logger("Addons: FAILED installing theme $theme");
454                 return false;
455         }
456
457 }}
458
459
460
461 // check service_class restrictions. If there are no service_classes defined, everything is allowed.
462 // if $usage is supplied, we check against a maximum count and return true if the current usage is
463 // less than the subscriber plan allows. Otherwise we return boolean true or false if the property
464 // is allowed (or not) in this subscriber plan. An unset property for this service plan means
465 // the property is allowed, so it is only necessary to provide negative properties for each plan,
466 // or what the subscriber is not allowed to do.
467
468
469 function service_class_allows($uid,$property,$usage = false) {
470
471         if ($uid == local_user()) {
472                 $service_class = $a->user['service_class'];
473         } else {
474                 $r = q("SELECT `service_class` FROM `user` WHERE `uid` = %d LIMIT 1",
475                         intval($uid)
476                 );
477                 if (dbm::is_result($r)) {
478                         $service_class = $r[0]['service_class'];
479                 }
480         }
481
482         if (! x($service_class)) {
483                 // everything is allowed
484                 return true;
485         }
486
487         $arr = get_config('service_class',$service_class);
488         if (! is_array($arr) || (! count($arr))) {
489                 return true;
490         }
491
492         if ($usage === false) {
493                 return ((x($arr[$property])) ? (bool) $arr['property'] : true);
494         } else {
495                 if (! array_key_exists($property,$arr)) {
496                         return true;
497                 }
498                 return (((intval($usage)) < intval($arr[$property])) ? true : false);
499         }
500 }
501
502
503 function service_class_fetch($uid,$property) {
504
505         if ($uid == local_user()) {
506                 $service_class = $a->user['service_class'];
507         } else {
508                 $r = q("SELECT `service_class` FROM `user` WHERE `uid` = %d LIMIT 1",
509                         intval($uid)
510                 );
511                 if (dbm::is_result($r)) {
512                         $service_class = $r[0]['service_class'];
513                 }
514         }
515         if (! x($service_class))
516                 return false; // everything is allowed
517
518         $arr = get_config('service_class',$service_class);
519         if (! is_array($arr) || (! count($arr)))
520                 return false;
521
522         return((array_key_exists($property,$arr)) ? $arr[$property] : false);
523
524 }
525
526 function upgrade_link($bbcode = false) {
527         $l = get_config('service_class','upgrade_link');
528         if (! $l) {
529                 return '';
530         }
531         if ($bbcode) {
532                 $t = sprintf('[url=%s]' . t('Click here to upgrade.') . '[/url]', $l);
533         } else {
534                 $t = sprintf('<a href="%s">' . t('Click here to upgrade.') . '</div>', $l);
535         }
536         return $t;
537 }
538
539 function upgrade_message($bbcode = false) {
540         $x = upgrade_link($bbcode);
541         return t('This action exceeds the limits set by your subscription plan.') . (($x) ? ' ' . $x : '') ;
542 }
543
544 function upgrade_bool_message($bbcode = false) {
545         $x = upgrade_link($bbcode);
546         return t('This action is not available under your subscription plan.') . (($x) ? ' ' . $x : '') ;
547 }
548
549 /**
550  * @brief Get the full path to relevant theme files by filename
551  *
552  * This function search in the theme directory (and if not present in global theme directory)
553  * if there is a directory with the file extension and  for a file with the given
554  * filename.
555  *
556  * @param string $file Filename
557  * @param string $root Full root path
558  * @return string Path to the file or empty string if the file isn't found
559  */
560 function theme_include($file, $root = '') {
561         // Make sure $root ends with a slash / if it's not blank
562         if ($root !== '' && $root[strlen($root)-1] !== '/') {
563                 $root = $root . '/';
564         }
565         $theme_info = $a->theme_info;
566         if (is_array($theme_info) && array_key_exists('extends',$theme_info)) {
567                 $parent = $theme_info['extends'];
568         } else {
569                 $parent = 'NOPATH';
570         }
571         $theme = current_theme();
572         $thname = $theme;
573         $ext = substr($file,strrpos($file,'.')+1);
574         $paths = array(
575                 "{$root}view/theme/$thname/$ext/$file",
576                 "{$root}view/theme/$parent/$ext/$file",
577                 "{$root}view/$ext/$file",
578         );
579         foreach ($paths as $p) {
580                 // strpos() is faster than strstr when checking if one string is in another (http://php.net/manual/en/function.strstr.php)
581                 if (strpos($p,'NOPATH') !== false) {
582                         continue;
583                 } elseif (file_exists($p)) {
584                         return $p;
585                 }
586         }
587         return '';
588 }