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) {
186 $stamp1 = microtime(true);
190 if (is_array($a->hooks) && array_key_exists($name, $a->hooks))
191 foreach ($a->hooks[$name] as $hook)
192 call_single_hook($a, $name, $hook, $data);
196 * @brief Calls a single hook.
198 * @param string $name of the hook to call
199 * @param array $hook Hook data
200 * @param string|array &$data to transmit to the callback handler
202 function call_single_hook($a, $name, $hook, &$data = null) {
203 // Don't run a theme's hook if the user isn't using the theme
204 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false)
207 @include_once($hook[0]);
208 if (function_exists($hook[1])) {
212 // remove orphan hooks
213 $condition = array('hook' => $name, 'file' => $hook[0], 'function' => $hook[1]);
214 dba::delete('hook', $condition);
218 //check if an app_menu hook exist for plugin $name.
219 //Return true if the plugin is an app
220 function plugin_is_app($name) {
223 if (is_array($a->hooks) && (array_key_exists('app_menu',$a->hooks))) {
224 foreach ($a->hooks['app_menu'] as $hook) {
225 if ($hook[0] == 'addon/'.$name.'/'.$name.'.php')
234 * @brief Parse plugin comment in search of plugin infos.
239 * * Description: A plugin which plugs in
241 * * Author: John <profile url>
242 * * Author: Jane <email>
245 * @param string $plugin the name of the plugin
246 * @return array with the plugin information
249 function get_plugin_info($plugin) {
261 if (!is_file("addon/$plugin/$plugin.php")) return $info;
263 $stamp1 = microtime(true);
264 $f = file_get_contents("addon/$plugin/$plugin.php");
265 $a->save_timestamp($stamp1, "file");
267 $r = preg_match("|/\*.*\*/|msU", $f, $m);
270 $ll = explode("\n", $m[0]);
271 foreach ( $ll as $l ) {
272 $l = trim($l,"\t\n\r */");
274 list($k,$v) = array_map("trim", explode(":",$l,2));
276 if ($k == "author") {
277 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
279 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
281 $info['author'][] = array('name'=>$v);
284 if (array_key_exists($k,$info)) {
298 * @brief Parse theme comment in search of theme infos.
303 * * Description: My Cool Theme
305 * * Author: John <profile url>
306 * * Maintainer: Jane <profile url>
309 * @param string $theme the name of the theme
313 function get_theme_info($theme) {
318 'maintainer' => array(),
321 'experimental' => false,
322 'unsupported' => false
325 if (file_exists("view/theme/$theme/experimental"))
326 $info['experimental'] = true;
327 if (file_exists("view/theme/$theme/unsupported"))
328 $info['unsupported'] = true;
330 if (!is_file("view/theme/$theme/theme.php")) return $info;
333 $stamp1 = microtime(true);
334 $f = file_get_contents("view/theme/$theme/theme.php");
335 $a->save_timestamp($stamp1, "file");
337 $r = preg_match("|/\*.*\*/|msU", $f, $m);
340 $ll = explode("\n", $m[0]);
341 foreach ( $ll as $l ) {
342 $l = trim($l,"\t\n\r */");
344 list($k,$v) = array_map("trim", explode(":",$l,2));
346 if ($k == "author") {
348 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
350 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
352 $info['author'][] = array('name'=>$v);
354 } elseif ($k == "maintainer") {
355 $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
357 $info['maintainer'][] = array('name'=>$m[1], 'link'=>$m[2]);
359 $info['maintainer'][] = array('name'=>$v);
362 if (array_key_exists($k,$info)) {
375 * @brief Returns the theme's screenshot.
377 * The screenshot is expected as view/theme/$theme/screenshot.[png|jpg].
379 * @param sring $theme The name of the theme
382 function get_theme_screenshot($theme) {
383 $exts = array('.png','.jpg');
384 foreach ($exts as $ext) {
385 if (file_exists('view/theme/' . $theme . '/screenshot' . $ext)) {
386 return(System::baseUrl() . '/view/theme/' . $theme . '/screenshot' . $ext);
389 return(System::baseUrl() . '/images/blank.png');
392 // install and uninstall theme
393 function uninstall_theme($theme) {
394 logger("Addons: uninstalling theme " . $theme);
396 include_once("view/theme/$theme/theme.php");
397 if (function_exists("{$theme}_uninstall")) {
398 $func = "{$theme}_uninstall";
403 function install_theme($theme) {
404 // silently fail if theme was removed
406 if (! file_exists("view/theme/$theme/theme.php")) {
410 logger("Addons: installing theme $theme");
412 include_once("view/theme/$theme/theme.php");
414 if (function_exists("{$theme}_install")) {
415 $func = "{$theme}_install";
419 logger("Addons: FAILED installing theme $theme");
426 * @brief Get the full path to relevant theme files by filename
428 * This function search in the theme directory (and if not present in global theme directory)
429 * if there is a directory with the file extension and for a file with the given
432 * @param string $file Filename
433 * @param string $root Full root path
434 * @return string Path to the file or empty string if the file isn't found
436 function theme_include($file, $root = '') {
437 $file = basename($file);
439 // Make sure $root ends with a slash / if it's not blank
440 if ($root !== '' && $root[strlen($root)-1] !== '/') {
443 $theme_info = get_app()->theme_info;
444 if (is_array($theme_info) && array_key_exists('extends',$theme_info)) {
445 $parent = $theme_info['extends'];
449 $theme = current_theme();
451 $ext = substr($file,strrpos($file,'.')+1);
453 "{$root}view/theme/$thname/$ext/$file",
454 "{$root}view/theme/$parent/$ext/$file",
455 "{$root}view/$ext/$file",
457 foreach ($paths as $p) {
458 // strpos() is faster than strstr when checking if one string is in another (http://php.net/manual/en/function.strstr.php)
459 if (strpos($p,'NOPATH') !== false) {
461 } elseif (file_exists($p)) {