]> git.mxchange.org Git - friendica.git/blob - mod/nodeinfo.php
Uncommon logger levels in Friendica (#5453)
[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\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('appnet')) {
78                         $nodeinfo['services']['inbound'][] = 'appnet';
79                 }
80                 if (Addon::isEnabled('appnet') || Addon::isEnabled('buffer')) {
81                         $nodeinfo['services']['outbound'][] = 'appnet';
82                 }
83                 if (Addon::isEnabled('blogger')) {
84                         $nodeinfo['services']['outbound'][] = 'blogger';
85                 }
86                 if (Addon::isEnabled('dwpost')) {
87                         $nodeinfo['services']['outbound'][] = 'dreamwidth';
88                 }
89                 if (Addon::isEnabled('fbpost') || Addon::isEnabled('buffer')) {
90                         $nodeinfo['services']['outbound'][] = 'facebook';
91                 }
92                 if (Addon::isEnabled('statusnet')) {
93                         $nodeinfo['services']['inbound'][] = 'gnusocial';
94                         $nodeinfo['services']['outbound'][] = 'gnusocial';
95                 }
96
97                 if (Addon::isEnabled('gpluspost') || Addon::isEnabled('buffer')) {
98                         $nodeinfo['services']['outbound'][] = 'google';
99                 }
100                 if (Addon::isEnabled('ijpost')) {
101                         $nodeinfo['services']['outbound'][] = 'insanejournal';
102                 }
103                 if (Addon::isEnabled('libertree')) {
104                         $nodeinfo['services']['outbound'][] = 'libertree';
105                 }
106                 if (Addon::isEnabled('buffer')) {
107                         $nodeinfo['services']['outbound'][] = 'linkedin';
108                 }
109                 if (Addon::isEnabled('ljpost')) {
110                         $nodeinfo['services']['outbound'][] = 'livejournal';
111                 }
112                 if (Addon::isEnabled('buffer')) {
113                         $nodeinfo['services']['outbound'][] = 'pinterest';
114                 }
115                 if (Addon::isEnabled('posterous')) {
116                         $nodeinfo['services']['outbound'][] = 'posterous';
117                 }
118                 if (Addon::isEnabled('pumpio')) {
119                         $nodeinfo['services']['inbound'][] = 'pumpio';
120                         $nodeinfo['services']['outbound'][] = 'pumpio';
121                 }
122
123                 if ($smtp) {
124                         $nodeinfo['services']['outbound'][] = 'smtp';
125                 }
126                 if (Addon::isEnabled('tumblr')) {
127                         $nodeinfo['services']['outbound'][] = 'tumblr';
128                 }
129                 if (Addon::isEnabled('twitter') || Addon::isEnabled('buffer')) {
130                         $nodeinfo['services']['outbound'][] = 'twitter';
131                 }
132                 if (Addon::isEnabled('wppost')) {
133                         $nodeinfo['services']['outbound'][] = 'wordpress';
134                 }
135                 $nodeinfo['metadata']['protocols'] = $nodeinfo['protocols'];
136                 $nodeinfo['metadata']['protocols']['outbound'][] = 'atom1.0';
137                 $nodeinfo['metadata']['protocols']['inbound'][] = 'atom1.0';
138                 $nodeinfo['metadata']['protocols']['inbound'][] = 'rss2.0';
139
140                 $nodeinfo['metadata']['services'] = $nodeinfo['services'];
141
142                 if (Addon::isEnabled('twitter')) {
143                         $nodeinfo['metadata']['services']['inbound'][] = 'twitter';
144                 }
145
146                 $nodeinfo['metadata']['explicitContent'] = Config::get('system', 'explicit_content', false) == true;
147         }
148
149         header('Content-type: application/json; charset=utf-8');
150         echo json_encode($nodeinfo, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
151         exit;
152 }
153
154
155
156 function nodeinfo_cron() {
157
158         $a = get_app();
159
160         // If the addon 'statistics_json' is enabled then disable it and activate nodeinfo.
161         if (Addon::isEnabled('statistics_json')) {
162                 Config::set('system', 'nodeinfo', true);
163
164                 $addon = 'statistics_json';
165                 $addons = Config::get('system', 'addon');
166                 $addons_arr = [];
167
168                 if ($addons) {
169                         $addons_arr = explode(',',str_replace(' ', '',$addons));
170
171                         $idx = array_search($addon, $addons_arr);
172                         if ($idx !== false) {
173                                 unset($addons_arr[$idx]);
174                                 Addon::uninstall($addon);
175                                 Config::set('system', 'addon', implode(', ',$addons_arr));
176                         }
177                 }
178         }
179
180         if (!Config::get('system', 'nodeinfo')) {
181                 return;
182         }
183
184         logger('cron_start');
185
186         $users = q("SELECT `user`.`uid`, `user`.`login_date`, `contact`.`last-item`
187                         FROM `user`
188                         INNER JOIN `profile` ON `profile`.`uid` = `user`.`uid` AND `profile`.`is-default`
189                         INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`
190                         WHERE (`profile`.`publish` OR `profile`.`net-publish`) AND `user`.`verified`
191                                 AND NOT `user`.`blocked` AND NOT `user`.`account_removed`
192                                 AND NOT `user`.`account_expired`");
193         if (is_array($users)) {
194                 $total_users = count($users);
195                 $active_users_halfyear = 0;
196                 $active_users_monthly = 0;
197
198                 $halfyear = time() - (180 * 24 * 60 * 60);
199                 $month = time() - (30 * 24 * 60 * 60);
200
201                 foreach ($users AS $user) {
202                         if ((strtotime($user['login_date']) > $halfyear) ||
203                                 (strtotime($user['last-item']) > $halfyear)) {
204                                 ++$active_users_halfyear;
205                         }
206                         if ((strtotime($user['login_date']) > $month) ||
207                                 (strtotime($user['last-item']) > $month)) {
208                                 ++$active_users_monthly;
209                         }
210                 }
211                 Config::set('nodeinfo', 'total_users', $total_users);
212                 Config::set('nodeinfo', 'active_users_halfyear', $active_users_halfyear);
213                 Config::set('nodeinfo', 'active_users_monthly', $active_users_monthly);
214
215                 logger('total_users: ' . $total_users . '/' . $active_users_halfyear. '/' . $active_users_monthly, LOGGER_DEBUG);
216         }
217
218         $local_posts = DBA::count('thread', ["`wall` AND NOT `deleted` AND `uid` != 0"]);
219         Config::set('nodeinfo', 'local_posts', $local_posts);
220         logger('local_posts: ' . $local_posts, LOGGER_DEBUG);
221
222         $local_comments = DBA::count('item', ["`origin` AND `id` != `parent` AND NOT `deleted` AND `uid` != 0"]);
223         Config::set('nodeinfo', 'local_comments', $local_comments);
224         logger('local_comments: ' . $local_comments, LOGGER_DEBUG);
225
226         // Now trying to register
227         $url = 'http://the-federation.info/register/'.$a->get_hostname();
228         logger('registering url: '.$url, LOGGER_DEBUG);
229         $ret = Network::fetchUrl($url);
230         logger('registering answer: '.$ret, LOGGER_DEBUG);
231
232         logger('cron_end');
233 }