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