3 * @file mod/nodeinfo.php
5 * Documentation: http://nodeinfo.diaspora.software/schema.html
9 use Friendica\Core\System;
10 use Friendica\Core\Config;
12 require_once 'include/plugin.php';
14 function nodeinfo_wellknown(App $a) {
15 $nodeinfo = array('links' => array(array('rel' => 'http://nodeinfo.diaspora.software/ns/schema/1.0',
16 'href' => System::baseUrl().'/nodeinfo/1.0')));
18 header('Content-type: application/json; charset=utf-8');
19 echo json_encode($nodeinfo, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
23 function nodeinfo_init(App $a) {
24 if (!Config::get('system', 'nodeinfo')) {
25 http_status_exit(404);
29 if (($a->argc != 2) || ($a->argv[1] != '1.0')) {
30 http_status_exit(404);
34 $smtp = (function_exists('imap_open') && !Config::get('system', 'imap_disabled') && !Config::get('system', 'dfrn_only'));
37 $nodeinfo['version'] = '1.0';
38 $nodeinfo['software'] = array('name' => 'friendica', 'version' => FRIENDICA_VERSION.'-'.DB_UPDATE_VERSION);
40 $nodeinfo['protocols'] = array();
41 $nodeinfo['protocols']['inbound'] = array();
42 $nodeinfo['protocols']['outbound'] = array();
44 if (Config::get('system', 'diaspora_enabled')) {
45 $nodeinfo['protocols']['inbound'][] = 'diaspora';
46 $nodeinfo['protocols']['outbound'][] = 'diaspora';
49 $nodeinfo['protocols']['inbound'][] = 'friendica';
50 $nodeinfo['protocols']['outbound'][] = 'friendica';
52 if (!Config::get('system', 'ostatus_disabled')) {
53 $nodeinfo['protocols']['inbound'][] = 'gnusocial';
54 $nodeinfo['protocols']['outbound'][] = 'gnusocial';
57 $nodeinfo['services'] = array();
58 $nodeinfo['services']['inbound'] = array();
59 $nodeinfo['services']['outbound'] = array();
61 $nodeinfo['usage'] = array();
63 $nodeinfo['openRegistrations'] = ($a->config['register_policy'] != 0);
65 $nodeinfo['metadata'] = array('nodeName' => $a->config['sitename']);
67 if (Config::get('system', 'nodeinfo')) {
69 $nodeinfo['usage']['users'] = array('total' => (int)Config::get('nodeinfo', 'total_users'),
70 'activeHalfyear' => (int)Config::get('nodeinfo', 'active_users_halfyear'),
71 'activeMonth' => (int)Config::get('nodeinfo', 'active_users_monthly'));
72 $nodeinfo['usage']['localPosts'] = (int)Config::get('nodeinfo', 'local_posts');
73 $nodeinfo['usage']['localComments'] = (int)Config::get('nodeinfo', 'local_comments');
75 if (plugin_enabled('appnet')) {
76 $nodeinfo['services']['inbound'][] = 'appnet';
78 if (plugin_enabled('appnet') || plugin_enabled('buffer')) {
79 $nodeinfo['services']['outbound'][] = 'appnet';
81 if (plugin_enabled('blogger')) {
82 $nodeinfo['services']['outbound'][] = 'blogger';
84 if (plugin_enabled('dwpost')) {
85 $nodeinfo['services']['outbound'][] = 'dreamwidth';
87 if (plugin_enabled('fbpost') || plugin_enabled('buffer')) {
88 $nodeinfo['services']['outbound'][] = 'facebook';
90 if (plugin_enabled('statusnet')) {
91 $nodeinfo['services']['inbound'][] = 'gnusocial';
92 $nodeinfo['services']['outbound'][] = 'gnusocial';
95 if (plugin_enabled('gpluspost') || plugin_enabled('buffer')) {
96 $nodeinfo['services']['outbound'][] = 'google';
98 if (plugin_enabled('ijpost')) {
99 $nodeinfo['services']['outbound'][] = 'insanejournal';
101 if (plugin_enabled('libertree')) {
102 $nodeinfo['services']['outbound'][] = 'libertree';
104 if (plugin_enabled('buffer')) {
105 $nodeinfo['services']['outbound'][] = 'linkedin';
107 if (plugin_enabled('ljpost')) {
108 $nodeinfo['services']['outbound'][] = 'livejournal';
110 if (plugin_enabled('buffer')) {
111 $nodeinfo['services']['outbound'][] = 'pinterest';
113 if (plugin_enabled('posterous')) {
114 $nodeinfo['services']['outbound'][] = 'posterous';
116 if (plugin_enabled('pumpio')) {
117 $nodeinfo['services']['inbound'][] = 'pumpio';
118 $nodeinfo['services']['outbound'][] = 'pumpio';
122 $nodeinfo['services']['outbound'][] = 'smtp';
124 if (plugin_enabled('tumblr')) {
125 $nodeinfo['services']['outbound'][] = 'tumblr';
127 if (plugin_enabled('twitter') || plugin_enabled('buffer')) {
128 $nodeinfo['services']['outbound'][] = 'twitter';
130 if (plugin_enabled('wppost')) {
131 $nodeinfo['services']['outbound'][] = 'wordpress';
133 $nodeinfo['metadata']['protocols'] = $nodeinfo['protocols'];
134 $nodeinfo['metadata']['protocols']['outbound'][] = 'atom1.0';
135 $nodeinfo['metadata']['protocols']['inbound'][] = 'atom1.0';
136 $nodeinfo['metadata']['protocols']['inbound'][] = 'rss2.0';
138 $nodeinfo['metadata']['services'] = $nodeinfo['services'];
140 if (plugin_enabled('twitter')) {
141 $nodeinfo['metadata']['services']['inbound'][] = 'twitter';
145 header('Content-type: application/json; charset=utf-8');
146 echo json_encode($nodeinfo, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
152 function nodeinfo_cron() {
156 // If the plugin 'statistics_json' is enabled then disable it and actrivate nodeinfo.
157 if (plugin_enabled('statistics_json')) {
158 Config::set('system', 'nodeinfo', true);
160 $plugin = 'statistics_json';
161 $plugins = Config::get('system', 'addon');
162 $plugins_arr = array();
165 $plugins_arr = explode(',',str_replace(' ', '',$plugins));
167 $idx = array_search($plugin, $plugins_arr);
168 if ($idx !== false) {
169 unset($plugins_arr[$idx]);
170 uninstall_plugin($plugin);
171 Config::set('system', 'addon', implode(', ',$plugins_arr));
176 if (!Config::get('system', 'nodeinfo')) {
179 $last = Config::get('nodeinfo', 'last_calucation');
182 // Calculate every 24 hours
183 $next = $last + (24 * 60 * 60);
184 if ($next > time()) {
185 logger('calculation intervall not reached');
189 logger('cron_start');
191 $users = q("SELECT `user`.`uid`, `user`.`login_date`, `contact`.`last-item`
193 INNER JOIN `profile` ON `profile`.`uid` = `user`.`uid` AND `profile`.`is-default`
194 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`
195 WHERE (`profile`.`publish` OR `profile`.`net-publish`) AND `user`.`verified`
196 AND NOT `user`.`blocked` AND NOT `user`.`account_removed`
197 AND NOT `user`.`account_expired`");
198 if (is_array($users)) {
199 $total_users = count($users);
200 $active_users_halfyear = 0;
201 $active_users_monthly = 0;
203 $halfyear = time() - (180 * 24 * 60 * 60);
204 $month = time() - (30 * 24 * 60 * 60);
206 foreach ($users AS $user) {
207 if ((strtotime($user['login_date']) > $halfyear) ||
208 (strtotime($user['last-item']) > $halfyear)) {
209 ++$active_users_halfyear;
211 if ((strtotime($user['login_date']) > $month) ||
212 (strtotime($user['last-item']) > $month)) {
213 ++$active_users_monthly;
216 Config::set('nodeinfo', 'total_users', $total_users);
217 logger('total_users: '.$total_users, LOGGER_DEBUG);
219 Config::set('nodeinfo', 'active_users_halfyear', $active_users_halfyear);
220 Config::set('nodeinfo', 'active_users_monthly', $active_users_monthly);
223 $posts = q("SELECT COUNT(*) AS `local_posts` FROM `thread` WHERE `thread`.`wall` AND `thread`.`uid` != 0");
225 if (!is_array($posts)) {
228 $local_posts = $posts[0]['local_posts'];
230 Config::set('nodeinfo', 'local_posts', $local_posts);
232 logger('local_posts: '.$local_posts, LOGGER_DEBUG);
234 $posts = q("SELECT COUNT(*) AS `local_comments` FROM `contact`
235 INNER JOIN `item` ON `item`.`contact-id` = `contact`.`id` AND `item`.`uid` = `contact`.`uid` AND
236 `item`.`id` != `item`.`parent` AND `item`.`network` IN ('%s', '%s', '%s')
237 WHERE `contact`.`self`",
238 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_DFRN));
240 if (!is_array($posts)) {
241 $local_comments = -1;
243 $local_comments = $posts[0]['local_comments'];
245 Config::set('nodeinfo', 'local_comments', $local_comments);
247 // Now trying to register
248 $url = 'http://the-federation.info/register/'.$a->get_hostname();
249 logger('registering url: '.$url, LOGGER_DEBUG);
250 $ret = fetch_url($url);
251 logger('registering answer: '.$ret, LOGGER_DEBUG);
254 Config::set('nodeinfo', 'last_calucation', time());