3 * @file src/Core/Addon.php
5 namespace Friendica\Core;
8 use Friendica\Database\DBA;
9 use Friendica\Database\DBM;
11 require_once 'include/dba.php';
14 * Some functions to handle addons
19 * @brief uninstalls an addon.
21 * @param string $addon name of the addon
24 public static function uninstall($addon)
26 logger("Addons: uninstalling " . $addon);
27 DBA::delete('addon', ['name' => $addon]);
29 @include_once('addon/' . $addon . '/' . $addon . '.php');
30 if (function_exists($addon . '_uninstall')) {
31 $func = $addon . '_uninstall';
37 * @brief installs an addon.
39 * @param string $addon name of the addon
42 public static function install($addon)
44 // silently fail if addon was removed
46 if (!file_exists('addon/' . $addon . '/' . $addon . '.php')) {
49 logger("Addons: installing " . $addon);
50 $t = @filemtime('addon/' . $addon . '/' . $addon . '.php');
51 @include_once('addon/' . $addon . '/' . $addon . '.php');
52 if (function_exists($addon . '_install')) {
53 $func = $addon . '_install';
56 $addon_admin = (function_exists($addon."_addon_admin") ? 1 : 0);
58 DBA::insert('addon', ['name' => $addon, 'installed' => true,
59 'timestamp' => $t, 'plugin_admin' => $addon_admin]);
61 // we can add the following with the previous SQL
62 // once most site tables have been updated.
63 // This way the system won't fall over dead during the update.
65 if (file_exists('addon/' . $addon . '/.hidden')) {
66 DBA::update('addon', ['hidden' => true], ['name' => $addon]);
70 logger("Addons: FAILED installing " . $addon);
76 * reload all updated addons
78 public static function reload()
80 $addons = Config::get('system', 'addon');
81 if (strlen($addons)) {
82 $r = DBA::select('addon', [], ['installed' => 1]);
83 if (DBM::is_result($r)) {
84 $installed = DBA::inArray($r);
89 $addon_list = explode(',', $addons);
91 if (count($addon_list)) {
92 foreach ($addon_list as $addon) {
93 $addon = trim($addon);
94 $fname = 'addon/' . $addon . '/' . $addon . '.php';
96 if (file_exists($fname)) {
97 $t = @filemtime($fname);
98 foreach ($installed as $i) {
99 if (($i['name'] == $addon) && ($i['timestamp'] != $t)) {
100 logger('Reloading addon: ' . $i['name']);
101 @include_once($fname);
103 if (function_exists($addon . '_uninstall')) {
104 $func = $addon . '_uninstall';
107 if (function_exists($addon . '_install')) {
108 $func = $addon . '_install';
111 DBA::update('addon', ['timestamp' => $t], ['id' => $i['id']]);
121 * @brief check if addon is enabled
123 * @param string $addon
126 public static function isEnabled($addon)
128 return DBA::exists('addon', ['installed' => true, 'name' => $addon]);
133 * @brief registers a hook.
135 * @param string $hook the name of the hook
136 * @param string $file the name of the file that hooks into
137 * @param string $function the name of the function that the hook will call
138 * @param int $priority A priority (defaults to 0)
141 public static function registerHook($hook, $file, $function, $priority = 0)
143 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
144 $exists = DBA::exists('hook', $condition);
149 $r = DBA::insert('hook', ['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]);
155 * @brief unregisters a hook.
157 * @param string $hook the name of the hook
158 * @param string $file the name of the file that hooks into
159 * @param string $function the name of the function that the hook called
162 public static function unregisterHook($hook, $file, $function)
164 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
165 $r = DBA::delete('hook', $condition);
172 public static function loadHooks()
176 $r = DBA::select('hook', ['hook', 'file', 'function'], [], ['order' => ['priority' => 'desc', 'file']]);
178 while ($rr = DBA::fetch($r)) {
179 if (! array_key_exists($rr['hook'], $a->hooks)) {
180 $a->hooks[$rr['hook']] = [];
182 $a->hooks[$rr['hook']][] = [$rr['file'],$rr['function']];
188 * @brief Forks a hook.
190 * Use this function when you want to fork a hook via the worker.
192 * @param string $name of the hook to call
193 * @param string|array $data to transmit to the callback handler
195 public static function forkHooks($priority, $name, $data = null)
199 if (is_array($a->hooks) && array_key_exists($name, $a->hooks)) {
200 foreach ($a->hooks[$name] as $hook) {
201 Worker::add($priority, 'ForkHook', $name, $hook, $data);
207 * @brief Calls a hook.
209 * Use this function when you want to be able to allow a hook to manipulate
212 * @param string $name of the hook to call
213 * @param string|array &$data to transmit to the callback handler
215 public static function callHooks($name, &$data = null)
219 if (is_array($a->hooks) && array_key_exists($name, $a->hooks)) {
220 foreach ($a->hooks[$name] as $hook) {
221 self::callSingleHook($a, $name, $hook, $data);
227 * @brief Calls a single hook.
230 * @param string $name of the hook to call
231 * @param array $hook Hook data
232 * @param string|array &$data to transmit to the callback handler
234 public static function callSingleHook(App $a, $name, $hook, &$data = null)
236 // Don't run a theme's hook if the user isn't using the theme
237 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/' . $a->getCurrentTheme()) === false) {
241 @include_once($hook[0]);
242 if (function_exists($hook[1])) {
246 // remove orphan hooks
247 $condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
248 DBA::delete('hook', $condition, ['cascade' => false]);
253 * check if an app_menu hook exist for addon $name.
254 * Return true if the addon is an app
256 public static function isApp($name)
260 if (is_array($a->hooks) && (array_key_exists('app_menu', $a->hooks))) {
261 foreach ($a->hooks['app_menu'] as $hook) {
262 if ($hook[0] == 'addon/'.$name.'/'.$name.'.php') {
272 * @brief Parse addon comment in search of addon infos.
277 * * Description: An addon which plugs in
279 * * Author: John <profile url>
280 * * Author: Jane <email>
281 * * Maintainer: Jess <email>
284 * @param string $addon the name of the addon
285 * @return array with the addon information
287 public static function getInfo($addon)
300 if (!is_file("addon/$addon/$addon.php")) {
304 $stamp1 = microtime(true);
305 $f = file_get_contents("addon/$addon/$addon.php");
306 $a->save_timestamp($stamp1, "file");
308 $r = preg_match("|/\*.*\*/|msU", $f, $m);
311 $ll = explode("\n", $m[0]);
312 foreach ($ll as $l) {
313 $l = trim($l, "\t\n\r */");
315 list($type, $v) = array_map("trim", explode(":", $l, 2));
316 $type = strtolower($type);
317 if ($type == "author" || $type == "maintainer") {
318 $r = preg_match("|([^<]+)<([^>]+)>|", $v, $m);
320 $info[$type][] = ['name' => $m[1], 'link' => $m[2]];
322 $info[$type][] = ['name' => $v];
325 if (array_key_exists($type, $info)) {