3 * @file src/Core/Addon.php
5 namespace Friendica\Core;
8 use Friendica\Database\DBA;
10 require_once 'include/dba.php';
13 * Some functions to handle addons
18 * @brief uninstalls an addon.
20 * @param string $addon name of the addon
23 public static function uninstall($addon)
25 logger("Addons: uninstalling " . $addon);
26 DBA::delete('addon', ['name' => $addon]);
28 @include_once('addon/' . $addon . '/' . $addon . '.php');
29 if (function_exists($addon . '_uninstall')) {
30 $func = $addon . '_uninstall';
36 * @brief installs an addon.
38 * @param string $addon name of the addon
41 public static function install($addon)
43 // silently fail if addon was removed
45 if (!file_exists('addon/' . $addon . '/' . $addon . '.php')) {
48 logger("Addons: installing " . $addon);
49 $t = @filemtime('addon/' . $addon . '/' . $addon . '.php');
50 @include_once('addon/' . $addon . '/' . $addon . '.php');
51 if (function_exists($addon . '_install')) {
52 $func = $addon . '_install';
55 $addon_admin = (function_exists($addon."_addon_admin") ? 1 : 0);
57 DBA::insert('addon', ['name' => $addon, 'installed' => true,
58 'timestamp' => $t, 'plugin_admin' => $addon_admin]);
60 // we can add the following with the previous SQL
61 // once most site tables have been updated.
62 // This way the system won't fall over dead during the update.
64 if (file_exists('addon/' . $addon . '/.hidden')) {
65 DBA::update('addon', ['hidden' => true], ['name' => $addon]);
69 logger("Addons: FAILED installing " . $addon);
75 * reload all updated addons
77 public static function reload()
79 $addons = Config::get('system', 'addon');
80 if (strlen($addons)) {
81 $r = DBA::select('addon', [], ['installed' => 1]);
82 if (DBA::isResult($r)) {
83 $installed = DBA::toArray($r);
88 $addon_list = explode(',', $addons);
90 if (count($addon_list)) {
91 foreach ($addon_list as $addon) {
92 $addon = trim($addon);
93 $fname = 'addon/' . $addon . '/' . $addon . '.php';
95 if (file_exists($fname)) {
96 $t = @filemtime($fname);
97 foreach ($installed as $i) {
98 if (($i['name'] == $addon) && ($i['timestamp'] != $t)) {
99 logger('Reloading addon: ' . $i['name']);
100 @include_once($fname);
102 if (function_exists($addon . '_uninstall')) {
103 $func = $addon . '_uninstall';
106 if (function_exists($addon . '_install')) {
107 $func = $addon . '_install';
110 DBA::update('addon', ['timestamp' => $t], ['id' => $i['id']]);
120 * @brief check if addon is enabled
122 * @param string $addon
125 public static function isEnabled($addon)
127 return DBA::exists('addon', ['installed' => true, 'name' => $addon]);
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 public static function registerHook($hook, $file, $function, $priority = 0)
142 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
143 $exists = DBA::exists('hook', $condition);
148 $r = DBA::insert('hook', ['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]);
154 * @brief unregisters a hook.
156 * @param string $hook the name of the hook
157 * @param string $file the name of the file that hooks into
158 * @param string $function the name of the function that the hook called
161 public static function unregisterHook($hook, $file, $function)
163 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
164 $r = DBA::delete('hook', $condition);
171 public static function loadHooks()
175 $r = DBA::select('hook', ['hook', 'file', 'function'], [], ['order' => ['priority' => 'desc', 'file']]);
177 while ($rr = DBA::fetch($r)) {
178 if (! array_key_exists($rr['hook'], $a->hooks)) {
179 $a->hooks[$rr['hook']] = [];
181 $a->hooks[$rr['hook']][] = [$rr['file'],$rr['function']];
187 * @brief Forks a hook.
189 * Use this function when you want to fork a hook via the worker.
191 * @param string $name of the hook to call
192 * @param string|array $data to transmit to the callback handler
194 public static function forkHooks($priority, $name, $data = null)
198 if (is_array($a->hooks) && array_key_exists($name, $a->hooks)) {
199 foreach ($a->hooks[$name] as $hook) {
200 Worker::add($priority, 'ForkHook', $name, $hook, $data);
206 * @brief Calls a hook.
208 * Use this function when you want to be able to allow a hook to manipulate
211 * @param string $name of the hook to call
212 * @param string|array &$data to transmit to the callback handler
214 public static function callHooks($name, &$data = null)
218 if (is_array($a->hooks) && array_key_exists($name, $a->hooks)) {
219 foreach ($a->hooks[$name] as $hook) {
220 self::callSingleHook($a, $name, $hook, $data);
226 * @brief Calls a single hook.
229 * @param string $name of the hook to call
230 * @param array $hook Hook data
231 * @param string|array &$data to transmit to the callback handler
233 public static function callSingleHook(App $a, $name, $hook, &$data = null)
235 // Don't run a theme's hook if the user isn't using the theme
236 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/' . $a->getCurrentTheme()) === false) {
240 @include_once($hook[0]);
241 if (function_exists($hook[1])) {
245 // remove orphan hooks
246 $condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
247 DBA::delete('hook', $condition, ['cascade' => false]);
252 * check if an app_menu hook exist for addon $name.
253 * Return true if the addon is an app
255 public static function isApp($name)
259 if (is_array($a->hooks) && (array_key_exists('app_menu', $a->hooks))) {
260 foreach ($a->hooks['app_menu'] as $hook) {
261 if ($hook[0] == 'addon/'.$name.'/'.$name.'.php') {
271 * @brief Parse addon comment in search of addon infos.
276 * * Description: An addon which plugs in
278 * * Author: John <profile url>
279 * * Author: Jane <email>
280 * * Maintainer: Jess <email>
283 * @param string $addon the name of the addon
284 * @return array with the addon information
286 public static function getInfo($addon)
299 if (!is_file("addon/$addon/$addon.php")) {
303 $stamp1 = microtime(true);
304 $f = file_get_contents("addon/$addon/$addon.php");
305 $a->save_timestamp($stamp1, "file");
307 $r = preg_match("|/\*.*\*/|msU", $f, $m);
310 $ll = explode("\n", $m[0]);
311 foreach ($ll as $l) {
312 $l = trim($l, "\t\n\r */");
314 list($type, $v) = array_map("trim", explode(":", $l, 2));
315 $type = strtolower($type);
316 if ($type == "author" || $type == "maintainer") {
317 $r = preg_match("|([^<]+)<([^>]+)>|", $v, $m);
319 $info[$type][] = ['name' => $m[1], 'link' => $m[2]];
321 $info[$type][] = ['name' => $v];
324 if (array_key_exists($type, $info)) {