]> git.mxchange.org Git - friendica.git/blob - src/Module/Admin/Federation.php
Merge pull request #8957 from annando/server-peers
[friendica.git] / src / Module / Admin / Federation.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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                         'nextcloud'   => ['name' => 'Nextcloud', 'color' => '#1cafff'], // Logo color
46                         'peertube'    => ['name' => 'Peertube', 'color' => '#ffad5c'], // One of the logo colors
47                         'pixelfed'    => ['name' => 'Pixelfed', 'color' => '#11da47'], // One of the logo colors
48                         'pleroma'     => ['name' => 'Pleroma', 'color' => '#E46F0F'], // Orange from the text that is used on Pleroma instances
49                         'plume'       => ['name' => 'Plume', 'color' => '#7765e3'], // From the homepage
50                         'socialhome'  => ['name' => 'SocialHome', 'color' => '#52056b'], // lilac from the Django Image used at the Socialhome homepage
51                         'wordpress'   => ['name' => 'WordPress', 'color' => '#016087'], // Background color of the homepage
52                         'writefreely' => ['name' => 'WriteFreely', 'color' => '#292929'], // Font color of the homepage
53                         'other'       => ['name' => DI::l10n()->t('Other'), 'color' => '#F1007E'], // ActivityPub main color
54                 ];
55
56                 $platforms = array_keys($systems);
57
58                 $counts = [];
59                 foreach ($platforms as $platform) {
60                         $counts[$platform] = [];
61                 }
62
63                 $total = 0;
64                 $users = 0;
65
66                 $gservers = DBA::p("SELECT COUNT(*) AS `total`, SUM(`registered-users`) AS `users`, `platform`,
67                         ANY_VALUE(`network`) AS `network`, MAX(`version`) AS `version`
68                         FROM `gserver` WHERE NOT `failed` GROUP BY `platform`");
69                 while ($gserver = DBA::fetch($gservers)) {
70                         $total += $gserver['total'];
71                         $users += $gserver['users'];
72
73                         $versionCounts = [];
74                         $versions = DBA::p("SELECT COUNT(*) AS `total`, `version` FROM `gserver`
75                                 WHERE NOT `failed` AND `platform` = ?
76                                 GROUP BY `version` ORDER BY `version`", $gserver['platform']);
77                         while ($version = DBA::fetch($versions)) {
78                                 $version['version'] = str_replace(["\n", "\r", "\t"], " ", $version['version']);
79
80                                 if (in_array($gserver['platform'], ['Red Matrix', 'redmatrix', 'red'])) {
81                                         $version['version'] = 'Red ' . $version['version'];
82                                 }
83
84                                 $versionCounts[] = $version;
85                         }
86                         DBA::close($versions);
87
88                         $platform = $gserver['platform'] = strtolower($gserver['platform']);
89
90                         if ($platform == 'friendika') {
91                                 $platform = 'friendica';
92                         } elseif (in_array($platform, ['red matrix', 'redmatrix', 'red'])) {
93                                 $platform = 'hubzilla';
94                         } elseif(stristr($platform, 'pleroma')) {
95                                 $platform = 'pleroma';
96                         } elseif(stristr($platform, 'statusnet')) {
97                                 $platform = 'gnusocial';
98                         } elseif(stristr($platform, 'wordpress')) {
99                                 $platform = 'wordpress';
100                         } elseif (!in_array($platform, $platforms)) {
101                                 $platform = 'other';
102                         }
103
104                         if ($platform != $gserver['platform']) {
105                                 if ($platform == 'other') {
106                                         $versionCounts = $counts[$platform][1] ?? [];
107                                         $versionCounts[] = ['version' => $gserver['platform'] ?: DI::l10n()->t('unknown'), 'total' => $gserver['total']];
108                                         $gserver['version'] = '';
109                                 } else {
110                                         $versionCounts = array_merge($versionCounts, $counts[$platform][1] ?? []);
111                                 }
112
113                                 $gserver['platform'] = $platform;
114                                 $gserver['total'] += $counts[$platform][0]['total'] ?? 0;
115                                 $gserver['users'] += $counts[$platform][0]['users'] ?? 0;
116                         }
117
118                         if ($platform == 'friendica') {
119                                 $versionCounts = self::reformaFriendicaVersions($versionCounts);
120                         } elseif ($platform == 'pleroma') {
121                                 $versionCounts = self::reformaPleromaVersions($versionCounts);
122                         } elseif ($platform == 'diaspora') {
123                                 $versionCounts = self::reformaDiasporaVersions($versionCounts);
124                         }
125
126                         $versionCounts = self::sortVersion($versionCounts);
127
128                         $gserver['platform'] = $systems[$platform]['name'];
129
130                         $counts[$platform] = [$gserver, $versionCounts, str_replace([' ', '%'], '', $platform), $systems[$platform]['color']];
131                 }
132                 DBA::close($gserver);
133
134                 // some helpful text
135                 $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.');
136                 $hint = DI::l10n()->t('The <em>Auto Discovered Contact Directory</em> feature is not enabled, it will improve the data displayed here.');
137
138                 // load the template, replace the macros and return the page content
139                 $t = Renderer::getMarkupTemplate('admin/federation.tpl');
140                 return Renderer::replaceMacros($t, [
141                         '$title' => DI::l10n()->t('Administration'),
142                         '$page' => DI::l10n()->t('Federation Statistics'),
143                         '$intro' => $intro,
144                         '$hint' => $hint,
145                         '$autoactive' => DI::config()->get('system', 'poco_completion'),
146                         '$counts' => $counts,
147                         '$version' => FRIENDICA_VERSION,
148                         '$legendtext' => DI::l10n()->t('Currently this node is aware of %d nodes with %d registered users from the following platforms:', $total, $users),
149                 ]);
150         }
151
152         /**
153          * early friendica versions have the format x.x.xxxx where xxxx is the
154          * DB version stamp; those should be operated out and versions be combined
155          *
156          * @param array $versionCounts list of version numbers
157          * @return array with cleaned version numbers
158          */
159         private static function reformaFriendicaVersions(array $versionCounts)
160         {
161                 $newV = [];
162                 $newVv = [];
163                 foreach ($versionCounts as $vv) {
164                         $newVC = $vv['total'];
165                         $newVV = $vv['version'];
166                         $lastDot = strrpos($newVV, '.');
167                         $len = strlen($newVV) - 1;
168                         if (($lastDot == $len - 4) && (!strrpos($newVV, '-rc') == $len - 3)) {
169                                 $newVV = substr($newVV, 0, $lastDot);
170                         }
171                         if (isset($newV[$newVV])) {
172                                 $newV[$newVV] += $newVC;
173                         } else {
174                                 $newV[$newVV] = $newVC;
175                         }
176                 }
177                 foreach ($newV as $key => $value) {
178                         array_push($newVv, ['total' => $value, 'version' => $key]);
179                 }
180                 $versionCounts = $newVv;
181
182                 return $versionCounts;
183         }
184
185         /**
186          * in the DB the Diaspora versions have the format x.x.x.x-xx the last
187          * part (-xx) should be removed to clean up the versions from the "head
188          * commit" information and combined into a single entry for x.x.x.x
189          *
190          * @param array $versionCounts list of version numbers
191          * @return array with cleaned version numbers
192          */
193         private static function reformaDiasporaVersions(array $versionCounts)
194         {
195                 $newV = [];
196                 $newVv = [];
197                 foreach ($versionCounts as $vv) {
198                         $newVC = $vv['total'];
199                         $newVV = $vv['version'];
200                         $posDash = strpos($newVV, '-');
201                         if ($posDash) {
202                                 $newVV = substr($newVV, 0, $posDash);
203                         }
204                         if (isset($newV[$newVV])) {
205                                 $newV[$newVV] += $newVC;
206                         } else {
207                                 $newV[$newVV] = $newVC;
208                         }
209                 }
210                 foreach ($newV as $key => $value) {
211                         array_push($newVv, ['total' => $value, 'version' => $key]);
212                 }
213                 $versionCounts = $newVv;
214
215                 return $versionCounts;
216         }
217
218         /**
219          * Clean up Pleroma version numbers
220          *
221          * @param array $versionCounts list of version numbers
222          * @return array with cleaned version numbers
223          */
224         private static function reformaPleromaVersions(array $versionCounts)
225         {
226                 $compacted = [];
227                 foreach ($versionCounts as $key => $value) {
228                         $version = $versionCounts[$key]['version'];
229                         $parts = explode(' ', trim($version));
230                         do {
231                                 $part = array_pop($parts);
232                         } while (!empty($parts) && ((strlen($part) >= 40) || (strlen($part) <= 3)));
233                         // only take the x.x.x part of the version, not the "release" after the dash
234                         if (!empty($part) && strpos($part, '-')) {
235                                 $part = explode('-', $part)[0];
236                         }
237                         if (!empty($part)) {
238                                 if (empty($compacted[$part])) {
239                                         $compacted[$part] = $versionCounts[$key]['total'];
240                                 } else {
241                                         $compacted[$part] += $versionCounts[$key]['total'];
242                                 }
243                         }
244                 }
245
246                 $versionCounts = [];
247                 foreach ($compacted as $version => $pl_total) {
248                         $versionCounts[] = ['version' => $version, 'total' => $pl_total];
249                 }
250
251                 return $versionCounts;
252         }
253
254         /**
255          * Reformat, sort and compact version numbers
256          *
257          * @param array $versionCounts list of version numbers
258          * @return array with reformatted version numbers
259          */
260         private static function sortVersion(array $versionCounts)
261         {
262                 //
263                 // clean up version numbers
264                 //
265                 // some platforms do not provide version information, add a unkown there
266                 // to the version string for the displayed list.
267                 foreach ($versionCounts as $key => $value) {
268                         if ($versionCounts[$key]['version'] == '') {
269                                 $versionCounts[$key] = ['total' => $versionCounts[$key]['total'], 'version' => DI::l10n()->t('unknown')];
270                         }
271                 }
272
273                 // Assure that the versions are sorted correctly
274                 $v2 = [];
275                 $versions = [];
276                 foreach ($versionCounts as $vv) {
277                         $version = trim(strip_tags($vv["version"]));
278                         $v2[$version] = $vv;
279                         $versions[] = $version;
280                 }
281
282                 usort($versions, 'version_compare');
283
284                 $versionCounts = [];
285                 foreach ($versions as $version) {
286                         $versionCounts[] = $v2[$version];
287                 }
288
289                 return $versionCounts;
290         }
291 }