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