3 * @file include/identity.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 if(! function_exists('call_hooks')) {
209 function call_hooks($name, &$data = null) {
210 $stamp1 = microtime(true);
214 #logger($name, LOGGER_ALL);
216 if((is_array($a->hooks)) && (array_key_exists($name,$a->hooks))) {
217 foreach($a->hooks[$name] as $hook) {
218 // Don't run a theme's hook if the user isn't using the theme
219 if(strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false)
222 @include_once($hook[0]);
223 if(function_exists($hook[1])) {
225 //logger($name." => ".$hook[0].":".$func."()", LOGGER_DEBUG);
229 // remove orphan hooks
230 q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
240 //check if an app_menu hook exist for plugin $name.
241 //Return true if the plugin is an app
242 if(! function_exists('plugin_is_app')) {
243 function plugin_is_app($name) {
246 if(is_array($a->hooks) && (array_key_exists('app_menu',$a->hooks))) {
247 foreach($a->hooks['app_menu'] as $hook) {
248 if($hook[0] == 'addon/'.$name.'/'.$name.'.php')
257 * @brief Parse plugin comment in search of plugin infos.
262 * * Description: A plugin which plugs in
264 * * Author: John <profile url>
265 * * Author: Jane <email>
268 * @param string $plugin the name of the plugin
269 * @return array with the plugin information
272 if (! function_exists('get_plugin_info')){
273 function get_plugin_info($plugin){
285 if (!is_file("addon/$plugin/$plugin.php")) return $info;
287 $stamp1 = microtime(true);
288 $f = file_get_contents("addon/$plugin/$plugin.php");
289 $a->save_timestamp($stamp1, "file");
291 $r = preg_match("|/\*.*\*/|msU", $f, $m);
294 $ll = explode("\n", $m[0]);
295 foreach( $ll as $l ) {
296 $l = trim($l,"\t\n\r */");
298 list($k,$v) = array_map("trim", explode(":",$l,2));
301 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
303 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
305 $info['author'][] = array('name'=>$v);
308 if (array_key_exists($k,$info)){
322 * @brief Parse theme comment in search of theme infos.
327 * * Description: My Cool Theme
329 * * Author: John <profile url>
330 * * Maintainer: Jane <profile url>
333 * @param string $theme the name of the theme
337 if (! function_exists('get_theme_info')){
338 function get_theme_info($theme){
343 'maintainer' => array(),
346 'experimental' => false,
347 'unsupported' => false
350 if(file_exists("view/theme/$theme/experimental"))
351 $info['experimental'] = true;
352 if(file_exists("view/theme/$theme/unsupported"))
353 $info['unsupported'] = true;
355 if (!is_file("view/theme/$theme/theme.php")) return $info;
358 $stamp1 = microtime(true);
359 $f = file_get_contents("view/theme/$theme/theme.php");
360 $a->save_timestamp($stamp1, "file");
362 $r = preg_match("|/\*.*\*/|msU", $f, $m);
365 $ll = explode("\n", $m[0]);
366 foreach( $ll as $l ) {
367 $l = trim($l,"\t\n\r */");
369 list($k,$v) = array_map("trim", explode(":",$l,2));
373 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
375 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
377 $info['author'][] = array('name'=>$v);
380 elseif ($k=="maintainer"){
381 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
383 $info['maintainer'][] = array('name'=>$m[1], 'link'=>$m[2]);
385 $info['maintainer'][] = array('name'=>$v);
388 if (array_key_exists($k,$info)){
401 * @brief Returns the theme's screenshot.
403 * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
405 * @param sring $theme The name of the theme
408 function get_theme_screenshot($theme) {
410 $exts = array('.png','.jpg');
411 foreach($exts as $ext) {
412 if(file_exists('view/theme/' . $theme . '/screenshot' . $ext))
413 return($a->get_baseurl() . '/view/theme/' . $theme . '/screenshot' . $ext);
415 return($a->get_baseurl() . '/images/blank.png');
418 // install and uninstall theme
419 if (! function_exists('uninstall_theme')){
420 function uninstall_theme($theme){
421 logger("Addons: uninstalling theme " . $theme);
423 @include_once("view/theme/$theme/theme.php");
424 if(function_exists("{$theme}_uninstall")) {
425 $func = "{$theme}_uninstall";
430 if (! function_exists('install_theme')){
431 function install_theme($theme) {
432 // silently fail if theme was removed
434 if(! file_exists("view/theme/$theme/theme.php"))
437 logger("Addons: installing theme $theme");
439 @include_once("view/theme/$theme/theme.php");
441 if(function_exists("{$theme}_install")) {
442 $func = "{$theme}_install";
447 logger("Addons: FAILED installing theme $theme");
455 // check service_class restrictions. If there are no service_classes defined, everything is allowed.
456 // if $usage is supplied, we check against a maximum count and return true if the current usage is
457 // less than the subscriber plan allows. Otherwise we return boolean true or false if the property
458 // is allowed (or not) in this subscriber plan. An unset property for this service plan means
459 // the property is allowed, so it is only necessary to provide negative properties for each plan,
460 // or what the subscriber is not allowed to do.
463 function service_class_allows($uid,$property,$usage = false) {
465 if($uid == local_user()) {
466 $service_class = $a->user['service_class'];
469 $r = q("SELECT `service_class` FROM `user` WHERE `uid` = %d LIMIT 1",
472 if($r !== false and count($r)) {
473 $service_class = $r[0]['service_class'];
476 if(! x($service_class))
477 return true; // everything is allowed
479 $arr = get_config('service_class',$service_class);
480 if(! is_array($arr) || (! count($arr)))
484 return ((x($arr[$property])) ? (bool) $arr['property'] : true);
486 if(! array_key_exists($property,$arr))
488 return (((intval($usage)) < intval($arr[$property])) ? true : false);
493 function service_class_fetch($uid,$property) {
495 if($uid == local_user()) {
496 $service_class = $a->user['service_class'];
499 $r = q("SELECT `service_class` FROM `user` WHERE `uid` = %d LIMIT 1",
502 if($r !== false and count($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');
522 $t = sprintf('[url=%s]' . t('Click here to upgrade.') . '[/url]', $l);
524 $t = sprintf('<a href="%s">' . t('Click here to upgrade.') . '</div>', $l);
528 function upgrade_message($bbcode = false) {
529 $x = upgrade_link($bbcode);
530 return t('This action exceeds the limits set by your subscription plan.') . (($x) ? ' ' . $x : '') ;
533 function upgrade_bool_message($bbcode = false) {
534 $x = upgrade_link($bbcode);
535 return t('This action is not available under your subscription plan.') . (($x) ? ' ' . $x : '') ;