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 $file = str_replace(get_app()->get_basepath() . DIRECTORY_SEPARATOR, '', $file);
144 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
145 $exists = DBA::exists('hook', $condition);
150 $r = DBA::insert('hook', ['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 public static function unregisterHook($hook, $file, $function)
165 $relative_file = str_replace(get_app()->get_basepath() . DIRECTORY_SEPARATOR, '', $file);
167 // This here is only needed for fixing a problem that existed on the develop branch
168 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
169 DBA::delete('hook', $condition);
171 $condition = ['hook' => $hook, 'file' => $relative_file, 'function' => $function];
172 $r = DBA::delete('hook', $condition);
179 public static function loadHooks()
183 $r = DBA::select('hook', ['hook', 'file', 'function'], [], ['order' => ['priority' => 'desc', 'file']]);
185 while ($rr = DBA::fetch($r)) {
186 if (! array_key_exists($rr['hook'], $a->hooks)) {
187 $a->hooks[$rr['hook']] = [];
189 $a->hooks[$rr['hook']][] = [$rr['file'],$rr['function']];
195 * @brief Forks a hook.
197 * Use this function when you want to fork a hook via the worker.
199 * @param string $name of the hook to call
200 * @param string|array $data to transmit to the callback handler
202 public static function forkHooks($priority, $name, $data = null)
206 if (is_array($a->hooks) && array_key_exists($name, $a->hooks)) {
207 foreach ($a->hooks[$name] as $hook) {
208 Worker::add($priority, 'ForkHook', $name, $hook, $data);
214 * @brief Calls a hook.
216 * Use this function when you want to be able to allow a hook to manipulate
219 * @param string $name of the hook to call
220 * @param string|array &$data to transmit to the callback handler
222 public static function callHooks($name, &$data = null)
226 if (is_array($a->hooks) && array_key_exists($name, $a->hooks)) {
227 foreach ($a->hooks[$name] as $hook) {
228 self::callSingleHook($a, $name, $hook, $data);
234 * @brief Calls a single hook.
237 * @param string $name of the hook to call
238 * @param array $hook Hook data
239 * @param string|array &$data to transmit to the callback handler
241 public static function callSingleHook(App $a, $name, $hook, &$data = null)
243 // Don't run a theme's hook if the user isn't using the theme
244 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/' . $a->getCurrentTheme()) === false) {
248 @include_once($hook[0]);
249 if (function_exists($hook[1])) {
253 // remove orphan hooks
254 $condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
255 DBA::delete('hook', $condition, ['cascade' => false]);
260 * check if an app_menu hook exist for addon $name.
261 * Return true if the addon is an app
263 public static function isApp($name)
267 if (is_array($a->hooks) && (array_key_exists('app_menu', $a->hooks))) {
268 foreach ($a->hooks['app_menu'] as $hook) {
269 if ($hook[0] == 'addon/'.$name.'/'.$name.'.php') {
279 * @brief Parse addon comment in search of addon infos.
284 * * Description: An addon which plugs in
286 * * Author: John <profile url>
287 * * Author: Jane <email>
288 * * Maintainer: Jess <email>
291 * @param string $addon the name of the addon
292 * @return array with the addon information
294 public static function getInfo($addon)
307 if (!is_file("addon/$addon/$addon.php")) {
311 $stamp1 = microtime(true);
312 $f = file_get_contents("addon/$addon/$addon.php");
313 $a->save_timestamp($stamp1, "file");
315 $r = preg_match("|/\*.*\*/|msU", $f, $m);
318 $ll = explode("\n", $m[0]);
319 foreach ($ll as $l) {
320 $l = trim($l, "\t\n\r */");
322 $addon_info = array_map("trim", explode(":", $l, 2));
323 if (count($addon_info) < 2) {
327 list($type, $v) = $addon_info;
328 $type = strtolower($type);
329 if ($type == "author" || $type == "maintainer") {
330 $r = preg_match("|([^<]+)<([^>]+)>|", $v, $m);
332 $info[$type][] = ['name' => $m[1], 'link' => $m[2]];
334 $info[$type][] = ['name' => $v];
337 if (array_key_exists($type, $info)) {