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