3 * @file mod/nodeinfo.php
5 * Documentation: http://nodeinfo.diaspora.software/schema.html
8 use \Friendica\Core\Config;
10 require_once 'include/plugin.php';
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')));
16 header('Content-type: application/json; charset=utf-8');
17 echo json_encode($nodeinfo, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
21 function nodeinfo_init(App $a) {
22 if (!Config::get('system', 'nodeinfo')) {
23 http_status_exit(404);
27 if (($a->argc != 2) OR ($a->argv[1] != '1.0')) {
28 http_status_exit(404);
32 $smtp = (function_exists('imap_open') AND !Config::get('system', 'imap_disabled') AND !Config::get('system', 'dfrn_only'));
35 $nodeinfo['version'] = '1.0';
36 $nodeinfo['software'] = array('name' => 'friendica', 'version' => FRIENDICA_VERSION.'-'.DB_UPDATE_VERSION);
38 $nodeinfo['protocols'] = array();
39 $nodeinfo['protocols']['inbound'] = array();
40 $nodeinfo['protocols']['outbound'] = array();
42 if (Config::get('system', 'diaspora_enabled')) {
43 $nodeinfo['protocols']['inbound'][] = 'diaspora';
44 $nodeinfo['protocols']['outbound'][] = 'diaspora';
47 $nodeinfo['protocols']['inbound'][] = 'friendica';
48 $nodeinfo['protocols']['outbound'][] = 'friendica';
50 if (!Config::get('system', 'ostatus_disabled')) {
51 $nodeinfo['protocols']['inbound'][] = 'gnusocial';
52 $nodeinfo['protocols']['outbound'][] = 'gnusocial';
55 $nodeinfo['services'] = array();
56 $nodeinfo['services']['inbound'] = array();
57 $nodeinfo['services']['outbound'] = array();
59 $nodeinfo['usage'] = array();
61 $nodeinfo['openRegistrations'] = ($a->config['register_policy'] != 0);
63 $nodeinfo['metadata'] = array('nodeName' => $a->config['sitename']);
65 if (Config::get('system', 'nodeinfo')) {
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');
73 if (plugin_enabled('appnet')) {
74 $nodeinfo['services']['inbound'][] = 'appnet';
76 if (plugin_enabled('appnet') OR plugin_enabled('buffer')) {
77 $nodeinfo['services']['outbound'][] = 'appnet';
79 if (plugin_enabled('blogger')) {
80 $nodeinfo['services']['outbound'][] = 'blogger';
82 if (plugin_enabled('dwpost')) {
83 $nodeinfo['services']['outbound'][] = 'dreamwidth';
85 if (plugin_enabled('fbpost') OR plugin_enabled('buffer')) {
86 $nodeinfo['services']['outbound'][] = 'facebook';
88 if (plugin_enabled('statusnet')) {
89 $nodeinfo['services']['inbound'][] = 'gnusocial';
90 $nodeinfo['services']['outbound'][] = 'gnusocial';
93 if (plugin_enabled('gpluspost') OR plugin_enabled('buffer')) {
94 $nodeinfo['services']['outbound'][] = 'google';
96 if (plugin_enabled('ijpost')) {
97 $nodeinfo['services']['outbound'][] = 'insanejournal';
99 if (plugin_enabled('libertree')) {
100 $nodeinfo['services']['outbound'][] = 'libertree';
102 if (plugin_enabled('buffer')) {
103 $nodeinfo['services']['outbound'][] = 'linkedin';
105 if (plugin_enabled('ljpost')) {
106 $nodeinfo['services']['outbound'][] = 'livejournal';
108 if (plugin_enabled('buffer')) {
109 $nodeinfo['services']['outbound'][] = 'pinterest';
111 if (plugin_enabled('posterous')) {
112 $nodeinfo['services']['outbound'][] = 'posterous';
114 if (plugin_enabled('pumpio')) {
115 $nodeinfo['services']['inbound'][] = 'pumpio';
116 $nodeinfo['services']['outbound'][] = 'pumpio';
120 $nodeinfo['services']['outbound'][] = 'smtp';
122 if (plugin_enabled('tumblr')) {
123 $nodeinfo['services']['outbound'][] = 'tumblr';
125 if (plugin_enabled('twitter') OR plugin_enabled('buffer')) {
126 $nodeinfo['services']['outbound'][] = 'twitter';
128 if (plugin_enabled('wppost')) {
129 $nodeinfo['services']['outbound'][] = 'wordpress';
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';
136 $nodeinfo['metadata']['services'] = $nodeinfo['services'];
138 if (plugin_enabled('twitter')) {
139 $nodeinfo['metadata']['services']['inbound'][] = 'twitter';
143 header('Content-type: application/json; charset=utf-8');
144 echo json_encode($nodeinfo, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
150 function nodeinfo_cron() {
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);
158 $plugin = 'statistics_json';
159 $plugins = Config::get('system', 'addon');
160 $plugins_arr = array();
163 $plugins_arr = explode(',',str_replace(' ', '',$plugins));
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));
174 if (!Config::get('system', 'nodeinfo')) {
177 $last = Config::get('nodeinfo', 'last_calucation');
180 // Calculate every 24 hours
181 $next = $last + (24 * 60 * 60);
182 if ($next > time()) {
183 logger('calculation intervall not reached');
187 logger('cron_start');
189 $users = qu("SELECT `user`.`uid`, `user`.`login_date`, `contact`.`last-item`
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;
201 $halfyear = time() - (180 * 24 * 60 * 60);
202 $month = time() - (30 * 24 * 60 * 60);
204 foreach ($users AS $user) {
205 if ((strtotime($user['login_date']) > $halfyear) OR
206 (strtotime($user['last-item']) > $halfyear)) {
207 ++$active_users_halfyear;
209 if ((strtotime($user['login_date']) > $month) OR
210 (strtotime($user['last-item']) > $month)) {
211 ++$active_users_monthly;
214 Config::set('nodeinfo', 'total_users', $total_users);
215 logger('total_users: '.$total_users, LOGGER_DEBUG);
217 Config::set('nodeinfo', 'active_users_halfyear', $active_users_halfyear);
218 Config::set('nodeinfo', 'active_users_monthly', $active_users_monthly);
221 $posts = qu("SELECT COUNT(*) AS local_posts FROM `thread` WHERE `thread`.`wall` AND `thread`.`uid` != 0");
223 if (!is_array($posts)) {
226 $local_posts = $posts[0]['local_posts'];
228 Config::set('nodeinfo', 'local_posts', $local_posts);
230 logger('local_posts: '.$local_posts, LOGGER_DEBUG);
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));
238 if (!is_array($posts)) {
239 $local_comments = -1;
241 $local_comments = $posts[0]['local_comments'];
243 Config::set('nodeinfo', 'local_comments', $local_comments);
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);
252 Config::set('nodeinfo', 'last_calucation', time());