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