]> git.mxchange.org Git - friendica.git/blob - mod/nodeinfo.php
Improve Console/Config display for array values
[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 require_once 'include/dba.php';
14
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('appnet')) {
77                         $nodeinfo['services']['inbound'][] = 'appnet';
78                 }
79                 if (Addon::isEnabled('appnet') || Addon::isEnabled('buffer')) {
80                         $nodeinfo['services']['outbound'][] = 'appnet';
81                 }
82                 if (Addon::isEnabled('blogger')) {
83                         $nodeinfo['services']['outbound'][] = 'blogger';
84                 }
85                 if (Addon::isEnabled('dwpost')) {
86                         $nodeinfo['services']['outbound'][] = 'dreamwidth';
87                 }
88                 if (Addon::isEnabled('fbpost') || Addon::isEnabled('buffer')) {
89                         $nodeinfo['services']['outbound'][] = 'facebook';
90                 }
91                 if (Addon::isEnabled('statusnet')) {
92                         $nodeinfo['services']['inbound'][] = 'gnusocial';
93                         $nodeinfo['services']['outbound'][] = 'gnusocial';
94                 }
95
96                 if (Addon::isEnabled('gpluspost') || Addon::isEnabled('buffer')) {
97                         $nodeinfo['services']['outbound'][] = 'google';
98                 }
99                 if (Addon::isEnabled('ijpost')) {
100                         $nodeinfo['services']['outbound'][] = 'insanejournal';
101                 }
102                 if (Addon::isEnabled('libertree')) {
103                         $nodeinfo['services']['outbound'][] = 'libertree';
104                 }
105                 if (Addon::isEnabled('buffer')) {
106                         $nodeinfo['services']['outbound'][] = 'linkedin';
107                 }
108                 if (Addon::isEnabled('ljpost')) {
109                         $nodeinfo['services']['outbound'][] = 'livejournal';
110                 }
111                 if (Addon::isEnabled('buffer')) {
112                         $nodeinfo['services']['outbound'][] = 'pinterest';
113                 }
114                 if (Addon::isEnabled('posterous')) {
115                         $nodeinfo['services']['outbound'][] = 'posterous';
116                 }
117                 if (Addon::isEnabled('pumpio')) {
118                         $nodeinfo['services']['inbound'][] = 'pumpio';
119                         $nodeinfo['services']['outbound'][] = 'pumpio';
120                 }
121
122                 if ($smtp) {
123                         $nodeinfo['services']['outbound'][] = 'smtp';
124                 }
125                 if (Addon::isEnabled('tumblr')) {
126                         $nodeinfo['services']['outbound'][] = 'tumblr';
127                 }
128                 if (Addon::isEnabled('twitter') || Addon::isEnabled('buffer')) {
129                         $nodeinfo['services']['outbound'][] = 'twitter';
130                 }
131                 if (Addon::isEnabled('wppost')) {
132                         $nodeinfo['services']['outbound'][] = 'wordpress';
133                 }
134                 $nodeinfo['metadata']['protocols'] = $nodeinfo['protocols'];
135                 $nodeinfo['metadata']['protocols']['outbound'][] = 'atom1.0';
136                 $nodeinfo['metadata']['protocols']['inbound'][] = 'atom1.0';
137                 $nodeinfo['metadata']['protocols']['inbound'][] = 'rss2.0';
138
139                 $nodeinfo['metadata']['services'] = $nodeinfo['services'];
140
141                 if (Addon::isEnabled('twitter')) {
142                         $nodeinfo['metadata']['services']['inbound'][] = 'twitter';
143                 }
144
145                 $nodeinfo['metadata']['explicitContent'] = Config::get('system', 'explicit_content', false) == true;
146         }
147
148         header('Content-type: application/json; charset=utf-8');
149         echo json_encode($nodeinfo, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
150         exit;
151 }
152
153
154
155 function nodeinfo_cron() {
156
157         $a = get_app();
158
159         // If the addon 'statistics_json' is enabled then disable it and activate nodeinfo.
160         if (Addon::isEnabled('statistics_json')) {
161                 Config::set('system', 'nodeinfo', true);
162
163                 $addon = 'statistics_json';
164                 $addons = Config::get('system', 'addon');
165                 $addons_arr = [];
166
167                 if ($addons) {
168                         $addons_arr = explode(',',str_replace(' ', '',$addons));
169
170                         $idx = array_search($addon, $addons_arr);
171                         if ($idx !== false) {
172                                 unset($addons_arr[$idx]);
173                                 Addon::uninstall($addon);
174                                 Config::set('system', 'addon', implode(', ',$addons_arr));
175                         }
176                 }
177         }
178
179         if (!Config::get('system', 'nodeinfo')) {
180                 return;
181         }
182
183         logger('cron_start');
184
185         $users = q("SELECT `user`.`uid`, `user`.`login_date`, `contact`.`last-item`
186                         FROM `user`
187                         INNER JOIN `profile` ON `profile`.`uid` = `user`.`uid` AND `profile`.`is-default`
188                         INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`
189                         WHERE (`profile`.`publish` OR `profile`.`net-publish`) AND `user`.`verified`
190                                 AND NOT `user`.`blocked` AND NOT `user`.`account_removed`
191                                 AND NOT `user`.`account_expired`");
192         if (is_array($users)) {
193                 $total_users = count($users);
194                 $active_users_halfyear = 0;
195                 $active_users_monthly = 0;
196
197                 $halfyear = time() - (180 * 24 * 60 * 60);
198                 $month = time() - (30 * 24 * 60 * 60);
199
200                 foreach ($users AS $user) {
201                         if ((strtotime($user['login_date']) > $halfyear) ||
202                                 (strtotime($user['last-item']) > $halfyear)) {
203                                 ++$active_users_halfyear;
204                         }
205                         if ((strtotime($user['login_date']) > $month) ||
206                                 (strtotime($user['last-item']) > $month)) {
207                                 ++$active_users_monthly;
208                         }
209                 }
210                 Config::set('nodeinfo', 'total_users', $total_users);
211                 Config::set('nodeinfo', 'active_users_halfyear', $active_users_halfyear);
212                 Config::set('nodeinfo', 'active_users_monthly', $active_users_monthly);
213
214                 logger('total_users: ' . $total_users . '/' . $active_users_halfyear. '/' . $active_users_monthly, LOGGER_DEBUG);
215         }
216
217         $local_posts = dba::count('thread', ["`wall` AND NOT `deleted` AND `uid` != 0"]);
218         Config::set('nodeinfo', 'local_posts', $local_posts);
219         logger('local_posts: ' . $local_posts, LOGGER_DEBUG);
220
221         $local_comments = dba::count('item', ["`origin` AND `id` != `parent` AND NOT `deleted` AND `uid` != 0"]);
222         Config::set('nodeinfo', 'local_comments', $local_comments);
223         logger('local_comments: ' . $local_comments, LOGGER_DEBUG);
224
225         // Now trying to register
226         $url = 'http://the-federation.info/register/'.$a->get_hostname();
227         logger('registering url: '.$url, LOGGER_DEBUG);
228         $ret = Network::fetchUrl($url);
229         logger('registering answer: '.$ret, LOGGER_DEBUG);
230
231         logger('cron_end');
232 }