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