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');
88 foreach ($addons as $name => $data) {
89 if (empty($data['admin'])) {
93 $addons_admin[$name] = [
94 'url' => 'admin/addons/' . $name,
100 return $addons_admin;
105 * Synchronize addons:
107 * system.addon contains a comma-separated list of names
108 * of addons which are used on this system.
109 * Go through the database list of already installed addons, and if we have
110 * an entry, but it isn't in the config list, call the uninstall procedure
111 * and mark it uninstalled in the database (for now we'll remove it).
112 * Then go through the config list and if we have a addon that isn't installed,
113 * call the install procedure and add it to the database.
116 public static function loadAddons()
118 self::$addons = array_keys(DI::config()->get('addons') ?? []);
122 * uninstalls an addon.
124 * @param string $addon name of the addon
128 public static function uninstall(string $addon)
130 $addon = Strings::sanitizeFilePathItem($addon);
132 Logger::debug("Addon {addon}: {action}", ['action' => 'uninstall', 'addon' => $addon]);
133 DI::config()->delete('addons', $addon);
135 @include_once('addon/' . $addon . '/' . $addon . '.php');
136 if (function_exists($addon . '_uninstall')) {
137 $func = $addon . '_uninstall';
141 Hook::delete(['file' => 'addon/' . $addon . '/' . $addon . '.php']);
143 unset(self::$addons[array_search($addon, self::$addons)]);
149 * @param string $addon name of the addon
153 public static function install(string $addon): bool
155 $addon = Strings::sanitizeFilePathItem($addon);
157 $addon_file_path = 'addon/' . $addon . '/' . $addon . '.php';
159 // silently fail if addon was removed of if $addon is funky
160 if (!file_exists($addon_file_path)) {
164 Logger::debug("Addon {addon}: {action}", ['action' => 'install', 'addon' => $addon]);
165 $t = @filemtime($addon_file_path);
166 @include_once($addon_file_path);
167 if (function_exists($addon . '_install')) {
168 $func = $addon . '_install';
172 DI::config()->set('addons', $addon,[
174 'admin' => function_exists($addon . '_addon_admin'),
177 if (!self::isEnabled($addon)) {
178 self::$addons[] = $addon;
185 * reload all updated addons
189 public static function reload()
191 $addons = DI::config()->get('addons');
193 foreach ($addons as $name => $data) {
194 $addonname = Strings::sanitizeFilePathItem(trim($name));
195 $addon_file_path = 'addon/' . $addonname . '/' . $addonname . '.php';
196 if (file_exists($addon_file_path) && $data['last_update'] == filemtime($addon_file_path)) {
197 // Addon unmodified, skipping
201 Logger::debug("Addon {addon}: {action}", ['action' => 'reload', 'addon' => $name]);
203 self::uninstall($name);
204 self::install($name);
209 * Parse addon comment in search of addon infos.
214 * * Description: An addon which plugs in
216 * * Author: John <profile url>
217 * * Author: Jane <email>
218 * * Maintainer: Jess <email>
221 * @param string $addon the name of the addon
222 * @return array with the addon information
225 public static function getInfo(string $addon): array
227 $addon = Strings::sanitizeFilePathItem($addon);
238 if (!is_file("addon/$addon/$addon.php")) {
242 DI::profiler()->startRecording('file');
243 $f = file_get_contents("addon/$addon/$addon.php");
244 DI::profiler()->stopRecording();
246 $r = preg_match("|/\*.*\*/|msU", $f, $m);
249 $ll = explode("\n", $m[0]);
250 foreach ($ll as $l) {
251 $l = trim($l, "\t\n\r */");
253 $addon_info = array_map("trim", explode(":", $l, 2));
254 if (count($addon_info) < 2) {
258 list($type, $v) = $addon_info;
259 $type = strtolower($type);
260 if ($type == "author" || $type == "maintainer") {
261 $r = preg_match("|([^<]+)<([^>]+)>|", $v, $m);
263 if (!empty($m[2]) && empty(parse_url($m[2], PHP_URL_SCHEME))) {
264 $contact = Contact::getByURL($m[2], false);
265 if (!empty($contact['url'])) {
266 $m[2] = $contact['url'];
269 $info[$type][] = ['name' => $m[1], 'link' => $m[2]];
271 $info[$type][] = ['name' => $v];
274 if (array_key_exists($type, $info)) {
285 * Checks if the provided addon is enabled
287 * @param string $addon
290 public static function isEnabled(string $addon): bool
292 return in_array($addon, self::$addons);
296 * Returns a list of the enabled addon names
300 public static function getEnabledList(): array
302 return self::$addons;
306 * Returns the list of non-hidden enabled addon names
311 public static function getVisibleList(): array
313 $visible_addons = [];
314 $addons = DI::config()->get('addons');
315 foreach ($addons as $name => $data) {
316 $visible_addons[] = $name;
319 return $visible_addons;