]> git.mxchange.org Git - friendica.git/blob - include/plugin.php
Merge pull request #2148 from annando/issue-1871
[friendica.git] / include / plugin.php
1 <?php
2 /**
3  * @file include/identity.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(count($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(count($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         if(count($r)) {
191                 foreach($r as $rr) {
192                         if(! array_key_exists($rr['hook'],$a->hooks))
193                                 $a->hooks[$rr['hook']] = array();
194                         $a->hooks[$rr['hook']][] = array($rr['file'],$rr['function']);
195                 }
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 if(! function_exists('call_hooks')) {
209 function call_hooks($name, &$data = null) {
210         $stamp1 = microtime(true);
211
212         $a = get_app();
213
214         #logger($name, LOGGER_ALL);
215
216         if((is_array($a->hooks)) && (array_key_exists($name,$a->hooks))) {
217                 foreach($a->hooks[$name] as $hook) {
218                         // Don't run a theme's hook if the user isn't using the theme
219                         if(strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false)
220                                 continue;
221
222                         @include_once($hook[0]);
223                         if(function_exists($hook[1])) {
224                                 $func = $hook[1];
225                                 //logger($name." => ".$hook[0].":".$func."()", LOGGER_DEBUG);
226                                 $func($a,$data);
227                         }
228                         else {
229                                 // remove orphan hooks
230                                 q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
231                                         dbesc($name),
232                                         dbesc($hook[0]),
233                                         dbesc($hook[1])
234                                 );
235                         }
236                 }
237         }
238 }}
239
240 //check if an app_menu hook exist for plugin $name.
241 //Return true if the plugin is an app
242 if(! function_exists('plugin_is_app')) {
243 function plugin_is_app($name) {
244         $a = get_app();
245
246         if(is_array($a->hooks) && (array_key_exists('app_menu',$a->hooks))) {
247                 foreach($a->hooks['app_menu'] as $hook) {
248                         if($hook[0] == 'addon/'.$name.'/'.$name.'.php')
249                                 return true;
250                 }
251         }
252
253         return false;
254 }}
255
256 /**
257  * @brief Parse plugin comment in search of plugin infos.
258  *
259  * like
260  * \code
261  *...* Name: Plugin
262  *   * Description: A plugin which plugs in
263  * . * Version: 1.2.3
264  *   * Author: John <profile url>
265  *   * Author: Jane <email>
266  *   *
267  *  *\endcode
268  * @param string $plugin the name of the plugin
269  * @return array with the plugin information
270  */
271
272 if (! function_exists('get_plugin_info')){
273 function get_plugin_info($plugin){
274
275         $a = get_app();
276
277         $info=Array(
278                 'name' => $plugin,
279                 'description' => "",
280                 'author' => array(),
281                 'version' => "",
282                 'status' => ""
283         );
284
285         if (!is_file("addon/$plugin/$plugin.php")) return $info;
286
287         $stamp1 = microtime(true);
288         $f = file_get_contents("addon/$plugin/$plugin.php");
289         $a->save_timestamp($stamp1, "file");
290
291         $r = preg_match("|/\*.*\*/|msU", $f, $m);
292
293         if ($r){
294                 $ll = explode("\n", $m[0]);
295                 foreach( $ll as $l ) {
296                         $l = trim($l,"\t\n\r */");
297                         if ($l!=""){
298                                 list($k,$v) = array_map("trim", explode(":",$l,2));
299                                 $k= strtolower($k);
300                                 if ($k=="author"){
301                                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
302                                         if ($r) {
303                                                 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
304                                         } else {
305                                                 $info['author'][] = array('name'=>$v);
306                                         }
307                                 } else {
308                                         if (array_key_exists($k,$info)){
309                                                 $info[$k]=$v;
310                                         }
311                                 }
312
313                         }
314                 }
315
316         }
317         return $info;
318 }}
319
320
321 /**
322  * @brief Parse theme comment in search of theme infos.
323  * 
324  * like
325  * \code
326  * ..* Name: My Theme
327  *   * Description: My Cool Theme
328  * . * Version: 1.2.3
329  *   * Author: John <profile url>
330  *   * Maintainer: Jane <profile url>
331  *   *
332  * \endcode
333  * @param string $theme the name of the theme
334  * @return array
335  */
336
337 if (! function_exists('get_theme_info')){
338 function get_theme_info($theme){
339         $info=Array(
340                 'name' => $theme,
341                 'description' => "",
342                 'author' => array(),
343                 'maintainer' => array(),
344                 'version' => "",
345                 'credits' => "",
346                 'experimental' => false,
347                 'unsupported' => false
348         );
349
350         if(file_exists("view/theme/$theme/experimental"))
351                 $info['experimental'] = true;
352         if(file_exists("view/theme/$theme/unsupported"))
353                 $info['unsupported'] = true;
354
355         if (!is_file("view/theme/$theme/theme.php")) return $info;
356
357         $a = get_app();
358         $stamp1 = microtime(true);
359         $f = file_get_contents("view/theme/$theme/theme.php");
360         $a->save_timestamp($stamp1, "file");
361
362         $r = preg_match("|/\*.*\*/|msU", $f, $m);
363
364         if ($r){
365                 $ll = explode("\n", $m[0]);
366                 foreach( $ll as $l ) {
367                         $l = trim($l,"\t\n\r */");
368                         if ($l!=""){
369                                 list($k,$v) = array_map("trim", explode(":",$l,2));
370                                 $k= strtolower($k);
371                                 if ($k=="author"){
372
373                                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
374                                         if ($r) {
375                                                 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
376                                         } else {
377                                                 $info['author'][] = array('name'=>$v);
378                                         }
379                                 }
380                                 elseif ($k=="maintainer"){
381                                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
382                                         if ($r) {
383                                                 $info['maintainer'][] = array('name'=>$m[1], 'link'=>$m[2]);
384                                         } else {
385                                                 $info['maintainer'][] = array('name'=>$v);
386                                         }
387                                 } else {
388                                         if (array_key_exists($k,$info)){
389                                                 $info[$k]=$v;
390                                         }
391                                 }
392
393                         }
394                 }
395
396         }
397         return $info;
398 }}
399
400 /**
401  * @brief Returns the theme's screenshot.
402  *
403  * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
404  *
405  * @param sring $theme The name of the theme
406  * @return string
407  */
408 function get_theme_screenshot($theme) {
409         $a = get_app();
410         $exts = array('.png','.jpg');
411         foreach($exts as $ext) {
412                 if(file_exists('view/theme/' . $theme . '/screenshot' . $ext))
413                         return($a->get_baseurl() . '/view/theme/' . $theme . '/screenshot' . $ext);
414         }
415         return($a->get_baseurl() . '/images/blank.png');
416 }
417
418 // install and uninstall theme
419 if (! function_exists('uninstall_theme')){
420 function uninstall_theme($theme){
421         logger("Addons: uninstalling theme " . $theme);
422
423         @include_once("view/theme/$theme/theme.php");
424         if(function_exists("{$theme}_uninstall")) {
425                 $func = "{$theme}_uninstall";
426                 $func();
427         }
428 }}
429
430 if (! function_exists('install_theme')){
431 function install_theme($theme) {
432         // silently fail if theme was removed
433
434         if(! file_exists("view/theme/$theme/theme.php"))
435                 return false;
436
437         logger("Addons: installing theme $theme");
438
439         @include_once("view/theme/$theme/theme.php");
440
441         if(function_exists("{$theme}_install")) {
442                 $func = "{$theme}_install";
443                 $func();
444                 return true;
445         }
446         else {
447                 logger("Addons: FAILED installing theme $theme");
448                 return false;
449         }
450
451 }}
452
453
454
455 // check service_class restrictions. If there are no service_classes defined, everything is allowed.
456 // if $usage is supplied, we check against a maximum count and return true if the current usage is
457 // less than the subscriber plan allows. Otherwise we return boolean true or false if the property
458 // is allowed (or not) in this subscriber plan. An unset property for this service plan means
459 // the property is allowed, so it is only necessary to provide negative properties for each plan,
460 // or what the subscriber is not allowed to do.
461
462
463 function service_class_allows($uid,$property,$usage = false) {
464
465         if($uid == local_user()) {
466                 $service_class = $a->user['service_class'];
467         }
468         else {
469                 $r = q("SELECT `service_class` FROM `user` WHERE `uid` = %d LIMIT 1",
470                         intval($uid)
471                 );
472                 if($r !== false and count($r)) {
473                         $service_class = $r[0]['service_class'];
474                 }
475         }
476         if(! x($service_class))
477                 return true; // everything is allowed
478
479         $arr = get_config('service_class',$service_class);
480         if(! is_array($arr) || (! count($arr)))
481                 return true;
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                 return (((intval($usage)) < intval($arr[$property])) ? true : false);
489         }
490 }
491
492
493 function service_class_fetch($uid,$property) {
494
495         if($uid == local_user()) {
496                 $service_class = $a->user['service_class'];
497         }
498         else {
499                 $r = q("SELECT `service_class` FROM `user` WHERE `uid` = %d LIMIT 1",
500                         intval($uid)
501                 );
502                 if($r !== false and count($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         if($bbcode)
522                 $t = sprintf('[url=%s]' . t('Click here to upgrade.') . '[/url]', $l);
523         else
524                 $t = sprintf('<a href="%s">' . t('Click here to upgrade.') . '</div>', $l);
525         return $t;
526 }
527
528 function upgrade_message($bbcode = false) {
529         $x = upgrade_link($bbcode);
530         return t('This action exceeds the limits set by your subscription plan.') . (($x) ? ' ' . $x : '') ;
531 }
532
533 function upgrade_bool_message($bbcode = false) {
534         $x = upgrade_link($bbcode);
535         return t('This action is not available under your subscription plan.') . (($x) ? ' ' . $x : '') ;
536 }