]> git.mxchange.org Git - friendica.git/blob - include/plugin.php
60ef6138b9c4712145fb3c69ecf9d2e7c574936f
[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 function load_hooks() {
187         $a = get_app();
188         $a->hooks = array();
189         $r = dba::select('hook', array('hook', 'file', 'function'), array(), array('order' => array('priority' => 'desc', 'file')));
190
191         while ($rr = dba::fetch($r)) {
192                 if (! array_key_exists($rr['hook'],$a->hooks)) {
193                         $a->hooks[$rr['hook']] = array();
194                 }
195                 $a->hooks[$rr['hook']][] = array($rr['file'],$rr['function']);
196         }
197 }
198
199 /**
200  * @brief Calls a hook.
201  *
202  * Use this function when you want to be able to allow a hook to manipulate
203  * the provided data.
204  *
205  * @param string $name of the hook to call
206  * @param string|array &$data to transmit to the callback handler
207  */
208 function call_hooks($name, &$data = null) {
209         $stamp1 = microtime(true);
210
211         $a = get_app();
212
213         if (is_array($a->hooks) && array_key_exists($name, $a->hooks))
214                 foreach ($a->hooks[$name] as $hook)
215                         call_single_hook($a, $name, $hook, $data);
216 }
217
218 /**
219  * @brief Calls a single hook.
220  *
221  * @param string $name of the hook to call
222  * @param array $hook Hook data
223  * @param string|array &$data to transmit to the callback handler
224  */
225 function call_single_hook($a, $name, $hook, &$data = null) {
226         // Don't run a theme's hook if the user isn't using the theme
227         if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false)
228                 return;
229
230         @include_once($hook[0]);
231         if (function_exists($hook[1])) {
232                 $func = $hook[1];
233                 $func($a, $data);
234         } else {
235                 // remove orphan hooks
236                 q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
237                         dbesc($name),
238                         dbesc($hook[0]),
239                         dbesc($hook[1])
240                 );
241         }
242 }
243
244 //check if an app_menu hook exist for plugin $name.
245 //Return true if the plugin is an app
246 if (! function_exists('plugin_is_app')) {
247 function plugin_is_app($name) {
248         $a = get_app();
249
250         if (is_array($a->hooks) && (array_key_exists('app_menu',$a->hooks))) {
251                 foreach ($a->hooks['app_menu'] as $hook) {
252                         if ($hook[0] == 'addon/'.$name.'/'.$name.'.php')
253                                 return true;
254                 }
255         }
256
257         return false;
258 }}
259
260 /**
261  * @brief Parse plugin comment in search of plugin infos.
262  *
263  * like
264  * \code
265  *...* Name: Plugin
266  *   * Description: A plugin which plugs in
267  * . * Version: 1.2.3
268  *   * Author: John <profile url>
269  *   * Author: Jane <email>
270  *   *
271  *  *\endcode
272  * @param string $plugin the name of the plugin
273  * @return array with the plugin information
274  */
275
276 if (! function_exists('get_plugin_info')){
277 function get_plugin_info($plugin){
278
279         $a = get_app();
280
281         $info=Array(
282                 'name' => $plugin,
283                 'description' => "",
284                 'author' => array(),
285                 'version' => "",
286                 'status' => ""
287         );
288
289         if (!is_file("addon/$plugin/$plugin.php")) return $info;
290
291         $stamp1 = microtime(true);
292         $f = file_get_contents("addon/$plugin/$plugin.php");
293         $a->save_timestamp($stamp1, "file");
294
295         $r = preg_match("|/\*.*\*/|msU", $f, $m);
296
297         if ($r){
298                 $ll = explode("\n", $m[0]);
299                 foreach ( $ll as $l ) {
300                         $l = trim($l,"\t\n\r */");
301                         if ($l!=""){
302                                 list($k,$v) = array_map("trim", explode(":",$l,2));
303                                 $k= strtolower($k);
304                                 if ($k=="author"){
305                                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
306                                         if ($r) {
307                                                 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
308                                         } else {
309                                                 $info['author'][] = array('name'=>$v);
310                                         }
311                                 } else {
312                                         if (array_key_exists($k,$info)){
313                                                 $info[$k]=$v;
314                                         }
315                                 }
316
317                         }
318                 }
319
320         }
321         return $info;
322 }}
323
324
325 /**
326  * @brief Parse theme comment in search of theme infos.
327  *
328  * like
329  * \code
330  * ..* Name: My Theme
331  *   * Description: My Cool Theme
332  * . * Version: 1.2.3
333  *   * Author: John <profile url>
334  *   * Maintainer: Jane <profile url>
335  *   *
336  * \endcode
337  * @param string $theme the name of the theme
338  * @return array
339  */
340
341 if (! function_exists('get_theme_info')){
342 function get_theme_info($theme){
343         $info=Array(
344                 'name' => $theme,
345                 'description' => "",
346                 'author' => array(),
347                 'maintainer' => array(),
348                 'version' => "",
349                 'credits' => "",
350                 'experimental' => false,
351                 'unsupported' => false
352         );
353
354         if (file_exists("view/theme/$theme/experimental"))
355                 $info['experimental'] = true;
356         if (file_exists("view/theme/$theme/unsupported"))
357                 $info['unsupported'] = true;
358
359         if (!is_file("view/theme/$theme/theme.php")) return $info;
360
361         $a = get_app();
362         $stamp1 = microtime(true);
363         $f = file_get_contents("view/theme/$theme/theme.php");
364         $a->save_timestamp($stamp1, "file");
365
366         $r = preg_match("|/\*.*\*/|msU", $f, $m);
367
368         if ($r){
369                 $ll = explode("\n", $m[0]);
370                 foreach ( $ll as $l ) {
371                         $l = trim($l,"\t\n\r */");
372                         if ($l!=""){
373                                 list($k,$v) = array_map("trim", explode(":",$l,2));
374                                 $k= strtolower($k);
375                                 if ($k=="author"){
376
377                                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
378                                         if ($r) {
379                                                 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
380                                         } else {
381                                                 $info['author'][] = array('name'=>$v);
382                                         }
383                                 }
384                                 elseif ($k=="maintainer"){
385                                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
386                                         if ($r) {
387                                                 $info['maintainer'][] = array('name'=>$m[1], 'link'=>$m[2]);
388                                         } else {
389                                                 $info['maintainer'][] = array('name'=>$v);
390                                         }
391                                 } else {
392                                         if (array_key_exists($k,$info)){
393                                                 $info[$k]=$v;
394                                         }
395                                 }
396
397                         }
398                 }
399
400         }
401         return $info;
402 }}
403
404 /**
405  * @brief Returns the theme's screenshot.
406  *
407  * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
408  *
409  * @param sring $theme The name of the theme
410  * @return string
411  */
412 function get_theme_screenshot($theme) {
413         $exts = array('.png','.jpg');
414         foreach ($exts as $ext) {
415                 if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
416                         return(App::get_baseurl() . '/view/theme/' . $theme . '/screenshot' . $ext);
417                 }
418         }
419         return(App::get_baseurl() . '/images/blank.png');
420 }
421
422 // install and uninstall theme
423 if (! function_exists('uninstall_theme')){
424 function uninstall_theme($theme){
425         logger("Addons: uninstalling theme " . $theme);
426
427         include_once("view/theme/$theme/theme.php");
428         if (function_exists("{$theme}_uninstall")) {
429                 $func = "{$theme}_uninstall";
430                 $func();
431         }
432 }}
433
434 if (! function_exists('install_theme')){
435 function install_theme($theme) {
436         // silently fail if theme was removed
437
438         if (! file_exists("view/theme/$theme/theme.php")) {
439                 return false;
440         }
441
442         logger("Addons: installing theme $theme");
443
444         include_once("view/theme/$theme/theme.php");
445
446         if (function_exists("{$theme}_install")) {
447                 $func = "{$theme}_install";
448                 $func();
449                 return true;
450         } else {
451                 logger("Addons: FAILED installing theme $theme");
452                 return false;
453         }
454
455 }}
456
457
458
459 // check service_class restrictions. If there are no service_classes defined, everything is allowed.
460 // if $usage is supplied, we check against a maximum count and return true if the current usage is
461 // less than the subscriber plan allows. Otherwise we return boolean true or false if the property
462 // is allowed (or not) in this subscriber plan. An unset property for this service plan means
463 // the property is allowed, so it is only necessary to provide negative properties for each plan,
464 // or what the subscriber is not allowed to do.
465
466
467 function service_class_allows($uid,$property,$usage = false) {
468
469         if ($uid == local_user()) {
470                 $service_class = $a->user['service_class'];
471         } else {
472                 $r = q("SELECT `service_class` FROM `user` WHERE `uid` = %d LIMIT 1",
473                         intval($uid)
474                 );
475                 if (dbm::is_result($r)) {
476                         $service_class = $r[0]['service_class'];
477                 }
478         }
479
480         if (! x($service_class)) {
481                 // everything is allowed
482                 return true;
483         }
484
485         $arr = get_config('service_class',$service_class);
486         if (! is_array($arr) || (! count($arr))) {
487                 return true;
488         }
489
490         if ($usage === false) {
491                 return ((x($arr[$property])) ? (bool) $arr['property'] : true);
492         } else {
493                 if (! array_key_exists($property,$arr)) {
494                         return true;
495                 }
496                 return (((intval($usage)) < intval($arr[$property])) ? true : false);
497         }
498 }
499
500
501 function service_class_fetch($uid,$property) {
502
503         if ($uid == local_user()) {
504                 $service_class = $a->user['service_class'];
505         } else {
506                 $r = q("SELECT `service_class` FROM `user` WHERE `uid` = %d LIMIT 1",
507                         intval($uid)
508                 );
509                 if (dbm::is_result($r)) {
510                         $service_class = $r[0]['service_class'];
511                 }
512         }
513         if (! x($service_class))
514                 return false; // everything is allowed
515
516         $arr = get_config('service_class',$service_class);
517         if (! is_array($arr) || (! count($arr)))
518                 return false;
519
520         return((array_key_exists($property,$arr)) ? $arr[$property] : false);
521
522 }
523
524 function upgrade_link($bbcode = false) {
525         $l = get_config('service_class','upgrade_link');
526         if (! $l) {
527                 return '';
528         }
529         if ($bbcode) {
530                 $t = sprintf('[url=%s]' . t('Click here to upgrade.') . '[/url]', $l);
531         } else {
532                 $t = sprintf('<a href="%s">' . t('Click here to upgrade.') . '</div>', $l);
533         }
534         return $t;
535 }
536
537 function upgrade_message($bbcode = false) {
538         $x = upgrade_link($bbcode);
539         return t('This action exceeds the limits set by your subscription plan.') . (($x) ? ' ' . $x : '') ;
540 }
541
542 function upgrade_bool_message($bbcode = false) {
543         $x = upgrade_link($bbcode);
544         return t('This action is not available under your subscription plan.') . (($x) ? ' ' . $x : '') ;
545 }
546
547 /**
548  * @brief Get the full path to relevant theme files by filename
549  *
550  * This function search in the theme directory (and if not present in global theme directory)
551  * if there is a directory with the file extension and  for a file with the given
552  * filename.
553  *
554  * @param string $file Filename
555  * @param string $root Full root path
556  * @return string Path to the file or empty string if the file isn't found
557  */
558 function theme_include($file, $root = '') {
559         // Make sure $root ends with a slash / if it's not blank
560         if ($root !== '' && $root[strlen($root)-1] !== '/') {
561                 $root = $root . '/';
562         }
563         $theme_info = get_app()->theme_info;
564         if (is_array($theme_info) && array_key_exists('extends',$theme_info)) {
565                 $parent = $theme_info['extends'];
566         } else {
567                 $parent = 'NOPATH';
568         }
569         $theme = current_theme();
570         $thname = $theme;
571         $ext = substr($file,strrpos($file,'.')+1);
572         $paths = array(
573                 "{$root}view/theme/$thname/$ext/$file",
574                 "{$root}view/theme/$parent/$ext/$file",
575                 "{$root}view/$ext/$file",
576         );
577         foreach ($paths as $p) {
578                 // strpos() is faster than strstr when checking if one string is in another (http://php.net/manual/en/function.strstr.php)
579                 if (strpos($p,'NOPATH') !== false) {
580                         continue;
581                 } elseif (file_exists($p)) {
582                         return $p;
583                 }
584         }
585         return '';
586 }