]> git.mxchange.org Git - friendica.git/blob - include/plugin.php
New dba functions
[friendica.git] / include / plugin.php
1 <?php
2 /**
3  * @file include/plugin.php
4  *
5  * @brief Some functions to handle addons and themes.
6  */
7
8 use Friendica\App;
9
10 /**
11  * @brief uninstalls an addon.
12  *
13  * @param string $plugin name of the addon
14  * @return boolean
15  */
16 if (! function_exists('uninstall_plugin')){
17 function uninstall_plugin($plugin){
18         logger("Addons: uninstalling " . $plugin);
19         q("DELETE FROM `addon` WHERE `name` = '%s' ",
20                 dbesc($plugin)
21         );
22
23         @include_once('addon/' . $plugin . '/' . $plugin . '.php');
24         if (function_exists($plugin . '_uninstall')) {
25                 $func = $plugin . '_uninstall';
26                 $func();
27         }
28 }}
29
30 /**
31  * @brief installs an addon.
32  *
33  * @param string $plugin name of the addon
34  * @return bool
35  */
36 if (! function_exists('install_plugin')){
37 function install_plugin($plugin) {
38         // silently fail if plugin was removed
39
40         if (! file_exists('addon/' . $plugin . '/' . $plugin . '.php'))
41                 return false;
42         logger("Addons: installing " . $plugin);
43         $t = @filemtime('addon/' . $plugin . '/' . $plugin . '.php');
44         @include_once('addon/' . $plugin . '/' . $plugin . '.php');
45         if (function_exists($plugin . '_install')) {
46                 $func = $plugin . '_install';
47                 $func();
48
49                 $plugin_admin = (function_exists($plugin."_plugin_admin") ? 1 : 0);
50
51                 dba::insert('addon', array('name' => $plugin, 'installed' => true,
52                                         'timestamp' => $t, 'plugin_admin' => $plugin_admin));
53
54                 // we can add the following with the previous SQL
55                 // once most site tables have been updated.
56                 // This way the system won't fall over dead during the update.
57
58                 if (file_exists('addon/' . $plugin . '/.hidden')) {
59                         dba::update('addon', array('hidden' => true), array('name' => $plugin));
60                 }
61                 return true;
62         }
63         else {
64                 logger("Addons: FAILED installing " . $plugin);
65                 return false;
66         }
67
68 }}
69
70 // reload all updated plugins
71
72 if (! function_exists('reload_plugins')) {
73 function reload_plugins() {
74         $plugins = get_config('system','addon');
75         if (strlen($plugins)) {
76
77                 $r = q("SELECT * FROM `addon` WHERE `installed` = 1");
78                 if (dbm::is_result($r))
79                         $installed = $r;
80                 else
81                         $installed = array();
82
83                 $parr = explode(',',$plugins);
84
85                 if (count($parr)) {
86                         foreach ($parr as $pl) {
87
88                                 $pl = trim($pl);
89
90                                 $fname = 'addon/' . $pl . '/' . $pl . '.php';
91
92                                 if (file_exists($fname)) {
93                                         $t = @filemtime($fname);
94                                         foreach ($installed as $i) {
95                                                 if (($i['name'] == $pl) && ($i['timestamp'] != $t)) {
96                                                         logger('Reloading plugin: ' . $i['name']);
97                                                         @include_once($fname);
98
99                                                         if (function_exists($pl . '_uninstall')) {
100                                                                 $func = $pl . '_uninstall';
101                                                                 $func();
102                                                         }
103                                                         if (function_exists($pl . '_install')) {
104                                                                 $func = $pl . '_install';
105                                                                 $func();
106                                                         }
107                                                         dba::update('addon', array('timestamp' => $t), array('id' => $i['id']));
108                                                 }
109                                         }
110                                 }
111                         }
112                 }
113         }
114
115 }}
116
117 /**
118  * @brief check if addon is enabled
119  *
120  * @param string $plugin
121  * @return boolean
122  */
123 function plugin_enabled($plugin) {
124         $r = dba::select('addon', array('id'), array('installed' => true, 'name' => $plugin), array('limit' => 1));
125         return ((dbm::is_result($r)) && (count($r) > 0));
126 }
127
128
129 /**
130  * @brief registers a hook.
131  *
132  * @param string $hook the name of the hook
133  * @param string $file the name of the file that hooks into
134  * @param string $function the name of the function that the hook will call
135  * @param int $priority A priority (defaults to 0)
136  * @return mixed|bool
137  */
138 if (! function_exists('register_hook')) {
139 function register_hook($hook,$file,$function,$priority=0) {
140
141         $r = q("SELECT * FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' LIMIT 1",
142                 dbesc($hook),
143                 dbesc($file),
144                 dbesc($function)
145         );
146         if (dbm::is_result($r))
147                 return true;
148
149         $r = dba::insert('hook', array('hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority));
150
151         return $r;
152 }}
153
154 /**
155  * @brief unregisters a hook.
156  *
157  * @param string $hook the name of the hook
158  * @param string $file the name of the file that hooks into
159  * @param string $function the name of the function that the hook called
160  * @return array
161  */
162 if (! function_exists('unregister_hook')) {
163 function unregister_hook($hook,$file,$function) {
164
165         $r = q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
166                 dbesc($hook),
167                 dbesc($file),
168                 dbesc($function)
169         );
170         return $r;
171 }}
172
173
174 function load_hooks() {
175         $a = get_app();
176         $a->hooks = array();
177         $r = dba::select('hook', array('hook', 'file', 'function'), array(), array('order' => array('priority' => 'desc', 'file')));
178
179         while ($rr = dba::fetch($r)) {
180                 if (! array_key_exists($rr['hook'],$a->hooks)) {
181                         $a->hooks[$rr['hook']] = array();
182                 }
183                 $a->hooks[$rr['hook']][] = array($rr['file'],$rr['function']);
184         }
185         dba::close($r);
186 }
187
188 /**
189  * @brief Calls a hook.
190  *
191  * Use this function when you want to be able to allow a hook to manipulate
192  * the provided data.
193  *
194  * @param string $name of the hook to call
195  * @param string|array &$data to transmit to the callback handler
196  */
197 function call_hooks($name, &$data = null) {
198         $stamp1 = microtime(true);
199
200         $a = get_app();
201
202         if (is_array($a->hooks) && array_key_exists($name, $a->hooks))
203                 foreach ($a->hooks[$name] as $hook)
204                         call_single_hook($a, $name, $hook, $data);
205 }
206
207 /**
208  * @brief Calls a single hook.
209  *
210  * @param string $name of the hook to call
211  * @param array $hook Hook data
212  * @param string|array &$data to transmit to the callback handler
213  */
214 function call_single_hook($a, $name, $hook, &$data = null) {
215         // Don't run a theme's hook if the user isn't using the theme
216         if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false)
217                 return;
218
219         @include_once($hook[0]);
220         if (function_exists($hook[1])) {
221                 $func = $hook[1];
222                 $func($a, $data);
223         } else {
224                 // remove orphan hooks
225                 q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
226                         dbesc($name),
227                         dbesc($hook[0]),
228                         dbesc($hook[1])
229                 );
230         }
231 }
232
233 //check if an app_menu hook exist for plugin $name.
234 //Return true if the plugin is an app
235 if (! function_exists('plugin_is_app')) {
236 function plugin_is_app($name) {
237         $a = get_app();
238
239         if (is_array($a->hooks) && (array_key_exists('app_menu',$a->hooks))) {
240                 foreach ($a->hooks['app_menu'] as $hook) {
241                         if ($hook[0] == 'addon/'.$name.'/'.$name.'.php')
242                                 return true;
243                 }
244         }
245
246         return false;
247 }}
248
249 /**
250  * @brief Parse plugin comment in search of plugin infos.
251  *
252  * like
253  * \code
254  *...* Name: Plugin
255  *   * Description: A plugin which plugs in
256  * . * Version: 1.2.3
257  *   * Author: John <profile url>
258  *   * Author: Jane <email>
259  *   *
260  *  *\endcode
261  * @param string $plugin the name of the plugin
262  * @return array with the plugin information
263  */
264
265 if (! function_exists('get_plugin_info')){
266 function get_plugin_info($plugin){
267
268         $a = get_app();
269
270         $info=Array(
271                 'name' => $plugin,
272                 'description' => "",
273                 'author' => array(),
274                 'version' => "",
275                 'status' => ""
276         );
277
278         if (!is_file("addon/$plugin/$plugin.php")) return $info;
279
280         $stamp1 = microtime(true);
281         $f = file_get_contents("addon/$plugin/$plugin.php");
282         $a->save_timestamp($stamp1, "file");
283
284         $r = preg_match("|/\*.*\*/|msU", $f, $m);
285
286         if ($r){
287                 $ll = explode("\n", $m[0]);
288                 foreach ( $ll as $l ) {
289                         $l = trim($l,"\t\n\r */");
290                         if ($l!=""){
291                                 list($k,$v) = array_map("trim", explode(":",$l,2));
292                                 $k= strtolower($k);
293                                 if ($k=="author"){
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                                 } else {
301                                         if (array_key_exists($k,$info)){
302                                                 $info[$k]=$v;
303                                         }
304                                 }
305
306                         }
307                 }
308
309         }
310         return $info;
311 }}
312
313
314 /**
315  * @brief Parse theme comment in search of theme infos.
316  *
317  * like
318  * \code
319  * ..* Name: My Theme
320  *   * Description: My Cool Theme
321  * . * Version: 1.2.3
322  *   * Author: John <profile url>
323  *   * Maintainer: Jane <profile url>
324  *   *
325  * \endcode
326  * @param string $theme the name of the theme
327  * @return array
328  */
329
330 if (! function_exists('get_theme_info')){
331 function get_theme_info($theme){
332         $info=Array(
333                 'name' => $theme,
334                 'description' => "",
335                 'author' => array(),
336                 'maintainer' => array(),
337                 'version' => "",
338                 'credits' => "",
339                 'experimental' => false,
340                 'unsupported' => false
341         );
342
343         if (file_exists("view/theme/$theme/experimental"))
344                 $info['experimental'] = true;
345         if (file_exists("view/theme/$theme/unsupported"))
346                 $info['unsupported'] = true;
347
348         if (!is_file("view/theme/$theme/theme.php")) return $info;
349
350         $a = get_app();
351         $stamp1 = microtime(true);
352         $f = file_get_contents("view/theme/$theme/theme.php");
353         $a->save_timestamp($stamp1, "file");
354
355         $r = preg_match("|/\*.*\*/|msU", $f, $m);
356
357         if ($r){
358                 $ll = explode("\n", $m[0]);
359                 foreach ( $ll as $l ) {
360                         $l = trim($l,"\t\n\r */");
361                         if ($l!=""){
362                                 list($k,$v) = array_map("trim", explode(":",$l,2));
363                                 $k= strtolower($k);
364                                 if ($k=="author"){
365
366                                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
367                                         if ($r) {
368                                                 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
369                                         } else {
370                                                 $info['author'][] = array('name'=>$v);
371                                         }
372                                 }
373                                 elseif ($k=="maintainer"){
374                                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
375                                         if ($r) {
376                                                 $info['maintainer'][] = array('name'=>$m[1], 'link'=>$m[2]);
377                                         } else {
378                                                 $info['maintainer'][] = array('name'=>$v);
379                                         }
380                                 } else {
381                                         if (array_key_exists($k,$info)){
382                                                 $info[$k]=$v;
383                                         }
384                                 }
385
386                         }
387                 }
388
389         }
390         return $info;
391 }}
392
393 /**
394  * @brief Returns the theme's screenshot.
395  *
396  * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
397  *
398  * @param sring $theme The name of the theme
399  * @return string
400  */
401 function get_theme_screenshot($theme) {
402         $exts = array('.png','.jpg');
403         foreach ($exts as $ext) {
404                 if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
405                         return(App::get_baseurl() . '/view/theme/' . $theme . '/screenshot' . $ext);
406                 }
407         }
408         return(App::get_baseurl() . '/images/blank.png');
409 }
410
411 // install and uninstall theme
412 if (! function_exists('uninstall_theme')){
413 function uninstall_theme($theme){
414         logger("Addons: uninstalling theme " . $theme);
415
416         include_once("view/theme/$theme/theme.php");
417         if (function_exists("{$theme}_uninstall")) {
418                 $func = "{$theme}_uninstall";
419                 $func();
420         }
421 }}
422
423 if (! function_exists('install_theme')){
424 function install_theme($theme) {
425         // silently fail if theme was removed
426
427         if (! file_exists("view/theme/$theme/theme.php")) {
428                 return false;
429         }
430
431         logger("Addons: installing theme $theme");
432
433         include_once("view/theme/$theme/theme.php");
434
435         if (function_exists("{$theme}_install")) {
436                 $func = "{$theme}_install";
437                 $func();
438                 return true;
439         } else {
440                 logger("Addons: FAILED installing theme $theme");
441                 return false;
442         }
443
444 }}
445
446
447
448 // check service_class restrictions. If there are no service_classes defined, everything is allowed.
449 // if $usage is supplied, we check against a maximum count and return true if the current usage is
450 // less than the subscriber plan allows. Otherwise we return boolean true or false if the property
451 // is allowed (or not) in this subscriber plan. An unset property for this service plan means
452 // the property is allowed, so it is only necessary to provide negative properties for each plan,
453 // or what the subscriber is not allowed to do.
454
455
456 function service_class_allows($uid,$property,$usage = false) {
457
458         if ($uid == local_user()) {
459                 $service_class = $a->user['service_class'];
460         } else {
461                 $r = q("SELECT `service_class` FROM `user` WHERE `uid` = %d LIMIT 1",
462                         intval($uid)
463                 );
464                 if (dbm::is_result($r)) {
465                         $service_class = $r[0]['service_class'];
466                 }
467         }
468
469         if (! x($service_class)) {
470                 // everything is allowed
471                 return true;
472         }
473
474         $arr = get_config('service_class',$service_class);
475         if (! is_array($arr) || (! count($arr))) {
476                 return true;
477         }
478
479         if ($usage === false) {
480                 return ((x($arr[$property])) ? (bool) $arr['property'] : true);
481         } else {
482                 if (! array_key_exists($property,$arr)) {
483                         return true;
484                 }
485                 return (((intval($usage)) < intval($arr[$property])) ? true : false);
486         }
487 }
488
489
490 function service_class_fetch($uid,$property) {
491
492         if ($uid == local_user()) {
493                 $service_class = $a->user['service_class'];
494         } else {
495                 $r = q("SELECT `service_class` FROM `user` WHERE `uid` = %d LIMIT 1",
496                         intval($uid)
497                 );
498                 if (dbm::is_result($r)) {
499                         $service_class = $r[0]['service_class'];
500                 }
501         }
502         if (! x($service_class))
503                 return false; // everything is allowed
504
505         $arr = get_config('service_class',$service_class);
506         if (! is_array($arr) || (! count($arr)))
507                 return false;
508
509         return((array_key_exists($property,$arr)) ? $arr[$property] : false);
510
511 }
512
513 function upgrade_link($bbcode = false) {
514         $l = get_config('service_class','upgrade_link');
515         if (! $l) {
516                 return '';
517         }
518         if ($bbcode) {
519                 $t = sprintf('[url=%s]' . t('Click here to upgrade.') . '[/url]', $l);
520         } else {
521                 $t = sprintf('<a href="%s">' . t('Click here to upgrade.') . '</div>', $l);
522         }
523         return $t;
524 }
525
526 function upgrade_message($bbcode = false) {
527         $x = upgrade_link($bbcode);
528         return t('This action exceeds the limits set by your subscription plan.') . (($x) ? ' ' . $x : '') ;
529 }
530
531 function upgrade_bool_message($bbcode = false) {
532         $x = upgrade_link($bbcode);
533         return t('This action is not available under your subscription plan.') . (($x) ? ' ' . $x : '') ;
534 }
535
536 /**
537  * @brief Get the full path to relevant theme files by filename
538  *
539  * This function search in the theme directory (and if not present in global theme directory)
540  * if there is a directory with the file extension and  for a file with the given
541  * filename.
542  *
543  * @param string $file Filename
544  * @param string $root Full root path
545  * @return string Path to the file or empty string if the file isn't found
546  */
547 function theme_include($file, $root = '') {
548         // Make sure $root ends with a slash / if it's not blank
549         if ($root !== '' && $root[strlen($root)-1] !== '/') {
550                 $root = $root . '/';
551         }
552         $theme_info = get_app()->theme_info;
553         if (is_array($theme_info) && array_key_exists('extends',$theme_info)) {
554                 $parent = $theme_info['extends'];
555         } else {
556                 $parent = 'NOPATH';
557         }
558         $theme = current_theme();
559         $thname = $theme;
560         $ext = substr($file,strrpos($file,'.')+1);
561         $paths = array(
562                 "{$root}view/theme/$thname/$ext/$file",
563                 "{$root}view/theme/$parent/$ext/$file",
564                 "{$root}view/$ext/$file",
565         );
566         foreach ($paths as $p) {
567                 // strpos() is faster than strstr when checking if one string is in another (http://php.net/manual/en/function.strstr.php)
568                 if (strpos($p,'NOPATH') !== false) {
569                         continue;
570                 } elseif (file_exists($p)) {
571                         return $p;
572                 }
573         }
574         return '';
575 }