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