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