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