]> git.mxchange.org Git - friendica.git/blob - mod/nodeinfo.php
Merge pull request #2 from friendica/master
[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\Addon;
10 use Friendica\Core\System;
11 use Friendica\Core\Config;
12 use Friendica\Util\Network;
13
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']]];
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                 System::httpExit(404);
26                 killme();
27         }
28
29         if (($a->argc != 2) || ($a->argv[1] != '1.0')) {
30                 System::httpExit(404);
31                 killme();
32         }
33
34         $smtp = (function_exists('imap_open') && !Config::get('system', 'imap_disabled') && !Config::get('system', 'dfrn_only'));
35
36         $nodeinfo = [];
37         $nodeinfo['version'] = '1.0';
38         $nodeinfo['software'] = ['name' => 'friendica', 'version' => FRIENDICA_VERSION.'-'.DB_UPDATE_VERSION];
39
40         $nodeinfo['protocols'] = [];
41         $nodeinfo['protocols']['inbound'] = [];
42         $nodeinfo['protocols']['outbound'] = [];
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'] = [];
58         $nodeinfo['services']['inbound'] = [];
59         $nodeinfo['services']['outbound'] = [];
60
61         $nodeinfo['usage'] = [];
62
63         $nodeinfo['openRegistrations'] = ($a->config['register_policy'] != 0);
64
65         $nodeinfo['metadata'] = ['nodeName' => $a->config['sitename']];
66
67         if (Config::get('system', 'nodeinfo')) {
68
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');
74
75                 if (Addon::isEnabled('appnet')) {
76                         $nodeinfo['services']['inbound'][] = 'appnet';
77                 }
78                 if (Addon::isEnabled('appnet') || Addon::isEnabled('buffer')) {
79                         $nodeinfo['services']['outbound'][] = 'appnet';
80                 }
81                 if (Addon::isEnabled('blogger')) {
82                         $nodeinfo['services']['outbound'][] = 'blogger';
83                 }
84                 if (Addon::isEnabled('dwpost')) {
85                         $nodeinfo['services']['outbound'][] = 'dreamwidth';
86                 }
87                 if (Addon::isEnabled('fbpost') || Addon::isEnabled('buffer')) {
88                         $nodeinfo['services']['outbound'][] = 'facebook';
89                 }
90                 if (Addon::isEnabled('statusnet')) {
91                         $nodeinfo['services']['inbound'][] = 'gnusocial';
92                         $nodeinfo['services']['outbound'][] = 'gnusocial';
93                 }
94
95                 if (Addon::isEnabled('gpluspost') || Addon::isEnabled('buffer')) {
96                         $nodeinfo['services']['outbound'][] = 'google';
97                 }
98                 if (Addon::isEnabled('ijpost')) {
99                         $nodeinfo['services']['outbound'][] = 'insanejournal';
100                 }
101                 if (Addon::isEnabled('libertree')) {
102                         $nodeinfo['services']['outbound'][] = 'libertree';
103                 }
104                 if (Addon::isEnabled('buffer')) {
105                         $nodeinfo['services']['outbound'][] = 'linkedin';
106                 }
107                 if (Addon::isEnabled('ljpost')) {
108                         $nodeinfo['services']['outbound'][] = 'livejournal';
109                 }
110                 if (Addon::isEnabled('buffer')) {
111                         $nodeinfo['services']['outbound'][] = 'pinterest';
112                 }
113                 if (Addon::isEnabled('posterous')) {
114                         $nodeinfo['services']['outbound'][] = 'posterous';
115                 }
116                 if (Addon::isEnabled('pumpio')) {
117                         $nodeinfo['services']['inbound'][] = 'pumpio';
118                         $nodeinfo['services']['outbound'][] = 'pumpio';
119                 }
120
121                 if ($smtp) {
122                         $nodeinfo['services']['outbound'][] = 'smtp';
123                 }
124                 if (Addon::isEnabled('tumblr')) {
125                         $nodeinfo['services']['outbound'][] = 'tumblr';
126                 }
127                 if (Addon::isEnabled('twitter') || Addon::isEnabled('buffer')) {
128                         $nodeinfo['services']['outbound'][] = 'twitter';
129                 }
130                 if (Addon::isEnabled('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 (Addon::isEnabled('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 addon 'statistics_json' is enabled then disable it and actrivate nodeinfo.
157         if (Addon::isEnabled('statistics_json')) {
158                 Config::set('system', 'nodeinfo', true);
159
160                 $addon = 'statistics_json';
161                 $addons = Config::get('system', 'addon');
162                 $addons_arr = [];
163
164                 if ($addons) {
165                         $addons_arr = explode(',',str_replace(' ', '',$addons));
166
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));
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(*) 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));
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 = Network::fetchUrl($url);
251         logger('registering answer: '.$ret, LOGGER_DEBUG);
252
253         logger('cron_end');
254         Config::set('nodeinfo', 'last_calucation', time());
255 }