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 q("UPDATE `addon` SET `hidden` = 1 WHERE `name` = '%s'",
66 logger("Addons: FAILED installing " . $plugin);
72 // reload all updated plugins
74 if (! function_exists('reload_plugins')) {
75 function reload_plugins() {
76 $plugins = get_config('system','addon');
77 if (strlen($plugins)) {
79 $r = q("SELECT * FROM `addon` WHERE `installed` = 1");
80 if (dbm::is_result($r))
85 $parr = explode(',',$plugins);
88 foreach ($parr as $pl) {
92 $fname = 'addon/' . $pl . '/' . $pl . '.php';
94 if (file_exists($fname)) {
95 $t = @filemtime($fname);
96 foreach ($installed as $i) {
97 if (($i['name'] == $pl) && ($i['timestamp'] != $t)) {
98 logger('Reloading plugin: ' . $i['name']);
99 @include_once($fname);
101 if (function_exists($pl . '_uninstall')) {
102 $func = $pl . '_uninstall';
105 if (function_exists($pl . '_install')) {
106 $func = $pl . '_install';
109 q("UPDATE `addon` SET `timestamp` = %d WHERE `id` = %d",
123 * @brief check if addon is enabled
125 * @param string $plugin
128 function plugin_enabled($plugin) {
129 $r = q("SELECT * FROM `addon` WHERE `installed` = 1 AND `name` = '%s'", $plugin);
130 return ((dbm::is_result($r)) && (count($r) > 0));
135 * @brief registers a hook.
137 * @param string $hook the name of the hook
138 * @param string $file the name of the file that hooks into
139 * @param string $function the name of the function that the hook will call
140 * @param int $priority A priority (defaults to 0)
143 if (! function_exists('register_hook')) {
144 function register_hook($hook,$file,$function,$priority=0) {
146 $r = q("SELECT * FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' LIMIT 1",
151 if (dbm::is_result($r))
154 $r = dba::insert('hook', array('hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority));
160 * @brief unregisters a hook.
162 * @param string $hook the name of the hook
163 * @param string $file the name of the file that hooks into
164 * @param string $function the name of the function that the hook called
167 if (! function_exists('unregister_hook')) {
168 function unregister_hook($hook,$file,$function) {
170 $r = q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
179 function load_hooks() {
182 $r = dba::select('hook', array('hook', 'file', 'function'), array(), array('order' => array('priority' => 'desc', 'file')));
184 while ($rr = dba::fetch($r)) {
185 if (! array_key_exists($rr['hook'],$a->hooks)) {
186 $a->hooks[$rr['hook']] = array();
188 $a->hooks[$rr['hook']][] = array($rr['file'],$rr['function']);
193 * @brief Calls a hook.
195 * Use this function when you want to be able to allow a hook to manipulate
198 * @param string $name of the hook to call
199 * @param string|array &$data to transmit to the callback handler
201 function call_hooks($name, &$data = null) {
202 $stamp1 = microtime(true);
206 if (is_array($a->hooks) && array_key_exists($name, $a->hooks))
207 foreach ($a->hooks[$name] as $hook)
208 call_single_hook($a, $name, $hook, $data);
212 * @brief Calls a single hook.
214 * @param string $name of the hook to call
215 * @param array $hook Hook data
216 * @param string|array &$data to transmit to the callback handler
218 function call_single_hook($a, $name, $hook, &$data = null) {
219 // Don't run a theme's hook if the user isn't using the theme
220 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false)
223 @include_once($hook[0]);
224 if (function_exists($hook[1])) {
228 // remove orphan hooks
229 q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
237 //check if an app_menu hook exist for plugin $name.
238 //Return true if the plugin is an app
239 if (! function_exists('plugin_is_app')) {
240 function plugin_is_app($name) {
243 if (is_array($a->hooks) && (array_key_exists('app_menu',$a->hooks))) {
244 foreach ($a->hooks['app_menu'] as $hook) {
245 if ($hook[0] == 'addon/'.$name.'/'.$name.'.php')
254 * @brief Parse plugin comment in search of plugin infos.
259 * * Description: A plugin which plugs in
261 * * Author: John <profile url>
262 * * Author: Jane <email>
265 * @param string $plugin the name of the plugin
266 * @return array with the plugin information
269 if (! function_exists('get_plugin_info')){
270 function get_plugin_info($plugin){
282 if (!is_file("addon/$plugin/$plugin.php")) return $info;
284 $stamp1 = microtime(true);
285 $f = file_get_contents("addon/$plugin/$plugin.php");
286 $a->save_timestamp($stamp1, "file");
288 $r = preg_match("|/\*.*\*/|msU", $f, $m);
291 $ll = explode("\n", $m[0]);
292 foreach ( $ll as $l ) {
293 $l = trim($l,"\t\n\r */");
295 list($k,$v) = array_map("trim", explode(":",$l,2));
298 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
300 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
302 $info['author'][] = array('name'=>$v);
305 if (array_key_exists($k,$info)){
319 * @brief Parse theme comment in search of theme infos.
324 * * Description: My Cool Theme
326 * * Author: John <profile url>
327 * * Maintainer: Jane <profile url>
330 * @param string $theme the name of the theme
334 if (! function_exists('get_theme_info')){
335 function get_theme_info($theme){
340 'maintainer' => array(),
343 'experimental' => false,
344 'unsupported' => false
347 if (file_exists("view/theme/$theme/experimental"))
348 $info['experimental'] = true;
349 if (file_exists("view/theme/$theme/unsupported"))
350 $info['unsupported'] = true;
352 if (!is_file("view/theme/$theme/theme.php")) return $info;
355 $stamp1 = microtime(true);
356 $f = file_get_contents("view/theme/$theme/theme.php");
357 $a->save_timestamp($stamp1, "file");
359 $r = preg_match("|/\*.*\*/|msU", $f, $m);
362 $ll = explode("\n", $m[0]);
363 foreach ( $ll as $l ) {
364 $l = trim($l,"\t\n\r */");
366 list($k,$v) = array_map("trim", explode(":",$l,2));
370 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
372 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
374 $info['author'][] = array('name'=>$v);
377 elseif ($k=="maintainer"){
378 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
380 $info['maintainer'][] = array('name'=>$m[1], 'link'=>$m[2]);
382 $info['maintainer'][] = array('name'=>$v);
385 if (array_key_exists($k,$info)){
398 * @brief Returns the theme's screenshot.
400 * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
402 * @param sring $theme The name of the theme
405 function get_theme_screenshot($theme) {
406 $exts = array('.png','.jpg');
407 foreach ($exts as $ext) {
408 if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
409 return(App::get_baseurl() . '/view/theme/' . $theme . '/screenshot' . $ext);
412 return(App::get_baseurl() . '/images/blank.png');
415 // install and uninstall theme
416 if (! function_exists('uninstall_theme')){
417 function uninstall_theme($theme){
418 logger("Addons: uninstalling theme " . $theme);
420 include_once("view/theme/$theme/theme.php");
421 if (function_exists("{$theme}_uninstall")) {
422 $func = "{$theme}_uninstall";
427 if (! function_exists('install_theme')){
428 function install_theme($theme) {
429 // silently fail if theme was removed
431 if (! file_exists("view/theme/$theme/theme.php")) {
435 logger("Addons: installing theme $theme");
437 include_once("view/theme/$theme/theme.php");
439 if (function_exists("{$theme}_install")) {
440 $func = "{$theme}_install";
444 logger("Addons: FAILED installing theme $theme");
452 // check service_class restrictions. If there are no service_classes defined, everything is allowed.
453 // if $usage is supplied, we check against a maximum count and return true if the current usage is
454 // less than the subscriber plan allows. Otherwise we return boolean true or false if the property
455 // is allowed (or not) in this subscriber plan. An unset property for this service plan means
456 // the property is allowed, so it is only necessary to provide negative properties for each plan,
457 // or what the subscriber is not allowed to do.
460 function service_class_allows($uid,$property,$usage = false) {
462 if ($uid == local_user()) {
463 $service_class = $a->user['service_class'];
465 $r = q("SELECT `service_class` FROM `user` WHERE `uid` = %d LIMIT 1",
468 if (dbm::is_result($r)) {
469 $service_class = $r[0]['service_class'];
473 if (! x($service_class)) {
474 // everything is allowed
478 $arr = get_config('service_class',$service_class);
479 if (! is_array($arr) || (! count($arr))) {
483 if ($usage === false) {
484 return ((x($arr[$property])) ? (bool) $arr['property'] : true);
486 if (! array_key_exists($property,$arr)) {
489 return (((intval($usage)) < intval($arr[$property])) ? true : false);
494 function service_class_fetch($uid,$property) {
496 if ($uid == local_user()) {
497 $service_class = $a->user['service_class'];
499 $r = q("SELECT `service_class` FROM `user` WHERE `uid` = %d LIMIT 1",
502 if (dbm::is_result($r)) {
503 $service_class = $r[0]['service_class'];
506 if (! x($service_class))
507 return false; // everything is allowed
509 $arr = get_config('service_class',$service_class);
510 if (! is_array($arr) || (! count($arr)))
513 return((array_key_exists($property,$arr)) ? $arr[$property] : false);
517 function upgrade_link($bbcode = false) {
518 $l = get_config('service_class','upgrade_link');
523 $t = sprintf('[url=%s]' . t('Click here to upgrade.') . '[/url]', $l);
525 $t = sprintf('<a href="%s">' . t('Click here to upgrade.') . '</div>', $l);
530 function upgrade_message($bbcode = false) {
531 $x = upgrade_link($bbcode);
532 return t('This action exceeds the limits set by your subscription plan.') . (($x) ? ' ' . $x : '') ;
535 function upgrade_bool_message($bbcode = false) {
536 $x = upgrade_link($bbcode);
537 return t('This action is not available under your subscription plan.') . (($x) ? ' ' . $x : '') ;
541 * @brief Get the full path to relevant theme files by filename
543 * This function search in the theme directory (and if not present in global theme directory)
544 * if there is a directory with the file extension and for a file with the given
547 * @param string $file Filename
548 * @param string $root Full root path
549 * @return string Path to the file or empty string if the file isn't found
551 function theme_include($file, $root = '') {
552 // Make sure $root ends with a slash / if it's not blank
553 if ($root !== '' && $root[strlen($root)-1] !== '/') {
556 $theme_info = get_app()->theme_info;
557 if (is_array($theme_info) && array_key_exists('extends',$theme_info)) {
558 $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) {
574 } elseif (file_exists($p)) {