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