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