3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Admin;
24 use Friendica\Core\Renderer;
25 use Friendica\Database\DBA;
27 use Friendica\Module\BaseAdmin;
29 class Federation extends BaseAdmin
31 protected function content(array $request = []): string
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"
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
62 $platforms = array_keys($systems);
65 foreach ($platforms as $platform) {
66 $counts[$platform] = [];
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'];
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']);
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'];
94 $versionCounts[] = $version;
96 DBA::close($versions);
98 $platform = $gserver['platform'] = strtolower($gserver['platform']);
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'])) {
114 } elseif (!in_array($platform, $platforms)) {
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'] = '';
124 $versionCounts = array_merge($versionCounts, $counts[$platform][1] ?? []);
127 $gserver['platform'] = $platform;
128 $gserver['total'] += $counts[$platform][0]['total'] ?? 0;
129 $gserver['users'] += $counts[$platform][0]['users'] ?? 0;
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);
144 if (!in_array($platform, ['other', 'relay', 'mistpark'])) {
145 $versionCounts = self::sortVersion($versionCounts);
147 ksort($versionCounts);
150 $gserver['platform'] = $systems[$platform]['name'];
152 $counts[$platform] = [$gserver, $versionCounts, str_replace([' ', '%'], '', $platform), $systems[$platform]['color']];
154 DBA::close($gserver);
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.');
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'),
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),
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
175 * @param array $versionCounts list of version numbers
176 * @return array with cleaned version numbers
178 private static function reformaFriendicaVersions(array $versionCounts)
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);
191 if (isset($newV[$newVV])) {
192 $newV[$newVV] += $newVC;
194 $newV[$newVV] = $newVC;
197 foreach ($newV as $key => $value) {
198 array_push($newVv, ['total' => $value, 'version' => $key]);
200 $versionCounts = $newVv;
202 return $versionCounts;
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
210 * @param array $versionCounts list of version numbers
211 * @return array with cleaned version numbers
213 private static function reformaDiasporaVersions(array $versionCounts)
217 foreach ($versionCounts as $vv) {
218 $newVC = $vv['total'];
219 $newVV = $vv['version'];
220 $posDash = strpos($newVV, '-');
222 $newVV = substr($newVV, 0, $posDash);
224 if (isset($newV[$newVV])) {
225 $newV[$newVV] += $newVC;
227 $newV[$newVV] = $newVC;
230 foreach ($newV as $key => $value) {
231 array_push($newVv, ['total' => $value, 'version' => $key]);
233 $versionCounts = $newVv;
235 return $versionCounts;
239 * Clean up Pleroma version numbers
241 * @param array $versionCounts list of version numbers
242 * @return array with cleaned version numbers
244 private static function reformaPleromaVersions(array $versionCounts)
247 foreach ($versionCounts as $key => $value) {
248 $version = $versionCounts[$key]['version'];
249 $parts = explode(' ', trim($version));
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];
258 if (empty($compacted[$part])) {
259 $compacted[$part] = $versionCounts[$key]['total'];
261 $compacted[$part] += $versionCounts[$key]['total'];
267 foreach ($compacted as $version => $pl_total) {
268 $versionCounts[] = ['version' => $version, 'total' => $pl_total];
271 return $versionCounts;
275 * Clean up version numbers
277 * @param array $versionCounts list of version numbers
278 * @return array with cleaned version numbers
280 private static function removeVersionSuffixes(array $versionCounts)
283 foreach ($versionCounts as $key => $value) {
284 $version = $versionCounts[$key]['version'];
286 foreach ([' ', '+', '-', '#', '_', '~'] as $delimiter) {
287 $parts = explode($delimiter, trim($version));
288 $version = array_shift($parts);
291 if (empty($compacted[$version])) {
292 $compacted[$version] = $versionCounts[$key]['total'];
294 $compacted[$version] += $versionCounts[$key]['total'];
299 foreach ($compacted as $version => $pl_total) {
300 $versionCounts[] = ['version' => $version, 'total' => $pl_total];
303 return $versionCounts;
307 * Clean up relay version numbers
309 * @param array $versionCounts list of version numbers
310 * @return array with cleaned version numbers
312 private static function reformatRelayVersions(array $versionCounts)
315 foreach ($versionCounts as $key => $value) {
316 $version = $versionCounts[$key]['version'];
318 $parts = explode(' ', trim($version));
319 $version = array_shift($parts);
321 if (empty($compacted[$version])) {
322 $compacted[$version] = $versionCounts[$key]['total'];
324 $compacted[$version] += $versionCounts[$key]['total'];
329 foreach ($compacted as $version => $pl_total) {
330 $versionCounts[] = ['version' => $version, 'total' => $pl_total];
333 return $versionCounts;
337 * Reformat, sort and compact version numbers
339 * @param array $versionCounts list of version numbers
340 * @return array with reformatted version numbers
342 private static function sortVersion(array $versionCounts)
345 // clean up version numbers
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')];
355 // Assure that the versions are sorted correctly
358 foreach ($versionCounts as $vv) {
359 $version = trim(strip_tags($vv["version"]));
361 $versions[] = $version;
364 usort($versions, 'version_compare');
367 foreach ($versions as $version) {
368 $versionCounts[] = $v2[$version];
371 return $versionCounts;