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