3 * @file src/Core/Addon.php
5 namespace Friendica\Core;
7 use Friendica\Core\Config;
8 use Friendica\Database\DBM;
9 use Friendica\Core\Worker;
13 require_once 'include/dba.php';
16 * Some functions to handle addons
21 * @brief uninstalls an addon.
23 * @param string $addon name of the addon
26 public static function uninstall($addon)
28 logger("Addons: uninstalling " . $addon);
29 dba::delete('addon', ['name' => $addon]);
31 @include_once('addon/' . $addon . '/' . $addon . '.php');
32 if (function_exists($addon . '_uninstall')) {
33 $func = $addon . '_uninstall';
39 * @brief installs an addon.
41 * @param string $addon name of the addon
44 public static function install($addon)
46 // silently fail if addon was removed
48 if (!file_exists('addon/' . $addon . '/' . $addon . '.php')) {
51 logger("Addons: installing " . $addon);
52 $t = @filemtime('addon/' . $addon . '/' . $addon . '.php');
53 @include_once('addon/' . $addon . '/' . $addon . '.php');
54 if (function_exists($addon . '_install')) {
55 $func = $addon . '_install';
58 $addon_admin = (function_exists($addon."_addon_admin") ? 1 : 0);
60 dba::insert('addon', ['name' => $addon, 'installed' => true,
61 'timestamp' => $t, 'plugin_admin' => $addon_admin]);
63 // we can add the following with the previous SQL
64 // once most site tables have been updated.
65 // This way the system won't fall over dead during the update.
67 if (file_exists('addon/' . $addon . '/.hidden')) {
68 dba::update('addon', ['hidden' => true], ['name' => $addon]);
72 logger("Addons: FAILED installing " . $addon);
78 * reload all updated addons
80 public static function reload()
82 $addons = Config::get('system', 'addon');
83 if (strlen($addons)) {
84 $r = dba::select('addon', [], ['installed' => 1]);
85 if (DBM::is_result($r)) {
86 $installed = dba::inArray($r);
91 $addon_list = explode(',', $addons);
93 if (count($addon_list)) {
94 foreach ($addon_list as $addon) {
95 $addon = trim($addon);
96 $fname = 'addon/' . $addon . '/' . $addon . '.php';
98 if (file_exists($fname)) {
99 $t = @filemtime($fname);
100 foreach ($installed as $i) {
101 if (($i['name'] == $addon) && ($i['timestamp'] != $t)) {
102 logger('Reloading addon: ' . $i['name']);
103 @include_once($fname);
105 if (function_exists($addon . '_uninstall')) {
106 $func = $addon . '_uninstall';
109 if (function_exists($addon . '_install')) {
110 $func = $addon . '_install';
113 dba::update('addon', ['timestamp' => $t], ['id' => $i['id']]);
123 * @brief check if addon is enabled
125 * @param string $addon
128 public static function isEnabled($addon)
130 return dba::exists('addon', ['installed' => true, 'name' => $addon]);
135 * @brief registers a hook.
137 * @param string $hook the name of the hook
138 * @param string $file the name of the file that hooks into
139 * @param string $function the name of the function that the hook will call
140 * @param int $priority A priority (defaults to 0)
143 public static function registerHook($hook, $file, $function, $priority = 0)
145 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
146 $exists = dba::exists('hook', $condition);
151 $r = dba::insert('hook', ['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]);
157 * @brief unregisters a hook.
159 * @param string $hook the name of the hook
160 * @param string $file the name of the file that hooks into
161 * @param string $function the name of the function that the hook called
164 public static function unregisterHook($hook, $file, $function)
166 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
167 $r = dba::delete('hook', $condition);
174 public static function loadHooks()
178 $r = dba::select('hook', ['hook', 'file', 'function'], [], ['order' => ['priority' => 'desc', 'file']]);
180 while ($rr = dba::fetch($r)) {
181 if (! array_key_exists($rr['hook'], $a->hooks)) {
182 $a->hooks[$rr['hook']] = [];
184 $a->hooks[$rr['hook']][] = [$rr['file'],$rr['function']];
190 * @brief Forks a hook.
192 * Use this function when you want to fork a hook via the worker.
194 * @param string $name of the hook to call
195 * @param string|array $data to transmit to the callback handler
197 public static function forkHooks($priority, $name, $data = null)
201 if (is_array($a->hooks) && array_key_exists($name, $a->hooks)) {
202 foreach ($a->hooks[$name] as $hook) {
203 Worker::add($priority, 'ForkHook', $name, $hook, $data);
209 * @brief Calls a hook.
211 * Use this function when you want to be able to allow a hook to manipulate
214 * @param string $name of the hook to call
215 * @param string|array &$data to transmit to the callback handler
217 public static function callHooks($name, &$data = null)
221 if (is_array($a->hooks) && array_key_exists($name, $a->hooks)) {
222 foreach ($a->hooks[$name] as $hook) {
223 self::callSingleHook($a, $name, $hook, $data);
229 * @brief Calls a single hook.
231 * @param string $name of the hook to call
232 * @param array $hook Hook data
233 * @param string|array &$data to transmit to the callback handler
235 public static function callSingleHook($a, $name, $hook, &$data = null)
237 // Don't run a theme's hook if the user isn't using the theme
238 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/'.current_theme()) === false) {
242 @include_once($hook[0]);
243 if (function_exists($hook[1])) {
247 // remove orphan hooks
248 $condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
249 dba::delete('hook', $condition);
254 * check if an app_menu hook exist for addon $name.
255 * Return true if the addon is an app
257 public static function isApp($name)
261 if (is_array($a->hooks) && (array_key_exists('app_menu', $a->hooks))) {
262 foreach ($a->hooks['app_menu'] as $hook) {
263 if ($hook[0] == 'addon/'.$name.'/'.$name.'.php') {
273 * @brief Parse addon comment in search of addon infos.
278 * * Description: An addon which plugs in
280 * * Author: John <profile url>
281 * * Author: Jane <email>
282 * * Maintainer: Jess <email>
285 * @param string $addon the name of the addon
286 * @return array with the addon information
288 public static function getInfo($addon)
301 if (!is_file("addon/$addon/$addon.php")) {
305 $stamp1 = microtime(true);
306 $f = file_get_contents("addon/$addon/$addon.php");
307 $a->save_timestamp($stamp1, "file");
309 $r = preg_match("|/\*.*\*/|msU", $f, $m);
312 $ll = explode("\n", $m[0]);
313 foreach ($ll as $l) {
314 $l = trim($l, "\t\n\r */");
316 list($type, $v) = array_map("trim", explode(":", $l, 2));
317 $type = strtolower($type);
318 if ($type == "author" || $type == "maintainer") {
319 $r = preg_match("|([^<]+)<([^>]+)>|", $v, $m);
321 $info[$type][] = ['name' => $m[1], 'link' => $m[2]];
323 $info[$type][] = ['name' => $v];
326 if (array_key_exists($type, $info)) {