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