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