]> git.mxchange.org Git - friendica.git/blob - src/Module/NodeInfo.php
Merge pull request #1 from friendica/develop
[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 init(array $parameters = [])
17         {
18                 $config = self::getApp()->getConfig();
19
20                 if (!$config->get('system', 'nodeinfo')) {
21                         throw new \Friendica\Network\HTTPException\NotFoundException();
22                 }
23         }
24
25         public static function rawContent(array $parameters = [])
26         {
27                 $app = self::getApp();
28
29                 // @TODO: Replace with parameter from router
30                 // if the first argument is ".well-known", print the well-known text
31                 if (($app->argc > 1) && ($app->argv[0] == '.well-known')) {
32                         self::printWellKnown($app);
33                 // otherwise print the nodeinfo
34                 } else {
35                         self::printNodeInfo($app);
36                 }
37         }
38
39         /**
40          * Prints the well-known nodeinfo redirect
41          *
42          * @param App $app
43          *
44          * @throws \Friendica\Network\HTTPException\NotFoundException
45          */
46         private static function printWellKnown(App $app)
47         {
48                 $config = $app->getConfig();
49
50                 if (!$config->get('system', 'nodeinfo')) {
51                         throw new \Friendica\Network\HTTPException\NotFoundException();
52                 }
53
54                 $nodeinfo = [
55                         'links' => [[
56                                 'rel'  => 'http://nodeinfo.diaspora.software/ns/schema/1.0',
57                                 'href' => $app->getBaseURL() . '/nodeinfo/1.0']]
58                 ];
59
60                 header('Content-type: application/json; charset=utf-8');
61                 echo json_encode($nodeinfo, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
62                 exit;
63         }
64
65         /**
66          * Print the nodeinfo
67          *
68          * @param App $app
69          */
70         private static function printNodeInfo(App $app)
71         {
72                 $config = $app->getConfig();
73
74                 $smtp = (function_exists('imap_open') && !$config->get('system', 'imap_disabled') && !$config->get('system', 'dfrn_only'));
75
76                 $nodeinfo = [
77                         'version'           => 1.0,
78                         'software'          => [
79                                 'name'    => 'friendica',
80                                 'version' => FRIENDICA_VERSION . '-' . DB_UPDATE_VERSION,
81                         ],
82                         'protocols'         => [
83                                 'inbound'  => [
84                                         'friendica',
85                                 ],
86                                 'outbound' => [
87                                         'friendica',
88                                 ],
89                         ],
90                         'services'          => [
91                                 'inbound'  => [],
92                                 'outbound' => [],
93                         ],
94                         'usage'             => [],
95                         'openRegistrations' => intval($config->get('config', 'register_policy')) !== Register::CLOSED,
96                         'metadata'          => [
97                                 'nodeName' => $config->get('config', 'sitename'),
98                         ],
99                 ];
100
101                 if (!empty($config->get('system', 'diaspora_enabled'))) {
102                         $nodeinfo['protocols']['inbound'][] = 'diaspora';
103                         $nodeinfo['protocols']['outbound'][] = 'diaspora';
104                 }
105
106                 if (empty($config->get('system', 'ostatus_disabled'))) {
107                         $nodeinfo['protocols']['inbound'][] = 'gnusocial';
108                         $nodeinfo['protocols']['outbound'][] = 'gnusocial';
109                 }
110
111                 if (!empty($config->get('system', 'nodeinfo'))) {
112
113                         $nodeinfo['usage']['users'] = [
114                                 'total'          => intval($config->get('nodeinfo', 'total_users')),
115                                 'activeHalfyear' => intval($config->get('nodeinfo', 'active_users_halfyear')),
116                                 'activeMonth'    => intval($config->get('nodeinfo', 'active_users_monthly'))
117                         ];
118                         $nodeinfo['usage']['localPosts'] = intval($config->get('nodeinfo', 'local_posts'));
119                         $nodeinfo['usage']['localComments'] = intval($config->get('nodeinfo', 'local_comments'));
120
121                         if (Addon::isEnabled('blogger')) {
122                                 $nodeinfo['services']['outbound'][] = 'blogger';
123                         }
124                         if (Addon::isEnabled('dwpost')) {
125                                 $nodeinfo['services']['outbound'][] = 'dreamwidth';
126                         }
127                         if (Addon::isEnabled('statusnet')) {
128                                 $nodeinfo['services']['inbound'][] = 'gnusocial';
129                                 $nodeinfo['services']['outbound'][] = 'gnusocial';
130                         }
131                         if (Addon::isEnabled('ijpost')) {
132                                 $nodeinfo['services']['outbound'][] = 'insanejournal';
133                         }
134                         if (Addon::isEnabled('libertree')) {
135                                 $nodeinfo['services']['outbound'][] = 'libertree';
136                         }
137                         if (Addon::isEnabled('buffer')) {
138                                 $nodeinfo['services']['outbound'][] = 'linkedin';
139                         }
140                         if (Addon::isEnabled('ljpost')) {
141                                 $nodeinfo['services']['outbound'][] = 'livejournal';
142                         }
143                         if (Addon::isEnabled('buffer')) {
144                                 $nodeinfo['services']['outbound'][] = 'pinterest';
145                         }
146                         if (Addon::isEnabled('posterous')) {
147                                 $nodeinfo['services']['outbound'][] = 'posterous';
148                         }
149                         if (Addon::isEnabled('pumpio')) {
150                                 $nodeinfo['services']['inbound'][] = 'pumpio';
151                                 $nodeinfo['services']['outbound'][] = 'pumpio';
152                         }
153
154                         if ($smtp) {
155                                 $nodeinfo['services']['outbound'][] = 'smtp';
156                         }
157                         if (Addon::isEnabled('tumblr')) {
158                                 $nodeinfo['services']['outbound'][] = 'tumblr';
159                         }
160                         if (Addon::isEnabled('twitter') || Addon::isEnabled('buffer')) {
161                                 $nodeinfo['services']['outbound'][] = 'twitter';
162                         }
163                         if (Addon::isEnabled('wppost')) {
164                                 $nodeinfo['services']['outbound'][] = 'wordpress';
165                         }
166                         $nodeinfo['metadata']['protocols'] = $nodeinfo['protocols'];
167                         $nodeinfo['metadata']['protocols']['outbound'][] = 'atom1.0';
168                         $nodeinfo['metadata']['protocols']['inbound'][] = 'atom1.0';
169                         $nodeinfo['metadata']['protocols']['inbound'][] = 'rss2.0';
170
171                         $nodeinfo['metadata']['services'] = $nodeinfo['services'];
172
173                         if (Addon::isEnabled('twitter')) {
174                                 $nodeinfo['metadata']['services']['inbound'][] = 'twitter';
175                         }
176
177                         $nodeinfo['metadata']['explicitContent'] = $config->get('system', 'explicit_content', false) == true;
178                 }
179
180                 header('Content-type: application/json; charset=utf-8');
181                 echo json_encode($nodeinfo, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
182                 exit;
183         }
184 }