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');
94 foreach ($addons as $name => $data) {
95 if (empty($data['admin'])) {
99 $addons_admin[$name] = [
100 'url' => 'admin/addons/' . $name,
106 return $addons_admin;
111 * Synchronize addons:
113 * system.addon contains a comma-separated list of names
114 * of addons which are used on this system.
115 * Go through the database list of already installed addons, and if we have
116 * an entry, but it isn't in the config list, call the uninstall procedure
117 * and mark it uninstalled in the database (for now we'll remove it).
118 * Then go through the config list and if we have a addon that isn't installed,
119 * call the install procedure and add it to the database.
122 public static function loadAddons()
124 self::$addons = array_keys(DI::config()->get('addons') ?? []);
128 * uninstalls an addon.
130 * @param string $addon name of the addon
134 public static function uninstall(string $addon)
136 $addon = Strings::sanitizeFilePathItem($addon);
138 Logger::debug("Addon {addon}: {action}", ['action' => 'uninstall', 'addon' => $addon]);
139 DI::config()->delete('addons', $addon);
141 @include_once('addon/' . $addon . '/' . $addon . '.php');
142 if (function_exists($addon . '_uninstall')) {
143 $func = $addon . '_uninstall';
147 Hook::delete(['file' => 'addon/' . $addon . '/' . $addon . '.php']);
149 unset(self::$addons[array_search($addon, self::$addons)]);
155 * @param string $addon name of the addon
159 public static function install(string $addon): bool
161 $addon = Strings::sanitizeFilePathItem($addon);
163 $addon_file_path = 'addon/' . $addon . '/' . $addon . '.php';
165 // silently fail if addon was removed of if $addon is funky
166 if (!file_exists($addon_file_path)) {
170 Logger::debug("Addon {addon}: {action}", ['action' => 'install', 'addon' => $addon]);
171 $t = @filemtime($addon_file_path);
172 @include_once($addon_file_path);
173 if (function_exists($addon . '_install')) {
174 $func = $addon . '_install';
178 DI::config()->set('addons', $addon, [
180 'admin' => function_exists($addon . '_addon_admin'),
183 if (!self::isEnabled($addon)) {
184 self::$addons[] = $addon;
191 * reload all updated addons
195 public static function reload()
197 $addons = DI::config()->get('addons');
199 foreach ($addons as $name => $data) {
200 $addonname = Strings::sanitizeFilePathItem(trim($name));
201 $addon_file_path = 'addon/' . $addonname . '/' . $addonname . '.php';
202 if (file_exists($addon_file_path) && $data['last_update'] == filemtime($addon_file_path)) {
203 // Addon unmodified, skipping
207 Logger::debug("Addon {addon}: {action}", ['action' => 'reload', 'addon' => $name]);
209 self::uninstall($name);
210 self::install($name);
215 * Parse addon comment in search of addon infos.
220 * * Description: An addon which plugs in
222 * * Author: John <profile url>
223 * * Author: Jane <email>
224 * * Maintainer: Jess <email>
227 * @param string $addon the name of the addon
228 * @return array with the addon information
231 public static function getInfo(string $addon): array
233 $addon = Strings::sanitizeFilePathItem($addon);
244 if (!is_file("addon/$addon/$addon.php")) {
248 DI::profiler()->startRecording('file');
249 $f = file_get_contents("addon/$addon/$addon.php");
250 DI::profiler()->stopRecording();
252 $r = preg_match("|/\*.*\*/|msU", $f, $m);
255 $ll = explode("\n", $m[0]);
256 foreach ($ll as $l) {
257 $l = trim($l, "\t\n\r */");
259 $addon_info = array_map("trim", explode(":", $l, 2));
260 if (count($addon_info) < 2) {
264 list($type, $v) = $addon_info;
265 $type = strtolower($type);
266 if ($type == "author" || $type == "maintainer") {
267 $r = preg_match("|([^<]+)<([^>]+)>|", $v, $m);
269 if (!empty($m[2]) && empty(parse_url($m[2], PHP_URL_SCHEME))) {
270 $contact = Contact::getByURL($m[2], false);
271 if (!empty($contact['url'])) {
272 $m[2] = $contact['url'];
275 $info[$type][] = ['name' => $m[1], 'link' => $m[2]];
277 $info[$type][] = ['name' => $v];
280 if (array_key_exists($type, $info)) {
291 * Checks if the provided addon is enabled
293 * @param string $addon
296 public static function isEnabled(string $addon): bool
298 return in_array($addon, self::$addons);
302 * Returns a list of the enabled addon names
306 public static function getEnabledList(): array
308 return self::$addons;
312 * Returns the list of non-hidden enabled addon names
317 public static function getVisibleList(): array
319 $visible_addons = [];
320 $addons = DI::config()->get('addons');
321 foreach ($addons as $name => $data) {
322 $visible_addons[] = $name;
325 return $visible_addons;