]> git.mxchange.org Git - friendica.git/blob - src/Module/Admin/Federation.php
Merge pull request #11208 from annando/federation-systems
[friendica.git] / src / Module / Admin / Federation.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Module\Admin;
23
24 use Friendica\Core\Renderer;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Module\BaseAdmin;
28
29 class Federation extends BaseAdmin
30 {
31         protected function content(array $request = []): string
32         {
33                 parent::content();
34
35                 // get counts on active federation systems this node is knowing
36                 // We list the more common systems by name. The rest is counted as "other"
37                 $systems = [
38                         'friendica'    => ['name' => 'Friendica', 'color' => '#ffc018'], // orange from the logo
39                         'birdsitelive' => ['name' => 'BirdsiteLIVE', 'color' => '#1b6ec2'], // Color from the page
40                         'bookwyrm'     => ['name' => 'BookWyrm', 'color' => '#00d1b2'], // Color from the page
41                         'diaspora'     => ['name' => 'Diaspora', 'color' => '#a1a1a1'], // logo is black and white, makes a gray
42                         'funkwhale'    => ['name' => 'Funkwhale', 'color' => '#4082B4'], // From the homepage
43                         'gnusocial'    => ['name' => 'GNU Social/Statusnet', 'color' => '#a22430'], // dark red from the logo
44                         'hubzilla'     => ['name' => 'Hubzilla/Red Matrix', 'color' => '#43488a'], // blue from the logo
45                         'lemmy'        => ['name' => 'Lemmy', 'color' => '#00c853'], // Green from the page
46                         'mastodon'     => ['name' => 'Mastodon', 'color' => '#1a9df9'], // blue from the Mastodon logo
47                         'misskey'      => ['name' => 'Misskey', 'color' => '#ccfefd'], // Font color of the homepage
48                         'mobilizon'    => ['name' => 'Mobilizon', 'color' => '#ffd599'], // Background color of parts of the homepage
49                         'nextcloud'    => ['name' => 'Nextcloud', 'color' => '#1cafff'], // Logo color
50                         'mistpark'     => ['name' => 'Nomad projects (Mistpark, Osada, Roadhouse, Zap)', 'color' => '#348a4a'], // Green like the Mistpark green
51                         'peertube'     => ['name' => 'Peertube', 'color' => '#ffad5c'], // One of the logo colors
52                         'pixelfed'     => ['name' => 'Pixelfed', 'color' => '#11da47'], // One of the logo colors
53                         'pleroma'      => ['name' => 'Pleroma', 'color' => '#E46F0F'], // Orange from the text that is used on Pleroma instances
54                         'plume'        => ['name' => 'Plume', 'color' => '#7765e3'], // From the homepage
55                         'relay'        => ['name' => 'ActivityPub Relay', 'color' => '#888888'], // Grey like the second color of the ActivityPub logo
56                         'socialhome'   => ['name' => 'SocialHome', 'color' => '#52056b'], // lilac from the Django Image used at the Socialhome homepage
57                         'wordpress'    => ['name' => 'WordPress', 'color' => '#016087'], // Background color of the homepage
58                         'writefreely'  => ['name' => 'WriteFreely', 'color' => '#292929'], // Font color of the homepage
59                         'other'        => ['name' => DI::l10n()->t('Other'), 'color' => '#F1007E'], // ActivityPub main color
60                 ];
61
62                 $platforms = array_keys($systems);
63
64                 $counts = [];
65                 foreach ($platforms as $platform) {
66                         $counts[$platform] = [];
67                 }
68
69                 $total = 0;
70                 $users = 0;
71
72                 $gservers = DBA::p("SELECT COUNT(*) AS `total`, SUM(`registered-users`) AS `users`, `platform`,
73                         ANY_VALUE(`network`) AS `network`, MAX(`version`) AS `version`
74                         FROM `gserver` WHERE NOT `failed` GROUP BY `platform`");
75                 while ($gserver = DBA::fetch($gservers)) {
76                         $total += $gserver['total'];
77                         $users += $gserver['users'];
78
79                         $versionCounts = [];
80                         $versions = DBA::p("SELECT COUNT(*) AS `total`, `version` FROM `gserver`
81                                 WHERE NOT `failed` AND `platform` = ?
82                                 GROUP BY `version` ORDER BY `version`", $gserver['platform']);
83                         while ($version = DBA::fetch($versions)) {
84                                 $version['version'] = str_replace(["\n", "\r", "\t"], " ", $version['version']);
85
86                                 if (in_array($gserver['platform'], ['Red Matrix', 'redmatrix', 'red'])) {
87                                         $version['version'] = 'Red ' . $version['version'];
88                                 } elseif (in_array($gserver['platform'], ['osada', 'mistpark', 'roadhouse', 'zap'])) {
89                                         $version['version'] = $gserver['platform'] . ' ' . $version['version'];
90                                 } elseif (in_array($gserver['platform'], ['activityrelay', 'pub-relay', 'selective-relay', 'aoderelay'])) {
91                                         $version['version'] = $gserver['platform'] . '-' . $version['version'];
92                                 }
93
94                                 $versionCounts[] = $version;
95                         }
96                         DBA::close($versions);
97
98                         $platform = $gserver['platform'] = strtolower($gserver['platform']);
99
100                         if ($platform == 'friendika') {
101                                 $platform = 'friendica';
102                         } elseif (in_array($platform, ['red matrix', 'redmatrix', 'red'])) {
103                                 $platform = 'hubzilla';
104                         } elseif (in_array($platform, ['mistpark', 'osada', 'roadhouse', 'zap'])) {
105                                 $platform = 'mistpark';
106                         } elseif(stristr($platform, 'pleroma')) {
107                                 $platform = 'pleroma';
108                         } elseif(stristr($platform, 'statusnet')) {
109                                 $platform = 'gnusocial';
110                         } elseif(stristr($platform, 'wordpress')) {
111                                 $platform = 'wordpress';
112                         } elseif (in_array($platform, ['activityrelay', 'pub-relay', 'selective-relay', 'aoderelay'])) {
113                                 $platform = 'relay';
114                         } elseif (!in_array($platform, $platforms)) {
115                                 $platform = 'other';
116                         }
117
118                         if ($platform != $gserver['platform']) {
119                                 if ($platform == 'other') {
120                                         $versionCounts = $counts[$platform][1] ?? [];
121                                         $versionCounts[] = ['version' => $gserver['platform'] ?: DI::l10n()->t('unknown'), 'total' => $gserver['total']];
122                                         $gserver['version'] = '';
123                                 } else {
124                                         $versionCounts = array_merge($versionCounts, $counts[$platform][1] ?? []);
125                                 }
126
127                                 $gserver['platform'] = $platform;
128                                 $gserver['total'] += $counts[$platform][0]['total'] ?? 0;
129                                 $gserver['users'] += $counts[$platform][0]['users'] ?? 0;
130                         }
131
132                         if ($platform == 'friendica') {
133                                 $versionCounts = self::reformaFriendicaVersions($versionCounts);
134                         } elseif ($platform == 'pleroma') {
135                                 $versionCounts = self::reformaPleromaVersions($versionCounts);
136                         } elseif ($platform == 'diaspora') {
137                                 $versionCounts = self::reformaDiasporaVersions($versionCounts);
138                         } elseif ($platform == 'relay') {
139                                 $versionCounts = self::reformatRelayVersions($versionCounts);
140                         } elseif (in_array($platform, ['funkwhale', 'mastodon', 'mobilizon', 'misskey'])) {
141                                 $versionCounts = self::removeVersionSuffixes($versionCounts);
142                         }
143
144                         if (!in_array($platform, ['other', 'relay', 'mistpark'])) {
145                                 $versionCounts = self::sortVersion($versionCounts);
146                         } else {
147                                 ksort($versionCounts);
148                         }
149
150                         $gserver['platform'] = $systems[$platform]['name'];
151
152                         $counts[$platform] = [$gserver, $versionCounts, str_replace([' ', '%'], '', $platform), $systems[$platform]['color']];
153                 }
154                 DBA::close($gserver);
155
156                 // some helpful text
157                 $intro = DI::l10n()->t('This page offers you some numbers to the known part of the federated social network your Friendica node is part of. These numbers are not complete but only reflect the part of the network your node is aware of.');
158
159                 // load the template, replace the macros and return the page content
160                 $t = Renderer::getMarkupTemplate('admin/federation.tpl');
161                 return Renderer::replaceMacros($t, [
162                         '$title' => DI::l10n()->t('Administration'),
163                         '$page' => DI::l10n()->t('Federation Statistics'),
164                         '$intro' => $intro,
165                         '$counts' => $counts,
166                         '$version' => FRIENDICA_VERSION,
167                         '$legendtext' => DI::l10n()->t('Currently this node is aware of %d nodes with %d registered users from the following platforms:', $total, $users),
168                 ]);
169         }
170
171         /**
172          * early friendica versions have the format x.x.xxxx where xxxx is the
173          * DB version stamp; those should be operated out and versions be combined
174          *
175          * @param array $versionCounts list of version numbers
176          * @return array with cleaned version numbers
177          */
178         private static function reformaFriendicaVersions(array $versionCounts)
179         {
180                 $newV = [];
181                 $newVv = [];
182                 foreach ($versionCounts as $vv) {
183                         $newVC = $vv['total'];
184                         $newVV = $vv['version'];
185                         $lastDot = strrpos($newVV, '.');
186                         $firstDash = strpos($newVV, '-');
187                         $len = strlen($newVV) - 1;
188                         if (($lastDot == $len - 4) && (!strrpos($newVV, '-rc') == $len - 3) && (!$firstDash == $len - 1)) {
189                                 $newVV = substr($newVV, 0, $lastDot);
190                         }
191                         if (isset($newV[$newVV])) {
192                                 $newV[$newVV] += $newVC;
193                         } else {
194                                 $newV[$newVV] = $newVC;
195                         }
196                 }
197                 foreach ($newV as $key => $value) {
198                         array_push($newVv, ['total' => $value, 'version' => $key]);
199                 }
200                 $versionCounts = $newVv;
201
202                 return $versionCounts;
203         }
204
205         /**
206          * in the DB the Diaspora versions have the format x.x.x.x-xx the last
207          * part (-xx) should be removed to clean up the versions from the "head
208          * commit" information and combined into a single entry for x.x.x.x
209          *
210          * @param array $versionCounts list of version numbers
211          * @return array with cleaned version numbers
212          */
213         private static function reformaDiasporaVersions(array $versionCounts)
214         {
215                 $newV = [];
216                 $newVv = [];
217                 foreach ($versionCounts as $vv) {
218                         $newVC = $vv['total'];
219                         $newVV = $vv['version'];
220                         $posDash = strpos($newVV, '-');
221                         if ($posDash) {
222                                 $newVV = substr($newVV, 0, $posDash);
223                         }
224                         if (isset($newV[$newVV])) {
225                                 $newV[$newVV] += $newVC;
226                         } else {
227                                 $newV[$newVV] = $newVC;
228                         }
229                 }
230                 foreach ($newV as $key => $value) {
231                         array_push($newVv, ['total' => $value, 'version' => $key]);
232                 }
233                 $versionCounts = $newVv;
234
235                 return $versionCounts;
236         }
237
238         /**
239          * Clean up Pleroma version numbers
240          *
241          * @param array $versionCounts list of version numbers
242          * @return array with cleaned version numbers
243          */
244         private static function reformaPleromaVersions(array $versionCounts)
245         {
246                 $compacted = [];
247                 foreach ($versionCounts as $key => $value) {
248                         $version = $versionCounts[$key]['version'];
249                         $parts = explode(' ', trim($version));
250                         do {
251                                 $part = array_pop($parts);
252                         } while (!empty($parts) && ((strlen($part) >= 40) || (strlen($part) <= 3)));
253                         // only take the x.x.x part of the version, not the "release" after the dash
254                         if (!empty($part) && strpos($part, '-')) {
255                                 $part = explode('-', $part)[0];
256                         }
257                         if (!empty($part)) {
258                                 if (empty($compacted[$part])) {
259                                         $compacted[$part] = $versionCounts[$key]['total'];
260                                 } else {
261                                         $compacted[$part] += $versionCounts[$key]['total'];
262                                 }
263                         }
264                 }
265
266                 $versionCounts = [];
267                 foreach ($compacted as $version => $pl_total) {
268                         $versionCounts[] = ['version' => $version, 'total' => $pl_total];
269                 }
270
271                 return $versionCounts;
272         }
273
274         /**
275          * Clean up version numbers
276          *
277          * @param array $versionCounts list of version numbers
278          * @return array with cleaned version numbers
279          */
280         private static function removeVersionSuffixes(array $versionCounts)
281         {
282                 $compacted = [];
283                 foreach ($versionCounts as $key => $value) {
284                         $version = $versionCounts[$key]['version'];
285
286                         foreach ([' ', '+', '-', '#', '_', '~'] as $delimiter) {
287                                 $parts = explode($delimiter, trim($version));
288                                 $version = array_shift($parts);
289                         }
290
291                         if (empty($compacted[$version])) {
292                                 $compacted[$version] = $versionCounts[$key]['total'];
293                         } else {
294                                 $compacted[$version] += $versionCounts[$key]['total'];
295                         }
296                 }
297
298                 $versionCounts = [];
299                 foreach ($compacted as $version => $pl_total) {
300                         $versionCounts[] = ['version' => $version, 'total' => $pl_total];
301                 }
302
303                 return $versionCounts;
304         }
305
306         /**
307          * Clean up relay version numbers
308          *
309          * @param array $versionCounts list of version numbers
310          * @return array with cleaned version numbers
311          */
312         private static function reformatRelayVersions(array $versionCounts)
313         {
314                 $compacted = [];
315                 foreach ($versionCounts as $key => $value) {
316                         $version = $versionCounts[$key]['version'];
317
318                         $parts = explode(' ', trim($version));
319                         $version = array_shift($parts);
320
321                         if (empty($compacted[$version])) {
322                                 $compacted[$version] = $versionCounts[$key]['total'];
323                         } else {
324                                 $compacted[$version] += $versionCounts[$key]['total'];
325                         }
326                 }
327
328                 $versionCounts = [];
329                 foreach ($compacted as $version => $pl_total) {
330                         $versionCounts[] = ['version' => $version, 'total' => $pl_total];
331                 }
332
333                 return $versionCounts;
334         }
335
336         /**
337          * Reformat, sort and compact version numbers
338          *
339          * @param array $versionCounts list of version numbers
340          * @return array with reformatted version numbers
341          */
342         private static function sortVersion(array $versionCounts)
343         {
344                 //
345                 // clean up version numbers
346                 //
347                 // some platforms do not provide version information, add a unkown there
348                 // to the version string for the displayed list.
349                 foreach ($versionCounts as $key => $value) {
350                         if ($versionCounts[$key]['version'] == '') {
351                                 $versionCounts[$key] = ['total' => $versionCounts[$key]['total'], 'version' => DI::l10n()->t('unknown')];
352                         }
353                 }
354
355                 // Assure that the versions are sorted correctly
356                 $v2 = [];
357                 $versions = [];
358                 foreach ($versionCounts as $vv) {
359                         $version = trim(strip_tags($vv["version"]));
360                         $v2[$version] = $vv;
361                         $versions[] = $version;
362                 }
363
364                 usort($versions, 'version_compare');
365
366                 $versionCounts = [];
367                 foreach ($versions as $version) {
368                         $versionCounts[] = $v2[$version];
369                 }
370
371                 return $versionCounts;
372         }
373 }