3 * @copyright Copyright (C) 2010-2021, 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 public static function content(array $parameters = [])
33 parent::content($parameters);
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 '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
59 $platforms = array_keys($systems);
62 foreach ($platforms as $platform) {
63 $counts[$platform] = [];
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'];
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']);
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'];
91 $versionCounts[] = $version;
93 DBA::close($versions);
95 $platform = $gserver['platform'] = strtolower($gserver['platform']);
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'])) {
111 } elseif (!in_array($platform, $platforms)) {
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'] = '';
121 $versionCounts = array_merge($versionCounts, $counts[$platform][1] ?? []);
124 $gserver['platform'] = $platform;
125 $gserver['total'] += $counts[$platform][0]['total'] ?? 0;
126 $gserver['users'] += $counts[$platform][0]['users'] ?? 0;
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);
141 $versionCounts = self::sortVersion($versionCounts);
143 $gserver['platform'] = $systems[$platform]['name'];
145 $counts[$platform] = [$gserver, $versionCounts, str_replace([' ', '%'], '', $platform), $systems[$platform]['color']];
147 DBA::close($gserver);
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.');
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'),
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),
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
168 * @param array $versionCounts list of version numbers
169 * @return array with cleaned version numbers
171 private static function reformaFriendicaVersions(array $versionCounts)
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);
184 if (isset($newV[$newVV])) {
185 $newV[$newVV] += $newVC;
187 $newV[$newVV] = $newVC;
190 foreach ($newV as $key => $value) {
191 array_push($newVv, ['total' => $value, 'version' => $key]);
193 $versionCounts = $newVv;
195 return $versionCounts;
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
203 * @param array $versionCounts list of version numbers
204 * @return array with cleaned version numbers
206 private static function reformaDiasporaVersions(array $versionCounts)
210 foreach ($versionCounts as $vv) {
211 $newVC = $vv['total'];
212 $newVV = $vv['version'];
213 $posDash = strpos($newVV, '-');
215 $newVV = substr($newVV, 0, $posDash);
217 if (isset($newV[$newVV])) {
218 $newV[$newVV] += $newVC;
220 $newV[$newVV] = $newVC;
223 foreach ($newV as $key => $value) {
224 array_push($newVv, ['total' => $value, 'version' => $key]);
226 $versionCounts = $newVv;
228 return $versionCounts;
232 * Clean up Pleroma version numbers
234 * @param array $versionCounts list of version numbers
235 * @return array with cleaned version numbers
237 private static function reformaPleromaVersions(array $versionCounts)
240 foreach ($versionCounts as $key => $value) {
241 $version = $versionCounts[$key]['version'];
242 $parts = explode(' ', trim($version));
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];
251 if (empty($compacted[$part])) {
252 $compacted[$part] = $versionCounts[$key]['total'];
254 $compacted[$part] += $versionCounts[$key]['total'];
260 foreach ($compacted as $version => $pl_total) {
261 $versionCounts[] = ['version' => $version, 'total' => $pl_total];
264 return $versionCounts;
268 * Clean up version numbers
270 * @param array $versionCounts list of version numbers
271 * @return array with cleaned version numbers
273 private static function removeVersionSuffixes(array $versionCounts)
276 foreach ($versionCounts as $key => $value) {
277 $version = $versionCounts[$key]['version'];
279 foreach ([' ', '+', '-', '#', '_', '~'] as $delimiter) {
280 $parts = explode($delimiter, trim($version));
281 $version = array_shift($parts);
284 if (empty($compacted[$version])) {
285 $compacted[$version] = $versionCounts[$key]['total'];
287 $compacted[$version] += $versionCounts[$key]['total'];
292 foreach ($compacted as $version => $pl_total) {
293 $versionCounts[] = ['version' => $version, 'total' => $pl_total];
296 return $versionCounts;
300 * Clean up relay version numbers
302 * @param array $versionCounts list of version numbers
303 * @return array with cleaned version numbers
305 private static function reformaRelayVersions(array $versionCounts)
308 foreach ($versionCounts as $key => $value) {
309 $version = $versionCounts[$key]['version'];
311 $parts = explode(' ', trim($version));
312 $version = array_shift($parts);
314 if (empty($compacted[$version])) {
315 $compacted[$version] = $versionCounts[$key]['total'];
317 $compacted[$version] += $versionCounts[$key]['total'];
322 foreach ($compacted as $version => $pl_total) {
323 $versionCounts[] = ['version' => $version, 'total' => $pl_total];
326 return $versionCounts;
330 * Reformat, sort and compact version numbers
332 * @param array $versionCounts list of version numbers
333 * @return array with reformatted version numbers
335 private static function sortVersion(array $versionCounts)
338 // clean up version numbers
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')];
348 // Assure that the versions are sorted correctly
351 foreach ($versionCounts as $vv) {
352 $version = trim(strip_tags($vv["version"]));
354 $versions[] = $version;
357 usort($versions, 'version_compare');
360 foreach ($versions as $version) {
361 $versionCounts[] = $v2[$version];
364 return $versionCounts;