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