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