]> git.mxchange.org Git - friendica.git/blob - src/Core/Addon.php
be3e70a540415754c5d912d7f4845ec5fd01821d
[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                 foreach ($addons as $name => $data) {
89                         if (empty($data['admin'])) {
90                                 continue;
91                         }
92
93                         $addons_admin[$name] = [
94                                 'url' => 'admin/addons/' . $name,
95                                 'name' => $name,
96                                 'class' => 'addon'
97                         ];
98                 }
99
100                 return $addons_admin;
101         }
102
103
104         /**
105          * Synchronize addons:
106          *
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.
114          *
115          */
116         public static function loadAddons()
117         {
118                 self::$addons = array_keys(DI::config()->get('addons') ?? []);
119         }
120
121         /**
122          * uninstalls an addon.
123          *
124          * @param string $addon name of the addon
125          * @return void
126          * @throws \Exception
127          */
128         public static function uninstall(string $addon)
129         {
130                 $addon = Strings::sanitizeFilePathItem($addon);
131
132                 Logger::debug("Addon {addon}: {action}", ['action' => 'uninstall', 'addon' => $addon]);
133                 DI::config()->delete('addons', $addon);
134
135                 @include_once('addon/' . $addon . '/' . $addon . '.php');
136                 if (function_exists($addon . '_uninstall')) {
137                         $func = $addon . '_uninstall';
138                         $func();
139                 }
140
141                 Hook::delete(['file' => 'addon/' . $addon . '/' . $addon . '.php']);
142
143                 unset(self::$addons[array_search($addon, self::$addons)]);
144         }
145
146         /**
147          * installs an addon.
148          *
149          * @param string $addon name of the addon
150          * @return bool
151          * @throws \Exception
152          */
153         public static function install(string $addon): bool
154         {
155                 $addon = Strings::sanitizeFilePathItem($addon);
156
157                 $addon_file_path = 'addon/' . $addon . '/' . $addon . '.php';
158
159                 // silently fail if addon was removed of if $addon is funky
160                 if (!file_exists($addon_file_path)) {
161                         return false;
162                 }
163
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';
169                         $func(DI::app());
170                 }
171
172                 DI::config()->set('addons', $addon,[
173                         'last_update' => $t,
174                         'admin' => function_exists($addon . '_addon_admin'),
175                 ]);
176
177                 if (!self::isEnabled($addon)) {
178                         self::$addons[] = $addon;
179                 }
180
181                 return true;
182         }
183
184         /**
185          * reload all updated addons
186          *
187          * @return void
188          */
189         public static function reload()
190         {
191                 $addons = DI::config()->get('addons');
192
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
198                                 continue;
199                         }
200
201                         Logger::debug("Addon {addon}: {action}", ['action' => 'reload', 'addon' => $name]);
202
203                         self::uninstall($name);
204                         self::install($name);
205                 }
206         }
207
208         /**
209          * Parse addon comment in search of addon infos.
210          *
211          * like
212          * \code
213          *   * Name: addon
214          *   * Description: An addon which plugs in
215          * . * Version: 1.2.3
216          *   * Author: John <profile url>
217          *   * Author: Jane <email>
218          *   * Maintainer: Jess <email>
219          *   *
220          *   *\endcode
221          * @param string $addon the name of the addon
222          * @return array with the addon information
223          * @throws \Exception
224          */
225         public static function getInfo(string $addon): array
226         {
227                 $addon = Strings::sanitizeFilePathItem($addon);
228
229                 $info = [
230                         'name' => $addon,
231                         'description' => "",
232                         'author' => [],
233                         'maintainer' => [],
234                         'version' => "",
235                         'status' => ""
236                 ];
237
238                 if (!is_file("addon/$addon/$addon.php")) {
239                         return $info;
240                 }
241
242                 DI::profiler()->startRecording('file');
243                 $f = file_get_contents("addon/$addon/$addon.php");
244                 DI::profiler()->stopRecording();
245
246                 $r = preg_match("|/\*.*\*/|msU", $f, $m);
247
248                 if ($r) {
249                         $ll = explode("\n", $m[0]);
250                         foreach ($ll as $l) {
251                                 $l = trim($l, "\t\n\r */");
252                                 if ($l != "") {
253                                         $addon_info = array_map("trim", explode(":", $l, 2));
254                                         if (count($addon_info) < 2) {
255                                                 continue;
256                                         }
257
258                                         list($type, $v) = $addon_info;
259                                         $type = strtolower($type);
260                                         if ($type == "author" || $type == "maintainer") {
261                                                 $r = preg_match("|([^<]+)<([^>]+)>|", $v, $m);
262                                                 if ($r) {
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'];
267                                                                 }
268                                                         }
269                                                         $info[$type][] = ['name' => $m[1], 'link' => $m[2]];
270                                                 } else {
271                                                         $info[$type][] = ['name' => $v];
272                                                 }
273                                         } else {
274                                                 if (array_key_exists($type, $info)) {
275                                                         $info[$type] = $v;
276                                                 }
277                                         }
278                                 }
279                         }
280                 }
281                 return $info;
282         }
283
284         /**
285          * Checks if the provided addon is enabled
286          *
287          * @param string $addon
288          * @return boolean
289          */
290         public static function isEnabled(string $addon): bool
291         {
292                 return in_array($addon, self::$addons);
293         }
294
295         /**
296          * Returns a list of the enabled addon names
297          *
298          * @return array
299          */
300         public static function getEnabledList(): array
301         {
302                 return self::$addons;
303         }
304
305         /**
306          * Returns the list of non-hidden enabled addon names
307          *
308          * @return array
309          * @throws \Exception
310          */
311         public static function getVisibleList(): array
312         {
313                 $visible_addons = [];
314                 $addons = DI::config()->get('addons');
315                 foreach ($addons as $name => $data) {
316                         $visible_addons[] = $name;
317                 }
318
319                 return $visible_addons;
320         }
321 }