3 * @file src/Core/Addon.php
5 namespace Friendica\Core;
8 use Friendica\BaseObject;
9 use Friendica\Database\DBA;
11 require_once 'include/dba.php';
14 * Some functions to handle addons
16 class Addon extends BaseObject
19 * @brief Synchronise addons:
21 * system.addon contains a comma-separated list of names
22 * of addons which are used on this system.
23 * Go through the database list of already installed addons, and if we have
24 * an entry, but it isn't in the config list, call the uninstall procedure
25 * and mark it uninstalled in the database (for now we'll remove it).
26 * Then go through the config list and if we have a addon that isn't installed,
27 * call the install procedure and add it to the database.
30 public static function check()
34 $r = DBA::select('addon', [], ['installed' => 1]);
35 if (DBA::isResult($r)) {
36 $installed = DBA::toArray($r);
41 $addons = Config::get('system', 'addon');
45 $addons_arr = explode(',', str_replace(' ', '', $addons));
48 $a->addons = $addons_arr;
52 if (count($installed)) {
53 foreach ($installed as $i) {
54 if (!in_array($i['name'], $addons_arr)) {
55 self::uninstall($i['name']);
57 $installed_arr[] = $i['name'];
62 if (count($addons_arr)) {
63 foreach ($addons_arr as $p) {
64 if (!in_array($p, $installed_arr)) {
76 * @brief uninstalls an addon.
78 * @param string $addon name of the addon
81 public static function uninstall($addon)
83 logger("Addons: uninstalling " . $addon);
84 DBA::delete('addon', ['name' => $addon]);
86 @include_once('addon/' . $addon . '/' . $addon . '.php');
87 if (function_exists($addon . '_uninstall')) {
88 $func = $addon . '_uninstall';
94 * @brief installs an addon.
96 * @param string $addon name of the addon
99 public static function install($addon)
101 // silently fail if addon was removed
103 if (!file_exists('addon/' . $addon . '/' . $addon . '.php')) {
106 logger("Addons: installing " . $addon);
107 $t = @filemtime('addon/' . $addon . '/' . $addon . '.php');
108 @include_once('addon/' . $addon . '/' . $addon . '.php');
109 if (function_exists($addon . '_install')) {
110 $func = $addon . '_install';
113 $addon_admin = (function_exists($addon."_addon_admin") ? 1 : 0);
115 DBA::insert('addon', ['name' => $addon, 'installed' => true,
116 'timestamp' => $t, 'plugin_admin' => $addon_admin]);
118 // we can add the following with the previous SQL
119 // once most site tables have been updated.
120 // This way the system won't fall over dead during the update.
122 if (file_exists('addon/' . $addon . '/.hidden')) {
123 DBA::update('addon', ['hidden' => true], ['name' => $addon]);
127 logger("Addons: FAILED installing " . $addon);
133 * reload all updated addons
135 public static function reload()
137 $addons = Config::get('system', 'addon');
138 if (strlen($addons)) {
139 $r = DBA::select('addon', [], ['installed' => 1]);
140 if (DBA::isResult($r)) {
141 $installed = DBA::toArray($r);
146 $addon_list = explode(',', $addons);
148 if (count($addon_list)) {
149 foreach ($addon_list as $addon) {
150 $addon = trim($addon);
151 $fname = 'addon/' . $addon . '/' . $addon . '.php';
153 if (file_exists($fname)) {
154 $t = @filemtime($fname);
155 foreach ($installed as $i) {
156 if (($i['name'] == $addon) && ($i['timestamp'] != $t)) {
157 logger('Reloading addon: ' . $i['name']);
158 @include_once($fname);
160 if (function_exists($addon . '_uninstall')) {
161 $func = $addon . '_uninstall';
164 if (function_exists($addon . '_install')) {
165 $func = $addon . '_install';
168 DBA::update('addon', ['timestamp' => $t], ['id' => $i['id']]);
178 * @brief check if addon is enabled
180 * @param string $addon
183 public static function isEnabled($addon)
185 return DBA::exists('addon', ['installed' => true, 'name' => $addon]);
190 * @brief registers a hook.
192 * @param string $hook the name of the hook
193 * @param string $file the name of the file that hooks into
194 * @param string $function the name of the function that the hook will call
195 * @param int $priority A priority (defaults to 0)
198 public static function registerHook($hook, $file, $function, $priority = 0)
200 $file = str_replace(self::getApp()->get_basepath() . DIRECTORY_SEPARATOR, '', $file);
202 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
203 $exists = DBA::exists('hook', $condition);
208 $r = DBA::insert('hook', ['hook' => $hook, 'file' => $file, 'function' => $function, 'priority' => $priority]);
214 * @brief unregisters a hook.
216 * @param string $hook the name of the hook
217 * @param string $file the name of the file that hooks into
218 * @param string $function the name of the function that the hook called
221 public static function unregisterHook($hook, $file, $function)
223 $relative_file = str_replace(self::getApp()->get_basepath() . DIRECTORY_SEPARATOR, '', $file);
225 // This here is only needed for fixing a problem that existed on the develop branch
226 $condition = ['hook' => $hook, 'file' => $file, 'function' => $function];
227 DBA::delete('hook', $condition);
229 $condition = ['hook' => $hook, 'file' => $relative_file, 'function' => $function];
230 $r = DBA::delete('hook', $condition);
237 public static function loadHooks()
241 $r = DBA::select('hook', ['hook', 'file', 'function'], [], ['order' => ['priority' => 'desc', 'file']]);
243 while ($rr = DBA::fetch($r)) {
244 if (! array_key_exists($rr['hook'], $a->hooks)) {
245 $a->hooks[$rr['hook']] = [];
247 $a->hooks[$rr['hook']][] = [$rr['file'],$rr['function']];
253 * @brief Forks a hook.
255 * Use this function when you want to fork a hook via the worker.
257 * @param string $name of the hook to call
258 * @param string|array $data to transmit to the callback handler
260 public static function forkHooks($priority, $name, $data = null)
264 if (is_array($a->hooks) && array_key_exists($name, $a->hooks)) {
265 foreach ($a->hooks[$name] as $hook) {
266 Worker::add($priority, 'ForkHook', $name, $hook, $data);
272 * @brief Calls a hook.
274 * Use this function when you want to be able to allow a hook to manipulate
277 * @param string $name of the hook to call
278 * @param string|array &$data to transmit to the callback handler
280 public static function callHooks($name, &$data = null)
284 if (is_array($a->hooks) && array_key_exists($name, $a->hooks)) {
285 foreach ($a->hooks[$name] as $hook) {
286 self::callSingleHook($a, $name, $hook, $data);
292 * @brief Calls a single hook.
295 * @param string $name of the hook to call
296 * @param array $hook Hook data
297 * @param string|array &$data to transmit to the callback handler
299 public static function callSingleHook(App $a, $name, $hook, &$data = null)
301 // Don't run a theme's hook if the user isn't using the theme
302 if (strpos($hook[0], 'view/theme/') !== false && strpos($hook[0], 'view/theme/' . $a->getCurrentTheme()) === false) {
306 @include_once($hook[0]);
307 if (function_exists($hook[1])) {
311 // remove orphan hooks
312 $condition = ['hook' => $name, 'file' => $hook[0], 'function' => $hook[1]];
313 DBA::delete('hook', $condition, ['cascade' => false]);
318 * check if an app_menu hook exist for addon $name.
319 * Return true if the addon is an app
321 public static function isApp($name)
325 if (is_array($a->hooks) && (array_key_exists('app_menu', $a->hooks))) {
326 foreach ($a->hooks['app_menu'] as $hook) {
327 if ($hook[0] == 'addon/'.$name.'/'.$name.'.php') {
337 * @brief Parse addon comment in search of addon infos.
342 * * Description: An addon which plugs in
344 * * Author: John <profile url>
345 * * Author: Jane <email>
346 * * Maintainer: Jess <email>
349 * @param string $addon the name of the addon
350 * @return array with the addon information
352 public static function getInfo($addon)
365 if (!is_file("addon/$addon/$addon.php")) {
369 $stamp1 = microtime(true);
370 $f = file_get_contents("addon/$addon/$addon.php");
371 $a->save_timestamp($stamp1, "file");
373 $r = preg_match("|/\*.*\*/|msU", $f, $m);
376 $ll = explode("\n", $m[0]);
377 foreach ($ll as $l) {
378 $l = trim($l, "\t\n\r */");
380 $addon_info = array_map("trim", explode(":", $l, 2));
381 if (count($addon_info) < 2) {
385 list($type, $v) = $addon_info;
386 $type = strtolower($type);
387 if ($type == "author" || $type == "maintainer") {
388 $r = preg_match("|([^<]+)<([^>]+)>|", $v, $m);
390 $info[$type][] = ['name' => $m[1], 'link' => $m[2]];
392 $info[$type][] = ['name' => $v];
395 if (array_key_exists($type, $info)) {