]> git.mxchange.org Git - friendica.git/blob - mod/nodeinfo.php
Merge pull request #3789 from Alkarex/french_regions
[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\App;
9 use Friendica\Core\System;
10 use Friendica\Core\Config;
11
12 require_once 'include/plugin.php';
13
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')));
17
18         header('Content-type: application/json; charset=utf-8');
19         echo json_encode($nodeinfo, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
20         exit;
21 }
22
23 function nodeinfo_init(App $a) {
24         if (!Config::get('system', 'nodeinfo')) {
25                 http_status_exit(404);
26                 killme();
27         }
28
29         if (($a->argc != 2) || ($a->argv[1] != '1.0')) {
30                 http_status_exit(404);
31                 killme();
32         }
33
34         $smtp = (function_exists('imap_open') && !Config::get('system', 'imap_disabled') && !Config::get('system', 'dfrn_only'));
35
36         $nodeinfo = array();
37         $nodeinfo['version'] = '1.0';
38         $nodeinfo['software'] = array('name' => 'friendica', 'version' => FRIENDICA_VERSION.'-'.DB_UPDATE_VERSION);
39
40         $nodeinfo['protocols'] = array();
41         $nodeinfo['protocols']['inbound'] = array();
42         $nodeinfo['protocols']['outbound'] = array();
43
44         if (Config::get('system', 'diaspora_enabled')) {
45                 $nodeinfo['protocols']['inbound'][] = 'diaspora';
46                 $nodeinfo['protocols']['outbound'][] = 'diaspora';
47         }
48
49         $nodeinfo['protocols']['inbound'][] = 'friendica';
50         $nodeinfo['protocols']['outbound'][] = 'friendica';
51
52         if (!Config::get('system', 'ostatus_disabled')) {
53                 $nodeinfo['protocols']['inbound'][] = 'gnusocial';
54                 $nodeinfo['protocols']['outbound'][] = 'gnusocial';
55         }
56
57         $nodeinfo['services'] = array();
58         $nodeinfo['services']['inbound'] = array();
59         $nodeinfo['services']['outbound'] = array();
60
61         $nodeinfo['usage'] = array();
62
63         $nodeinfo['openRegistrations'] = ($a->config['register_policy'] != 0);
64
65         $nodeinfo['metadata'] = array('nodeName' => $a->config['sitename']);
66
67         if (Config::get('system', 'nodeinfo')) {
68
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');
74
75                 if (plugin_enabled('appnet')) {
76                         $nodeinfo['services']['inbound'][] = 'appnet';
77                 }
78                 if (plugin_enabled('appnet') || plugin_enabled('buffer')) {
79                         $nodeinfo['services']['outbound'][] = 'appnet';
80                 }
81                 if (plugin_enabled('blogger')) {
82                         $nodeinfo['services']['outbound'][] = 'blogger';
83                 }
84                 if (plugin_enabled('dwpost')) {
85                         $nodeinfo['services']['outbound'][] = 'dreamwidth';
86                 }
87                 if (plugin_enabled('fbpost') || plugin_enabled('buffer')) {
88                         $nodeinfo['services']['outbound'][] = 'facebook';
89                 }
90                 if (plugin_enabled('statusnet')) {
91                         $nodeinfo['services']['inbound'][] = 'gnusocial';
92                         $nodeinfo['services']['outbound'][] = 'gnusocial';
93                 }
94
95                 if (plugin_enabled('gpluspost') || plugin_enabled('buffer')) {
96                         $nodeinfo['services']['outbound'][] = 'google';
97                 }
98                 if (plugin_enabled('ijpost')) {
99                         $nodeinfo['services']['outbound'][] = 'insanejournal';
100                 }
101                 if (plugin_enabled('libertree')) {
102                         $nodeinfo['services']['outbound'][] = 'libertree';
103                 }
104                 if (plugin_enabled('buffer')) {
105                         $nodeinfo['services']['outbound'][] = 'linkedin';
106                 }
107                 if (plugin_enabled('ljpost')) {
108                         $nodeinfo['services']['outbound'][] = 'livejournal';
109                 }
110                 if (plugin_enabled('buffer')) {
111                         $nodeinfo['services']['outbound'][] = 'pinterest';
112                 }
113                 if (plugin_enabled('posterous')) {
114                         $nodeinfo['services']['outbound'][] = 'posterous';
115                 }
116                 if (plugin_enabled('pumpio')) {
117                         $nodeinfo['services']['inbound'][] = 'pumpio';
118                         $nodeinfo['services']['outbound'][] = 'pumpio';
119                 }
120
121                 if ($smtp) {
122                         $nodeinfo['services']['outbound'][] = 'smtp';
123                 }
124                 if (plugin_enabled('tumblr')) {
125                         $nodeinfo['services']['outbound'][] = 'tumblr';
126                 }
127                 if (plugin_enabled('twitter') || plugin_enabled('buffer')) {
128                         $nodeinfo['services']['outbound'][] = 'twitter';
129                 }
130                 if (plugin_enabled('wppost')) {
131                         $nodeinfo['services']['outbound'][] = 'wordpress';
132                 }
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';
137
138                 $nodeinfo['metadata']['services'] = $nodeinfo['services'];
139
140                 if (plugin_enabled('twitter')) {
141                         $nodeinfo['metadata']['services']['inbound'][] = 'twitter';
142                 }
143         }
144
145         header('Content-type: application/json; charset=utf-8');
146         echo json_encode($nodeinfo, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
147         exit;
148 }
149
150
151
152 function nodeinfo_cron() {
153
154         $a = get_app();
155
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);
159
160                 $plugin = 'statistics_json';
161                 $plugins = Config::get('system', 'addon');
162                 $plugins_arr = array();
163
164                 if ($plugins) {
165                         $plugins_arr = explode(',',str_replace(' ', '',$plugins));
166
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));
172                         }
173                 }
174         }
175
176         if (!Config::get('system', 'nodeinfo')) {
177                 return;
178         }
179         $last = Config::get('nodeinfo', 'last_calucation');
180
181         if ($last) {
182                 // Calculate every 24 hours
183                 $next = $last + (24 * 60 * 60);
184                 if ($next > time()) {
185                         logger('calculation intervall not reached');
186                         return;
187                 }
188         }
189         logger('cron_start');
190
191         $users = q("SELECT `user`.`uid`, `user`.`login_date`, `contact`.`last-item`
192                         FROM `user`
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;
202
203                         $halfyear = time() - (180 * 24 * 60 * 60);
204                         $month = time() - (30 * 24 * 60 * 60);
205
206                         foreach ($users AS $user) {
207                                 if ((strtotime($user['login_date']) > $halfyear) ||
208                                         (strtotime($user['last-item']) > $halfyear)) {
209                                         ++$active_users_halfyear;
210                                 }
211                                 if ((strtotime($user['login_date']) > $month) ||
212                                         (strtotime($user['last-item']) > $month)) {
213                                         ++$active_users_monthly;
214                                 }
215                         }
216                         Config::set('nodeinfo', 'total_users', $total_users);
217                         logger('total_users: '.$total_users, LOGGER_DEBUG);
218
219                         Config::set('nodeinfo', 'active_users_halfyear', $active_users_halfyear);
220                         Config::set('nodeinfo', 'active_users_monthly', $active_users_monthly);
221         }
222
223         $posts = q("SELECT COUNT(*) AS local_posts FROM `thread` WHERE `thread`.`wall` AND `thread`.`uid` != 0");
224
225         if (!is_array($posts)) {
226                 $local_posts = -1;
227         } else {
228                 $local_posts = $posts[0]['local_posts'];
229         }
230         Config::set('nodeinfo', 'local_posts', $local_posts);
231
232         logger('local_posts: '.$local_posts, LOGGER_DEBUG);
233
234         $posts = q("SELECT COUNT(*) 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));
239
240         if (!is_array($posts)) {
241                 $local_comments = -1;
242         } else {
243                 $local_comments = $posts[0]['local_comments'];
244         }
245         Config::set('nodeinfo', 'local_comments', $local_comments);
246
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);
252
253         logger('cron_end');
254         Config::set('nodeinfo', 'last_calucation', time());
255 }