]> git.mxchange.org Git - friendica.git/blob - src/Module/NodeInfo.php
Add System::htmlUpdateExit() method to refactor update modules
[friendica.git] / src / Module / NodeInfo.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;
23
24 use Friendica\BaseModule;
25 use Friendica\Core\Addon;
26 use Friendica\DI;
27 use stdClass;
28
29 /**
30  * Standardized way of exposing metadata about a server running one of the distributed social networks.
31  * @see https://github.com/jhass/nodeinfo/blob/master/PROTOCOL.md
32  */
33 class NodeInfo extends BaseModule
34 {
35         public static function rawContent(array $parameters = [])
36         {
37                 if ($parameters['version'] == '1.0') {
38                         self::printNodeInfo1();
39                 } elseif ($parameters['version'] == '2.0') {
40                         self::printNodeInfo2();
41                 } else {
42                         throw new \Friendica\Network\HTTPException\NotFoundException();
43                 }
44         }
45
46         /**
47          * Return the supported services
48          *
49          * @return Object with supported services
50         */
51         private static function getUsage()
52         {
53                 $config = DI::config();
54
55                 $usage = new stdClass();
56
57                 if (!empty($config->get('system', 'nodeinfo'))) {
58                         $usage->users = [
59                                 'total'          => intval($config->get('nodeinfo', 'total_users')),
60                                 'activeHalfyear' => intval($config->get('nodeinfo', 'active_users_halfyear')),
61                                 'activeMonth'    => intval($config->get('nodeinfo', 'active_users_monthly'))
62                         ];
63                         $usage->localPosts = intval($config->get('nodeinfo', 'local_posts'));
64                         $usage->localComments = intval($config->get('nodeinfo', 'local_comments'));
65                 }
66
67                 return $usage;
68         }
69
70         /**
71          * Return the supported services
72          *
73          * @return array with supported services
74         */
75         private static function getServices()
76         {
77                 $services = [
78                         'inbound'  => [],
79                         'outbound' => [],
80                 ];
81
82                 if (Addon::isEnabled('blogger')) {
83                         $services['outbound'][] = 'blogger';
84                 }
85                 if (Addon::isEnabled('dwpost')) {
86                         $services['outbound'][] = 'dreamwidth';
87                 }
88                 if (Addon::isEnabled('statusnet')) {
89                         $services['inbound'][] = 'gnusocial';
90                         $services['outbound'][] = 'gnusocial';
91                 }
92                 if (Addon::isEnabled('ijpost')) {
93                         $services['outbound'][] = 'insanejournal';
94                 }
95                 if (Addon::isEnabled('libertree')) {
96                         $services['outbound'][] = 'libertree';
97                 }
98                 if (Addon::isEnabled('buffer')) {
99                         $services['outbound'][] = 'linkedin';
100                 }
101                 if (Addon::isEnabled('ljpost')) {
102                         $services['outbound'][] = 'livejournal';
103                 }
104                 if (Addon::isEnabled('buffer')) {
105                         $services['outbound'][] = 'pinterest';
106                 }
107                 if (Addon::isEnabled('posterous')) {
108                         $services['outbound'][] = 'posterous';
109                 }
110                 if (Addon::isEnabled('pumpio')) {
111                         $services['inbound'][] = 'pumpio';
112                         $services['outbound'][] = 'pumpio';
113                 }
114
115                 $services['outbound'][] = 'smtp';
116
117                 if (Addon::isEnabled('tumblr')) {
118                         $services['outbound'][] = 'tumblr';
119                 }
120                 if (Addon::isEnabled('twitter') || Addon::isEnabled('buffer')) {
121                         $services['outbound'][] = 'twitter';
122                 }
123                 if (Addon::isEnabled('wppost')) {
124                         $services['outbound'][] = 'wordpress';
125                 }
126
127                 return $services;
128         }
129
130         /**
131          * Print the nodeinfo version 1
132          */
133         private static function printNodeInfo1()
134         {
135                 $config = DI::config();
136
137                 $nodeinfo = [
138                         'version'           => '1.0',
139                         'software'          => [
140                                 'name'    => 'friendica',
141                                 'version' => FRIENDICA_VERSION . '-' . DB_UPDATE_VERSION,
142                         ],
143                         'protocols'         => [
144                                 'inbound'  => [
145                                         'friendica'
146                                 ],
147                                 'outbound' => [
148                                         'friendica'
149                                 ],
150                         ],
151                         'services'          => [],
152                         'usage'             => [],
153                         'openRegistrations' => intval($config->get('config', 'register_policy')) !== Register::CLOSED,
154                         'metadata'          => [
155                                 'nodeName' => $config->get('config', 'sitename'),
156                         ],
157                 ];
158
159                 if (!empty($config->get('system', 'diaspora_enabled'))) {
160                         $nodeinfo['protocols']['inbound'][] = 'diaspora';
161                         $nodeinfo['protocols']['outbound'][] = 'diaspora';
162                 }
163
164                 if (empty($config->get('system', 'ostatus_disabled'))) {
165                         $nodeinfo['protocols']['inbound'][] = 'gnusocial';
166                         $nodeinfo['protocols']['outbound'][] = 'gnusocial';
167                 }
168
169                 $nodeinfo['usage'] = self::getUsage();
170
171                 $nodeinfo['services'] = self::getServices();
172
173                 $nodeinfo['metadata']['protocols'] = $nodeinfo['protocols'];
174                 $nodeinfo['metadata']['protocols']['outbound'][] = 'atom1.0';
175                 $nodeinfo['metadata']['protocols']['inbound'][] = 'atom1.0';
176                 $nodeinfo['metadata']['protocols']['inbound'][] = 'rss2.0';
177
178                 $nodeinfo['metadata']['services'] = $nodeinfo['services'];
179
180                 if (Addon::isEnabled('twitter')) {
181                         $nodeinfo['metadata']['services']['inbound'][] = 'twitter';
182                 }
183
184                 $nodeinfo['metadata']['explicitContent'] = $config->get('system', 'explicit_content', false) == true;
185
186                 header('Content-type: application/json; charset=utf-8');
187                 echo json_encode($nodeinfo, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
188                 exit;
189         }
190
191         /**
192          * Print the nodeinfo version 2
193          */
194         private static function printNodeInfo2()
195         {
196                 $config = DI::config();
197
198                 $imap = (function_exists('imap_open') && !$config->get('system', 'imap_disabled') && !$config->get('system', 'dfrn_only'));
199
200                 $nodeinfo = [
201                         'version'           => '2.0',
202                         'software'          => [
203                                 'name'    => 'friendica',
204                                 'version' => FRIENDICA_VERSION . '-' . DB_UPDATE_VERSION,
205                         ],
206                         'protocols'         => ['dfrn', 'activitypub'],
207                         'services'          => [],
208                         'usage'             => [],
209                         'openRegistrations' => intval($config->get('config', 'register_policy')) !== Register::CLOSED,
210                         'metadata'          => [
211                                 'nodeName' => $config->get('config', 'sitename'),
212                         ],
213                 ];
214
215                 if (!empty($config->get('system', 'diaspora_enabled'))) {
216                         $nodeinfo['protocols'][] = 'diaspora';
217                 }
218
219                 if (empty($config->get('system', 'ostatus_disabled'))) {
220                         $nodeinfo['protocols'][] = 'ostatus';
221                 }
222
223                 $nodeinfo['usage'] = self::getUsage();
224
225                 $nodeinfo['services'] = self::getServices();
226
227                 if (Addon::isEnabled('twitter')) {
228                         $nodeinfo['services']['inbound'][] = 'twitter';
229                 }
230
231                 $nodeinfo['services']['inbound'][]  = 'atom1.0';
232                 $nodeinfo['services']['inbound'][]  = 'rss2.0';
233                 $nodeinfo['services']['outbound'][] = 'atom1.0';
234
235                 if ($imap) {
236                         $nodeinfo['services']['inbound'][] = 'imap';
237                 }
238
239                 $nodeinfo['metadata']['explicitContent'] = $config->get('system', 'explicit_content', false) == true;
240
241                 header('Content-type: application/json; charset=utf-8');
242                 echo json_encode($nodeinfo, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
243                 exit;
244         }
245 }