3 * @file include/plugin.php
5 * @brief Some functions to handle addons and themes.
9 use Friendica\Core\System;
12 * @brief uninstalls an addon.
14 * @param string $plugin name of the addon
17 if (! function_exists('uninstall_plugin')){
18 function uninstall_plugin($plugin){
19 logger("Addons: uninstalling " . $plugin);
20 q("DELETE FROM `addon` WHERE `name` = '%s' ",
24 @include_once('addon/' . $plugin . '/' . $plugin . '.php');
25 if (function_exists($plugin . '_uninstall')) {
26 $func = $plugin . '_uninstall';
32 * @brief installs an addon.
34 * @param string $plugin name of the addon
37 if (! function_exists('install_plugin')){
38 function install_plugin($plugin) {
39 // silently fail if plugin was removed
41 if (! file_exists('addon/' . $plugin . '/' . $plugin . '.php'))
43 logger("Addons: installing " . $plugin);
44 $t = @filemtime('addon/' . $plugin . '/' . $plugin . '.php');
45 @include_once('addon/' . $plugin . '/' . $plugin . '.php');
46 if (function_exists($plugin . '_install')) {
47 $func = $plugin . '_install';
50 $plugin_admin = (function_exists($plugin."_plugin_admin") ? 1 : 0);
52 dba::insert('addon', array('name' => $plugin, 'installed' => true,
53 'timestamp' => $t, 'plugin_admin' => $plugin_admin));
55 // we can add the following with the previous SQL
56 // once most site tables have been updated.
57 // This way the system won't fall over dead during the update.
59 if (file_exists('addon/' . $plugin . '/.hidden')) {
60 dba::update('addon', array('hidden' => true), array('name' => $plugin));
65 logger("Addons: FAILED installing " . $plugin);
71 // reload all updated plugins
73 if (! function_exists('reload_plugins')) {
74 function reload_plugins() {
75 $plugins = get_config('system','addon');
76 if (strlen($plugins)) {
78 $r = q("SELECT * FROM `addon` WHERE `installed` = 1");
79 if (dbm::is_result($r))
84 $parr = explode(',',$plugins);
87 foreach ($parr as $pl) {
91 $fname = 'addon/' . $pl . '/' . $pl . '.php';
93 if (file_exists($fname)) {
94 $t = @filemtime($fname);
95 foreach ($installed as $i) {
96 if (($i['name'] == $pl) && ($i['timestamp'] != $t)) {
97 logger('Reloading plugin: ' . $i['name']);
98 @include_once($fname);
100 if (function_exists($pl . '_uninstall')) {
101 $func = $pl . '_uninstall';
104 if (function_exists($pl . '_install')) {
105 $func = $pl . '_install';
108 dba::update('addon', array('timestamp' => $t), array('id' => $i['id']));
119 * @brief check if addon is enabled
121 * @param string $plugin
124 function plugin_enabled($plugin) {
125 return dba::exists('addon', array('installed' => true, 'name' => $plugin));
130 * @brief registers a hook.
132 * @param string $hook the name of the hook
133 * @param string $file the name of the file that hooks into
134 * @param string $function the name of the function that the hook will call
135 * @param int $priority A priority (defaults to 0)
138 if (! function_exists('register_hook')) {
139 function register_hook($hook,$file,$function,$priority=0) {
141 $r = q("SELECT * FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' LIMIT 1",
146 if (dbm::is_result($r))
149 $r = dba::insert('hook', array('hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority));
155 * @brief unregisters a hook.
157 * @param string $hook the name of the hook
158 * @param string $file the name of the file that hooks into
159 * @param string $function the name of the function that the hook called
162 if (! function_exists('unregister_hook')) {
163 function unregister_hook($hook,$file,$function) {
165 $r = q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
174 function load_hooks() {
177 $r = dba::select('hook', array('hook', 'file', 'function'), array(), array('order' => array('priority' => 'desc', 'file')));
179 while ($rr = dba::fetch($r)) {
180 if (! array_key_exists($rr['hook'],$a->hooks)) {
181 $a->hooks[$rr['hook']] = array();
183 $a->hooks[$rr['hook']][] = array($rr['file'],$rr['function']);
189 * @brief Calls a hook.
191 * Use this function when you want to be able to allow a hook to manipulate
194 * @param string $name of the hook to call
195 * @param string|array &$data to transmit to the callback handler
197 function call_hooks($name, &$data = null) {
198 $stamp1 = microtime(true);
202 if (is_array($a->hooks) && array_key_exists($name, $a->hooks))
203 foreach ($a->hooks[$name] as $hook)
204 call_single_hook($a, $name, $hook, $data);
208 * @brief Calls a single hook.
210 * @param string $name of the hook to call
211 * @param array $hook Hook data
212 * @param string|array &$data to transmit to the callback handler
214 function call_single_hook($a, $name, $hook, &$data = null) {
215 // Don't run a theme's hook if the user isn't using the theme
216 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false)
219 @include_once($hook[0]);
220 if (function_exists($hook[1])) {
224 // remove orphan hooks
225 q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
233 //check if an app_menu hook exist for plugin $name.
234 //Return true if the plugin is an app
235 if (! function_exists('plugin_is_app')) {
236 function plugin_is_app($name) {
239 if (is_array($a->hooks) && (array_key_exists('app_menu',$a->hooks))) {
240 foreach ($a->hooks['app_menu'] as $hook) {
241 if ($hook[0] == 'addon/'.$name.'/'.$name.'.php')
250 * @brief Parse plugin comment in search of plugin infos.
255 * * Description: A plugin which plugs in
257 * * Author: John <profile url>
258 * * Author: Jane <email>
261 * @param string $plugin the name of the plugin
262 * @return array with the plugin information
265 if (! function_exists('get_plugin_info')){
266 function get_plugin_info($plugin){
278 if (!is_file("addon/$plugin/$plugin.php")) return $info;
280 $stamp1 = microtime(true);
281 $f = file_get_contents("addon/$plugin/$plugin.php");
282 $a->save_timestamp($stamp1, "file");
284 $r = preg_match("|/\*.*\*/|msU", $f, $m);
287 $ll = explode("\n", $m[0]);
288 foreach ( $ll as $l ) {
289 $l = trim($l,"\t\n\r */");
291 list($k,$v) = array_map("trim", explode(":",$l,2));
294 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
296 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
298 $info['author'][] = array('name'=>$v);
301 if (array_key_exists($k,$info)){
315 * @brief Parse theme comment in search of theme infos.
320 * * Description: My Cool Theme
322 * * Author: John <profile url>
323 * * Maintainer: Jane <profile url>
326 * @param string $theme the name of the theme
330 if (! function_exists('get_theme_info')){
331 function get_theme_info($theme){
336 'maintainer' => array(),
339 'experimental' => false,
340 'unsupported' => false
343 if (file_exists("view/theme/$theme/experimental"))
344 $info['experimental'] = true;
345 if (file_exists("view/theme/$theme/unsupported"))
346 $info['unsupported'] = true;
348 if (!is_file("view/theme/$theme/theme.php")) return $info;
351 $stamp1 = microtime(true);
352 $f = file_get_contents("view/theme/$theme/theme.php");
353 $a->save_timestamp($stamp1, "file");
355 $r = preg_match("|/\*.*\*/|msU", $f, $m);
358 $ll = explode("\n", $m[0]);
359 foreach ( $ll as $l ) {
360 $l = trim($l,"\t\n\r */");
362 list($k,$v) = array_map("trim", explode(":",$l,2));
366 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
368 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
370 $info['author'][] = array('name'=>$v);
373 elseif ($k=="maintainer"){
374 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
376 $info['maintainer'][] = array('name'=>$m[1], 'link'=>$m[2]);
378 $info['maintainer'][] = array('name'=>$v);
381 if (array_key_exists($k,$info)){
394 * @brief Returns the theme's screenshot.
396 * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
398 * @param sring $theme The name of the theme
401 function get_theme_screenshot($theme) {
402 $exts = array('.png','.jpg');
403 foreach ($exts as $ext) {
404 if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
405 return(System::baseUrl() . '/view/theme/' . $theme . '/screenshot' . $ext);
408 return(System::baseUrl() . '/images/blank.png');
411 // install and uninstall theme
412 if (! function_exists('uninstall_theme')){
413 function uninstall_theme($theme){
414 logger("Addons: uninstalling theme " . $theme);
416 include_once("view/theme/$theme/theme.php");
417 if (function_exists("{$theme}_uninstall")) {
418 $func = "{$theme}_uninstall";
423 if (! function_exists('install_theme')){
424 function install_theme($theme) {
425 // silently fail if theme was removed
427 if (! file_exists("view/theme/$theme/theme.php")) {
431 logger("Addons: installing theme $theme");
433 include_once("view/theme/$theme/theme.php");
435 if (function_exists("{$theme}_install")) {
436 $func = "{$theme}_install";
440 logger("Addons: FAILED installing theme $theme");
448 // check service_class restrictions. If there are no service_classes defined, everything is allowed.
449 // if $usage is supplied, we check against a maximum count and return true if the current usage is
450 // less than the subscriber plan allows. Otherwise we return boolean true or false if the property
451 // is allowed (or not) in this subscriber plan. An unset property for this service plan means
452 // the property is allowed, so it is only necessary to provide negative properties for each plan,
453 // or what the subscriber is not allowed to do.
456 function service_class_allows($uid,$property,$usage = false) {
458 if ($uid == local_user()) {
459 $service_class = $a->user['service_class'];
461 $r = q("SELECT `service_class` FROM `user` WHERE `uid` = %d LIMIT 1",
464 if (dbm::is_result($r)) {
465 $service_class = $r[0]['service_class'];
469 if (! x($service_class)) {
470 // everything is allowed
474 $arr = get_config('service_class',$service_class);
475 if (! is_array($arr) || (! count($arr))) {
479 if ($usage === false) {
480 return ((x($arr[$property])) ? (bool) $arr['property'] : true);
482 if (! array_key_exists($property,$arr)) {
485 return (((intval($usage)) < intval($arr[$property])) ? true : false);
490 function service_class_fetch($uid,$property) {
492 if ($uid == local_user()) {
493 $service_class = $a->user['service_class'];
495 $r = q("SELECT `service_class` FROM `user` WHERE `uid` = %d LIMIT 1",
498 if (dbm::is_result($r)) {
499 $service_class = $r[0]['service_class'];
502 if (! x($service_class))
503 return false; // everything is allowed
505 $arr = get_config('service_class',$service_class);
506 if (! is_array($arr) || (! count($arr)))
509 return((array_key_exists($property,$arr)) ? $arr[$property] : false);
513 function upgrade_link($bbcode = false) {
514 $l = get_config('service_class','upgrade_link');
519 $t = sprintf('[url=%s]' . t('Click here to upgrade.') . '[/url]', $l);
521 $t = sprintf('<a href="%s">' . t('Click here to upgrade.') . '</div>', $l);
526 function upgrade_message($bbcode = false) {
527 $x = upgrade_link($bbcode);
528 return t('This action exceeds the limits set by your subscription plan.') . (($x) ? ' ' . $x : '') ;
531 function upgrade_bool_message($bbcode = false) {
532 $x = upgrade_link($bbcode);
533 return t('This action is not available under your subscription plan.') . (($x) ? ' ' . $x : '') ;
537 * @brief Get the full path to relevant theme files by filename
539 * This function search in the theme directory (and if not present in global theme directory)
540 * if there is a directory with the file extension and for a file with the given
543 * @param string $file Filename
544 * @param string $root Full root path
545 * @return string Path to the file or empty string if the file isn't found
547 function theme_include($file, $root = '') {
548 $file = basename($file);
550 // Make sure $root ends with a slash / if it's not blank
551 if ($root !== '' && $root[strlen($root)-1] !== '/') {
554 $theme_info = get_app()->theme_info;
555 if (is_array($theme_info) && array_key_exists('extends',$theme_info)) {
556 $parent = $theme_info['extends'];
560 $theme = current_theme();
562 $ext = substr($file,strrpos($file,'.')+1);
564 "{$root}view/theme/$thname/$ext/$file",
565 "{$root}view/theme/$parent/$ext/$file",
566 "{$root}view/$ext/$file",
568 foreach ($paths as $p) {
569 // strpos() is faster than strstr when checking if one string is in another (http://php.net/manual/en/function.strstr.php)
570 if (strpos($p,'NOPATH') !== false) {
572 } elseif (file_exists($p)) {