3 * @file include/plugin.php
5 * @brief Some functions to handle addons and themes.
10 * @brief uninstalls an addon.
12 * @param string $plugin name of the addon
15 if (! function_exists('uninstall_plugin')){
16 function uninstall_plugin($plugin){
17 logger("Addons: uninstalling " . $plugin);
18 q("DELETE FROM `addon` WHERE `name` = '%s' ",
22 @include_once('addon/' . $plugin . '/' . $plugin . '.php');
23 if(function_exists($plugin . '_uninstall')) {
24 $func = $plugin . '_uninstall';
30 * @brief installs an addon.
32 * @param string $plugin name of the addon
35 if (! function_exists('install_plugin')){
36 function install_plugin($plugin) {
37 // silently fail if plugin was removed
39 if(! file_exists('addon/' . $plugin . '/' . $plugin . '.php'))
41 logger("Addons: installing " . $plugin);
42 $t = @filemtime('addon/' . $plugin . '/' . $plugin . '.php');
43 @include_once('addon/' . $plugin . '/' . $plugin . '.php');
44 if(function_exists($plugin . '_install')) {
45 $func = $plugin . '_install';
48 $plugin_admin = (function_exists($plugin."_plugin_admin")?1:0);
50 $r = q("INSERT INTO `addon` (`name`, `installed`, `timestamp`, `plugin_admin`) VALUES ( '%s', 1, %d , %d ) ",
56 // we can add the following with the previous SQL
57 // once most site tables have been updated.
58 // This way the system won't fall over dead during the update.
60 if(file_exists('addon/' . $plugin . '/.hidden')) {
61 q("UPDATE `addon` SET `hidden` = 1 WHERE `name` = '%s'",
68 logger("Addons: FAILED installing " . $plugin);
74 // reload all updated plugins
76 if(! function_exists('reload_plugins')) {
77 function reload_plugins() {
78 $plugins = get_config('system','addon');
79 if(strlen($plugins)) {
81 $r = q("SELECT * FROM `addon` WHERE `installed` = 1");
87 $parr = explode(',',$plugins);
90 foreach($parr as $pl) {
94 $fname = 'addon/' . $pl . '/' . $pl . '.php';
96 if(file_exists($fname)) {
97 $t = @filemtime($fname);
98 foreach($installed as $i) {
99 if(($i['name'] == $pl) && ($i['timestamp'] != $t)) {
100 logger('Reloading plugin: ' . $i['name']);
101 @include_once($fname);
103 if(function_exists($pl . '_uninstall')) {
104 $func = $pl . '_uninstall';
107 if(function_exists($pl . '_install')) {
108 $func = $pl . '_install';
111 q("UPDATE `addon` SET `timestamp` = %d WHERE `id` = %d",
125 * @brief check if addon is enabled
127 * @param string $plugin
130 function plugin_enabled($plugin) {
131 $r = q("SELECT * FROM `addon` WHERE `installed` = 1 AND `name` = '%s'", $plugin);
132 return((bool)(count($r) > 0));
137 * @brief registers a hook.
139 * @param string $hook the name of the hook
140 * @param string $file the name of the file that hooks into
141 * @param string $function the name of the function that the hook will call
142 * @param int $priority A priority (defaults to 0)
145 if(! function_exists('register_hook')) {
146 function register_hook($hook,$file,$function,$priority=0) {
148 $r = q("SELECT * FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' LIMIT 1",
156 $r = q("INSERT INTO `hook` (`hook`, `file`, `function`, `priority`) VALUES ( '%s', '%s', '%s', '%s' ) ",
166 * @brief unregisters a hook.
168 * @param string $hook the name of the hook
169 * @param string $file the name of the file that hooks into
170 * @param string $function the name of the function that the hook called
173 if(! function_exists('unregister_hook')) {
174 function unregister_hook($hook,$file,$function) {
176 $r = q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
185 if(! function_exists('load_hooks')) {
186 function load_hooks() {
189 $r = q("SELECT * FROM `hook` WHERE 1 ORDER BY `priority` DESC, `file`");
192 if(! array_key_exists($rr['hook'],$a->hooks))
193 $a->hooks[$rr['hook']] = array();
194 $a->hooks[$rr['hook']][] = array($rr['file'],$rr['function']);
200 * @brief Calls a hook.
202 * Use this function when you want to be able to allow a hook to manipulate
205 * @param string $name of the hook to call
206 * @param string|array &$data to transmit to the callback handler
208 function call_hooks($name, &$data = null) {
209 $stamp1 = microtime(true);
213 if (is_array($a->hooks) && array_key_exists($name, $a->hooks))
214 foreach ($a->hooks[$name] as $hook)
215 call_single_hook($a, $name, $hook, $data);
219 * @brief Calls a single hook.
221 * @param string $name of the hook to call
222 * @param array $hook Hook data
223 * @param string|array &$data to transmit to the callback handler
225 function call_single_hook($a, $name, $hook, &$data = null) {
226 // Don't run a theme's hook if the user isn't using the theme
227 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false)
230 @include_once($hook[0]);
231 if (function_exists($hook[1])) {
235 // remove orphan hooks
236 q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
244 //check if an app_menu hook exist for plugin $name.
245 //Return true if the plugin is an app
246 if(! function_exists('plugin_is_app')) {
247 function plugin_is_app($name) {
250 if(is_array($a->hooks) && (array_key_exists('app_menu',$a->hooks))) {
251 foreach($a->hooks['app_menu'] as $hook) {
252 if($hook[0] == 'addon/'.$name.'/'.$name.'.php')
261 * @brief Parse plugin comment in search of plugin infos.
266 * * Description: A plugin which plugs in
268 * * Author: John <profile url>
269 * * Author: Jane <email>
272 * @param string $plugin the name of the plugin
273 * @return array with the plugin information
276 if (! function_exists('get_plugin_info')){
277 function get_plugin_info($plugin){
289 if (!is_file("addon/$plugin/$plugin.php")) return $info;
291 $stamp1 = microtime(true);
292 $f = file_get_contents("addon/$plugin/$plugin.php");
293 $a->save_timestamp($stamp1, "file");
295 $r = preg_match("|/\*.*\*/|msU", $f, $m);
298 $ll = explode("\n", $m[0]);
299 foreach( $ll as $l ) {
300 $l = trim($l,"\t\n\r */");
302 list($k,$v) = array_map("trim", explode(":",$l,2));
305 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
307 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
309 $info['author'][] = array('name'=>$v);
312 if (array_key_exists($k,$info)){
326 * @brief Parse theme comment in search of theme infos.
331 * * Description: My Cool Theme
333 * * Author: John <profile url>
334 * * Maintainer: Jane <profile url>
337 * @param string $theme the name of the theme
341 if (! function_exists('get_theme_info')){
342 function get_theme_info($theme){
347 'maintainer' => array(),
350 'experimental' => false,
351 'unsupported' => false
354 if(file_exists("view/theme/$theme/experimental"))
355 $info['experimental'] = true;
356 if(file_exists("view/theme/$theme/unsupported"))
357 $info['unsupported'] = true;
359 if (!is_file("view/theme/$theme/theme.php")) return $info;
362 $stamp1 = microtime(true);
363 $f = file_get_contents("view/theme/$theme/theme.php");
364 $a->save_timestamp($stamp1, "file");
366 $r = preg_match("|/\*.*\*/|msU", $f, $m);
369 $ll = explode("\n", $m[0]);
370 foreach( $ll as $l ) {
371 $l = trim($l,"\t\n\r */");
373 list($k,$v) = array_map("trim", explode(":",$l,2));
377 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
379 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
381 $info['author'][] = array('name'=>$v);
384 elseif ($k=="maintainer"){
385 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
387 $info['maintainer'][] = array('name'=>$m[1], 'link'=>$m[2]);
389 $info['maintainer'][] = array('name'=>$v);
392 if (array_key_exists($k,$info)){
405 * @brief Returns the theme's screenshot.
407 * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
409 * @param sring $theme The name of the theme
412 function get_theme_screenshot($theme) {
414 $exts = array('.png','.jpg');
415 foreach($exts as $ext) {
416 if(file_exists('view/theme/' . $theme . '/screenshot' . $ext))
417 return($a->get_baseurl() . '/view/theme/' . $theme . '/screenshot' . $ext);
419 return($a->get_baseurl() . '/images/blank.png');
422 // install and uninstall theme
423 if (! function_exists('uninstall_theme')){
424 function uninstall_theme($theme){
425 logger("Addons: uninstalling theme " . $theme);
427 @include_once("view/theme/$theme/theme.php");
428 if(function_exists("{$theme}_uninstall")) {
429 $func = "{$theme}_uninstall";
434 if (! function_exists('install_theme')){
435 function install_theme($theme) {
436 // silently fail if theme was removed
438 if(! file_exists("view/theme/$theme/theme.php"))
441 logger("Addons: installing theme $theme");
443 @include_once("view/theme/$theme/theme.php");
445 if(function_exists("{$theme}_install")) {
446 $func = "{$theme}_install";
451 logger("Addons: FAILED installing theme $theme");
459 // check service_class restrictions. If there are no service_classes defined, everything is allowed.
460 // if $usage is supplied, we check against a maximum count and return true if the current usage is
461 // less than the subscriber plan allows. Otherwise we return boolean true or false if the property
462 // is allowed (or not) in this subscriber plan. An unset property for this service plan means
463 // the property is allowed, so it is only necessary to provide negative properties for each plan,
464 // or what the subscriber is not allowed to do.
467 function service_class_allows($uid,$property,$usage = false) {
469 if($uid == local_user()) {
470 $service_class = $a->user['service_class'];
473 $r = q("SELECT `service_class` FROM `user` WHERE `uid` = %d LIMIT 1",
476 if($r !== false and count($r)) {
477 $service_class = $r[0]['service_class'];
480 if(! x($service_class))
481 return true; // everything is allowed
483 $arr = get_config('service_class',$service_class);
484 if(! is_array($arr) || (! count($arr)))
488 return ((x($arr[$property])) ? (bool) $arr['property'] : true);
490 if(! array_key_exists($property,$arr))
492 return (((intval($usage)) < intval($arr[$property])) ? true : false);
497 function service_class_fetch($uid,$property) {
499 if($uid == local_user()) {
500 $service_class = $a->user['service_class'];
503 $r = q("SELECT `service_class` FROM `user` WHERE `uid` = %d LIMIT 1",
506 if($r !== false and count($r)) {
507 $service_class = $r[0]['service_class'];
510 if(! x($service_class))
511 return false; // everything is allowed
513 $arr = get_config('service_class',$service_class);
514 if(! is_array($arr) || (! count($arr)))
517 return((array_key_exists($property,$arr)) ? $arr[$property] : false);
521 function upgrade_link($bbcode = false) {
522 $l = get_config('service_class','upgrade_link');
526 $t = sprintf('[url=%s]' . t('Click here to upgrade.') . '[/url]', $l);
528 $t = sprintf('<a href="%s">' . t('Click here to upgrade.') . '</div>', $l);
532 function upgrade_message($bbcode = false) {
533 $x = upgrade_link($bbcode);
534 return t('This action exceeds the limits set by your subscription plan.') . (($x) ? ' ' . $x : '') ;
537 function upgrade_bool_message($bbcode = false) {
538 $x = upgrade_link($bbcode);
539 return t('This action is not available under your subscription plan.') . (($x) ? ' ' . $x : '') ;
543 * @brief Get the full path to relevant theme files by filename
545 * This function search in the theme directory (and if not present in global theme directory)
546 * if there is a directory with the file extension and for a file with the given
549 * @param string $file Filename
550 * @param string $root Full root path
551 * @return string Path to the file or empty string if the file isn't found
553 function theme_include($file, $root = '') {
554 // Make sure $root ends with a slash / if it's not blank
555 if($root !== '' && $root[strlen($root)-1] !== '/')
557 $theme_info = $a->theme_info;
558 if(is_array($theme_info) AND array_key_exists('extends',$theme_info))
559 $parent = $theme_info['extends'];
562 $theme = current_theme();
564 $ext = substr($file,strrpos($file,'.')+1);
566 "{$root}view/theme/$thname/$ext/$file",
567 "{$root}view/theme/$parent/$ext/$file",
568 "{$root}view/$ext/$file",
570 foreach($paths as $p) {
571 // strpos() is faster than strstr when checking if one string is in another (http://php.net/manual/en/function.strstr.php)
572 if(strpos($p,'NOPATH') !== false)