3 * @file include/identity.php
6 // install and uninstall plugin
7 if (! function_exists('uninstall_plugin')){
8 function uninstall_plugin($plugin){
9 logger("Addons: uninstalling " . $plugin);
10 q("DELETE FROM `addon` WHERE `name` = '%s' ",
14 @include_once('addon/' . $plugin . '/' . $plugin . '.php');
15 if(function_exists($plugin . '_uninstall')) {
16 $func = $plugin . '_uninstall';
21 if (! function_exists('install_plugin')){
22 function install_plugin($plugin) {
23 // silently fail if plugin was removed
25 if(! file_exists('addon/' . $plugin . '/' . $plugin . '.php'))
27 logger("Addons: installing " . $plugin);
28 $t = @filemtime('addon/' . $plugin . '/' . $plugin . '.php');
29 @include_once('addon/' . $plugin . '/' . $plugin . '.php');
30 if(function_exists($plugin . '_install')) {
31 $func = $plugin . '_install';
34 $plugin_admin = (function_exists($plugin."_plugin_admin")?1:0);
36 $r = q("INSERT INTO `addon` (`name`, `installed`, `timestamp`, `plugin_admin`) VALUES ( '%s', 1, %d , %d ) ",
42 // we can add the following with the previous SQL
43 // once most site tables have been updated.
44 // This way the system won't fall over dead during the update.
46 if(file_exists('addon/' . $plugin . '/.hidden')) {
47 q("update addon set hidden = 1 where name = '%s'",
54 logger("Addons: FAILED installing " . $plugin);
60 // reload all updated plugins
62 if(! function_exists('reload_plugins')) {
63 function reload_plugins() {
64 $plugins = get_config('system','addon');
65 if(strlen($plugins)) {
67 $r = q("SELECT * FROM `addon` WHERE `installed` = 1");
73 $parr = explode(',',$plugins);
76 foreach($parr as $pl) {
80 $fname = 'addon/' . $pl . '/' . $pl . '.php';
82 if(file_exists($fname)) {
83 $t = @filemtime($fname);
84 foreach($installed as $i) {
85 if(($i['name'] == $pl) && ($i['timestamp'] != $t)) {
86 logger('Reloading plugin: ' . $i['name']);
87 @include_once($fname);
89 if(function_exists($pl . '_uninstall')) {
90 $func = $pl . '_uninstall';
93 if(function_exists($pl . '_install')) {
94 $func = $pl . '_install';
97 q("UPDATE `addon` SET `timestamp` = %d WHERE `id` = %d",
111 * @brief check if addon is enabled
113 * @param string $plugin
116 function plugin_enabled($plugin) {
117 $r = q("SELECT * FROM `addon` WHERE `installed` = 1 AND `name` = '%s'", $plugin);
118 return((bool)(count($r) > 0));
123 if(! function_exists('register_hook')) {
124 function register_hook($hook,$file,$function,$priority=0) {
126 $r = q("SELECT * FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' LIMIT 1",
134 $r = q("INSERT INTO `hook` (`hook`, `file`, `function`, `priority`) VALUES ( '%s', '%s', '%s', '%s' ) ",
143 if(! function_exists('unregister_hook')) {
144 function unregister_hook($hook,$file,$function) {
146 $r = q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
155 if(! function_exists('load_hooks')) {
156 function load_hooks() {
159 $r = q("SELECT * FROM `hook` WHERE 1 ORDER BY `priority` DESC, `file`");
162 if(! array_key_exists($rr['hook'],$a->hooks))
163 $a->hooks[$rr['hook']] = array();
164 $a->hooks[$rr['hook']][] = array($rr['file'],$rr['function']);
170 if(! function_exists('call_hooks')) {
171 function call_hooks($name, &$data = null) {
172 $stamp1 = microtime(true);
176 #logger($name, LOGGER_ALL);
178 if((is_array($a->hooks)) && (array_key_exists($name,$a->hooks))) {
179 foreach($a->hooks[$name] as $hook) {
180 // Don't run a theme's hook if the user isn't using the theme
181 if(strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false)
184 @include_once($hook[0]);
185 if(function_exists($hook[1])) {
187 //logger($name." => ".$hook[0].":".$func."()", LOGGER_DEBUG);
191 // remove orphan hooks
192 q("delete from hook where hook = '%s' and file = '%s' and function = '%s'",
202 //check if an app_menu hook exist for plugin $name.
203 //Return true if the plugin is an app
204 if(! function_exists('plugin_is_app')) {
205 function plugin_is_app($name) {
208 if(is_array($a->hooks) && (array_key_exists('app_menu',$a->hooks))) {
209 foreach($a->hooks['app_menu'] as $hook) {
210 if($hook[0] == 'addon/'.$name.'/'.$name.'.php')
219 * parse plugin comment in search of plugin infos.
223 * * Description: A plugin which plugs in
225 * * Author: John <profile url>
226 * * Author: Jane <email>
230 if (! function_exists('get_plugin_info')){
231 function get_plugin_info($plugin){
243 if (!is_file("addon/$plugin/$plugin.php")) return $info;
245 $stamp1 = microtime(true);
246 $f = file_get_contents("addon/$plugin/$plugin.php");
247 $a->save_timestamp($stamp1, "file");
249 $r = preg_match("|/\*.*\*/|msU", $f, $m);
252 $ll = explode("\n", $m[0]);
253 foreach( $ll as $l ) {
254 $l = trim($l,"\t\n\r */");
256 list($k,$v) = array_map("trim", explode(":",$l,2));
259 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
261 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
263 $info['author'][] = array('name'=>$v);
266 if (array_key_exists($k,$info)){
280 * parse theme comment in search of theme infos.
284 * * Description: My Cool Theme
286 * * Author: John <profile url>
287 * * Maintainer: Jane <profile url>
291 if (! function_exists('get_theme_info')){
292 function get_theme_info($theme){
297 'maintainer' => array(),
300 'experimental' => false,
301 'unsupported' => false
304 if(file_exists("view/theme/$theme/experimental"))
305 $info['experimental'] = true;
306 if(file_exists("view/theme/$theme/unsupported"))
307 $info['unsupported'] = true;
309 if (!is_file("view/theme/$theme/theme.php")) return $info;
312 $stamp1 = microtime(true);
313 $f = file_get_contents("view/theme/$theme/theme.php");
314 $a->save_timestamp($stamp1, "file");
316 $r = preg_match("|/\*.*\*/|msU", $f, $m);
319 $ll = explode("\n", $m[0]);
320 foreach( $ll as $l ) {
321 $l = trim($l,"\t\n\r */");
323 list($k,$v) = array_map("trim", explode(":",$l,2));
327 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
329 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
331 $info['author'][] = array('name'=>$v);
334 elseif ($k=="maintainer"){
335 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
337 $info['maintainer'][] = array('name'=>$m[1], 'link'=>$m[2]);
339 $info['maintainer'][] = array('name'=>$v);
342 if (array_key_exists($k,$info)){
355 function get_theme_screenshot($theme) {
357 $exts = array('.png','.jpg');
358 foreach($exts as $ext) {
359 if(file_exists('view/theme/' . $theme . '/screenshot' . $ext))
360 return($a->get_baseurl() . '/view/theme/' . $theme . '/screenshot' . $ext);
362 return($a->get_baseurl() . '/images/blank.png');
365 // install and uninstall theme
366 if (! function_exists('uninstall_theme')){
367 function uninstall_theme($theme){
368 logger("Addons: uninstalling theme " . $theme);
370 @include_once("view/theme/$theme/theme.php");
371 if(function_exists("{$theme}_uninstall")) {
372 $func = "{$theme}_uninstall";
377 if (! function_exists('install_theme')){
378 function install_theme($theme) {
379 // silently fail if theme was removed
381 if(! file_exists("view/theme/$theme/theme.php"))
384 logger("Addons: installing theme $theme");
386 @include_once("view/theme/$theme/theme.php");
388 if(function_exists("{$theme}_install")) {
389 $func = "{$theme}_install";
394 logger("Addons: FAILED installing theme $theme");
402 // check service_class restrictions. If there are no service_classes defined, everything is allowed.
403 // if $usage is supplied, we check against a maximum count and return true if the current usage is
404 // less than the subscriber plan allows. Otherwise we return boolean true or false if the property
405 // is allowed (or not) in this subscriber plan. An unset property for this service plan means
406 // the property is allowed, so it is only necessary to provide negative properties for each plan,
407 // or what the subscriber is not allowed to do.
410 function service_class_allows($uid,$property,$usage = false) {
412 if($uid == local_user()) {
413 $service_class = $a->user['service_class'];
416 $r = q("select service_class from user where uid = %d limit 1",
419 if($r !== false and count($r)) {
420 $service_class = $r[0]['service_class'];
423 if(! x($service_class))
424 return true; // everything is allowed
426 $arr = get_config('service_class',$service_class);
427 if(! is_array($arr) || (! count($arr)))
431 return ((x($arr[$property])) ? (bool) $arr['property'] : true);
433 if(! array_key_exists($property,$arr))
435 return (((intval($usage)) < intval($arr[$property])) ? true : false);
440 function service_class_fetch($uid,$property) {
442 if($uid == local_user()) {
443 $service_class = $a->user['service_class'];
446 $r = q("select service_class from user where uid = %d limit 1",
449 if($r !== false and count($r)) {
450 $service_class = $r[0]['service_class'];
453 if(! x($service_class))
454 return false; // everything is allowed
456 $arr = get_config('service_class',$service_class);
457 if(! is_array($arr) || (! count($arr)))
460 return((array_key_exists($property,$arr)) ? $arr[$property] : false);
464 function upgrade_link($bbcode = false) {
465 $l = get_config('service_class','upgrade_link');
469 $t = sprintf('[url=%s]' . t('Click here to upgrade.') . '[/url]', $l);
471 $t = sprintf('<a href="%s">' . t('Click here to upgrade.') . '</div>', $l);
475 function upgrade_message($bbcode = false) {
476 $x = upgrade_link($bbcode);
477 return t('This action exceeds the limits set by your subscription plan.') . (($x) ? ' ' . $x : '') ;
480 function upgrade_bool_message($bbcode = false) {
481 $x = upgrade_link($bbcode);
482 return t('This action is not available under your subscription plan.') . (($x) ? ' ' . $x : '') ;