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