]> git.mxchange.org Git - friendica.git/blob - mod/nodeinfo.php
Use DBA::exists() in Photo::exists()
[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\Config;
11 use Friendica\Core\Logger;
12 use Friendica\Core\System;
13 use Friendica\Database\DBA;
14 use Friendica\Util\Network;
15 function nodeinfo_wellknown(App $a) {
16         $nodeinfo = ['links' => [['rel' => 'http://nodeinfo.diaspora.software/ns/schema/1.0',
17                                         'href' => System::baseUrl().'/nodeinfo/1.0']]];
18
19         header('Content-type: application/json; charset=utf-8');
20         echo json_encode($nodeinfo, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
21         exit;
22 }
23
24 function nodeinfo_init(App $a) {
25         if (!Config::get('system', 'nodeinfo')) {
26                 System::httpExit(404);
27                 killme();
28         }
29
30         if (($a->argc != 2) || ($a->argv[1] != '1.0')) {
31                 System::httpExit(404);
32                 killme();
33         }
34
35         $smtp = (function_exists('imap_open') && !Config::get('system', 'imap_disabled') && !Config::get('system', 'dfrn_only'));
36
37         $nodeinfo = [];
38         $nodeinfo['version'] = '1.0';
39         $nodeinfo['software'] = ['name' => 'friendica', 'version' => FRIENDICA_VERSION.'-'.DB_UPDATE_VERSION];
40
41         $nodeinfo['protocols'] = [];
42         $nodeinfo['protocols']['inbound'] = [];
43         $nodeinfo['protocols']['outbound'] = [];
44
45         if (Config::get('system', 'diaspora_enabled')) {
46                 $nodeinfo['protocols']['inbound'][] = 'diaspora';
47                 $nodeinfo['protocols']['outbound'][] = 'diaspora';
48         }
49
50         $nodeinfo['protocols']['inbound'][] = 'friendica';
51         $nodeinfo['protocols']['outbound'][] = 'friendica';
52
53         if (!Config::get('system', 'ostatus_disabled')) {
54                 $nodeinfo['protocols']['inbound'][] = 'gnusocial';
55                 $nodeinfo['protocols']['outbound'][] = 'gnusocial';
56         }
57
58         $nodeinfo['services'] = [];
59         $nodeinfo['services']['inbound'] = [];
60         $nodeinfo['services']['outbound'] = [];
61
62         $nodeinfo['usage'] = [];
63
64         $nodeinfo['openRegistrations'] = intval(Config::get('config', 'register_policy')) !== REGISTER_CLOSED;
65
66         $nodeinfo['metadata'] = ['nodeName' => Config::get('config', 'sitename')];
67
68         if (Config::get('system', 'nodeinfo')) {
69
70                 $nodeinfo['usage']['users'] = ['total' => (int)Config::get('nodeinfo', 'total_users'),
71                                         'activeHalfyear' => (int)Config::get('nodeinfo', 'active_users_halfyear'),
72                                         'activeMonth' => (int)Config::get('nodeinfo', 'active_users_monthly')];
73                 $nodeinfo['usage']['localPosts'] = (int)Config::get('nodeinfo', 'local_posts');
74                 $nodeinfo['usage']['localComments'] = (int)Config::get('nodeinfo', 'local_comments');
75
76                 if (Addon::isEnabled('blogger')) {
77                         $nodeinfo['services']['outbound'][] = 'blogger';
78                 }
79                 if (Addon::isEnabled('dwpost')) {
80                         $nodeinfo['services']['outbound'][] = 'dreamwidth';
81                 }
82                 if (Addon::isEnabled('statusnet')) {
83                         $nodeinfo['services']['inbound'][] = 'gnusocial';
84                         $nodeinfo['services']['outbound'][] = 'gnusocial';
85                 }
86                 if (Addon::isEnabled('ijpost')) {
87                         $nodeinfo['services']['outbound'][] = 'insanejournal';
88                 }
89                 if (Addon::isEnabled('libertree')) {
90                         $nodeinfo['services']['outbound'][] = 'libertree';
91                 }
92                 if (Addon::isEnabled('buffer')) {
93                         $nodeinfo['services']['outbound'][] = 'linkedin';
94                 }
95                 if (Addon::isEnabled('ljpost')) {
96                         $nodeinfo['services']['outbound'][] = 'livejournal';
97                 }
98                 if (Addon::isEnabled('buffer')) {
99                         $nodeinfo['services']['outbound'][] = 'pinterest';
100                 }
101                 if (Addon::isEnabled('posterous')) {
102                         $nodeinfo['services']['outbound'][] = 'posterous';
103                 }
104                 if (Addon::isEnabled('pumpio')) {
105                         $nodeinfo['services']['inbound'][] = 'pumpio';
106                         $nodeinfo['services']['outbound'][] = 'pumpio';
107                 }
108
109                 if ($smtp) {
110                         $nodeinfo['services']['outbound'][] = 'smtp';
111                 }
112                 if (Addon::isEnabled('tumblr')) {
113                         $nodeinfo['services']['outbound'][] = 'tumblr';
114                 }
115                 if (Addon::isEnabled('twitter') || Addon::isEnabled('buffer')) {
116                         $nodeinfo['services']['outbound'][] = 'twitter';
117                 }
118                 if (Addon::isEnabled('wppost')) {
119                         $nodeinfo['services']['outbound'][] = 'wordpress';
120                 }
121                 $nodeinfo['metadata']['protocols'] = $nodeinfo['protocols'];
122                 $nodeinfo['metadata']['protocols']['outbound'][] = 'atom1.0';
123                 $nodeinfo['metadata']['protocols']['inbound'][] = 'atom1.0';
124                 $nodeinfo['metadata']['protocols']['inbound'][] = 'rss2.0';
125
126                 $nodeinfo['metadata']['services'] = $nodeinfo['services'];
127
128                 if (Addon::isEnabled('twitter')) {
129                         $nodeinfo['metadata']['services']['inbound'][] = 'twitter';
130                 }
131
132                 $nodeinfo['metadata']['explicitContent'] = Config::get('system', 'explicit_content', false) == true;
133         }
134
135         header('Content-type: application/json; charset=utf-8');
136         echo json_encode($nodeinfo, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
137         exit;
138 }
139
140
141
142 function nodeinfo_cron() {
143
144         $a = \get_app();
145
146         // If the addon 'statistics_json' is enabled then disable it and activate nodeinfo.
147         if (Addon::isEnabled('statistics_json')) {
148                 Config::set('system', 'nodeinfo', true);
149
150                 $addon = 'statistics_json';
151                 $addons = Config::get('system', 'addon');
152                 $addons_arr = [];
153
154                 if ($addons) {
155                         $addons_arr = explode(',',str_replace(' ', '',$addons));
156
157                         $idx = array_search($addon, $addons_arr);
158                         if ($idx !== false) {
159                                 unset($addons_arr[$idx]);
160                                 Addon::uninstall($addon);
161                                 Config::set('system', 'addon', implode(', ',$addons_arr));
162                         }
163                 }
164         }
165
166         if (!Config::get('system', 'nodeinfo')) {
167                 return;
168         }
169
170         Logger::log('cron_start');
171
172         $users = q("SELECT `user`.`uid`, `user`.`login_date`, `contact`.`last-item`
173                         FROM `user`
174                         INNER JOIN `profile` ON `profile`.`uid` = `user`.`uid` AND `profile`.`is-default`
175                         INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`
176                         WHERE (`profile`.`publish` OR `profile`.`net-publish`) AND `user`.`verified`
177                                 AND NOT `user`.`blocked` AND NOT `user`.`account_removed`
178                                 AND NOT `user`.`account_expired`");
179         if (is_array($users)) {
180                 $total_users = count($users);
181                 $active_users_halfyear = 0;
182                 $active_users_monthly = 0;
183
184                 $halfyear = time() - (180 * 24 * 60 * 60);
185                 $month = time() - (30 * 24 * 60 * 60);
186
187                 foreach ($users AS $user) {
188                         if ((strtotime($user['login_date']) > $halfyear) ||
189                                 (strtotime($user['last-item']) > $halfyear)) {
190                                 ++$active_users_halfyear;
191                         }
192                         if ((strtotime($user['login_date']) > $month) ||
193                                 (strtotime($user['last-item']) > $month)) {
194                                 ++$active_users_monthly;
195                         }
196                 }
197                 Config::set('nodeinfo', 'total_users', $total_users);
198                 Config::set('nodeinfo', 'active_users_halfyear', $active_users_halfyear);
199                 Config::set('nodeinfo', 'active_users_monthly', $active_users_monthly);
200
201                 Logger::log('total_users: ' . $total_users . '/' . $active_users_halfyear. '/' . $active_users_monthly, Logger::DEBUG);
202         }
203
204         $local_posts = DBA::count('thread', ["`wall` AND NOT `deleted` AND `uid` != 0"]);
205         Config::set('nodeinfo', 'local_posts', $local_posts);
206         Logger::log('local_posts: ' . $local_posts, Logger::DEBUG);
207
208         $local_comments = DBA::count('item', ["`origin` AND `id` != `parent` AND NOT `deleted` AND `uid` != 0"]);
209         Config::set('nodeinfo', 'local_comments', $local_comments);
210         Logger::log('local_comments: ' . $local_comments, Logger::DEBUG);
211
212         // Now trying to register
213         $url = 'http://the-federation.info/register/'.$a->getHostName();
214         Logger::log('registering url: '.$url, Logger::DEBUG);
215         $ret = Network::fetchUrl($url);
216         Logger::log('registering answer: '.$ret, Logger::DEBUG);
217
218         Logger::log('cron_end');
219 }