3 * @file mod/nodeinfo.php
5 * Documentation: http://nodeinfo.diaspora.software/schema.html
9 use Friendica\Core\Addon;
10 use Friendica\Core\System;
11 use Friendica\Core\Config;
12 use Friendica\Util\Network;
14 function nodeinfo_wellknown(App $a) {
15 $nodeinfo = ['links' => [['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 System::httpExit(404);
29 if (($a->argc != 2) || ($a->argv[1] != '1.0')) {
30 System::httpExit(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'] = ['name' => 'friendica', 'version' => FRIENDICA_VERSION.'-'.DB_UPDATE_VERSION];
40 $nodeinfo['protocols'] = [];
41 $nodeinfo['protocols']['inbound'] = [];
42 $nodeinfo['protocols']['outbound'] = [];
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'] = [];
58 $nodeinfo['services']['inbound'] = [];
59 $nodeinfo['services']['outbound'] = [];
61 $nodeinfo['usage'] = [];
63 $nodeinfo['openRegistrations'] = ($a->config['register_policy'] != 0);
65 $nodeinfo['metadata'] = ['nodeName' => $a->config['sitename']];
67 if (Config::get('system', 'nodeinfo')) {
69 $nodeinfo['usage']['users'] = ['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 (Addon::isEnabled('appnet')) {
76 $nodeinfo['services']['inbound'][] = 'appnet';
78 if (Addon::isEnabled('appnet') || Addon::isEnabled('buffer')) {
79 $nodeinfo['services']['outbound'][] = 'appnet';
81 if (Addon::isEnabled('blogger')) {
82 $nodeinfo['services']['outbound'][] = 'blogger';
84 if (Addon::isEnabled('dwpost')) {
85 $nodeinfo['services']['outbound'][] = 'dreamwidth';
87 if (Addon::isEnabled('fbpost') || Addon::isEnabled('buffer')) {
88 $nodeinfo['services']['outbound'][] = 'facebook';
90 if (Addon::isEnabled('statusnet')) {
91 $nodeinfo['services']['inbound'][] = 'gnusocial';
92 $nodeinfo['services']['outbound'][] = 'gnusocial';
95 if (Addon::isEnabled('gpluspost') || Addon::isEnabled('buffer')) {
96 $nodeinfo['services']['outbound'][] = 'google';
98 if (Addon::isEnabled('ijpost')) {
99 $nodeinfo['services']['outbound'][] = 'insanejournal';
101 if (Addon::isEnabled('libertree')) {
102 $nodeinfo['services']['outbound'][] = 'libertree';
104 if (Addon::isEnabled('buffer')) {
105 $nodeinfo['services']['outbound'][] = 'linkedin';
107 if (Addon::isEnabled('ljpost')) {
108 $nodeinfo['services']['outbound'][] = 'livejournal';
110 if (Addon::isEnabled('buffer')) {
111 $nodeinfo['services']['outbound'][] = 'pinterest';
113 if (Addon::isEnabled('posterous')) {
114 $nodeinfo['services']['outbound'][] = 'posterous';
116 if (Addon::isEnabled('pumpio')) {
117 $nodeinfo['services']['inbound'][] = 'pumpio';
118 $nodeinfo['services']['outbound'][] = 'pumpio';
122 $nodeinfo['services']['outbound'][] = 'smtp';
124 if (Addon::isEnabled('tumblr')) {
125 $nodeinfo['services']['outbound'][] = 'tumblr';
127 if (Addon::isEnabled('twitter') || Addon::isEnabled('buffer')) {
128 $nodeinfo['services']['outbound'][] = 'twitter';
130 if (Addon::isEnabled('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 (Addon::isEnabled('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 addon 'statistics_json' is enabled then disable it and actrivate nodeinfo.
157 if (Addon::isEnabled('statistics_json')) {
158 Config::set('system', 'nodeinfo', true);
160 $addon = 'statistics_json';
161 $addons = Config::get('system', 'addon');
165 $addons_arr = explode(',',str_replace(' ', '',$addons));
167 $idx = array_search($addon, $addons_arr);
168 if ($idx !== false) {
169 unset($addons_arr[$idx]);
170 Addon::uninstall($addon);
171 Config::set('system', 'addon', implode(', ',$addons_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 = Network::fetchUrl($url);
251 logger('registering answer: '.$ret, LOGGER_DEBUG);
254 Config::set('nodeinfo', 'last_calucation', time());