3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Core;
25 use Friendica\Model\Contact;
26 use Friendica\Util\Strings;
29 * Some functions to handle addons
34 * The addon sub-directory
37 const DIRECTORY = 'addon';
40 * List of the names of enabled addons
44 private static $addons = [];
47 * Returns the list of available addons with their current status and info.
48 * This list is made from scanning the addon/ folder.
49 * Unsupported addons are excluded unless they already are enabled or system.show_unsupported_addon is set.
54 public static function getAvailableList()
57 $files = glob('addon/*/');
58 if (is_array($files)) {
59 foreach ($files as $file) {
61 list($tmp, $addon) = array_map('trim', explode('/', $file));
62 $info = self::getInfo($addon);
64 if (DI::config()->get('system', 'show_unsupported_addons')
65 || strtolower($info['status']) != 'unsupported'
66 || self::isEnabled($addon)
68 $addons[] = [$addon, (self::isEnabled($addon) ? 'on' : 'off'), $info];
78 * Returns a list of addons that can be configured at the node level.
79 * The list is formatted for display in the admin panel aside.
84 public static function getAdminList()
87 $addons = DI::config()->get('addons');
89 foreach ($addons as $name => $data) {
90 if (empty($data['admin'])) {
94 $addons_admin[$name] = [
95 'url' => 'admin/addons/' . $name,
101 return $addons_admin;
106 * Synchronize addons:
108 * system.addon contains a comma-separated list of names
109 * of addons which are used on this system.
110 * Go through the database list of already installed addons, and if we have
111 * an entry, but it isn't in the config list, call the uninstall procedure
112 * and mark it uninstalled in the database (for now we'll remove it).
113 * Then go through the config list and if we have a addon that isn't installed,
114 * call the install procedure and add it to the database.
117 public static function loadAddons()
119 self::$addons = array_keys(DI::config()->get('addons') ?? []);
123 * uninstalls an addon.
125 * @param string $addon name of the addon
129 public static function uninstall(string $addon)
131 $addon = Strings::sanitizeFilePathItem($addon);
133 Logger::debug("Addon {addon}: {action}", ['action' => 'uninstall', 'addon' => $addon]);
134 DI::config()->delete('addons', $addon);
136 @include_once('addon/' . $addon . '/' . $addon . '.php');
137 if (function_exists($addon . '_uninstall')) {
138 $func = $addon . '_uninstall';
142 Hook::delete(['file' => 'addon/' . $addon . '/' . $addon . '.php']);
144 unset(self::$addons[array_search($addon, self::$addons)]);
150 * @param string $addon name of the addon
154 public static function install(string $addon): bool
156 $addon = Strings::sanitizeFilePathItem($addon);
158 $addon_file_path = 'addon/' . $addon . '/' . $addon . '.php';
160 // silently fail if addon was removed of if $addon is funky
161 if (!file_exists($addon_file_path)) {
165 Logger::debug("Addon {addon}: {action}", ['action' => 'install', 'addon' => $addon]);
166 $t = @filemtime($addon_file_path);
167 @include_once($addon_file_path);
168 if (function_exists($addon . '_install')) {
169 $func = $addon . '_install';
173 DI::config()->set('addons', $addon, [
175 'admin' => function_exists($addon . '_addon_admin'),
178 if (!self::isEnabled($addon)) {
179 self::$addons[] = $addon;
186 * reload all updated addons
190 public static function reload()
192 $addons = DI::config()->get('addons');
194 foreach ($addons as $name => $data) {
195 $addonname = Strings::sanitizeFilePathItem(trim($name));
196 $addon_file_path = 'addon/' . $addonname . '/' . $addonname . '.php';
197 if (file_exists($addon_file_path) && $data['last_update'] == filemtime($addon_file_path)) {
198 // Addon unmodified, skipping
202 Logger::debug("Addon {addon}: {action}", ['action' => 'reload', 'addon' => $name]);
204 self::uninstall($name);
205 self::install($name);
210 * Parse addon comment in search of addon infos.
215 * * Description: An addon which plugs in
217 * * Author: John <profile url>
218 * * Author: Jane <email>
219 * * Maintainer: Jess <email>
222 * @param string $addon the name of the addon
223 * @return array with the addon information
226 public static function getInfo(string $addon): array
228 $addon = Strings::sanitizeFilePathItem($addon);
239 if (!is_file("addon/$addon/$addon.php")) {
243 DI::profiler()->startRecording('file');
244 $f = file_get_contents("addon/$addon/$addon.php");
245 DI::profiler()->stopRecording();
247 $r = preg_match("|/\*.*\*/|msU", $f, $m);
250 $ll = explode("\n", $m[0]);
251 foreach ($ll as $l) {
252 $l = trim($l, "\t\n\r */");
254 $addon_info = array_map("trim", explode(":", $l, 2));
255 if (count($addon_info) < 2) {
259 list($type, $v) = $addon_info;
260 $type = strtolower($type);
261 if ($type == "author" || $type == "maintainer") {
262 $r = preg_match("|([^<]+)<([^>]+)>|", $v, $m);
264 if (!empty($m[2]) && empty(parse_url($m[2], PHP_URL_SCHEME))) {
265 $contact = Contact::getByURL($m[2], false);
266 if (!empty($contact['url'])) {
267 $m[2] = $contact['url'];
270 $info[$type][] = ['name' => $m[1], 'link' => $m[2]];
272 $info[$type][] = ['name' => $v];
275 if (array_key_exists($type, $info)) {
286 * Checks if the provided addon is enabled
288 * @param string $addon
291 public static function isEnabled(string $addon): bool
293 return in_array($addon, self::$addons);
297 * Returns a list of the enabled addon names
301 public static function getEnabledList(): array
303 return self::$addons;
307 * Returns the list of non-hidden enabled addon names
312 public static function getVisibleList(): array
314 $visible_addons = [];
315 $addons = DI::config()->get('addons');
316 foreach ($addons as $name => $data) {
317 $visible_addons[] = $name;
320 return $visible_addons;