]> git.mxchange.org Git - friendica.git/blob - include/plugin.php
forumlist: nodeinfo_plugin_enabled() is now plugin_enabled()
[friendica.git] / include / plugin.php
1 <?php
2 /*
3  * @file include/identity.php
4  */
5
6 // install and uninstall plugin
7 if (! function_exists('uninstall_plugin')){
8 function uninstall_plugin($plugin){
9         logger("Addons: uninstalling " . $plugin);
10         q("DELETE FROM `addon` WHERE `name` = '%s' ",
11                 dbesc($plugin)
12         );
13
14         @include_once('addon/' . $plugin . '/' . $plugin . '.php');
15         if(function_exists($plugin . '_uninstall')) {
16                 $func = $plugin . '_uninstall';
17                 $func();
18         }
19 }}
20
21 if (! function_exists('install_plugin')){
22 function install_plugin($plugin) {
23         // silently fail if plugin was removed
24
25         if(! file_exists('addon/' . $plugin . '/' . $plugin . '.php'))
26                 return false;
27         logger("Addons: installing " . $plugin);
28         $t = @filemtime('addon/' . $plugin . '/' . $plugin . '.php');
29         @include_once('addon/' . $plugin . '/' . $plugin . '.php');
30         if(function_exists($plugin . '_install')) {
31                 $func = $plugin . '_install';
32                 $func();
33
34                 $plugin_admin = (function_exists($plugin."_plugin_admin")?1:0);
35
36                 $r = q("INSERT INTO `addon` (`name`, `installed`, `timestamp`, `plugin_admin`) VALUES ( '%s', 1, %d , %d ) ",
37                         dbesc($plugin),
38                         intval($t),
39                         $plugin_admin
40                 );
41
42                 // we can add the following with the previous SQL
43                 // once most site tables have been updated.
44                 // This way the system won't fall over dead during the update.
45
46                 if(file_exists('addon/' . $plugin . '/.hidden')) {
47                         q("update addon set hidden = 1 where name = '%s'",
48                                 dbesc($plugin)
49                         );
50                 }
51                 return true;
52         }
53         else {
54                 logger("Addons: FAILED installing " . $plugin);
55                 return false;
56         }
57
58 }}
59
60 // reload all updated plugins
61
62 if(! function_exists('reload_plugins')) {
63 function reload_plugins() {
64         $plugins = get_config('system','addon');
65         if(strlen($plugins)) {
66
67                 $r = q("SELECT * FROM `addon` WHERE `installed` = 1");
68                 if(count($r))
69                         $installed = $r;
70                 else
71                         $installed = array();
72
73                 $parr = explode(',',$plugins);
74
75                 if(count($parr)) {
76                         foreach($parr as $pl) {
77
78                                 $pl = trim($pl);
79
80                                 $fname = 'addon/' . $pl . '/' . $pl . '.php';
81
82                                 if(file_exists($fname)) {
83                                         $t = @filemtime($fname);
84                                         foreach($installed as $i) {
85                                                 if(($i['name'] == $pl) && ($i['timestamp'] != $t)) {
86                                                         logger('Reloading plugin: ' . $i['name']);
87                                                         @include_once($fname);
88
89                                                         if(function_exists($pl . '_uninstall')) {
90                                                                 $func = $pl . '_uninstall';
91                                                                 $func();
92                                                         }
93                                                         if(function_exists($pl . '_install')) {
94                                                                 $func = $pl . '_install';
95                                                                 $func();
96                                                         }
97                                                         q("UPDATE `addon` SET `timestamp` = %d WHERE `id` = %d",
98                                                                 intval($t),
99                                                                 intval($i['id'])
100                                                         );
101                                                 }
102                                         }
103                                 }
104                         }
105                 }
106         }
107
108 }}
109
110 /*
111  * @brief check if addon is enabled
112  *
113  * @param string $plugin
114  * @return boolean
115  */
116 function plugin_enabled($plugin) {
117         $r = q("SELECT * FROM `addon` WHERE `installed` = 1 AND `name` = '%s'", $plugin);
118         return((bool)(count($r) > 0));
119 }
120
121
122
123 if(! function_exists('register_hook')) {
124 function register_hook($hook,$file,$function,$priority=0) {
125
126         $r = q("SELECT * FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' LIMIT 1",
127                 dbesc($hook),
128                 dbesc($file),
129                 dbesc($function)
130         );
131         if(count($r))
132                 return true;
133
134         $r = q("INSERT INTO `hook` (`hook`, `file`, `function`, `priority`) VALUES ( '%s', '%s', '%s', '%s' ) ",
135                 dbesc($hook),
136                 dbesc($file),
137                 dbesc($function),
138                 dbesc($priority)
139         );
140         return $r;
141 }}
142
143 if(! function_exists('unregister_hook')) {
144 function unregister_hook($hook,$file,$function) {
145
146         $r = q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
147                 dbesc($hook),
148                 dbesc($file),
149                 dbesc($function)
150         );
151         return $r;
152 }}
153
154
155 if(! function_exists('load_hooks')) {
156 function load_hooks() {
157         $a = get_app();
158         $a->hooks = array();
159         $r = q("SELECT * FROM `hook` WHERE 1 ORDER BY `priority` DESC, `file`");
160         if(count($r)) {
161                 foreach($r as $rr) {
162                         if(! array_key_exists($rr['hook'],$a->hooks))
163                                 $a->hooks[$rr['hook']] = array();
164                         $a->hooks[$rr['hook']][] = array($rr['file'],$rr['function']);
165                 }
166         }
167 }}
168
169
170 if(! function_exists('call_hooks')) {
171 function call_hooks($name, &$data = null) {
172         $stamp1 = microtime(true);
173
174         $a = get_app();
175
176         #logger($name, LOGGER_ALL);
177
178         if((is_array($a->hooks)) && (array_key_exists($name,$a->hooks))) {
179                 foreach($a->hooks[$name] as $hook) {
180                         // Don't run a theme's hook if the user isn't using the theme
181                         if(strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false)
182                                 continue;
183
184                         @include_once($hook[0]);
185                         if(function_exists($hook[1])) {
186                                 $func = $hook[1];
187                                 //logger($name." => ".$hook[0].":".$func."()", LOGGER_DEBUG);
188                                 $func($a,$data);
189                         }
190                         else {
191                                 // remove orphan hooks
192                                 q("delete from hook where hook = '%s' and file = '%s' and function = '%s'",
193                                         dbesc($name),
194                                         dbesc($hook[0]),
195                                         dbesc($hook[1])
196                                 );
197                         }
198                 }
199         }
200 }}
201
202 //check if an app_menu hook exist for plugin $name.
203 //Return true if the plugin is an app
204 if(! function_exists('plugin_is_app')) {
205 function plugin_is_app($name) {
206         $a = get_app();
207
208         if(is_array($a->hooks) && (array_key_exists('app_menu',$a->hooks))) {
209                 foreach($a->hooks['app_menu'] as $hook) {
210                         if($hook[0] == 'addon/'.$name.'/'.$name.'.php')
211                                 return true;
212                 }
213         }
214
215         return false;
216 }}
217
218 /*
219  * parse plugin comment in search of plugin infos.
220  * like
221  *
222  *       * Name: Plugin
223  *   * Description: A plugin which plugs in
224  *       * Version: 1.2.3
225  *   * Author: John <profile url>
226  *   * Author: Jane <email>
227  *   *
228  */
229
230 if (! function_exists('get_plugin_info')){
231 function get_plugin_info($plugin){
232
233         $a = get_app();
234
235         $info=Array(
236                 'name' => $plugin,
237                 'description' => "",
238                 'author' => array(),
239                 'version' => "",
240                 'status' => ""
241         );
242
243         if (!is_file("addon/$plugin/$plugin.php")) return $info;
244
245         $stamp1 = microtime(true);
246         $f = file_get_contents("addon/$plugin/$plugin.php");
247         $a->save_timestamp($stamp1, "file");
248
249         $r = preg_match("|/\*.*\*/|msU", $f, $m);
250
251         if ($r){
252                 $ll = explode("\n", $m[0]);
253                 foreach( $ll as $l ) {
254                         $l = trim($l,"\t\n\r */");
255                         if ($l!=""){
256                                 list($k,$v) = array_map("trim", explode(":",$l,2));
257                                 $k= strtolower($k);
258                                 if ($k=="author"){
259                                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
260                                         if ($r) {
261                                                 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
262                                         } else {
263                                                 $info['author'][] = array('name'=>$v);
264                                         }
265                                 } else {
266                                         if (array_key_exists($k,$info)){
267                                                 $info[$k]=$v;
268                                         }
269                                 }
270
271                         }
272                 }
273
274         }
275         return $info;
276 }}
277
278
279 /*
280  * parse theme comment in search of theme infos.
281  * like
282  *
283  *       * Name: My Theme
284  *   * Description: My Cool Theme
285  *       * Version: 1.2.3
286  *   * Author: John <profile url>
287  *   * Maintainer: Jane <profile url>
288  *   *
289  */
290
291 if (! function_exists('get_theme_info')){
292 function get_theme_info($theme){
293         $info=Array(
294                 'name' => $theme,
295                 'description' => "",
296                 'author' => array(),
297                 'maintainer' => array(),
298                 'version' => "",
299                 'credits' => "",
300                 'experimental' => false,
301                 'unsupported' => false
302         );
303
304         if(file_exists("view/theme/$theme/experimental"))
305                 $info['experimental'] = true;
306         if(file_exists("view/theme/$theme/unsupported"))
307                 $info['unsupported'] = true;
308
309         if (!is_file("view/theme/$theme/theme.php")) return $info;
310
311         $a = get_app();
312         $stamp1 = microtime(true);
313         $f = file_get_contents("view/theme/$theme/theme.php");
314         $a->save_timestamp($stamp1, "file");
315
316         $r = preg_match("|/\*.*\*/|msU", $f, $m);
317
318         if ($r){
319                 $ll = explode("\n", $m[0]);
320                 foreach( $ll as $l ) {
321                         $l = trim($l,"\t\n\r */");
322                         if ($l!=""){
323                                 list($k,$v) = array_map("trim", explode(":",$l,2));
324                                 $k= strtolower($k);
325                                 if ($k=="author"){
326
327                                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
328                                         if ($r) {
329                                                 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
330                                         } else {
331                                                 $info['author'][] = array('name'=>$v);
332                                         }
333                                 }
334                                 elseif ($k=="maintainer"){
335                                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
336                                         if ($r) {
337                                                 $info['maintainer'][] = array('name'=>$m[1], 'link'=>$m[2]);
338                                         } else {
339                                                 $info['maintainer'][] = array('name'=>$v);
340                                         }
341                                 } else {
342                                         if (array_key_exists($k,$info)){
343                                                 $info[$k]=$v;
344                                         }
345                                 }
346
347                         }
348                 }
349
350         }
351         return $info;
352 }}
353
354
355 function get_theme_screenshot($theme) {
356         $a = get_app();
357         $exts = array('.png','.jpg');
358         foreach($exts as $ext) {
359                 if(file_exists('view/theme/' . $theme . '/screenshot' . $ext))
360                         return($a->get_baseurl() . '/view/theme/' . $theme . '/screenshot' . $ext);
361         }
362         return($a->get_baseurl() . '/images/blank.png');
363 }
364
365 // install and uninstall theme
366 if (! function_exists('uninstall_theme')){
367 function uninstall_theme($theme){
368         logger("Addons: uninstalling theme " . $theme);
369
370         @include_once("view/theme/$theme/theme.php");
371         if(function_exists("{$theme}_uninstall")) {
372                 $func = "{$theme}_uninstall";
373                 $func();
374         }
375 }}
376
377 if (! function_exists('install_theme')){
378 function install_theme($theme) {
379         // silently fail if theme was removed
380
381         if(! file_exists("view/theme/$theme/theme.php"))
382                 return false;
383
384         logger("Addons: installing theme $theme");
385
386         @include_once("view/theme/$theme/theme.php");
387
388         if(function_exists("{$theme}_install")) {
389                 $func = "{$theme}_install";
390                 $func();
391                 return true;
392         }
393         else {
394                 logger("Addons: FAILED installing theme $theme");
395                 return false;
396         }
397
398 }}
399
400
401
402 // check service_class restrictions. If there are no service_classes defined, everything is allowed.
403 // if $usage is supplied, we check against a maximum count and return true if the current usage is
404 // less than the subscriber plan allows. Otherwise we return boolean true or false if the property
405 // is allowed (or not) in this subscriber plan. An unset property for this service plan means
406 // the property is allowed, so it is only necessary to provide negative properties for each plan,
407 // or what the subscriber is not allowed to do.
408
409
410 function service_class_allows($uid,$property,$usage = false) {
411
412         if($uid == local_user()) {
413                 $service_class = $a->user['service_class'];
414         }
415         else {
416                 $r = q("select service_class from user where uid = %d limit 1",
417                         intval($uid)
418                 );
419                 if($r !== false and count($r)) {
420                         $service_class = $r[0]['service_class'];
421                 }
422         }
423         if(! x($service_class))
424                 return true; // everything is allowed
425
426         $arr = get_config('service_class',$service_class);
427         if(! is_array($arr) || (! count($arr)))
428                 return true;
429
430         if($usage === false)
431                 return ((x($arr[$property])) ? (bool) $arr['property'] : true);
432         else {
433                 if(! array_key_exists($property,$arr))
434                         return true;
435                 return (((intval($usage)) < intval($arr[$property])) ? true : false);
436         }
437 }
438
439
440 function service_class_fetch($uid,$property) {
441
442         if($uid == local_user()) {
443                 $service_class = $a->user['service_class'];
444         }
445         else {
446                 $r = q("select service_class from user where uid = %d limit 1",
447                         intval($uid)
448                 );
449                 if($r !== false and count($r)) {
450                         $service_class = $r[0]['service_class'];
451                 }
452         }
453         if(! x($service_class))
454                 return false; // everything is allowed
455
456         $arr = get_config('service_class',$service_class);
457         if(! is_array($arr) || (! count($arr)))
458                 return false;
459
460         return((array_key_exists($property,$arr)) ? $arr[$property] : false);
461
462 }
463
464 function upgrade_link($bbcode = false) {
465         $l = get_config('service_class','upgrade_link');
466         if(! $l)
467                 return '';
468         if($bbcode)
469                 $t = sprintf('[url=%s]' . t('Click here to upgrade.') . '[/url]', $l);
470         else
471                 $t = sprintf('<a href="%s">' . t('Click here to upgrade.') . '</div>', $l);
472         return $t;
473 }
474
475 function upgrade_message($bbcode = false) {
476         $x = upgrade_link($bbcode);
477         return t('This action exceeds the limits set by your subscription plan.') . (($x) ? ' ' . $x : '') ;
478 }
479
480 function upgrade_bool_message($bbcode = false) {
481         $x = upgrade_link($bbcode);
482         return t('This action is not available under your subscription plan.') . (($x) ? ' ' . $x : '') ;
483 }