3 * @file include/plugin.php
5 * @brief Some functions to handle addons and themes.
11 * @brief uninstalls an addon.
13 * @param string $plugin name of the addon
16 if (! function_exists('uninstall_plugin')){
17 function uninstall_plugin($plugin){
18 logger("Addons: uninstalling " . $plugin);
19 q("DELETE FROM `addon` WHERE `name` = '%s' ",
23 @include_once('addon/' . $plugin . '/' . $plugin . '.php');
24 if (function_exists($plugin . '_uninstall')) {
25 $func = $plugin . '_uninstall';
31 * @brief installs an addon.
33 * @param string $plugin name of the addon
36 if (! function_exists('install_plugin')){
37 function install_plugin($plugin) {
38 // silently fail if plugin was removed
40 if (! file_exists('addon/' . $plugin . '/' . $plugin . '.php'))
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';
49 $plugin_admin = (function_exists($plugin."_plugin_admin")?1:0);
51 $r = q("INSERT INTO `addon` (`name`, `installed`, `timestamp`, `plugin_admin`) VALUES ( '%s', 1, %d , %d ) ",
57 // we can add the following with the previous SQL
58 // once most site tables have been updated.
59 // This way the system won't fall over dead during the update.
61 if (file_exists('addon/' . $plugin . '/.hidden')) {
62 q("UPDATE `addon` SET `hidden` = 1 WHERE `name` = '%s'",
69 logger("Addons: FAILED installing " . $plugin);
75 // reload all updated plugins
77 if (! function_exists('reload_plugins')) {
78 function reload_plugins() {
79 $plugins = get_config('system','addon');
80 if (strlen($plugins)) {
82 $r = q("SELECT * FROM `addon` WHERE `installed` = 1");
83 if (dbm::is_result($r))
88 $parr = explode(',',$plugins);
91 foreach ($parr as $pl) {
95 $fname = 'addon/' . $pl . '/' . $pl . '.php';
97 if (file_exists($fname)) {
98 $t = @filemtime($fname);
99 foreach ($installed as $i) {
100 if (($i['name'] == $pl) && ($i['timestamp'] != $t)) {
101 logger('Reloading plugin: ' . $i['name']);
102 @include_once($fname);
104 if (function_exists($pl . '_uninstall')) {
105 $func = $pl . '_uninstall';
108 if (function_exists($pl . '_install')) {
109 $func = $pl . '_install';
112 q("UPDATE `addon` SET `timestamp` = %d WHERE `id` = %d",
126 * @brief check if addon is enabled
128 * @param string $plugin
131 function plugin_enabled($plugin) {
132 $r = q("SELECT * FROM `addon` WHERE `installed` = 1 AND `name` = '%s'", $plugin);
133 return ((dbm::is_result($r)) && (count($r) > 0));
138 * @brief registers a hook.
140 * @param string $hook the name of the hook
141 * @param string $file the name of the file that hooks into
142 * @param string $function the name of the function that the hook will call
143 * @param int $priority A priority (defaults to 0)
146 if (! function_exists('register_hook')) {
147 function register_hook($hook,$file,$function,$priority=0) {
149 $r = q("SELECT * FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' LIMIT 1",
154 if (dbm::is_result($r))
157 $r = q("INSERT INTO `hook` (`hook`, `file`, `function`, `priority`) VALUES ( '%s', '%s', '%s', '%s' ) ",
167 * @brief unregisters a hook.
169 * @param string $hook the name of the hook
170 * @param string $file the name of the file that hooks into
171 * @param string $function the name of the function that the hook called
174 if (! function_exists('unregister_hook')) {
175 function unregister_hook($hook,$file,$function) {
177 $r = q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
186 if (! function_exists('load_hooks')) {
187 function load_hooks() {
190 $r = q("SELECT * FROM `hook` WHERE 1 ORDER BY `priority` DESC, `file`");
192 if (dbm::is_result($r)) {
193 foreach ($r as $rr) {
194 if (! array_key_exists($rr['hook'],$a->hooks))
195 $a->hooks[$rr['hook']] = array();
196 $a->hooks[$rr['hook']][] = array($rr['file'],$rr['function']);
202 * @brief Calls a hook.
204 * Use this function when you want to be able to allow a hook to manipulate
207 * @param string $name of the hook to call
208 * @param string|array &$data to transmit to the callback handler
210 function call_hooks($name, &$data = null) {
211 $stamp1 = microtime(true);
215 if (is_array($a->hooks) && array_key_exists($name, $a->hooks))
216 foreach ($a->hooks[$name] as $hook)
217 call_single_hook($a, $name, $hook, $data);
221 * @brief Calls a single hook.
223 * @param string $name of the hook to call
224 * @param array $hook Hook data
225 * @param string|array &$data to transmit to the callback handler
227 function call_single_hook($a, $name, $hook, &$data = null) {
228 // Don't run a theme's hook if the user isn't using the theme
229 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false)
232 @include_once($hook[0]);
233 if (function_exists($hook[1])) {
237 // remove orphan hooks
238 q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
246 //check if an app_menu hook exist for plugin $name.
247 //Return true if the plugin is an app
248 if (! function_exists('plugin_is_app')) {
249 function plugin_is_app($name) {
252 if (is_array($a->hooks) && (array_key_exists('app_menu',$a->hooks))) {
253 foreach ($a->hooks['app_menu'] as $hook) {
254 if ($hook[0] == 'addon/'.$name.'/'.$name.'.php')
263 * @brief Parse plugin comment in search of plugin infos.
268 * * Description: A plugin which plugs in
270 * * Author: John <profile url>
271 * * Author: Jane <email>
274 * @param string $plugin the name of the plugin
275 * @return array with the plugin information
278 if (! function_exists('get_plugin_info')){
279 function get_plugin_info($plugin){
291 if (!is_file("addon/$plugin/$plugin.php")) return $info;
293 $stamp1 = microtime(true);
294 $f = file_get_contents("addon/$plugin/$plugin.php");
295 $a->save_timestamp($stamp1, "file");
297 $r = preg_match("|/\*.*\*/|msU", $f, $m);
300 $ll = explode("\n", $m[0]);
301 foreach ( $ll as $l ) {
302 $l = trim($l,"\t\n\r */");
304 list($k,$v) = array_map("trim", explode(":",$l,2));
307 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
309 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
311 $info['author'][] = array('name'=>$v);
314 if (array_key_exists($k,$info)){
328 * @brief Parse theme comment in search of theme infos.
333 * * Description: My Cool Theme
335 * * Author: John <profile url>
336 * * Maintainer: Jane <profile url>
339 * @param string $theme the name of the theme
343 if (! function_exists('get_theme_info')){
344 function get_theme_info($theme){
349 'maintainer' => array(),
352 'experimental' => false,
353 'unsupported' => false
356 if (file_exists("view/theme/$theme/experimental"))
357 $info['experimental'] = true;
358 if (file_exists("view/theme/$theme/unsupported"))
359 $info['unsupported'] = true;
361 if (!is_file("view/theme/$theme/theme.php")) return $info;
364 $stamp1 = microtime(true);
365 $f = file_get_contents("view/theme/$theme/theme.php");
366 $a->save_timestamp($stamp1, "file");
368 $r = preg_match("|/\*.*\*/|msU", $f, $m);
371 $ll = explode("\n", $m[0]);
372 foreach ( $ll as $l ) {
373 $l = trim($l,"\t\n\r */");
375 list($k,$v) = array_map("trim", explode(":",$l,2));
379 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
381 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
383 $info['author'][] = array('name'=>$v);
386 elseif ($k=="maintainer"){
387 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
389 $info['maintainer'][] = array('name'=>$m[1], 'link'=>$m[2]);
391 $info['maintainer'][] = array('name'=>$v);
394 if (array_key_exists($k,$info)){
407 * @brief Returns the theme's screenshot.
409 * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
411 * @param sring $theme The name of the theme
414 function get_theme_screenshot($theme) {
415 $exts = array('.png','.jpg');
416 foreach ($exts as $ext) {
417 if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
418 return(App::get_baseurl() . '/view/theme/' . $theme . '/screenshot' . $ext);
421 return(App::get_baseurl() . '/images/blank.png');
424 // install and uninstall theme
425 if (! function_exists('uninstall_theme')){
426 function uninstall_theme($theme){
427 logger("Addons: uninstalling theme " . $theme);
429 include_once("view/theme/$theme/theme.php");
430 if (function_exists("{$theme}_uninstall")) {
431 $func = "{$theme}_uninstall";
436 if (! function_exists('install_theme')){
437 function install_theme($theme) {
438 // silently fail if theme was removed
440 if (! file_exists("view/theme/$theme/theme.php")) {
444 logger("Addons: installing theme $theme");
446 include_once("view/theme/$theme/theme.php");
448 if (function_exists("{$theme}_install")) {
449 $func = "{$theme}_install";
453 logger("Addons: FAILED installing theme $theme");
461 // check service_class restrictions. If there are no service_classes defined, everything is allowed.
462 // if $usage is supplied, we check against a maximum count and return true if the current usage is
463 // less than the subscriber plan allows. Otherwise we return boolean true or false if the property
464 // is allowed (or not) in this subscriber plan. An unset property for this service plan means
465 // the property is allowed, so it is only necessary to provide negative properties for each plan,
466 // or what the subscriber is not allowed to do.
469 function service_class_allows($uid,$property,$usage = false) {
471 if ($uid == local_user()) {
472 $service_class = $a->user['service_class'];
474 $r = q("SELECT `service_class` FROM `user` WHERE `uid` = %d LIMIT 1",
477 if (dbm::is_result($r)) {
478 $service_class = $r[0]['service_class'];
482 if (! x($service_class)) {
483 // everything is allowed
487 $arr = get_config('service_class',$service_class);
488 if (! is_array($arr) || (! count($arr))) {
492 if ($usage === false) {
493 return ((x($arr[$property])) ? (bool) $arr['property'] : true);
495 if (! array_key_exists($property,$arr)) {
498 return (((intval($usage)) < intval($arr[$property])) ? true : false);
503 function service_class_fetch($uid,$property) {
505 if ($uid == local_user()) {
506 $service_class = $a->user['service_class'];
508 $r = q("SELECT `service_class` FROM `user` WHERE `uid` = %d LIMIT 1",
511 if (dbm::is_result($r)) {
512 $service_class = $r[0]['service_class'];
515 if (! x($service_class))
516 return false; // everything is allowed
518 $arr = get_config('service_class',$service_class);
519 if (! is_array($arr) || (! count($arr)))
522 return((array_key_exists($property,$arr)) ? $arr[$property] : false);
526 function upgrade_link($bbcode = false) {
527 $l = get_config('service_class','upgrade_link');
532 $t = sprintf('[url=%s]' . t('Click here to upgrade.') . '[/url]', $l);
534 $t = sprintf('<a href="%s">' . t('Click here to upgrade.') . '</div>', $l);
539 function upgrade_message($bbcode = false) {
540 $x = upgrade_link($bbcode);
541 return t('This action exceeds the limits set by your subscription plan.') . (($x) ? ' ' . $x : '') ;
544 function upgrade_bool_message($bbcode = false) {
545 $x = upgrade_link($bbcode);
546 return t('This action is not available under your subscription plan.') . (($x) ? ' ' . $x : '') ;
550 * @brief Get the full path to relevant theme files by filename
552 * This function search in the theme directory (and if not present in global theme directory)
553 * if there is a directory with the file extension and for a file with the given
556 * @param string $file Filename
557 * @param string $root Full root path
558 * @return string Path to the file or empty string if the file isn't found
560 function theme_include($file, $root = '') {
561 // Make sure $root ends with a slash / if it's not blank
562 if ($root !== '' && $root[strlen($root)-1] !== '/') {
565 $theme_info = $a->theme_info;
566 if (is_array($theme_info) AND array_key_exists('extends',$theme_info)) {
567 $parent = $theme_info['extends'];
571 $theme = current_theme();
573 $ext = substr($file,strrpos($file,'.')+1);
575 "{$root}view/theme/$thname/$ext/$file",
576 "{$root}view/theme/$parent/$ext/$file",
577 "{$root}view/$ext/$file",
579 foreach ($paths as $p) {
580 // strpos() is faster than strstr when checking if one string is in another (http://php.net/manual/en/function.strstr.php)
581 if (strpos($p,'NOPATH') !== false) {
583 } elseif (file_exists($p)) {