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