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