3 * @file src/Core/Addon.php
5 namespace Friendica\Core;
7 use Friendica\BaseObject;
8 use Friendica\Database\DBA;
11 * Some functions to handle addons
13 class Addon extends BaseObject
16 * The addon sub-directory
19 const DIRECTORY = 'addon';
22 * List of the names of enabled addons
26 private static $addons = [];
29 * @brief Synchronize addons:
31 * system.addon contains a comma-separated list of names
32 * of addons which are used on this system.
33 * Go through the database list of already installed addons, and if we have
34 * an entry, but it isn't in the config list, call the uninstall procedure
35 * and mark it uninstalled in the database (for now we'll remove it).
36 * Then go through the config list and if we have a addon that isn't installed,
37 * call the install procedure and add it to the database.
40 public static function loadAddons()
42 $installed_addons = [];
44 $r = DBA::select('addon', [], ['installed' => 1]);
45 if (DBA::isResult($r)) {
46 $installed_addons = DBA::toArray($r);
49 $addons = Config::get('system', 'addon');
53 $addons_arr = explode(',', str_replace(' ', '', $addons));
56 self::$addons = $addons_arr;
60 foreach ($installed_addons as $addon) {
61 if (!self::isEnabled($addon['name'])) {
62 self::uninstall($addon['name']);
64 $installed_arr[] = $addon['name'];
68 foreach (self::$addons as $p) {
69 if (!in_array($p, $installed_arr)) {
76 * @brief uninstalls an addon.
78 * @param string $addon name of the addon
82 public static function uninstall($addon)
84 Logger::notice("Addon {addon}: {action}", ['action' => 'uninstall', 'addon' => $addon]);
85 DBA::delete('addon', ['name' => $addon]);
87 @include_once('addon/' . $addon . '/' . $addon . '.php');
88 if (function_exists($addon . '_uninstall')) {
89 $func = $addon . '_uninstall';
93 unset(self::$addons[array_search($addon, self::$addons)]);
97 * @brief installs an addon.
99 * @param string $addon name of the addon
103 public static function install($addon)
105 // silently fail if addon was removed
107 if (!file_exists('addon/' . $addon . '/' . $addon . '.php')) {
110 Logger::notice("Addon {addon}: {action}", ['action' => 'install', 'addon' => $addon]);
111 $t = @filemtime('addon/' . $addon . '/' . $addon . '.php');
112 @include_once('addon/' . $addon . '/' . $addon . '.php');
113 if (function_exists($addon . '_install')) {
114 $func = $addon . '_install';
117 $addon_admin = (function_exists($addon . "_addon_admin") ? 1 : 0);
119 DBA::insert('addon', ['name' => $addon, 'installed' => true,
120 'timestamp' => $t, 'plugin_admin' => $addon_admin]);
122 // we can add the following with the previous SQL
123 // once most site tables have been updated.
124 // This way the system won't fall over dead during the update.
126 if (file_exists('addon/' . $addon . '/.hidden')) {
127 DBA::update('addon', ['hidden' => true], ['name' => $addon]);
130 if (!self::isEnabled($addon)) {
131 self::$addons[] = $addon;
135 Logger::error("Addon {addon}: {action} failed", ['action' => 'uninstall', 'addon' => $addon]);
141 * reload all updated addons
143 public static function reload()
145 $addons = Config::get('system', 'addon');
146 if (strlen($addons)) {
147 $r = DBA::select('addon', [], ['installed' => 1]);
148 if (DBA::isResult($r)) {
149 $installed = DBA::toArray($r);
154 $addon_list = explode(',', $addons);
156 if (count($addon_list)) {
157 foreach ($addon_list as $addon) {
158 $addon = trim($addon);
159 $fname = 'addon/' . $addon . '/' . $addon . '.php';
161 if (file_exists($fname)) {
162 $t = @filemtime($fname);
163 foreach ($installed as $i) {
164 if (($i['name'] == $addon) && ($i['timestamp'] != $t)) {
166 Logger::notice("Addon {addon}: {action}", ['action' => 'reload', 'addon' => $i['name']]);
167 @include_once($fname);
169 if (function_exists($addon . '_uninstall')) {
170 $func = $addon . '_uninstall';
173 if (function_exists($addon . '_install')) {
174 $func = $addon . '_install';
177 DBA::update('addon', ['timestamp' => $t], ['id' => $i['id']]);
187 * @brief Parse addon comment in search of addon infos.
192 * * Description: An addon which plugs in
194 * * Author: John <profile url>
195 * * Author: Jane <email>
196 * * Maintainer: Jess <email>
199 * @param string $addon the name of the addon
200 * @return array with the addon information
203 public static function getInfo($addon)
216 if (!is_file("addon/$addon/$addon.php")) {
220 $stamp1 = microtime(true);
221 $f = file_get_contents("addon/$addon/$addon.php");
222 $a->saveTimestamp($stamp1, "file");
224 $r = preg_match("|/\*.*\*/|msU", $f, $m);
227 $ll = explode("\n", $m[0]);
228 foreach ($ll as $l) {
229 $l = trim($l, "\t\n\r */");
231 $addon_info = array_map("trim", explode(":", $l, 2));
232 if (count($addon_info) < 2) {
236 list($type, $v) = $addon_info;
237 $type = strtolower($type);
238 if ($type == "author" || $type == "maintainer") {
239 $r = preg_match("|([^<]+)<([^>]+)>|", $v, $m);
241 $info[$type][] = ['name' => $m[1], 'link' => $m[2]];
243 $info[$type][] = ['name' => $v];
246 if (array_key_exists($type, $info)) {
257 * Checks if the provided addon is enabled
259 * @param string $addon
262 public static function isEnabled($addon)
264 return in_array($addon, self::$addons);
268 * Returns a list of the enabled addon names
272 public static function getEnabledList()
274 return self::$addons;
278 * Saves the current enabled addon list in the system.addon config key
281 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
283 public static function saveEnabledList()
285 return Config::set("system", "addon", implode(", ", self::$addons));
289 * Returns the list of non-hidden enabled addon names
294 public static function getVisibleList()
296 $visible_addons = [];
297 $stmt = DBA::select('addon', ['name'], ['hidden' => false, 'installed' => true]);
298 if (DBA::isResult($stmt)) {
299 foreach (DBA::toArray($stmt) as $addon) {
300 $visible_addons[] = $addon['name'];
304 return $visible_addons;
308 * Shim of Hook::register left for backward compatibility purpose.
310 * @see Hook::register
311 * @deprecated since version 2018.12
312 * @param string $hook the name of the hook
313 * @param string $file the name of the file that hooks into
314 * @param string $function the name of the function that the hook will call
315 * @param int $priority A priority (defaults to 0)
317 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
319 public static function registerHook($hook, $file, $function, $priority = 0)
321 return Hook::register($hook, $file, $function, $priority);
325 * Shim of Hook::unregister left for backward compatibility purpose.
327 * @see Hook::unregister
328 * @deprecated since version 2018.12
329 * @param string $hook the name of the hook
330 * @param string $file the name of the file that hooks into
331 * @param string $function the name of the function that the hook called
333 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
335 public static function unregisterHook($hook, $file, $function)
337 return Hook::unregister($hook, $file, $function);
341 * Shim of Hook::callAll left for backward-compatibility purpose.
344 * @deprecated since version 2018.12
345 * @param string $name of the hook to call
346 * @param string|array &$data to transmit to the callback handler
347 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
349 public static function callHooks($name, &$data = null)
351 Hook::callAll($name, $data);