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