3 * @file include/plugin.php
5 * @brief Some functions to handle addons and themes.
9 use Friendica\Core\Config;
10 use Friendica\Core\System;
13 * @brief uninstalls an addon.
15 * @param string $plugin name of the addon
18 if (! function_exists('uninstall_plugin')){
19 function uninstall_plugin($plugin){
20 logger("Addons: uninstalling " . $plugin);
21 q("DELETE FROM `addon` WHERE `name` = '%s' ",
25 @include_once('addon/' . $plugin . '/' . $plugin . '.php');
26 if (function_exists($plugin . '_uninstall')) {
27 $func = $plugin . '_uninstall';
33 * @brief installs an addon.
35 * @param string $plugin name of the addon
38 if (! function_exists('install_plugin')){
39 function install_plugin($plugin) {
40 // silently fail if plugin was removed
42 if (! file_exists('addon/' . $plugin . '/' . $plugin . '.php'))
44 logger("Addons: installing " . $plugin);
45 $t = @filemtime('addon/' . $plugin . '/' . $plugin . '.php');
46 @include_once('addon/' . $plugin . '/' . $plugin . '.php');
47 if (function_exists($plugin . '_install')) {
48 $func = $plugin . '_install';
51 $plugin_admin = (function_exists($plugin."_plugin_admin") ? 1 : 0);
53 dba::insert('addon', array('name' => $plugin, 'installed' => true,
54 'timestamp' => $t, 'plugin_admin' => $plugin_admin));
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 dba::update('addon', array('hidden' => true), array('name' => $plugin));
66 logger("Addons: FAILED installing " . $plugin);
72 // reload all updated plugins
74 if (! function_exists('reload_plugins')) {
75 function reload_plugins() {
76 $plugins = Config::get('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 dba::update('addon', array('timestamp' => $t), array('id' => $i['id']));
120 * @brief check if addon is enabled
122 * @param string $plugin
125 function plugin_enabled($plugin) {
126 return dba::exists('addon', array('installed' => true, 'name' => $plugin));
131 * @brief registers a hook.
133 * @param string $hook the name of the hook
134 * @param string $file the name of the file that hooks into
135 * @param string $function the name of the function that the hook will call
136 * @param int $priority A priority (defaults to 0)
139 if (! function_exists('register_hook')) {
140 function register_hook($hook,$file,$function,$priority=0) {
142 $r = q("SELECT * FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' LIMIT 1",
147 if (dbm::is_result($r))
150 $r = dba::insert('hook', array('hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority));
156 * @brief unregisters a hook.
158 * @param string $hook the name of the hook
159 * @param string $file the name of the file that hooks into
160 * @param string $function the name of the function that the hook called
163 if (! function_exists('unregister_hook')) {
164 function unregister_hook($hook,$file,$function) {
166 $r = q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
175 function load_hooks() {
178 $r = dba::select('hook', array('hook', 'file', 'function'), array(), array('order' => array('priority' => 'desc', 'file')));
180 while ($rr = dba::fetch($r)) {
181 if (! array_key_exists($rr['hook'],$a->hooks)) {
182 $a->hooks[$rr['hook']] = array();
184 $a->hooks[$rr['hook']][] = array($rr['file'],$rr['function']);
190 * @brief Calls a hook.
192 * Use this function when you want to be able to allow a hook to manipulate
195 * @param string $name of the hook to call
196 * @param string|array &$data to transmit to the callback handler
198 function call_hooks($name, &$data = null) {
199 $stamp1 = microtime(true);
203 if (is_array($a->hooks) && array_key_exists($name, $a->hooks))
204 foreach ($a->hooks[$name] as $hook)
205 call_single_hook($a, $name, $hook, $data);
209 * @brief Calls a single hook.
211 * @param string $name of the hook to call
212 * @param array $hook Hook data
213 * @param string|array &$data to transmit to the callback handler
215 function call_single_hook($a, $name, $hook, &$data = null) {
216 // Don't run a theme's hook if the user isn't using the theme
217 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false)
220 @include_once($hook[0]);
221 if (function_exists($hook[1])) {
225 // remove orphan hooks
226 q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s'",
234 //check if an app_menu hook exist for plugin $name.
235 //Return true if the plugin is an app
236 if (! function_exists('plugin_is_app')) {
237 function plugin_is_app($name) {
240 if (is_array($a->hooks) && (array_key_exists('app_menu',$a->hooks))) {
241 foreach ($a->hooks['app_menu'] as $hook) {
242 if ($hook[0] == 'addon/'.$name.'/'.$name.'.php')
251 * @brief Parse plugin comment in search of plugin infos.
256 * * Description: A plugin which plugs in
258 * * Author: John <profile url>
259 * * Author: Jane <email>
262 * @param string $plugin the name of the plugin
263 * @return array with the plugin information
266 if (! function_exists('get_plugin_info')){
267 function get_plugin_info($plugin){
279 if (!is_file("addon/$plugin/$plugin.php")) return $info;
281 $stamp1 = microtime(true);
282 $f = file_get_contents("addon/$plugin/$plugin.php");
283 $a->save_timestamp($stamp1, "file");
285 $r = preg_match("|/\*.*\*/|msU", $f, $m);
288 $ll = explode("\n", $m[0]);
289 foreach ( $ll as $l ) {
290 $l = trim($l,"\t\n\r */");
292 list($k,$v) = array_map("trim", explode(":",$l,2));
295 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
297 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
299 $info['author'][] = array('name'=>$v);
302 if (array_key_exists($k,$info)){
316 * @brief Parse theme comment in search of theme infos.
321 * * Description: My Cool Theme
323 * * Author: John <profile url>
324 * * Maintainer: Jane <profile url>
327 * @param string $theme the name of the theme
331 if (! function_exists('get_theme_info')){
332 function get_theme_info($theme){
337 'maintainer' => array(),
340 'experimental' => false,
341 'unsupported' => false
344 if (file_exists("view/theme/$theme/experimental"))
345 $info['experimental'] = true;
346 if (file_exists("view/theme/$theme/unsupported"))
347 $info['unsupported'] = true;
349 if (!is_file("view/theme/$theme/theme.php")) return $info;
352 $stamp1 = microtime(true);
353 $f = file_get_contents("view/theme/$theme/theme.php");
354 $a->save_timestamp($stamp1, "file");
356 $r = preg_match("|/\*.*\*/|msU", $f, $m);
359 $ll = explode("\n", $m[0]);
360 foreach ( $ll as $l ) {
361 $l = trim($l,"\t\n\r */");
363 list($k,$v) = array_map("trim", explode(":",$l,2));
367 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
369 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
371 $info['author'][] = array('name'=>$v);
374 elseif ($k=="maintainer"){
375 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
377 $info['maintainer'][] = array('name'=>$m[1], 'link'=>$m[2]);
379 $info['maintainer'][] = array('name'=>$v);
382 if (array_key_exists($k,$info)){
395 * @brief Returns the theme's screenshot.
397 * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
399 * @param sring $theme The name of the theme
402 function get_theme_screenshot($theme) {
403 $exts = array('.png','.jpg');
404 foreach ($exts as $ext) {
405 if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
406 return(System::baseUrl() . '/view/theme/' . $theme . '/screenshot' . $ext);
409 return(System::baseUrl() . '/images/blank.png');
412 // install and uninstall theme
413 if (! function_exists('uninstall_theme')){
414 function uninstall_theme($theme){
415 logger("Addons: uninstalling theme " . $theme);
417 include_once("view/theme/$theme/theme.php");
418 if (function_exists("{$theme}_uninstall")) {
419 $func = "{$theme}_uninstall";
424 if (! function_exists('install_theme')){
425 function install_theme($theme) {
426 // silently fail if theme was removed
428 if (! file_exists("view/theme/$theme/theme.php")) {
432 logger("Addons: installing theme $theme");
434 include_once("view/theme/$theme/theme.php");
436 if (function_exists("{$theme}_install")) {
437 $func = "{$theme}_install";
441 logger("Addons: FAILED installing theme $theme");
448 * @brief Get the full path to relevant theme files by filename
450 * This function search in the theme directory (and if not present in global theme directory)
451 * if there is a directory with the file extension and for a file with the given
454 * @param string $file Filename
455 * @param string $root Full root path
456 * @return string Path to the file or empty string if the file isn't found
458 function theme_include($file, $root = '') {
459 $file = basename($file);
461 // Make sure $root ends with a slash / if it's not blank
462 if ($root !== '' && $root[strlen($root)-1] !== '/') {
465 $theme_info = get_app()->theme_info;
466 if (is_array($theme_info) && array_key_exists('extends',$theme_info)) {
467 $parent = $theme_info['extends'];
471 $theme = current_theme();
473 $ext = substr($file,strrpos($file,'.')+1);
475 "{$root}view/theme/$thname/$ext/$file",
476 "{$root}view/theme/$parent/$ext/$file",
477 "{$root}view/$ext/$file",
479 foreach ($paths as $p) {
480 // strpos() is faster than strstr when checking if one string is in another (http://php.net/manual/en/function.strstr.php)
481 if (strpos($p,'NOPATH') !== false) {
483 } elseif (file_exists($p)) {