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