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