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