]> git.mxchange.org Git - friendica.git/blob - mod/nodeinfo.php
We now return basic system data via nodeinfo at every time.
[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\Core\Config;
9
10 require_once('include/plugin.php');
11
12 function nodeinfo_wellknown(App $a) {
13         $nodeinfo = array('links' => array(array('rel' => 'http://nodeinfo.diaspora.software/ns/schema/1.0',
14                                         'href' => App::get_baseurl().'/nodeinfo/1.0')));
15
16         header('Content-type: application/json; charset=utf-8');
17         echo json_encode($nodeinfo, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
18         exit;
19 }
20
21 function nodeinfo_init(App $a) {
22         if (!Config::get('system', 'nodeinfo')) {
23                 http_status_exit(404);
24                 killme();
25         }
26
27         if (($a->argc != 2) OR ($a->argv[1] != '1.0')) {
28                 http_status_exit(404);
29                 killme();
30         }
31
32         $smtp = (function_exists('imap_open') AND !Config::get('system', 'imap_disabled') AND !Config::get('system', 'dfrn_only'));
33
34         $nodeinfo = array();
35         $nodeinfo['version'] = '1.0';
36         $nodeinfo['software'] = array('name' => 'friendica', 'version' => FRIENDICA_VERSION.'-'.DB_UPDATE_VERSION);
37
38         $nodeinfo['protocols'] = array();
39         $nodeinfo['protocols']['inbound'] = array();
40         $nodeinfo['protocols']['outbound'] = array();
41
42         if (Config::get('system', 'diaspora_enabled')) {
43                 $nodeinfo['protocols']['inbound'][] = 'diaspora';
44                 $nodeinfo['protocols']['outbound'][] = 'diaspora';
45         }
46
47         $nodeinfo['protocols']['inbound'][] = 'friendica';
48         $nodeinfo['protocols']['outbound'][] = 'friendica';
49
50         if (!Config::get('system', 'ostatus_disabled')) {
51                 $nodeinfo['protocols']['inbound'][] = 'gnusocial';
52                 $nodeinfo['protocols']['outbound'][] = 'gnusocial';
53         }
54
55         $nodeinfo['services'] = array();
56         $nodeinfo['services']['inbound'] = array();
57         $nodeinfo['services']['outbound'] = array();
58
59         $nodeinfo['usage'] = array();
60
61         $nodeinfo['openRegistrations'] = ($a->config['register_policy'] != 0);
62
63         $nodeinfo['metadata'] = array('nodeName' => $a->config['sitename']);
64
65         if (Config::get('system', 'nodeinfo')) {
66
67                 $nodeinfo['usage']['users'] = array('total' => (int)Config::get('nodeinfo', 'total_users'),
68                                         'activeHalfyear' => (int)Config::get('nodeinfo', 'active_users_halfyear'),
69                                         'activeMonth' => (int)Config::get('nodeinfo', 'active_users_monthly'));
70                 $nodeinfo['usage']['localPosts'] = (int)Config::get('nodeinfo', 'local_posts');
71                 $nodeinfo['usage']['localComments'] = (int)Config::get('nodeinfo', 'local_comments');
72
73                 if (plugin_enabled('appnet')) {
74                         $nodeinfo['services']['inbound'][] = 'appnet';
75                 }
76                 if (plugin_enabled('appnet') OR plugin_enabled('buffer')) {
77                         $nodeinfo['services']['outbound'][] = 'appnet';
78                 }
79                 if (plugin_enabled('blogger')) {
80                         $nodeinfo['services']['outbound'][] = 'blogger';
81                 }
82                 if (plugin_enabled('dwpost')) {
83                         $nodeinfo['services']['outbound'][] = 'dreamwidth';
84                 }
85                 if (plugin_enabled('fbpost') OR plugin_enabled('buffer')) {
86                         $nodeinfo['services']['outbound'][] = 'facebook';
87                 }
88                 if (plugin_enabled('statusnet')) {
89                         $nodeinfo['services']['inbound'][] = 'gnusocial';
90                         $nodeinfo['services']['outbound'][] = 'gnusocial';
91                 }
92
93                 if (plugin_enabled('gpluspost') OR plugin_enabled('buffer')) {
94                         $nodeinfo['services']['outbound'][] = 'google';
95                 }
96                 if (plugin_enabled('ijpost')) {
97                         $nodeinfo['services']['outbound'][] = 'insanejournal';
98                 }
99                 if (plugin_enabled('libertree')) {
100                         $nodeinfo['services']['outbound'][] = 'libertree';
101                 }
102                 if (plugin_enabled('buffer')) {
103                         $nodeinfo['services']['outbound'][] = 'linkedin';
104                 }
105                 if (plugin_enabled('ljpost')) {
106                         $nodeinfo['services']['outbound'][] = 'livejournal';
107                 }
108                 if (plugin_enabled('buffer')) {
109                         $nodeinfo['services']['outbound'][] = 'pinterest';
110                 }
111                 if (plugin_enabled('posterous')) {
112                         $nodeinfo['services']['outbound'][] = 'posterous';
113                 }
114                 if (plugin_enabled('pumpio')) {
115                         $nodeinfo['services']['inbound'][] = 'pumpio';
116                         $nodeinfo['services']['outbound'][] = 'pumpio';
117                 }
118
119                 if ($smtp) {
120                         $nodeinfo['services']['outbound'][] = 'smtp';
121                 }
122                 if (plugin_enabled('tumblr')) {
123                         $nodeinfo['services']['outbound'][] = 'tumblr';
124                 }
125                 if (plugin_enabled('twitter') OR plugin_enabled('buffer')) {
126                         $nodeinfo['services']['outbound'][] = 'twitter';
127                 }
128                 if (plugin_enabled('wppost')) {
129                         $nodeinfo['services']['outbound'][] = 'wordpress';
130                 }
131                 $nodeinfo['metadata']['protocols'] = $nodeinfo['protocols'];
132                 $nodeinfo['metadata']['protocols']['outbound'][] = 'atom1.0';
133                 $nodeinfo['metadata']['protocols']['inbound'][] = 'atom1.0';
134                 $nodeinfo['metadata']['protocols']['inbound'][] = 'rss2.0';
135
136                 $nodeinfo['metadata']['services'] = $nodeinfo['services'];
137
138                 if (plugin_enabled('twitter')) {
139                         $nodeinfo['metadata']['services']['inbound'][] = 'twitter';
140                 }
141         }
142
143         header('Content-type: application/json; charset=utf-8');
144         echo json_encode($nodeinfo, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
145         exit;
146 }
147
148
149
150 function nodeinfo_cron() {
151
152         $a = get_app();
153
154         // If the plugin 'statistics_json' is enabled then disable it and actrivate nodeinfo.
155         if (plugin_enabled('statistics_json')) {
156                 Config::set('system', 'nodeinfo', true);
157
158                 $plugin = 'statistics_json';
159                 $plugins = Config::get('system', 'addon');
160                 $plugins_arr = array();
161
162                 if ($plugins) {
163                         $plugins_arr = explode(',',str_replace(' ', '',$plugins));
164
165                         $idx = array_search($plugin, $plugins_arr);
166                         if ($idx !== false) {
167                                 unset($plugins_arr[$idx]);
168                                 uninstall_plugin($plugin);
169                                 Config::set('system', 'addon', implode(', ',$plugins_arr));
170                         }
171                 }
172         }
173
174         if (!Config::get('system', 'nodeinfo')) {
175                 return;
176         }
177         $last = Config::get('nodeinfo', 'last_calucation');
178
179         if ($last) {
180                 // Calculate every 24 hours
181                 $next = $last + (24 * 60 * 60);
182                 if ($next > time()) {
183                         logger('calculation intervall not reached');
184                         return;
185                 }
186         }
187         logger('cron_start');
188
189         $users = qu("SELECT `user`.`uid`, `user`.`login_date`, `contact`.`last-item`
190                         FROM `user`
191                         INNER JOIN `profile` ON `profile`.`uid` = `user`.`uid` AND `profile`.`is-default`
192                         INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`
193                         WHERE (`profile`.`publish` OR `profile`.`net-publish`) AND `user`.`verified`
194                                 AND NOT `user`.`blocked` AND NOT `user`.`account_removed`
195                                 AND NOT `user`.`account_expired`");
196         if (is_array($users)) {
197                         $total_users = count($users);
198                         $active_users_halfyear = 0;
199                         $active_users_monthly = 0;
200
201                         $halfyear = time() - (180 * 24 * 60 * 60);
202                         $month = time() - (30 * 24 * 60 * 60);
203
204                         foreach ($users AS $user) {
205                                 if ((strtotime($user['login_date']) > $halfyear) OR
206                                         (strtotime($user['last-item']) > $halfyear)) {
207                                         ++$active_users_halfyear;
208                                 }
209                                 if ((strtotime($user['login_date']) > $month) OR
210                                         (strtotime($user['last-item']) > $month)) {
211                                         ++$active_users_monthly;
212                                 }
213                         }
214                         Config::set('nodeinfo', 'total_users', $total_users);
215                         logger('total_users: '.$total_users, LOGGER_DEBUG);
216
217                         Config::set('nodeinfo', 'active_users_halfyear', $active_users_halfyear);
218                         Config::set('nodeinfo', 'active_users_monthly', $active_users_monthly);
219         }
220
221         $posts = qu("SELECT COUNT(*) AS local_posts FROM `thread` WHERE `thread`.`wall` AND `thread`.`uid` != 0");
222
223         if (!is_array($posts)) {
224                 $local_posts = -1;
225         } else {
226                 $local_posts = $posts[0]['local_posts'];
227         }
228         Config::set('nodeinfo', 'local_posts', $local_posts);
229
230         logger('local_posts: '.$local_posts, LOGGER_DEBUG);
231
232         $posts = qu("SELECT COUNT(*) FROM `contact`
233                         INNER JOIN `item` ON `item`.`contact-id` = `contact`.`id` AND `item`.`uid` = `contact`.`uid` AND
234                                 `item`.`id` != `item`.`parent` AND `item`.`network` IN ('%s', '%s', '%s')
235                         WHERE `contact`.`self`",
236                         dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_DFRN));
237
238         if (!is_array($posts)) {
239                 $local_comments = -1;
240         } else {
241                 $local_comments = $posts[0]['local_comments'];
242         }
243         Config::set('nodeinfo', 'local_comments', $local_comments);
244
245         // Now trying to register
246         $url = 'http://the-federation.info/register/'.$a->get_hostname();
247         logger('registering url: '.$url, LOGGER_DEBUG);
248         $ret = fetch_url($url);
249         logger('registering answer: '.$ret, LOGGER_DEBUG);
250
251         logger('cron_end');
252         Config::set('nodeinfo', 'last_calucation', time());
253 }
254
255 ?>