]> git.mxchange.org Git - friendica.git/blob - mod/nodeinfo.php
Merge pull request #6977 from annando/fix-reshare
[friendica.git] / mod / nodeinfo.php
1 <?php
2 /**
3  * @file mod/nodeinfo.php
4  *
5  * Documentation: http://nodeinfo.diaspora.software/schema.html
6 */
7
8 use Friendica\App;
9 use Friendica\Core\Addon;
10 use Friendica\Core\Config;
11 use Friendica\Core\Logger;
12 use Friendica\Core\System;
13 use Friendica\Database\DBA;
14 use Friendica\Util\Network;
15
16 function nodeinfo_wellknown(App $a) {
17         if (!Config::get('system', 'nodeinfo')) {
18                 System::httpExit(404);
19         }
20
21         $nodeinfo = ['links' => [['rel' => 'http://nodeinfo.diaspora.software/ns/schema/1.0',
22                                         'href' => System::baseUrl().'/nodeinfo/1.0']]];
23
24         header('Content-type: application/json; charset=utf-8');
25         echo json_encode($nodeinfo, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
26         exit;
27 }
28
29 function nodeinfo_init(App $a) {
30         if (!Config::get('system', 'nodeinfo')) {
31                 System::httpExit(404);
32         }
33
34         if (($a->argc != 2) || ($a->argv[1] != '1.0')) {
35                 System::httpExit(404);
36         }
37
38         $smtp = (function_exists('imap_open') && !Config::get('system', 'imap_disabled') && !Config::get('system', 'dfrn_only'));
39
40         $nodeinfo = [];
41         $nodeinfo['version'] = '1.0';
42         $nodeinfo['software'] = ['name' => 'friendica', 'version' => FRIENDICA_VERSION.'-'.DB_UPDATE_VERSION];
43
44         $nodeinfo['protocols'] = [];
45         $nodeinfo['protocols']['inbound'] = [];
46         $nodeinfo['protocols']['outbound'] = [];
47
48         if (Config::get('system', 'diaspora_enabled')) {
49                 $nodeinfo['protocols']['inbound'][] = 'diaspora';
50                 $nodeinfo['protocols']['outbound'][] = 'diaspora';
51         }
52
53         $nodeinfo['protocols']['inbound'][] = 'friendica';
54         $nodeinfo['protocols']['outbound'][] = 'friendica';
55
56         if (!Config::get('system', 'ostatus_disabled')) {
57                 $nodeinfo['protocols']['inbound'][] = 'gnusocial';
58                 $nodeinfo['protocols']['outbound'][] = 'gnusocial';
59         }
60
61         $nodeinfo['services'] = [];
62         $nodeinfo['services']['inbound'] = [];
63         $nodeinfo['services']['outbound'] = [];
64
65         $nodeinfo['usage'] = [];
66
67         $nodeinfo['openRegistrations'] = intval(Config::get('config', 'register_policy')) !== \Friendica\Module\Register::CLOSED;
68
69         $nodeinfo['metadata'] = ['nodeName' => Config::get('config', 'sitename')];
70
71         if (Config::get('system', 'nodeinfo')) {
72
73                 $nodeinfo['usage']['users'] = ['total' => (int)Config::get('nodeinfo', 'total_users'),
74                                         'activeHalfyear' => (int)Config::get('nodeinfo', 'active_users_halfyear'),
75                                         'activeMonth' => (int)Config::get('nodeinfo', 'active_users_monthly')];
76                 $nodeinfo['usage']['localPosts'] = (int)Config::get('nodeinfo', 'local_posts');
77                 $nodeinfo['usage']['localComments'] = (int)Config::get('nodeinfo', 'local_comments');
78
79                 if (Addon::isEnabled('blogger')) {
80                         $nodeinfo['services']['outbound'][] = 'blogger';
81                 }
82                 if (Addon::isEnabled('dwpost')) {
83                         $nodeinfo['services']['outbound'][] = 'dreamwidth';
84                 }
85                 if (Addon::isEnabled('statusnet')) {
86                         $nodeinfo['services']['inbound'][] = 'gnusocial';
87                         $nodeinfo['services']['outbound'][] = 'gnusocial';
88                 }
89                 if (Addon::isEnabled('ijpost')) {
90                         $nodeinfo['services']['outbound'][] = 'insanejournal';
91                 }
92                 if (Addon::isEnabled('libertree')) {
93                         $nodeinfo['services']['outbound'][] = 'libertree';
94                 }
95                 if (Addon::isEnabled('buffer')) {
96                         $nodeinfo['services']['outbound'][] = 'linkedin';
97                 }
98                 if (Addon::isEnabled('ljpost')) {
99                         $nodeinfo['services']['outbound'][] = 'livejournal';
100                 }
101                 if (Addon::isEnabled('buffer')) {
102                         $nodeinfo['services']['outbound'][] = 'pinterest';
103                 }
104                 if (Addon::isEnabled('posterous')) {
105                         $nodeinfo['services']['outbound'][] = 'posterous';
106                 }
107                 if (Addon::isEnabled('pumpio')) {
108                         $nodeinfo['services']['inbound'][] = 'pumpio';
109                         $nodeinfo['services']['outbound'][] = 'pumpio';
110                 }
111
112                 if ($smtp) {
113                         $nodeinfo['services']['outbound'][] = 'smtp';
114                 }
115                 if (Addon::isEnabled('tumblr')) {
116                         $nodeinfo['services']['outbound'][] = 'tumblr';
117                 }
118                 if (Addon::isEnabled('twitter') || Addon::isEnabled('buffer')) {
119                         $nodeinfo['services']['outbound'][] = 'twitter';
120                 }
121                 if (Addon::isEnabled('wppost')) {
122                         $nodeinfo['services']['outbound'][] = 'wordpress';
123                 }
124                 $nodeinfo['metadata']['protocols'] = $nodeinfo['protocols'];
125                 $nodeinfo['metadata']['protocols']['outbound'][] = 'atom1.0';
126                 $nodeinfo['metadata']['protocols']['inbound'][] = 'atom1.0';
127                 $nodeinfo['metadata']['protocols']['inbound'][] = 'rss2.0';
128
129                 $nodeinfo['metadata']['services'] = $nodeinfo['services'];
130
131                 if (Addon::isEnabled('twitter')) {
132                         $nodeinfo['metadata']['services']['inbound'][] = 'twitter';
133                 }
134
135                 $nodeinfo['metadata']['explicitContent'] = Config::get('system', 'explicit_content', false) == true;
136         }
137
138         header('Content-type: application/json; charset=utf-8');
139         echo json_encode($nodeinfo, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
140         exit;
141 }
142
143
144
145 function nodeinfo_cron() {
146
147         $a = \get_app();
148
149         // If the addon 'statistics_json' is enabled then disable it and activate nodeinfo.
150         if (Addon::isEnabled('statistics_json')) {
151                 Config::set('system', 'nodeinfo', true);
152
153                 $addon = 'statistics_json';
154                 $addons = Config::get('system', 'addon');
155
156                 if ($addons) {
157                         $addons_arr = explode(',',str_replace(' ', '',$addons));
158
159                         $idx = array_search($addon, $addons_arr);
160                         if ($idx !== false) {
161                                 unset($addons_arr[$idx]);
162                                 Addon::uninstall($addon);
163                                 Config::set('system', 'addon', implode(', ',$addons_arr));
164                         }
165                 }
166         }
167
168         if (!Config::get('system', 'nodeinfo')) {
169                 return;
170         }
171
172         Logger::log('cron_start');
173
174         $users = q("SELECT `user`.`uid`, `user`.`login_date`, `contact`.`last-item`
175                         FROM `user`
176                         INNER JOIN `profile` ON `profile`.`uid` = `user`.`uid` AND `profile`.`is-default`
177                         INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`
178                         WHERE (`profile`.`publish` OR `profile`.`net-publish`) AND `user`.`verified`
179                                 AND NOT `user`.`blocked` AND NOT `user`.`account_removed`
180                                 AND NOT `user`.`account_expired`");
181         if (is_array($users)) {
182                 $total_users = count($users);
183                 $active_users_halfyear = 0;
184                 $active_users_monthly = 0;
185
186                 $halfyear = time() - (180 * 24 * 60 * 60);
187                 $month = time() - (30 * 24 * 60 * 60);
188
189                 foreach ($users AS $user) {
190                         if ((strtotime($user['login_date']) > $halfyear) ||
191                                 (strtotime($user['last-item']) > $halfyear)) {
192                                 ++$active_users_halfyear;
193                         }
194                         if ((strtotime($user['login_date']) > $month) ||
195                                 (strtotime($user['last-item']) > $month)) {
196                                 ++$active_users_monthly;
197                         }
198                 }
199                 Config::set('nodeinfo', 'total_users', $total_users);
200                 Config::set('nodeinfo', 'active_users_halfyear', $active_users_halfyear);
201                 Config::set('nodeinfo', 'active_users_monthly', $active_users_monthly);
202
203                 Logger::log('total_users: ' . $total_users . '/' . $active_users_halfyear. '/' . $active_users_monthly, Logger::DEBUG);
204         }
205
206         $local_posts = DBA::count('thread', ["`wall` AND NOT `deleted` AND `uid` != 0"]);
207         Config::set('nodeinfo', 'local_posts', $local_posts);
208         Logger::log('local_posts: ' . $local_posts, Logger::DEBUG);
209
210         $local_comments = DBA::count('item', ["`origin` AND `id` != `parent` AND NOT `deleted` AND `uid` != 0"]);
211         Config::set('nodeinfo', 'local_comments', $local_comments);
212         Logger::log('local_comments: ' . $local_comments, Logger::DEBUG);
213
214         // Now trying to register
215         $url = 'http://the-federation.info/register/'.$a->getHostName();
216         Logger::log('registering url: '.$url, Logger::DEBUG);
217         $ret = Network::fetchUrl($url);
218         Logger::log('registering answer: '.$ret, Logger::DEBUG);
219
220         Logger::log('cron_end');
221 }