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");
82 if (dbm::is_result($r))
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 ((dbm::is_result($r)) && (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",
153 if (dbm::is_result($r))
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`");
191 if (dbm::is_result($r)) {
192 foreach ($r as $rr) {
193 if(! array_key_exists($rr['hook'],$a->hooks))
194 $a->hooks[$rr['hook']] = array();
195 $a->hooks[$rr['hook']][] = array($rr['file'],$rr['function']);
201 * @brief Calls a hook.
203 * Use this function when you want to be able to allow a hook to manipulate
206 * @param string $name of the hook to call
207 * @param string|array &$data to transmit to the callback handler
209 function call_hooks($name, &$data = null) {
210 $stamp1 = microtime(true);
214 if (is_array($a->hooks) && array_key_exists($name, $a->hooks))
215 foreach ($a->hooks[$name] as $hook)
216 call_single_hook($a, $name, $hook, $data);
220 * @brief Calls a single hook.
222 * @param string $name of the hook to call
223 * @param array $hook Hook data
224 * @param string|array &$data to transmit to the callback handler
226 function call_single_hook($a, $name, $hook, &$data = null) {
227 // Don't run a theme's hook if the user isn't using the theme
228 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false)
231 @include_once($hook[0]);
232 if (function_exists($hook[1])) {
236 // remove orphan hooks
237 q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
245 //check if an app_menu hook exist for plugin $name.
246 //Return true if the plugin is an app
247 if(! function_exists('plugin_is_app')) {
248 function plugin_is_app($name) {
251 if(is_array($a->hooks) && (array_key_exists('app_menu',$a->hooks))) {
252 foreach($a->hooks['app_menu'] as $hook) {
253 if($hook[0] == 'addon/'.$name.'/'.$name.'.php')
262 * @brief Parse plugin comment in search of plugin infos.
267 * * Description: A plugin which plugs in
269 * * Author: John <profile url>
270 * * Author: Jane <email>
273 * @param string $plugin the name of the plugin
274 * @return array with the plugin information
277 if (! function_exists('get_plugin_info')){
278 function get_plugin_info($plugin){
290 if (!is_file("addon/$plugin/$plugin.php")) return $info;
292 $stamp1 = microtime(true);
293 $f = file_get_contents("addon/$plugin/$plugin.php");
294 $a->save_timestamp($stamp1, "file");
296 $r = preg_match("|/\*.*\*/|msU", $f, $m);
299 $ll = explode("\n", $m[0]);
300 foreach( $ll as $l ) {
301 $l = trim($l,"\t\n\r */");
303 list($k,$v) = array_map("trim", explode(":",$l,2));
306 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
308 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
310 $info['author'][] = array('name'=>$v);
313 if (array_key_exists($k,$info)){
327 * @brief Parse theme comment in search of theme infos.
332 * * Description: My Cool Theme
334 * * Author: John <profile url>
335 * * Maintainer: Jane <profile url>
338 * @param string $theme the name of the theme
342 if (! function_exists('get_theme_info')){
343 function get_theme_info($theme){
348 'maintainer' => array(),
351 'experimental' => false,
352 'unsupported' => false
355 if(file_exists("view/theme/$theme/experimental"))
356 $info['experimental'] = true;
357 if(file_exists("view/theme/$theme/unsupported"))
358 $info['unsupported'] = true;
360 if (!is_file("view/theme/$theme/theme.php")) return $info;
363 $stamp1 = microtime(true);
364 $f = file_get_contents("view/theme/$theme/theme.php");
365 $a->save_timestamp($stamp1, "file");
367 $r = preg_match("|/\*.*\*/|msU", $f, $m);
370 $ll = explode("\n", $m[0]);
371 foreach( $ll as $l ) {
372 $l = trim($l,"\t\n\r */");
374 list($k,$v) = array_map("trim", explode(":",$l,2));
378 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
380 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
382 $info['author'][] = array('name'=>$v);
385 elseif ($k=="maintainer"){
386 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
388 $info['maintainer'][] = array('name'=>$m[1], 'link'=>$m[2]);
390 $info['maintainer'][] = array('name'=>$v);
393 if (array_key_exists($k,$info)){
406 * @brief Returns the theme's screenshot.
408 * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
410 * @param sring $theme The name of the theme
413 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(App::get_baseurl() . '/view/theme/' . $theme . '/screenshot' . $ext);
420 return(App::get_baseurl() . '/images/blank.png');
423 // install and uninstall theme
424 if (! function_exists('uninstall_theme')){
425 function uninstall_theme($theme){
426 logger("Addons: uninstalling theme " . $theme);
428 include_once("view/theme/$theme/theme.php");
429 if (function_exists("{$theme}_uninstall")) {
430 $func = "{$theme}_uninstall";
435 if (! function_exists('install_theme')){
436 function install_theme($theme) {
437 // silently fail if theme was removed
439 if (! file_exists("view/theme/$theme/theme.php")) {
443 logger("Addons: installing theme $theme");
445 include_once("view/theme/$theme/theme.php");
447 if (function_exists("{$theme}_install")) {
448 $func = "{$theme}_install";
452 logger("Addons: FAILED installing theme $theme");
460 // check service_class restrictions. If there are no service_classes defined, everything is allowed.
461 // if $usage is supplied, we check against a maximum count and return true if the current usage is
462 // less than the subscriber plan allows. Otherwise we return boolean true or false if the property
463 // is allowed (or not) in this subscriber plan. An unset property for this service plan means
464 // the property is allowed, so it is only necessary to provide negative properties for each plan,
465 // or what the subscriber is not allowed to do.
468 function service_class_allows($uid,$property,$usage = false) {
470 if ($uid == local_user()) {
471 $service_class = $a->user['service_class'];
473 $r = q("SELECT `service_class` FROM `user` WHERE `uid` = %d LIMIT 1",
476 if (dbm::is_result($r)) {
477 $service_class = $r[0]['service_class'];
481 if (! x($service_class)) {
482 // everything is allowed
486 $arr = get_config('service_class',$service_class);
487 if (! is_array($arr) || (! count($arr))) {
491 if ($usage === false) {
492 return ((x($arr[$property])) ? (bool) $arr['property'] : true);
494 if (! array_key_exists($property,$arr)) {
497 return (((intval($usage)) < intval($arr[$property])) ? true : false);
502 function service_class_fetch($uid,$property) {
504 if ($uid == local_user()) {
505 $service_class = $a->user['service_class'];
507 $r = q("SELECT `service_class` FROM `user` WHERE `uid` = %d LIMIT 1",
510 if (dbm::is_result($r)) {
511 $service_class = $r[0]['service_class'];
514 if(! x($service_class))
515 return false; // everything is allowed
517 $arr = get_config('service_class',$service_class);
518 if(! is_array($arr) || (! count($arr)))
521 return((array_key_exists($property,$arr)) ? $arr[$property] : false);
525 function upgrade_link($bbcode = false) {
526 $l = get_config('service_class','upgrade_link');
530 $t = sprintf('[url=%s]' . t('Click here to upgrade.') . '[/url]', $l);
532 $t = sprintf('<a href="%s">' . t('Click here to upgrade.') . '</div>', $l);
536 function upgrade_message($bbcode = false) {
537 $x = upgrade_link($bbcode);
538 return t('This action exceeds the limits set by your subscription plan.') . (($x) ? ' ' . $x : '') ;
541 function upgrade_bool_message($bbcode = false) {
542 $x = upgrade_link($bbcode);
543 return t('This action is not available under your subscription plan.') . (($x) ? ' ' . $x : '') ;
547 * @brief Get the full path to relevant theme files by filename
549 * This function search in the theme directory (and if not present in global theme directory)
550 * if there is a directory with the file extension and for a file with the given
553 * @param string $file Filename
554 * @param string $root Full root path
555 * @return string Path to the file or empty string if the file isn't found
557 function theme_include($file, $root = '') {
558 // Make sure $root ends with a slash / if it's not blank
559 if($root !== '' && $root[strlen($root)-1] !== '/')
561 $theme_info = $a->theme_info;
562 if(is_array($theme_info) AND array_key_exists('extends',$theme_info))
563 $parent = $theme_info['extends'];
566 $theme = current_theme();
568 $ext = substr($file,strrpos($file,'.')+1);
570 "{$root}view/theme/$thname/$ext/$file",
571 "{$root}view/theme/$parent/$ext/$file",
572 "{$root}view/$ext/$file",
574 foreach($paths as $p) {
575 // strpos() is faster than strstr when checking if one string is in another (http://php.net/manual/en/function.strstr.php)
576 if(strpos($p,'NOPATH') !== false)