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;
13 function nodeinfo_wellknown(App $a) {
14 $nodeinfo = ['links' => [['rel' => 'http://nodeinfo.diaspora.software/ns/schema/1.0',
15 'href' => System::baseUrl().'/nodeinfo/1.0']]];
17 header('Content-type: application/json; charset=utf-8');
18 echo json_encode($nodeinfo, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
22 function nodeinfo_init(App $a) {
23 if (!Config::get('system', 'nodeinfo')) {
24 http_status_exit(404);
28 if (($a->argc != 2) || ($a->argv[1] != '1.0')) {
29 http_status_exit(404);
33 $smtp = (function_exists('imap_open') && !Config::get('system', 'imap_disabled') && !Config::get('system', 'dfrn_only'));
36 $nodeinfo['version'] = '1.0';
37 $nodeinfo['software'] = ['name' => 'friendica', 'version' => FRIENDICA_VERSION.'-'.DB_UPDATE_VERSION];
39 $nodeinfo['protocols'] = [];
40 $nodeinfo['protocols']['inbound'] = [];
41 $nodeinfo['protocols']['outbound'] = [];
43 if (Config::get('system', 'diaspora_enabled')) {
44 $nodeinfo['protocols']['inbound'][] = 'diaspora';
45 $nodeinfo['protocols']['outbound'][] = 'diaspora';
48 $nodeinfo['protocols']['inbound'][] = 'friendica';
49 $nodeinfo['protocols']['outbound'][] = 'friendica';
51 if (!Config::get('system', 'ostatus_disabled')) {
52 $nodeinfo['protocols']['inbound'][] = 'gnusocial';
53 $nodeinfo['protocols']['outbound'][] = 'gnusocial';
56 $nodeinfo['services'] = [];
57 $nodeinfo['services']['inbound'] = [];
58 $nodeinfo['services']['outbound'] = [];
60 $nodeinfo['usage'] = [];
62 $nodeinfo['openRegistrations'] = ($a->config['register_policy'] != 0);
64 $nodeinfo['metadata'] = ['nodeName' => $a->config['sitename']];
66 if (Config::get('system', 'nodeinfo')) {
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');
74 if (Addon::isEnabled('appnet')) {
75 $nodeinfo['services']['inbound'][] = 'appnet';
77 if (Addon::isEnabled('appnet') || Addon::isEnabled('buffer')) {
78 $nodeinfo['services']['outbound'][] = 'appnet';
80 if (Addon::isEnabled('blogger')) {
81 $nodeinfo['services']['outbound'][] = 'blogger';
83 if (Addon::isEnabled('dwpost')) {
84 $nodeinfo['services']['outbound'][] = 'dreamwidth';
86 if (Addon::isEnabled('fbpost') || Addon::isEnabled('buffer')) {
87 $nodeinfo['services']['outbound'][] = 'facebook';
89 if (Addon::isEnabled('statusnet')) {
90 $nodeinfo['services']['inbound'][] = 'gnusocial';
91 $nodeinfo['services']['outbound'][] = 'gnusocial';
94 if (Addon::isEnabled('gpluspost') || Addon::isEnabled('buffer')) {
95 $nodeinfo['services']['outbound'][] = 'google';
97 if (Addon::isEnabled('ijpost')) {
98 $nodeinfo['services']['outbound'][] = 'insanejournal';
100 if (Addon::isEnabled('libertree')) {
101 $nodeinfo['services']['outbound'][] = 'libertree';
103 if (Addon::isEnabled('buffer')) {
104 $nodeinfo['services']['outbound'][] = 'linkedin';
106 if (Addon::isEnabled('ljpost')) {
107 $nodeinfo['services']['outbound'][] = 'livejournal';
109 if (Addon::isEnabled('buffer')) {
110 $nodeinfo['services']['outbound'][] = 'pinterest';
112 if (Addon::isEnabled('posterous')) {
113 $nodeinfo['services']['outbound'][] = 'posterous';
115 if (Addon::isEnabled('pumpio')) {
116 $nodeinfo['services']['inbound'][] = 'pumpio';
117 $nodeinfo['services']['outbound'][] = 'pumpio';
121 $nodeinfo['services']['outbound'][] = 'smtp';
123 if (Addon::isEnabled('tumblr')) {
124 $nodeinfo['services']['outbound'][] = 'tumblr';
126 if (Addon::isEnabled('twitter') || Addon::isEnabled('buffer')) {
127 $nodeinfo['services']['outbound'][] = 'twitter';
129 if (Addon::isEnabled('wppost')) {
130 $nodeinfo['services']['outbound'][] = 'wordpress';
132 $nodeinfo['metadata']['protocols'] = $nodeinfo['protocols'];
133 $nodeinfo['metadata']['protocols']['outbound'][] = 'atom1.0';
134 $nodeinfo['metadata']['protocols']['inbound'][] = 'atom1.0';
135 $nodeinfo['metadata']['protocols']['inbound'][] = 'rss2.0';
137 $nodeinfo['metadata']['services'] = $nodeinfo['services'];
139 if (Addon::isEnabled('twitter')) {
140 $nodeinfo['metadata']['services']['inbound'][] = 'twitter';
144 header('Content-type: application/json; charset=utf-8');
145 echo json_encode($nodeinfo, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
151 function nodeinfo_cron() {
155 // If the addon 'statistics_json' is enabled then disable it and actrivate nodeinfo.
156 if (Addon::isEnabled('statistics_json')) {
157 Config::set('system', 'nodeinfo', true);
159 $addon = 'statistics_json';
160 $addons = Config::get('system', 'addon');
164 $addons_arr = explode(',',str_replace(' ', '',$addons));
166 $idx = array_search($addon, $addons_arr);
167 if ($idx !== false) {
168 unset($addons_arr[$idx]);
169 Addon::uninstall($addon);
170 Config::set('system', 'addon', implode(', ',$addons_arr));
175 if (!Config::get('system', 'nodeinfo')) {
178 $last = Config::get('nodeinfo', 'last_calucation');
181 // Calculate every 24 hours
182 $next = $last + (24 * 60 * 60);
183 if ($next > time()) {
184 logger('calculation intervall not reached');
188 logger('cron_start');
190 $users = q("SELECT `user`.`uid`, `user`.`login_date`, `contact`.`last-item`
192 INNER JOIN `profile` ON `profile`.`uid` = `user`.`uid` AND `profile`.`is-default`
193 INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`
194 WHERE (`profile`.`publish` OR `profile`.`net-publish`) AND `user`.`verified`
195 AND NOT `user`.`blocked` AND NOT `user`.`account_removed`
196 AND NOT `user`.`account_expired`");
197 if (is_array($users)) {
198 $total_users = count($users);
199 $active_users_halfyear = 0;
200 $active_users_monthly = 0;
202 $halfyear = time() - (180 * 24 * 60 * 60);
203 $month = time() - (30 * 24 * 60 * 60);
205 foreach ($users AS $user) {
206 if ((strtotime($user['login_date']) > $halfyear) ||
207 (strtotime($user['last-item']) > $halfyear)) {
208 ++$active_users_halfyear;
210 if ((strtotime($user['login_date']) > $month) ||
211 (strtotime($user['last-item']) > $month)) {
212 ++$active_users_monthly;
215 Config::set('nodeinfo', 'total_users', $total_users);
216 logger('total_users: '.$total_users, LOGGER_DEBUG);
218 Config::set('nodeinfo', 'active_users_halfyear', $active_users_halfyear);
219 Config::set('nodeinfo', 'active_users_monthly', $active_users_monthly);
222 $posts = q("SELECT COUNT(*) AS `local_posts` FROM `thread` WHERE `thread`.`wall` AND `thread`.`uid` != 0");
224 if (!is_array($posts)) {
227 $local_posts = $posts[0]['local_posts'];
229 Config::set('nodeinfo', 'local_posts', $local_posts);
231 logger('local_posts: '.$local_posts, LOGGER_DEBUG);
233 $posts = q("SELECT COUNT(*) AS `local_comments` FROM `contact`
234 INNER JOIN `item` ON `item`.`contact-id` = `contact`.`id` AND `item`.`uid` = `contact`.`uid` AND
235 `item`.`id` != `item`.`parent` AND `item`.`network` IN ('%s', '%s', '%s')
236 WHERE `contact`.`self`",
237 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_DFRN));
239 if (!is_array($posts)) {
240 $local_comments = -1;
242 $local_comments = $posts[0]['local_comments'];
244 Config::set('nodeinfo', 'local_comments', $local_comments);
246 // Now trying to register
247 $url = 'http://the-federation.info/register/'.$a->get_hostname();
248 logger('registering url: '.$url, LOGGER_DEBUG);
249 $ret = fetch_url($url);
250 logger('registering answer: '.$ret, LOGGER_DEBUG);
253 Config::set('nodeinfo', 'last_calucation', time());