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