]> git.mxchange.org Git - friendica.git/blob - src/Module/NodeInfo.php
Twitter is also inbound
[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                 $config = $app->getConfig();
93
94                 $services = [
95                         'inbound'  => [],
96                         'outbound' => [],
97                 ];
98
99                 $smtp = (function_exists('imap_open') && !$config->get('system', 'imap_disabled') && !$config->get('system', 'dfrn_only'));
100
101                 if (Addon::isEnabled('blogger')) {
102                         $services['outbound'][] = 'blogger';
103                 }
104                 if (Addon::isEnabled('dwpost')) {
105                         $services['outbound'][] = 'dreamwidth';
106                 }
107                 if (Addon::isEnabled('statusnet')) {
108                         $services['inbound'][] = 'gnusocial';
109                         $services['outbound'][] = 'gnusocial';
110                 }
111                 if (Addon::isEnabled('ijpost')) {
112                         $services['outbound'][] = 'insanejournal';
113                 }
114                 if (Addon::isEnabled('libertree')) {
115                         $services['outbound'][] = 'libertree';
116                 }
117                 if (Addon::isEnabled('buffer')) {
118                         $services['outbound'][] = 'linkedin';
119                 }
120                 if (Addon::isEnabled('ljpost')) {
121                         $services['outbound'][] = 'livejournal';
122                 }
123                 if (Addon::isEnabled('buffer')) {
124                         $services['outbound'][] = 'pinterest';
125                 }
126                 if (Addon::isEnabled('posterous')) {
127                         $services['outbound'][] = 'posterous';
128                 }
129                 if (Addon::isEnabled('pumpio')) {
130                         $services['inbound'][] = 'pumpio';
131                         $services['outbound'][] = 'pumpio';
132                 }
133                 if ($smtp) {
134                         $services['outbound'][] = 'smtp';
135                 }
136                 if (Addon::isEnabled('tumblr')) {
137                         $services['outbound'][] = 'tumblr';
138                 }
139                 if (Addon::isEnabled('twitter') || Addon::isEnabled('buffer')) {
140                         $services['outbound'][] = 'twitter';
141                 }
142                 if (Addon::isEnabled('wppost')) {
143                         $services['outbound'][] = 'wordpress';
144                 }
145
146                 return $services;
147         }
148
149         /**
150          * Print the nodeinfo version 1
151          *
152          * @param App $app
153          */
154         private static function printNodeInfo1(App $app)
155         {
156                 $config = $app->getConfig();
157
158                 $nodeinfo = [
159                         'version'           => '1.0',
160                         'software'          => [
161                                 'name'    => 'friendica',
162                                 'version' => FRIENDICA_VERSION . '-' . DB_UPDATE_VERSION,
163                         ],
164                         'protocols'         => [
165                                 'inbound'  => [
166                                         'friendica', 'activitypub'
167                                 ],
168                                 'outbound' => [
169                                         'friendica', 'activitypub'
170                                 ],
171                         ],
172                         'services'          => [],
173                         'usage'             => [],
174                         'openRegistrations' => intval($config->get('config', 'register_policy')) !== Register::CLOSED,
175                         'metadata'          => [
176                                 'nodeName' => $config->get('config', 'sitename'),
177                         ],
178                 ];
179
180                 if (!empty($config->get('system', 'diaspora_enabled'))) {
181                         $nodeinfo['protocols']['inbound'][] = 'diaspora';
182                         $nodeinfo['protocols']['outbound'][] = 'diaspora';
183                 }
184
185                 if (empty($config->get('system', 'ostatus_disabled'))) {
186                         $nodeinfo['protocols']['inbound'][] = 'gnusocial';
187                         $nodeinfo['protocols']['outbound'][] = 'gnusocial';
188                 }
189
190                 $nodeinfo['usage'] = self::getUsage($app);
191
192                 $nodeinfo['services'] = self::getServices($app);
193
194                 $nodeinfo['metadata']['protocols'] = $nodeinfo['protocols'];
195                 $nodeinfo['metadata']['protocols']['outbound'][] = 'atom1.0';
196                 $nodeinfo['metadata']['protocols']['inbound'][] = 'atom1.0';
197                 $nodeinfo['metadata']['protocols']['inbound'][] = 'rss2.0';
198
199                 $nodeinfo['metadata']['services'] = $nodeinfo['services'];
200
201                 if (Addon::isEnabled('twitter')) {
202                         $nodeinfo['metadata']['services']['inbound'][] = 'twitter';
203                 }
204
205                 $nodeinfo['metadata']['explicitContent'] = $config->get('system', 'explicit_content', false) == true;
206
207                 header('Content-type: application/json; charset=utf-8');
208                 echo json_encode($nodeinfo, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
209                 exit;
210         }
211
212         /**
213          * Print the nodeinfo version 2
214          *
215          * @param App $app
216          */
217         private static function printNodeInfo2(App $app)
218         {
219                 $config = $app->getConfig();
220
221                 $nodeinfo = [
222                         'version'           => '2.0',
223                         'software'          => [
224                                 'name'    => 'friendica',
225                                 'version' => FRIENDICA_VERSION . '-' . DB_UPDATE_VERSION,
226                         ],
227                         'protocols'         => ['dfrn', 'activitypub'],
228                         'services'          => [],
229                         'usage'             => [],
230                         'openRegistrations' => intval($config->get('config', 'register_policy')) !== Register::CLOSED,
231                         'metadata'          => [
232                                 'nodeName' => $config->get('config', 'sitename'),
233                         ],
234                 ];
235
236                 if (!empty($config->get('system', 'diaspora_enabled'))) {
237                         $nodeinfo['protocols'][] = 'diaspora';
238                 }
239
240                 if (empty($config->get('system', 'ostatus_disabled'))) {
241                         $nodeinfo['protocols'][] = 'ostatus';
242                 }
243
244                 $nodeinfo['usage'] = self::getUsage($app);
245
246                 $nodeinfo['services'] = self::getServices($app);
247
248                 if (Addon::isEnabled('twitter')) {
249                         $nodeinfo['services']['inbound'][] = 'twitter';
250                 }
251
252                 $nodeinfo['services']['inbound'][]  = 'atom1.0';
253                 $nodeinfo['services']['outbound'][] = 'atom1.0';
254                 $nodeinfo['services']['outbound'][] = 'rss2.0';
255
256                 $nodeinfo['metadata']['explicitContent'] = $config->get('system', 'explicit_content', false) == true;
257
258                 header('Content-type: application/json; charset=utf-8');
259                 echo json_encode($nodeinfo, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
260                 exit;
261         }
262 }