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 function uninstall_plugin($plugin) {
20 logger("Addons: uninstalling " . $plugin);
21 dba::delete('addon', array('name' => $plugin));
23 @include_once('addon/' . $plugin . '/' . $plugin . '.php');
24 if (function_exists($plugin . '_uninstall')) {
25 $func = $plugin . '_uninstall';
31 * @brief installs an addon.
33 * @param string $plugin name of the addon
36 function install_plugin($plugin) {
37 // silently fail if plugin was removed
39 if (!file_exists('addon/' . $plugin . '/' . $plugin . '.php')) {
42 logger("Addons: installing " . $plugin);
43 $t = @filemtime('addon/' . $plugin . '/' . $plugin . '.php');
44 @include_once('addon/' . $plugin . '/' . $plugin . '.php');
45 if (function_exists($plugin . '_install')) {
46 $func = $plugin . '_install';
49 $plugin_admin = (function_exists($plugin."_plugin_admin") ? 1 : 0);
51 dba::insert('addon', array('name' => $plugin, 'installed' => true,
52 'timestamp' => $t, 'plugin_admin' => $plugin_admin));
54 // we can add the following with the previous SQL
55 // once most site tables have been updated.
56 // This way the system won't fall over dead during the update.
58 if (file_exists('addon/' . $plugin . '/.hidden')) {
59 dba::update('addon', array('hidden' => true), array('name' => $plugin));
63 logger("Addons: FAILED installing " . $plugin);
68 // reload all updated plugins
70 function reload_plugins() {
71 $plugins = Config::get('system', 'addon');
72 if (strlen($plugins)) {
74 $r = q("SELECT * FROM `addon` WHERE `installed` = 1");
75 if (DBM::is_result($r)) {
81 $parr = explode(',',$plugins);
84 foreach ($parr as $pl) {
88 $fname = 'addon/' . $pl . '/' . $pl . '.php';
90 if (file_exists($fname)) {
91 $t = @filemtime($fname);
92 foreach ($installed as $i) {
93 if (($i['name'] == $pl) && ($i['timestamp'] != $t)) {
94 logger('Reloading plugin: ' . $i['name']);
95 @include_once($fname);
97 if (function_exists($pl . '_uninstall')) {
98 $func = $pl . '_uninstall';
101 if (function_exists($pl . '_install')) {
102 $func = $pl . '_install';
105 dba::update('addon', array('timestamp' => $t), array('id' => $i['id']));
116 * @brief check if addon is enabled
118 * @param string $plugin
121 function plugin_enabled($plugin) {
122 return dba::exists('addon', array('installed' => true, 'name' => $plugin));
127 * @brief registers a hook.
129 * @param string $hook the name of the hook
130 * @param string $file the name of the file that hooks into
131 * @param string $function the name of the function that the hook will call
132 * @param int $priority A priority (defaults to 0)
135 function register_hook($hook, $file, $function, $priority=0) {
136 $condition = array('hook' => $hook, 'file' => $file, 'function' => $function);
137 $exists = dba::exists('hook', $condition);
142 $r = dba::insert('hook', array('hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority));
148 * @brief unregisters a hook.
150 * @param string $hook the name of the hook
151 * @param string $file the name of the file that hooks into
152 * @param string $function the name of the function that the hook called
155 function unregister_hook($hook, $file, $function) {
156 $condition = array('hook' => $hook, 'file' => $file, 'function' => $function);
157 $r = dba::delete('hook', $condition);
162 function load_hooks() {
165 $r = dba::select('hook', array('hook', 'file', 'function'), array(), array('order' => array('priority' => 'desc', 'file')));
167 while ($rr = dba::fetch($r)) {
168 if (! array_key_exists($rr['hook'],$a->hooks)) {
169 $a->hooks[$rr['hook']] = array();
171 $a->hooks[$rr['hook']][] = array($rr['file'],$rr['function']);
177 * @brief Calls a hook.
179 * Use this function when you want to be able to allow a hook to manipulate
182 * @param string $name of the hook to call
183 * @param string|array &$data to transmit to the callback handler
185 function call_hooks($name, &$data = null)
189 if (is_array($a->hooks) && array_key_exists($name, $a->hooks)) {
190 foreach ($a->hooks[$name] as $hook) {
191 call_single_hook($a, $name, $hook, $data);
197 * @brief Calls a single hook.
199 * @param string $name of the hook to call
200 * @param array $hook Hook data
201 * @param string|array &$data to transmit to the callback handler
203 function call_single_hook($a, $name, $hook, &$data = null) {
204 // Don't run a theme's hook if the user isn't using the theme
205 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false)
208 @include_once($hook[0]);
209 if (function_exists($hook[1])) {
213 // remove orphan hooks
214 $condition = array('hook' => $name, 'file' => $hook[0], 'function' => $hook[1]);
215 dba::delete('hook', $condition);
219 //check if an app_menu hook exist for plugin $name.
220 //Return true if the plugin is an app
221 function plugin_is_app($name) {
224 if (is_array($a->hooks) && (array_key_exists('app_menu',$a->hooks))) {
225 foreach ($a->hooks['app_menu'] as $hook) {
226 if ($hook[0] == 'addon/'.$name.'/'.$name.'.php')
235 * @brief Parse plugin comment in search of plugin infos.
240 * * Description: A plugin which plugs in
242 * * Author: John <profile url>
243 * * Author: Jane <email>
246 * @param string $plugin the name of the plugin
247 * @return array with the plugin information
250 function get_plugin_info($plugin) {
262 if (!is_file("addon/$plugin/$plugin.php")) return $info;
264 $stamp1 = microtime(true);
265 $f = file_get_contents("addon/$plugin/$plugin.php");
266 $a->save_timestamp($stamp1, "file");
268 $r = preg_match("|/\*.*\*/|msU", $f, $m);
271 $ll = explode("\n", $m[0]);
272 foreach ( $ll as $l ) {
273 $l = trim($l,"\t\n\r */");
275 list($k,$v) = array_map("trim", explode(":",$l,2));
277 if ($k == "author") {
278 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
280 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
282 $info['author'][] = array('name'=>$v);
285 if (array_key_exists($k,$info)) {
299 * @brief Parse theme comment in search of theme infos.
304 * * Description: My Cool Theme
306 * * Author: John <profile url>
307 * * Maintainer: Jane <profile url>
310 * @param string $theme the name of the theme
314 function get_theme_info($theme) {
319 'maintainer' => array(),
322 'experimental' => false,
323 'unsupported' => false
326 if (file_exists("view/theme/$theme/experimental"))
327 $info['experimental'] = true;
328 if (file_exists("view/theme/$theme/unsupported"))
329 $info['unsupported'] = true;
331 if (!is_file("view/theme/$theme/theme.php")) return $info;
334 $stamp1 = microtime(true);
335 $f = file_get_contents("view/theme/$theme/theme.php");
336 $a->save_timestamp($stamp1, "file");
338 $r = preg_match("|/\*.*\*/|msU", $f, $m);
341 $ll = explode("\n", $m[0]);
342 foreach ( $ll as $l ) {
343 $l = trim($l,"\t\n\r */");
345 list($k,$v) = array_map("trim", explode(":",$l,2));
347 if ($k == "author") {
349 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
351 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
353 $info['author'][] = array('name'=>$v);
355 } elseif ($k == "maintainer") {
356 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
358 $info['maintainer'][] = array('name'=>$m[1], 'link'=>$m[2]);
360 $info['maintainer'][] = array('name'=>$v);
363 if (array_key_exists($k,$info)) {
376 * @brief Returns the theme's screenshot.
378 * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
380 * @param sring $theme The name of the theme
383 function get_theme_screenshot($theme) {
384 $exts = array('.png','.jpg');
385 foreach ($exts as $ext) {
386 if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
387 return(System::baseUrl() . '/view/theme/' . $theme . '/screenshot' . $ext);
390 return(System::baseUrl() . '/images/blank.png');
393 // install and uninstall theme
394 function uninstall_theme($theme) {
395 logger("Addons: uninstalling theme " . $theme);
397 include_once("view/theme/$theme/theme.php");
398 if (function_exists("{$theme}_uninstall")) {
399 $func = "{$theme}_uninstall";
404 function install_theme($theme) {
405 // silently fail if theme was removed
407 if (! file_exists("view/theme/$theme/theme.php")) {
411 logger("Addons: installing theme $theme");
413 include_once("view/theme/$theme/theme.php");
415 if (function_exists("{$theme}_install")) {
416 $func = "{$theme}_install";
420 logger("Addons: FAILED installing theme $theme");
427 * @brief Get the full path to relevant theme files by filename
429 * This function search in the theme directory (and if not present in global theme directory)
430 * if there is a directory with the file extension and for a file with the given
433 * @param string $file Filename
434 * @param string $root Full root path
435 * @return string Path to the file or empty string if the file isn't found
437 function theme_include($file, $root = '') {
438 $file = basename($file);
440 // Make sure $root ends with a slash / if it's not blank
441 if ($root !== '' && $root[strlen($root)-1] !== '/') {
444 $theme_info = get_app()->theme_info;
445 if (is_array($theme_info) && array_key_exists('extends',$theme_info)) {
446 $parent = $theme_info['extends'];
450 $theme = current_theme();
452 $ext = substr($file,strrpos($file,'.')+1);
454 "{$root}view/theme/$thname/$ext/$file",
455 "{$root}view/theme/$parent/$ext/$file",
456 "{$root}view/$ext/$file",
458 foreach ($paths as $p) {
459 // strpos() is faster than strstr when checking if one string is in another (http://php.net/manual/en/function.strstr.php)
460 if (strpos($p,'NOPATH') !== false) {
462 } elseif (file_exists($p)) {