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 dba::insert('addon', array('name' => $plugin, 'installed' => true,
52 'timestamp' => $t, 'plugin_admin' => $plugin_admin));
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.
58 if (file_exists('addon/' . $plugin . '/.hidden')) {
59 dba::update('addon', array('hidden' => true), array('name' => $plugin));
64 logger("Addons: FAILED installing " . $plugin);
70 // reload all updated plugins
72 if (! function_exists('reload_plugins')) {
73 function reload_plugins() {
74 $plugins = get_config('system','addon');
75 if (strlen($plugins)) {
77 $r = q("SELECT * FROM `addon` WHERE `installed` = 1");
78 if (dbm::is_result($r))
83 $parr = explode(',',$plugins);
86 foreach ($parr as $pl) {
90 $fname = 'addon/' . $pl . '/' . $pl . '.php';
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);
99 if (function_exists($pl . '_uninstall')) {
100 $func = $pl . '_uninstall';
103 if (function_exists($pl . '_install')) {
104 $func = $pl . '_install';
107 dba::update('addon', array('timestamp' => $t), array('id' => $i['id']));
118 * @brief check if addon is enabled
120 * @param string $plugin
123 function plugin_enabled($plugin) {
124 return dba::exists('addon', array('installed' => true, 'name' => $plugin));
129 * @brief registers a hook.
131 * @param string $hook the name of the hook
132 * @param string $file the name of the file that hooks into
133 * @param string $function the name of the function that the hook will call
134 * @param int $priority A priority (defaults to 0)
137 if (! function_exists('register_hook')) {
138 function register_hook($hook,$file,$function,$priority=0) {
140 $r = q("SELECT * FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' LIMIT 1",
145 if (dbm::is_result($r))
148 $r = dba::insert('hook', array('hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority));
154 * @brief unregisters a hook.
156 * @param string $hook the name of the hook
157 * @param string $file the name of the file that hooks into
158 * @param string $function the name of the function that the hook called
161 if (! function_exists('unregister_hook')) {
162 function unregister_hook($hook,$file,$function) {
164 $r = q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
173 function load_hooks() {
176 $r = dba::select('hook', array('hook', 'file', 'function'), array(), array('order' => array('priority' => 'desc', 'file')));
178 while ($rr = dba::fetch($r)) {
179 if (! array_key_exists($rr['hook'],$a->hooks)) {
180 $a->hooks[$rr['hook']] = array();
182 $a->hooks[$rr['hook']][] = array($rr['file'],$rr['function']);
188 * @brief Calls a hook.
190 * Use this function when you want to be able to allow a hook to manipulate
193 * @param string $name of the hook to call
194 * @param string|array &$data to transmit to the callback handler
196 function call_hooks($name, &$data = null) {
197 $stamp1 = microtime(true);
201 if (is_array($a->hooks) && array_key_exists($name, $a->hooks))
202 foreach ($a->hooks[$name] as $hook)
203 call_single_hook($a, $name, $hook, $data);
207 * @brief Calls a single hook.
209 * @param string $name of the hook to call
210 * @param array $hook Hook data
211 * @param string|array &$data to transmit to the callback handler
213 function call_single_hook($a, $name, $hook, &$data = null) {
214 // Don't run a theme's hook if the user isn't using the theme
215 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false)
218 @include_once($hook[0]);
219 if (function_exists($hook[1])) {
223 // remove orphan hooks
224 q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
232 //check if an app_menu hook exist for plugin $name.
233 //Return true if the plugin is an app
234 if (! function_exists('plugin_is_app')) {
235 function plugin_is_app($name) {
238 if (is_array($a->hooks) && (array_key_exists('app_menu',$a->hooks))) {
239 foreach ($a->hooks['app_menu'] as $hook) {
240 if ($hook[0] == 'addon/'.$name.'/'.$name.'.php')
249 * @brief Parse plugin comment in search of plugin infos.
254 * * Description: A plugin which plugs in
256 * * Author: John <profile url>
257 * * Author: Jane <email>
260 * @param string $plugin the name of the plugin
261 * @return array with the plugin information
264 if (! function_exists('get_plugin_info')){
265 function get_plugin_info($plugin){
277 if (!is_file("addon/$plugin/$plugin.php")) return $info;
279 $stamp1 = microtime(true);
280 $f = file_get_contents("addon/$plugin/$plugin.php");
281 $a->save_timestamp($stamp1, "file");
283 $r = preg_match("|/\*.*\*/|msU", $f, $m);
286 $ll = explode("\n", $m[0]);
287 foreach ( $ll as $l ) {
288 $l = trim($l,"\t\n\r */");
290 list($k,$v) = array_map("trim", explode(":",$l,2));
293 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
295 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
297 $info['author'][] = array('name'=>$v);
300 if (array_key_exists($k,$info)){
314 * @brief Parse theme comment in search of theme infos.
319 * * Description: My Cool Theme
321 * * Author: John <profile url>
322 * * Maintainer: Jane <profile url>
325 * @param string $theme the name of the theme
329 if (! function_exists('get_theme_info')){
330 function get_theme_info($theme){
335 'maintainer' => array(),
338 'experimental' => false,
339 'unsupported' => false
342 if (file_exists("view/theme/$theme/experimental"))
343 $info['experimental'] = true;
344 if (file_exists("view/theme/$theme/unsupported"))
345 $info['unsupported'] = true;
347 if (!is_file("view/theme/$theme/theme.php")) return $info;
350 $stamp1 = microtime(true);
351 $f = file_get_contents("view/theme/$theme/theme.php");
352 $a->save_timestamp($stamp1, "file");
354 $r = preg_match("|/\*.*\*/|msU", $f, $m);
357 $ll = explode("\n", $m[0]);
358 foreach ( $ll as $l ) {
359 $l = trim($l,"\t\n\r */");
361 list($k,$v) = array_map("trim", explode(":",$l,2));
365 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
367 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
369 $info['author'][] = array('name'=>$v);
372 elseif ($k=="maintainer"){
373 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
375 $info['maintainer'][] = array('name'=>$m[1], 'link'=>$m[2]);
377 $info['maintainer'][] = array('name'=>$v);
380 if (array_key_exists($k,$info)){
393 * @brief Returns the theme's screenshot.
395 * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
397 * @param sring $theme The name of the theme
400 function get_theme_screenshot($theme) {
401 $exts = array('.png','.jpg');
402 foreach ($exts as $ext) {
403 if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
404 return(App::get_baseurl() . '/view/theme/' . $theme . '/screenshot' . $ext);
407 return(App::get_baseurl() . '/images/blank.png');
410 // install and uninstall theme
411 if (! function_exists('uninstall_theme')){
412 function uninstall_theme($theme){
413 logger("Addons: uninstalling theme " . $theme);
415 include_once("view/theme/$theme/theme.php");
416 if (function_exists("{$theme}_uninstall")) {
417 $func = "{$theme}_uninstall";
422 if (! function_exists('install_theme')){
423 function install_theme($theme) {
424 // silently fail if theme was removed
426 if (! file_exists("view/theme/$theme/theme.php")) {
430 logger("Addons: installing theme $theme");
432 include_once("view/theme/$theme/theme.php");
434 if (function_exists("{$theme}_install")) {
435 $func = "{$theme}_install";
439 logger("Addons: FAILED installing theme $theme");
447 // check service_class restrictions. If there are no service_classes defined, everything is allowed.
448 // if $usage is supplied, we check against a maximum count and return true if the current usage is
449 // less than the subscriber plan allows. Otherwise we return boolean true or false if the property
450 // is allowed (or not) in this subscriber plan. An unset property for this service plan means
451 // the property is allowed, so it is only necessary to provide negative properties for each plan,
452 // or what the subscriber is not allowed to do.
455 function service_class_allows($uid,$property,$usage = false) {
457 if ($uid == local_user()) {
458 $service_class = $a->user['service_class'];
460 $r = q("SELECT `service_class` FROM `user` WHERE `uid` = %d LIMIT 1",
463 if (dbm::is_result($r)) {
464 $service_class = $r[0]['service_class'];
468 if (! x($service_class)) {
469 // everything is allowed
473 $arr = get_config('service_class',$service_class);
474 if (! is_array($arr) || (! count($arr))) {
478 if ($usage === false) {
479 return ((x($arr[$property])) ? (bool) $arr['property'] : true);
481 if (! array_key_exists($property,$arr)) {
484 return (((intval($usage)) < intval($arr[$property])) ? true : false);
489 function service_class_fetch($uid,$property) {
491 if ($uid == local_user()) {
492 $service_class = $a->user['service_class'];
494 $r = q("SELECT `service_class` FROM `user` WHERE `uid` = %d LIMIT 1",
497 if (dbm::is_result($r)) {
498 $service_class = $r[0]['service_class'];
501 if (! x($service_class))
502 return false; // everything is allowed
504 $arr = get_config('service_class',$service_class);
505 if (! is_array($arr) || (! count($arr)))
508 return((array_key_exists($property,$arr)) ? $arr[$property] : false);
512 function upgrade_link($bbcode = false) {
513 $l = get_config('service_class','upgrade_link');
518 $t = sprintf('[url=%s]' . t('Click here to upgrade.') . '[/url]', $l);
520 $t = sprintf('<a href="%s">' . t('Click here to upgrade.') . '</div>', $l);
525 function upgrade_message($bbcode = false) {
526 $x = upgrade_link($bbcode);
527 return t('This action exceeds the limits set by your subscription plan.') . (($x) ? ' ' . $x : '') ;
530 function upgrade_bool_message($bbcode = false) {
531 $x = upgrade_link($bbcode);
532 return t('This action is not available under your subscription plan.') . (($x) ? ' ' . $x : '') ;
536 * @brief Get the full path to relevant theme files by filename
538 * This function search in the theme directory (and if not present in global theme directory)
539 * if there is a directory with the file extension and for a file with the given
542 * @param string $file Filename
543 * @param string $root Full root path
544 * @return string Path to the file or empty string if the file isn't found
546 function theme_include($file, $root = '') {
547 $file = basename($file);
549 // Make sure $root ends with a slash / if it's not blank
550 if ($root !== '' && $root[strlen($root)-1] !== '/') {
553 $theme_info = get_app()->theme_info;
554 if (is_array($theme_info) && array_key_exists('extends',$theme_info)) {
555 $parent = $theme_info['extends'];
559 $theme = current_theme();
561 $ext = substr($file,strrpos($file,'.')+1);
563 "{$root}view/theme/$thname/$ext/$file",
564 "{$root}view/theme/$parent/$ext/$file",
565 "{$root}view/$ext/$file",
567 foreach ($paths as $p) {
568 // strpos() is faster than strstr when checking if one string is in another (http://php.net/manual/en/function.strstr.php)
569 if (strpos($p,'NOPATH') !== false) {
571 } elseif (file_exists($p)) {