]> git.mxchange.org Git - friendica.git/blob - src/Core/Addon.php
Adapt doc
[friendica.git] / src / Core / Addon.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Core;
23
24 use Friendica\DI;
25 use Friendica\Model\Contact;
26 use Friendica\Util\Strings;
27
28 /**
29  * Some functions to handle addons
30  */
31 class Addon
32 {
33         /**
34          * The addon sub-directory
35          * @var string
36          */
37         const DIRECTORY = 'addon';
38
39         /**
40          * List of the names of enabled addons
41          *
42          * @var array
43          */
44         private static $addons = [];
45
46         /**
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.
50          *
51          * @return array
52          * @throws \Exception
53          */
54         public static function getAvailableList()
55         {
56                 $addons = [];
57                 $files = glob('addon/*/');
58                 if (is_array($files)) {
59                         foreach ($files as $file) {
60                                 if (is_dir($file)) {
61                                         list($tmp, $addon) = array_map('trim', explode('/', $file));
62                                         $info = self::getInfo($addon);
63
64                                         if (DI::config()->get('system', 'show_unsupported_addons')
65                                                 || strtolower($info['status']) != 'unsupported'
66                                                 || self::isEnabled($addon)
67                                         ) {
68                                                 $addons[] = [$addon, (self::isEnabled($addon) ? 'on' : 'off'), $info];
69                                         }
70                                 }
71                         }
72                 }
73
74                 return $addons;
75         }
76
77         /**
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.
80          *
81          * @return array
82          * @throws \Exception
83          */
84         public static function getAdminList()
85         {
86                 $addons_admin = [];
87                 $addons = DI::config()->get('addons');
88                 ksort($addons);
89                 foreach ($addons as $name => $data) {
90                         if (empty($data['admin'])) {
91                                 continue;
92                         }
93
94                         $addons_admin[$name] = [
95                                 'url' => 'admin/addons/' . $name,
96                                 'name' => $name,
97                                 'class' => 'addon'
98                         ];
99                 }
100
101                 return $addons_admin;
102         }
103
104
105         /**
106          * Synchronize addons:
107          *
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.
115          *
116          */
117         public static function loadAddons()
118         {
119                 self::$addons = array_keys(DI::config()->get('addons') ?? []);
120         }
121
122         /**
123          * uninstalls an addon.
124          *
125          * @param string $addon name of the addon
126          * @return void
127          * @throws \Exception
128          */
129         public static function uninstall(string $addon)
130         {
131                 $addon = Strings::sanitizeFilePathItem($addon);
132
133                 Logger::debug("Addon {addon}: {action}", ['action' => 'uninstall', 'addon' => $addon]);
134                 DI::config()->delete('addons', $addon);
135
136                 @include_once('addon/' . $addon . '/' . $addon . '.php');
137                 if (function_exists($addon . '_uninstall')) {
138                         $func = $addon . '_uninstall';
139                         $func();
140                 }
141
142                 Hook::delete(['file' => 'addon/' . $addon . '/' . $addon . '.php']);
143
144                 unset(self::$addons[array_search($addon, self::$addons)]);
145         }
146
147         /**
148          * installs an addon.
149          *
150          * @param string $addon name of the addon
151          * @return bool
152          * @throws \Exception
153          */
154         public static function install(string $addon): bool
155         {
156                 $addon = Strings::sanitizeFilePathItem($addon);
157
158                 $addon_file_path = 'addon/' . $addon . '/' . $addon . '.php';
159
160                 // silently fail if addon was removed of if $addon is funky
161                 if (!file_exists($addon_file_path)) {
162                         return false;
163                 }
164
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';
170                         $func(DI::app());
171                 }
172
173                 DI::config()->set('addons', $addon, [
174                         'last_update' => $t,
175                         'admin' => function_exists($addon . '_addon_admin'),
176                 ]);
177
178                 if (!self::isEnabled($addon)) {
179                         self::$addons[] = $addon;
180                 }
181
182                 return true;
183         }
184
185         /**
186          * reload all updated addons
187          *
188          * @return void
189          */
190         public static function reload()
191         {
192                 $addons = DI::config()->get('addons');
193
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
199                                 continue;
200                         }
201
202                         Logger::debug("Addon {addon}: {action}", ['action' => 'reload', 'addon' => $name]);
203
204                         self::uninstall($name);
205                         self::install($name);
206                 }
207         }
208
209         /**
210          * Parse addon comment in search of addon infos.
211          *
212          * like
213          * \code
214          *   * Name: addon
215          *   * Description: An addon which plugs in
216          * . * Version: 1.2.3
217          *   * Author: John <profile url>
218          *   * Author: Jane <email>
219          *   * Maintainer: Jess <email>
220          *   *
221          *   *\endcode
222          * @param string $addon the name of the addon
223          * @return array with the addon information
224          * @throws \Exception
225          */
226         public static function getInfo(string $addon): array
227         {
228                 $addon = Strings::sanitizeFilePathItem($addon);
229
230                 $info = [
231                         'name' => $addon,
232                         'description' => "",
233                         'author' => [],
234                         'maintainer' => [],
235                         'version' => "",
236                         'status' => ""
237                 ];
238
239                 if (!is_file("addon/$addon/$addon.php")) {
240                         return $info;
241                 }
242
243                 DI::profiler()->startRecording('file');
244                 $f = file_get_contents("addon/$addon/$addon.php");
245                 DI::profiler()->stopRecording();
246
247                 $r = preg_match("|/\*.*\*/|msU", $f, $m);
248
249                 if ($r) {
250                         $ll = explode("\n", $m[0]);
251                         foreach ($ll as $l) {
252                                 $l = trim($l, "\t\n\r */");
253                                 if ($l != "") {
254                                         $addon_info = array_map("trim", explode(":", $l, 2));
255                                         if (count($addon_info) < 2) {
256                                                 continue;
257                                         }
258
259                                         list($type, $v) = $addon_info;
260                                         $type = strtolower($type);
261                                         if ($type == "author" || $type == "maintainer") {
262                                                 $r = preg_match("|([^<]+)<([^>]+)>|", $v, $m);
263                                                 if ($r) {
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'];
268                                                                 }
269                                                         }
270                                                         $info[$type][] = ['name' => $m[1], 'link' => $m[2]];
271                                                 } else {
272                                                         $info[$type][] = ['name' => $v];
273                                                 }
274                                         } else {
275                                                 if (array_key_exists($type, $info)) {
276                                                         $info[$type] = $v;
277                                                 }
278                                         }
279                                 }
280                         }
281                 }
282                 return $info;
283         }
284
285         /**
286          * Checks if the provided addon is enabled
287          *
288          * @param string $addon
289          * @return boolean
290          */
291         public static function isEnabled(string $addon): bool
292         {
293                 return in_array($addon, self::$addons);
294         }
295
296         /**
297          * Returns a list of the enabled addon names
298          *
299          * @return array
300          */
301         public static function getEnabledList(): array
302         {
303                 return self::$addons;
304         }
305
306         /**
307          * Returns the list of non-hidden enabled addon names
308          *
309          * @return array
310          * @throws \Exception
311          */
312         public static function getVisibleList(): array
313         {
314                 $visible_addons = [];
315                 $addons = DI::config()->get('addons');
316                 foreach ($addons as $name => $data) {
317                         $visible_addons[] = $name;
318                 }
319
320                 return $visible_addons;
321         }
322 }