3 * @file include/plugin.php
5 * @brief Some functions to handle addons and themes.
9 use Friendica\Core\Config;
10 use Friendica\Core\System;
11 use Friendica\Database\DBM;
14 * @brief uninstalls an addon.
16 * @param string $plugin name of the addon
19 if (! function_exists('uninstall_plugin')){
20 function uninstall_plugin($plugin){
21 logger("Addons: uninstalling " . $plugin);
22 q("DELETE FROM `addon` WHERE `name` = '%s' ",
26 @include_once('addon/' . $plugin . '/' . $plugin . '.php');
27 if (function_exists($plugin . '_uninstall')) {
28 $func = $plugin . '_uninstall';
34 * @brief installs an addon.
36 * @param string $plugin name of the addon
39 if (! function_exists('install_plugin')){
40 function install_plugin($plugin) {
41 // silently fail if plugin was removed
43 if (! file_exists('addon/' . $plugin . '/' . $plugin . '.php'))
45 logger("Addons: installing " . $plugin);
46 $t = @filemtime('addon/' . $plugin . '/' . $plugin . '.php');
47 @include_once('addon/' . $plugin . '/' . $plugin . '.php');
48 if (function_exists($plugin . '_install')) {
49 $func = $plugin . '_install';
52 $plugin_admin = (function_exists($plugin."_plugin_admin") ? 1 : 0);
54 dba::insert('addon', array('name' => $plugin, 'installed' => true,
55 'timestamp' => $t, 'plugin_admin' => $plugin_admin));
57 // we can add the following with the previous SQL
58 // once most site tables have been updated.
59 // This way the system won't fall over dead during the update.
61 if (file_exists('addon/' . $plugin . '/.hidden')) {
62 dba::update('addon', array('hidden' => true), array('name' => $plugin));
67 logger("Addons: FAILED installing " . $plugin);
73 // reload all updated plugins
75 if (! function_exists('reload_plugins')) {
76 function reload_plugins() {
77 $plugins = Config::get('system','addon');
78 if (strlen($plugins)) {
80 $r = q("SELECT * FROM `addon` WHERE `installed` = 1");
81 if (DBM::is_result($r))
86 $parr = explode(',',$plugins);
89 foreach ($parr as $pl) {
93 $fname = 'addon/' . $pl . '/' . $pl . '.php';
95 if (file_exists($fname)) {
96 $t = @filemtime($fname);
97 foreach ($installed as $i) {
98 if (($i['name'] == $pl) && ($i['timestamp'] != $t)) {
99 logger('Reloading plugin: ' . $i['name']);
100 @include_once($fname);
102 if (function_exists($pl . '_uninstall')) {
103 $func = $pl . '_uninstall';
106 if (function_exists($pl . '_install')) {
107 $func = $pl . '_install';
110 dba::update('addon', array('timestamp' => $t), array('id' => $i['id']));
121 * @brief check if addon is enabled
123 * @param string $plugin
126 function plugin_enabled($plugin) {
127 return dba::exists('addon', array('installed' => true, 'name' => $plugin));
132 * @brief registers a hook.
134 * @param string $hook the name of the hook
135 * @param string $file the name of the file that hooks into
136 * @param string $function the name of the function that the hook will call
137 * @param int $priority A priority (defaults to 0)
140 if (! function_exists('register_hook')) {
141 function register_hook($hook,$file,$function,$priority=0) {
143 $r = q("SELECT * FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' LIMIT 1",
148 if (DBM::is_result($r))
151 $r = dba::insert('hook', array('hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority));
157 * @brief unregisters a hook.
159 * @param string $hook the name of the hook
160 * @param string $file the name of the file that hooks into
161 * @param string $function the name of the function that the hook called
164 if (! function_exists('unregister_hook')) {
165 function unregister_hook($hook,$file,$function) {
167 $r = q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
176 function load_hooks() {
179 $r = dba::select('hook', array('hook', 'file', 'function'), array(), array('order' => array('priority' => 'desc', 'file')));
181 while ($rr = dba::fetch($r)) {
182 if (! array_key_exists($rr['hook'],$a->hooks)) {
183 $a->hooks[$rr['hook']] = array();
185 $a->hooks[$rr['hook']][] = array($rr['file'],$rr['function']);
191 * @brief Calls a hook.
193 * Use this function when you want to be able to allow a hook to manipulate
196 * @param string $name of the hook to call
197 * @param string|array &$data to transmit to the callback handler
199 function call_hooks($name, &$data = null) {
200 $stamp1 = microtime(true);
204 if (is_array($a->hooks) && array_key_exists($name, $a->hooks))
205 foreach ($a->hooks[$name] as $hook)
206 call_single_hook($a, $name, $hook, $data);
210 * @brief Calls a single hook.
212 * @param string $name of the hook to call
213 * @param array $hook Hook data
214 * @param string|array &$data to transmit to the callback handler
216 function call_single_hook($a, $name, $hook, &$data = null) {
217 // Don't run a theme's hook if the user isn't using the theme
218 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false)
221 @include_once($hook[0]);
222 if (function_exists($hook[1])) {
226 // remove orphan hooks
227 q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
235 //check if an app_menu hook exist for plugin $name.
236 //Return true if the plugin is an app
237 if (! function_exists('plugin_is_app')) {
238 function plugin_is_app($name) {
241 if (is_array($a->hooks) && (array_key_exists('app_menu',$a->hooks))) {
242 foreach ($a->hooks['app_menu'] as $hook) {
243 if ($hook[0] == 'addon/'.$name.'/'.$name.'.php')
252 * @brief Parse plugin comment in search of plugin infos.
257 * * Description: A plugin which plugs in
259 * * Author: John <profile url>
260 * * Author: Jane <email>
263 * @param string $plugin the name of the plugin
264 * @return array with the plugin information
267 if (! function_exists('get_plugin_info')){
268 function get_plugin_info($plugin){
280 if (!is_file("addon/$plugin/$plugin.php")) return $info;
282 $stamp1 = microtime(true);
283 $f = file_get_contents("addon/$plugin/$plugin.php");
284 $a->save_timestamp($stamp1, "file");
286 $r = preg_match("|/\*.*\*/|msU", $f, $m);
289 $ll = explode("\n", $m[0]);
290 foreach ( $ll as $l ) {
291 $l = trim($l,"\t\n\r */");
293 list($k,$v) = array_map("trim", explode(":",$l,2));
296 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
298 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
300 $info['author'][] = array('name'=>$v);
303 if (array_key_exists($k,$info)){
317 * @brief Parse theme comment in search of theme infos.
322 * * Description: My Cool Theme
324 * * Author: John <profile url>
325 * * Maintainer: Jane <profile url>
328 * @param string $theme the name of the theme
332 if (! function_exists('get_theme_info')){
333 function get_theme_info($theme){
338 'maintainer' => array(),
341 'experimental' => false,
342 'unsupported' => false
345 if (file_exists("view/theme/$theme/experimental"))
346 $info['experimental'] = true;
347 if (file_exists("view/theme/$theme/unsupported"))
348 $info['unsupported'] = true;
350 if (!is_file("view/theme/$theme/theme.php")) return $info;
353 $stamp1 = microtime(true);
354 $f = file_get_contents("view/theme/$theme/theme.php");
355 $a->save_timestamp($stamp1, "file");
357 $r = preg_match("|/\*.*\*/|msU", $f, $m);
360 $ll = explode("\n", $m[0]);
361 foreach ( $ll as $l ) {
362 $l = trim($l,"\t\n\r */");
364 list($k,$v) = array_map("trim", explode(":",$l,2));
368 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
370 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
372 $info['author'][] = array('name'=>$v);
375 elseif ($k=="maintainer"){
376 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
378 $info['maintainer'][] = array('name'=>$m[1], 'link'=>$m[2]);
380 $info['maintainer'][] = array('name'=>$v);
383 if (array_key_exists($k,$info)){
396 * @brief Returns the theme's screenshot.
398 * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
400 * @param sring $theme The name of the theme
403 function get_theme_screenshot($theme) {
404 $exts = array('.png','.jpg');
405 foreach ($exts as $ext) {
406 if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
407 return(System::baseUrl() . '/view/theme/' . $theme . '/screenshot' . $ext);
410 return(System::baseUrl() . '/images/blank.png');
413 // install and uninstall theme
414 if (! function_exists('uninstall_theme')){
415 function uninstall_theme($theme){
416 logger("Addons: uninstalling theme " . $theme);
418 include_once("view/theme/$theme/theme.php");
419 if (function_exists("{$theme}_uninstall")) {
420 $func = "{$theme}_uninstall";
425 if (! function_exists('install_theme')){
426 function install_theme($theme) {
427 // silently fail if theme was removed
429 if (! file_exists("view/theme/$theme/theme.php")) {
433 logger("Addons: installing theme $theme");
435 include_once("view/theme/$theme/theme.php");
437 if (function_exists("{$theme}_install")) {
438 $func = "{$theme}_install";
442 logger("Addons: FAILED installing theme $theme");
449 * @brief Get the full path to relevant theme files by filename
451 * This function search in the theme directory (and if not present in global theme directory)
452 * if there is a directory with the file extension and for a file with the given
455 * @param string $file Filename
456 * @param string $root Full root path
457 * @return string Path to the file or empty string if the file isn't found
459 function theme_include($file, $root = '') {
460 $file = basename($file);
462 // Make sure $root ends with a slash / if it's not blank
463 if ($root !== '' && $root[strlen($root)-1] !== '/') {
466 $theme_info = get_app()->theme_info;
467 if (is_array($theme_info) && array_key_exists('extends',$theme_info)) {
468 $parent = $theme_info['extends'];
472 $theme = current_theme();
474 $ext = substr($file,strrpos($file,'.')+1);
476 "{$root}view/theme/$thname/$ext/$file",
477 "{$root}view/theme/$parent/$ext/$file",
478 "{$root}view/$ext/$file",
480 foreach ($paths as $p) {
481 // strpos() is faster than strstr when checking if one string is in another (http://php.net/manual/en/function.strstr.php)
482 if (strpos($p,'NOPATH') !== false) {
484 } elseif (file_exists($p)) {