Subversion Repositories oidplus

Rev

Rev 1000 | Rev 1029 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
635 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
776 daniel-mar 5
 * Copyright 2019 - 2022 Daniel Marschall, ViaThinkSoft
635 daniel-mar 6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
 
20
if (!defined('INSIDE_OIDPLUS')) die();
21
 
22
class OIDplusPageAdminRegistration extends OIDplusPagePluginAdmin {
23
 
24
        /*private*/ const QUERY_REGISTER_V1 =         '1.3.6.1.4.1.37476.2.5.2.1.1.1';
25
        /*private*/ const QUERY_UNREGISTER_V1 =       '1.3.6.1.4.1.37476.2.5.2.1.2.1';
26
        /*private*/ const QUERY_LISTALLSYSTEMIDS_V1 = '1.3.6.1.4.1.37476.2.5.2.1.3.1';
27
        /*private*/ const QUERY_LIVESTATUS_V1 =       '1.3.6.1.4.1.37476.2.5.2.1.4.1';
28
 
29
        public function csrfUnlock($actionID) {
30
                if ($actionID == 'verify_pubkey') return true;
31
                return parent::csrfUnlock($actionID);
32
        }
33
 
34
        public function action($actionID, $params) {
35
                if ($actionID == 'verify_pubkey') {
36
                        _CheckParamExists($params, 'challenge');
37
 
38
                        $payload = 'oidplus-verify-pubkey:'.sha3_512($params['challenge']);
39
 
40
                        $signature = '';
830 daniel-mar 41
                        if (!OIDplus::getPkiStatus() || !@openssl_sign($payload, $signature, OIDplus::getSystemPrivateKey())) {
635 daniel-mar 42
                                throw new OIDplusException(_L('Signature failed'));
43
                        }
44
 
45
                        return array(
46
                                "status" => 0,
47
                                "response" => base64_encode($signature)
48
                        );
49
                } else {
50
                        throw new OIDplusException(_L('Unknown action ID'));
51
                }
52
        }
53
 
54
        public function gui($id, &$out, &$handled) {
55
                if ($id === 'oidplus:srv_registration') {
56
                        $handled = true;
57
                        $out['title'] = _L('System registration settings');
801 daniel-mar 58
                        $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
635 daniel-mar 59
 
60
                        if (!OIDplus::authUtils()->isAdminLoggedIn()) {
800 daniel-mar 61
                                $out['icon'] = 'img/error.png';
635 daniel-mar 62
                                $out['text'] = '<p>'._L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')).'</p>';
63
                                return;
64
                        }
65
 
66
                        if (file_exists(__DIR__ . '/info$'.OIDplus::getCurrentLang().'.html')) {
67
                                $info = file_get_contents(__DIR__ . '/info$'.OIDplus::getCurrentLang().'.html');
68
                        } else {
69
                                $info = file_get_contents(__DIR__ . '/info.html');
70
                        }
71
 
72
                        list($html, $js, $css) = extractHtmlContents($info);
73
                        $info = '';
74
                        if (!empty($js))  $info .= "<script>\n$js\n</script>";
75
                        if (!empty($css)) $info .= "<style>\n$css\n</style>";
821 daniel-mar 76
                        $info .= stripHtmlComments($html);
635 daniel-mar 77
 
78
                        $out['text'] = $info;
79
 
80
                        if (!OIDplus::getPkiStatus()) {
81
                                $out['text'] .= '<p><font color="red">'._L('Error: Your system could not generate a private/public key pair. (OpenSSL is probably missing on your system). Therefore, you cannot register/unregister your OIDplus instance.').'</font></p>';
82
                        } else {
83
                                $out['text'] .= '<p><input type="button" onclick="openOidInPanel(\'oidplus:srvreg_status\');" value="'._L('Check status of the registration and collected data').'"></p>';
84
 
85
                                if (OIDplus::baseConfig()->getValue('REGISTRATION_HIDE_SYSTEM', false)) {
86
                                        $out['text'] .= '<p><font color="red"><b>'._L('Attention!').'</b> '._L('<code>REGISTRATION_HIDE_SYSTEM</code> is set in the local configuration file! Therefore, this system will not register itself, despite of the settings below.').'</font></p>';
87
                                }
88
 
89
                                $out['text'] .= '<p>'._L('You can adjust your privacy level here').':</p><p><select name="reg_privacy" id="reg_privacy">';
90
 
91
                                # ---
92
 
93
                                $out['text'] .= '<option value="0"';
94
                                if (OIDplus::config()->getValue('reg_privacy') == 0) {
95
                                        $out['text'] .= ' selected';
96
                                } else {
97
                                        $out['text'] .= '';
98
                                }
99
                                $out['text'] .= '>'._L('0 = Register to directory service and automatically publish RA/OID data at oid-info.com').'</option>';
100
 
101
                                # ---
102
 
103
                                $out['text'] .= '<option value="1"';
104
                                if (OIDplus::config()->getValue('reg_privacy') == 1) {
105
                                        $out['text'] .= ' selected';
106
                                } else {
107
                                        $out['text'] .= '';
108
                                }
109
                                $out['text'] .= '>'._L('1 = Only register to directory service').'</option>';
110
 
111
                                # ---
112
 
113
                                $out['text'] .= '<option value="2"';
114
                                if (OIDplus::config()->getValue('reg_privacy') == 2) {
115
                                        $out['text'] .= ' selected';
116
                                } else {
117
                                        $out['text'] .= '';
118
                                }
119
                                $out['text'] .= '>'._L('2 = Hide system').'</option>';
120
 
121
                                # ---
122
 
123
                                $out['text'] .= '</select> <input type="button" value="'._L('Change').'" onclick="OIDplusPageAdminRegistration.crudActionRegPrivacyUpdate()"></p>';
124
 
125
                                $out['text'] .= '<p>'._L('After clicking "change", your OIDplus system will contact the ViaThinkSoft server to adjust (add or remove information) your privacy setting. This may take a few minutes.').'</p>';
126
 
127
                                $out['text'] .= '<p>'._L('<i>Privacy information:</i> Please note that removing your system from the directory does not automatically delete information about OIDs which are already published at oid-info.com. To remove already submitted OIDs at oid-info.com, please contact the <a href="mailto:admin@oid-info.com">OID Repository Webmaster</a>.').'</p>';
128
                        }
129
                }
130
                if ($id === 'oidplus:srvreg_status') {
131
                        $handled = true;
132
                        $out['title'] = _L('Registration live status');
801 daniel-mar 133
                        $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
635 daniel-mar 134
 
135
                        if (!OIDplus::authUtils()->isAdminLoggedIn()) {
800 daniel-mar 136
                                $out['icon'] = 'img/error.png';
635 daniel-mar 137
                                $out['text'] = '<p>'._L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')).'</p>';
138
                                return;
139
                        }
140
 
141
                        $query = self::QUERY_LIVESTATUS_V1;
142
 
143
                        $payload = array(
777 daniel-mar 144
                                "query" => $query, // we must include $query to the playload, because we want to sign it
635 daniel-mar 145
                                "lang" => OIDplus::getCurrentLang(),
146
                                "system_id" => OIDplus::getSystemId(false)
147
                        );
148
 
149
                        $signature = '';
830 daniel-mar 150
                        if (!OIDplus::getPkiStatus() || !@openssl_sign(json_encode($payload), $signature, OIDplus::getSystemPrivateKey())) {
635 daniel-mar 151
                                throw new OIDplusException(_L('Signature failed'));
152
                        }
153
 
154
                        $data = array(
155
                                "payload" => $payload,
156
                                "signature" => base64_encode($signature)
157
                        );
158
 
159
                        if (!function_exists('curl_init')) {
160
                                throw new OIDplusException(_L('The "%1" PHP extension is not installed at your system. Please enable the PHP extension <code>%2</code>.','CURL','php_curl'));
161
                        }
162
 
163
                        $ch = curl_init();
164
                        if (ini_get('curl.cainfo') == '') curl_setopt($ch, CURLOPT_CAINFO, OIDplus::localpath() . 'vendor/cacert.pem');
165
                        curl_setopt($ch, CURLOPT_URL, 'https://oidplus.viathinksoft.com/reg2/query.php');
715 daniel-mar 166
                        curl_setopt($ch, CURLOPT_USERAGENT, 'ViaThinkSoft-OIDplus/2.0');
635 daniel-mar 167
                        curl_setopt($ch, CURLOPT_POST, 1);
168
                        if (function_exists('gzdeflate')) {
169
                                $compressed = "1";
170
                                $data2 = gzdeflate(json_encode($data));
171
                        } else {
172
                                $compressed = "0";
173
                                $data2 = json_encode($data);
174
                        }
175
                        curl_setopt($ch, CURLOPT_POSTFIELDS, "query=".urlencode($query)."&compressed=$compressed&data=".urlencode(base64_encode($data2)));
176
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
177
                        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
178
                        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
179
                        if (!($res = @curl_exec($ch))) {
180
                                throw new OIDplusException(_L('Communication with ViaThinkSoft server failed: %1',curl_error($ch)));
181
                        }
182
                        curl_close($ch);
183
 
184
                        $json = @json_decode($res, true);
185
 
186
                        if (!$json) {
800 daniel-mar 187
                                $out['icon'] = 'img/error.png';
635 daniel-mar 188
                                $out['text'] = _L('JSON reply from ViaThinkSoft decoding error: %1',$res);
189
                                return;
190
                        }
191
 
192
                        if (isset($json['error']) || ($json['status'] < 0)) {
800 daniel-mar 193
                                $out['icon'] = 'img/error.png';
635 daniel-mar 194
                                if (isset($json['error'])) {
195
                                        $out['text'] = _L('Received error status code: %1',$json['error']);
196
                                } else {
197
                                        $out['text'] = _L('Received error status code: %1',$json['status']);
198
                                }
199
                                return;
200
                        }
201
 
202
                        $out['text']  = '<p><a '.OIDplus::gui()->link('oidplus:srv_registration').'><img src="img/arrow_back.png" width="16" alt="'._L('Go back').'"> '._L('Go back to registration settings').'</a></p>' .
203
                                        $json['content'];
204
                }
205
        }
206
 
207
        protected function areWeRegistered() {
208
                // To check if we are registered. Check it "anonymously" (i.e. without revealing our system ID)
715 daniel-mar 209
                $res = url_get_contents('https://oidplus.viathinksoft.com/reg2/query.php?query='.self::QUERY_LISTALLSYSTEMIDS_V1);
635 daniel-mar 210
 
211
                $json = @json_decode($res, true);
212
 
213
                if (!$json) {
214
                        return false; // throw new OIDplusException(_L('JSON reply from ViaThinkSoft decoding error: %1',$res));
215
                }
216
 
217
                if (isset($json['error']) || ($json['status'] < 0)) {
218
                        if (isset($json['error'])) {
219
                                return false; // throw new OIDplusException(_L('Received error status code: %1',$json['error']));
220
                        } else {
221
                                return false; // throw new OIDplusException(_L('Received error status code: %1',$json['status']));
222
                        }
223
                }
224
 
225
                $list = $json['list'];
226
 
227
                return in_array(OIDplus::getSystemId(false), $list);
228
        }
229
 
230
        public function sendRegistrationQuery($privacy_level=null) {
231
                if (is_null($privacy_level)) {
232
                        $privacy_level = OIDplus::config()->getValue('reg_privacy');
233
                }
234
 
801 daniel-mar 235
                $system_url = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL);
635 daniel-mar 236
 
237
                // It is very important that we set the ping time NOW, because ViaThinkSoft might contact us during the ping,
238
                // and this would cause an endless loop!
239
                OIDplus::config()->setValue('reg_last_ping', time());
240
 
241
                if (!OIDplus::getPkiStatus()) return false;
242
 
243
                if ($privacy_level == 2) {
244
                        // The user wants to unregister,  but we only unregister if we are registered
245
                        if ($this->areWeRegistered()) {
246
                                $query = self::QUERY_UNREGISTER_V1;
247
 
248
                                $payload = array(
777 daniel-mar 249
                                        "query" => $query, // we must include $query to the payload, because we want to sign it
635 daniel-mar 250
                                        "system_id" => OIDplus::getSystemId(false)
251
                                );
252
 
253
                                $signature = '';
830 daniel-mar 254
                                if (!OIDplus::getPkiStatus() || !@openssl_sign(json_encode($payload), $signature, OIDplus::getSystemPrivateKey())) {
635 daniel-mar 255
                                        return false; // throw new OIDplusException(_L('Signature failed'));
256
                                }
257
 
258
                                $data = array(
259
                                        "payload" => $payload,
260
                                        "signature" => base64_encode($signature)
261
                                );
262
 
263
                                if (!function_exists('curl_init')) {
264
                                        return false; // throw new OIDplusException(_L('The "%1" PHP extension is not installed at your system. Please enable the PHP extension <code>%2</code>.','CURL','php_curl'));
265
                                }
266
 
267
                                $ch = curl_init();
268
                                if (ini_get('curl.cainfo') == '') curl_setopt($ch, CURLOPT_CAINFO, OIDplus::localpath() . 'vendor/cacert.pem');
269
                                curl_setopt($ch, CURLOPT_URL, 'https://oidplus.viathinksoft.com/reg2/query.php');
715 daniel-mar 270
                                curl_setopt($ch, CURLOPT_USERAGENT, 'ViaThinkSoft-OIDplus/2.0');
635 daniel-mar 271
                                curl_setopt($ch, CURLOPT_POST, 1);
272
                                if (function_exists('gzdeflate')) {
273
                                        $compressed = "1";
274
                                        $data2 = gzdeflate(json_encode($data));
275
                                } else {
276
                                        $compressed = "0";
277
                                        $data2 = json_encode($data);
278
                                }
279
                                curl_setopt($ch, CURLOPT_POSTFIELDS, "query=".urlencode($query)."&compressed=$compressed&data=".urlencode(base64_encode($data2)));
280
                                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
281
                                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
282
                                curl_setopt($ch, CURLOPT_AUTOREFERER, true);
283
                                if (!($res = @curl_exec($ch))) {
284
                                        return false; // throw new OIDplusException(_L('Communication with ViaThinkSoft server failed: %1',curl_error($ch)));
285
                                }
286
                                curl_close($ch);
287
 
288
                                $json = @json_decode($res, true);
289
 
290
                                if (!$json) {
291
                                        return false; // throw new OIDplusException(_L('JSON reply from ViaThinkSoft decoding error: %1',$res));
292
                                }
293
 
294
                                if (isset($json['error']) || ($json['status'] < 0)) {
295
                                        if (isset($json['error'])) {
296
                                                return false; // throw new OIDplusException(_L('Received error status code: %1',$json['error']));
297
                                        } else {
298
                                                return false; // throw new OIDplusException(_L('Received error status code: %1',$json['status']));
299
                                        }
300
                                }
301
                        }
302
                } else {
303
                        if ($privacy_level == 0) {
304
                                $adminExportPlugin = OIDplus::getPluginByOid('1.3.6.1.4.1.37476.2.5.2.4.3.400'); // OIDplusPageAdminOIDInfoExport
305
                                if (!is_null($adminExportPlugin)) {
941 daniel-mar 306
                                        list($oidinfo_xml, $dummy_content_type) = OIDplusPageAdminOIDInfoExport::outputXML(false); // no online check, because the query should be short (since the query is done while a visitor waits for the response)
635 daniel-mar 307
                                } else {
308
                                        $oidinfo_xml = false;
309
                                }
310
                        } else {
311
                                $oidinfo_xml = false;
312
                        }
313
 
314
                        $query = self::QUERY_REGISTER_V1;
315
 
316
                        $root_oids = array();
317
                        foreach (OIDplus::getEnabledObjectTypes() as $ot) {
318
                                if ($ot::ns() == 'oid') {
319
                                        $res = OIDplus::db()->query("select id from ###objects where " .
320
                                                                    "(parent = 'oid:' or " .
321
                                                                    // The following two cases are special cases e.g. if there are multiple PEN or UUID-OIDs, and the system owner decides to use the IANA PEN as root OID, but actually, it is not "his" root then! The OIDs inside the IANA PEN root are his root then!
322
                                                                    "parent in (select oid from ###asn1id where well_known = ?) or " .
323
                                                                    "parent in (select oid from ###iri where well_known = ?)) and " .
324
                                                                    // We assume hereby that RAs of well-known OIDs (e.g. IANA) will not use OIDplus for allocating OIDs:
325
                                                                    "id not in (select oid from ###asn1id where well_known = ?) and " .
326
                                                                    "id not in (select oid from ###iri where well_known = ?) " .
327
                                                                    "order by ".OIDplus::db()->natOrder('id'), array(true, true, true, true));
328
                                        while ($row = $res->fetch_array()) {
329
                                                $root_oids[] = substr($row['id'],strlen('oid:'));
330
                                        }
331
                                }
332
                        }
333
                        $payload = array(
777 daniel-mar 334
                                "query" => $query, // we must include $query to the payload, because we want to sign it
635 daniel-mar 335
                                "privacy_level" => $privacy_level,
336
                                "system_id" => OIDplus::getSystemId(false),
830 daniel-mar 337
                                "public_key" => OIDplus::getSystemPublicKey(),
635 daniel-mar 338
                                "system_url" => $system_url,
339
                                "hide_system_url" => 0,
340
                                "hide_public_key" => 0,
341
                                "admin_email" => OIDplus::config()->getValue('admin_email'),
342
                                "system_title" => OIDplus::config()->getValue('system_title'),
343
                                "oidinfo_xml" => @base64_encode($oidinfo_xml),
344
                                "root_oids" => $root_oids,
345
                                "system_version" => OIDplus::getVersion(),
346
                                "system_install_type" => OIDplus::getInstallType()
347
                        );
348
 
349
                        $signature = '';
830 daniel-mar 350
                        if (!OIDplus::getPkiStatus() || !@openssl_sign(json_encode($payload), $signature, OIDplus::getSystemPrivateKey())) {
635 daniel-mar 351
                                return false; // throw new OIDplusException(_L('Signature failed'));
352
                        }
353
 
354
                        $data = array(
355
                                "payload" => $payload,
356
                                "signature" => base64_encode($signature)
357
                        );
358
 
359
                        if (!function_exists('curl_init')) {
360
                                return false; // throw new OIDplusException(_L('The "%1" PHP extension is not installed at your system. Please enable the PHP extension <code>%2</code>.','CURL','php_curl'));
361
                        }
362
 
363
                        $ch = curl_init();
364
                        if (ini_get('curl.cainfo') == '') curl_setopt($ch, CURLOPT_CAINFO, OIDplus::localpath() . 'vendor/cacert.pem');
365
                        curl_setopt($ch, CURLOPT_URL, 'https://oidplus.viathinksoft.com/reg2/query.php');
715 daniel-mar 366
                        curl_setopt($ch, CURLOPT_USERAGENT, 'ViaThinkSoft-OIDplus/2.0');
635 daniel-mar 367
                        curl_setopt($ch, CURLOPT_POST, 1);
368
                        if (function_exists('gzdeflate')) {
369
                                $compressed = "1";
370
                                $data2 = gzdeflate(json_encode($data));
371
                        } else {
372
                                $compressed = "0";
373
                                $data2 = json_encode($data);
374
                        }
375
                        curl_setopt($ch, CURLOPT_POSTFIELDS, "query=".urlencode($query)."&compressed=$compressed&data=".urlencode(base64_encode($data2)));
376
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
377
                        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
378
                        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
379
                        if (!($res = @curl_exec($ch))) {
380
                                return false; // throw new OIDplusException(_L('Communication with ViaThinkSoft server failed: %1',curl_error($ch)));
381
                        }
382
                        curl_close($ch);
383
 
384
                        $json = @json_decode($res, true);
385
 
386
                        if (!$json) {
387
                                return false; // throw new OIDplusException(_L('JSON reply from ViaThinkSoft decoding error: %1',$res));
388
                        }
389
 
390
                        if (isset($json['error']) || ($json['status'] < 0)) {
391
                                if (isset($json['error'])) {
392
                                        return false; // throw new OIDplusException(_L('Received error status code: %1',$json['error']));
393
                                } else {
394
                                        return false; // throw new OIDplusException(_L('Received error status code: %1',$json['status']));
395
                                }
396
                        } else if ($json['status'] == 99/*Hash conflict*/) {
397
                                OIDplus::logger()->log("[WARN]A!", "Removing SystemID and key pair because there is a hash conflict with another OIDplus system!");
398
 
399
                                // Delete the system ID since we have a conflict with the 31-bit hash!
400
                                OIDplus::config()->setValue('oidplus_private_key', '');
401
                                OIDplus::config()->setValue('oidplus_public_key', '');
402
 
403
                                // Try to generate a new system ID
404
                                OIDplus::getPkiStatus(true);
405
 
406
                                // Enforce a new registration attempt at the next page visit
407
                                // We will not try again here, because that might lead to an endless loop if the VTS server would always return 'HASH_CONFLCIT'
408
                                OIDplus::config()->setValue('reg_last_ping', 0);
776 daniel-mar 409
                        } else if ($json['status'] == 0/*OK*/) {
410
                                // Note: whois.viathinksoft.de:43 uses VGWhoIs, which uses these patterns: https://github.com/danielmarschall/vgwhois/blob/master/main/pattern/oid
411
                                // If your system gets acknowledged by ViaThinkSoft, then vts_whois will be filled with that server name whois.viathinksoft.de:43
412
                                if (isset($json['vts_whois'])) OIDplus::config()->setValue('vts_whois', $json['vts_whois']);
413
 
414
                                // ViaThinkSoft certifies the system public key and other system attributes and root objects (requires human verification)
415
                                if (isset($json['vts_cert'])) OIDplus::config()->setValue('vts_cert', $json['vts_cert']);
416
                                if (isset($json['vts_ca'])) OIDplus::config()->setValue('vts_ca', $json['vts_ca']);
635 daniel-mar 417
                        }
418
                }
419
        }
420
 
421
        public function init($html=true) {
699 daniel-mar 422
                if (OIDplus::getEditionInfo()['vendor'] != 'ViaThinkSoft') {
423
                        throw new OIDplusException(_L('This plugin is only available in the ViaThinkSoft edition of OIDplus'));
424
                }
425
 
635 daniel-mar 426
                // Note: It is important that the default value is '2', otherwise, systems which don't have CURL will fail
427
                OIDplus::config()->prepareConfigKey('reg_privacy', '2=Hide your system, 1=Register your system to the ViaThinkSoft directory and oid-info.com, 0=Publish your system to ViaThinkSoft directory and all public contents (RA/OID) to oid-info.com', '2', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
428
                        if (($value != '0') && ($value != '1') && ($value != '2')) {
429
                                throw new OIDplusException(_L('Please enter either 0, 1 or 2.'));
430
                        }
431
                        // Now do a recheck and notify the ViaThinkSoft server
432
                        if (($value == 2) || !OIDplus::baseConfig()->getValue('REGISTRATION_HIDE_SYSTEM', false)) {
433
                                OIDplus::config()->setValue('reg_last_ping', 0);
434
                                $this->sendRegistrationQuery($value);
435
                        }
436
                });
437
                OIDplus::config()->prepareConfigKey('reg_ping_interval', 'Registration ping interval (in seconds)', '3600', OIDplusConfig::PROTECTION_HIDDEN, function($value) {
438
 
439
                });
440
                OIDplus::config()->prepareConfigKey('reg_last_ping', 'Last ping to ViaThinkSoft directory services', '0', OIDplusConfig::PROTECTION_HIDDEN, function($value) {
441
 
442
                });
776 daniel-mar 443
                OIDplus::config()->prepareConfigKey('vts_whois', 'ViaThinkSoft Whois Server (if this system is recognized)', '', OIDplusConfig::PROTECTION_READONLY, function($value) {
444
 
445
                });
446
                OIDplus::config()->prepareConfigKey('vts_cert', 'ViaThinkSoft certificate (requires registration)', '', OIDplusConfig::PROTECTION_HIDDEN, function($value) {
447
 
448
                });
449
                OIDplus::config()->prepareConfigKey('vts_ca', 'ViaThinkSoft certificate root (requires registration)', '', OIDplusConfig::PROTECTION_HIDDEN, function($value) {
450
 
451
                });
635 daniel-mar 452
                OIDplus::config()->prepareConfigKey('oobe_registration_done', '"Out Of Box Experience" wizard for OIDplusPageAdminRegistration done once?', '0', OIDplusConfig::PROTECTION_HIDDEN, function($value) {});
453
 
454
                // Is it time to register / renew the directory entry?
455
                // Note: REGISTRATION_HIDE_SYSTEM is an undocumented constant that can be put in the userdata/baseconfig/config.inc.php files of a test system accessing the same database as the productive system that is registered.
456
                // This avoids that the URL of a productive system is overridden with the URL of a cloned test system (since they use the same database, they also have the same system ID)
457
 
458
                if (OIDplus::config()->getValue('oobe_registration_done') == '1') {
459
                        if (!OIDplus::baseConfig()->getValue('REGISTRATION_HIDE_SYSTEM', false)) {
460
                                $privacy_level = OIDplus::config()->getValue('reg_privacy');
461
 
462
                                if (PHP_SAPI !== 'cli') { // don't register when called from CLI, otherwise the oidinfo XML can't convert relative links into absolute links
786 daniel-mar 463
                                        $last_ping = OIDplus::config()->getValue('reg_last_ping');
464
                                        if (!is_numeric($last_ping)) $last_ping = 0;
465
                                        $last_ping_interval = OIDplus::config()->getValue('reg_ping_interval');
466
                                        if (!is_numeric($last_ping_interval)) $last_ping_interval = 3600;
852 daniel-mar 467
 
468
                                        // Cronjobs get half ping interval, to make sure that a web visitor won't get any delay
469
                                        if (OIDplus::isCronjob()) $last_ping_interval /= 2;
470
 
786 daniel-mar 471
                                        if ((time()-$last_ping >= $last_ping_interval)) {
635 daniel-mar 472
                                                $this->sendRegistrationQuery();
473
                                        }
474
                                }
475
                        }
476
                }
477
        }
478
 
479
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
480
                if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
481
 
800 daniel-mar 482
                if (file_exists(__DIR__.'/img/main_icon16.png')) {
801 daniel-mar 483
                        $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
635 daniel-mar 484
                } else {
485
                        $tree_icon = null; // default icon (folder)
486
                }
487
 
488
                $json[] = array(
489
                        'id' => 'oidplus:srv_registration',
490
                        'icon' => $tree_icon,
491
                        'text' => _L('System registration')
492
                );
493
 
494
                return true;
495
        }
496
 
497
        public function tree_search($request) {
498
                return false;
499
        }
500
 
501
        public function implementsFeature($id) {
502
                if (strtolower($id) == '1.3.6.1.4.1.37476.2.5.2.3.1') return true; // oobeEntry, oobeRequested
1000 daniel-mar 503
                if (strtolower($id) == '1.3.6.1.4.1.37476.2.5.2.3.8') return true; // getNotifications()
635 daniel-mar 504
                return false;
505
        }
506
 
507
        public function oobeRequested(): bool {
508
                // Interface 1.3.6.1.4.1.37476.2.5.2.3.1
509
 
510
                return OIDplus::config()->getValue('oobe_registration_done') == '0';
511
        }
512
 
513
        public function oobeEntry($step, $do_edits, &$errors_happened)/*: void*/ {
514
                // Interface 1.3.6.1.4.1.37476.2.5.2.3.1
515
 
516
                echo '<p><u>'._L('Step %1: System registration and automatic publishing (optional)',$step).'</u></p>';
517
 
518
                if (file_exists(__DIR__ . '/info$'.OIDplus::getCurrentLang().'.html')) {
519
                        $info = file_get_contents(__DIR__ . '/info$'.OIDplus::getCurrentLang().'.html');
520
                } else {
521
                        $info = file_get_contents(__DIR__ . '/info.html');
522
                }
523
 
524
                // make sure the program works even if the user provided HTML is not UTF-8
721 daniel-mar 525
                $info = convert_to_utf8_no_bom($info);
635 daniel-mar 526
 
527
                echo $info;
528
 
529
                if (!function_exists('curl_init')) {
530
                        echo '<p><font color="red">';
531
                        echo _L('The "%1" PHP extension is not installed at your system. Please enable the PHP extension <code>%2</code>.','CURL','php_curl').' ';
532
                        echo _L('Therefore, you <b>cannot</b> register your OIDplus instance now.');
533
                        echo '</font></p>';
534
                        if ($do_edits) {
535
                                OIDplus::config()->setValue('oobe_registration_done', '1');
536
                        }
537
                        return;
538
                }
539
 
540
                $pki_status = OIDplus::getPkiStatus();
541
 
542
                if (!$pki_status) {
543
                        echo '<p><font color="red">';
544
                        echo _L('Your system could not generate a private/public key pair. (OpenSSL is probably missing on your system).').' ';
545
                        echo _L('Therefore, you <b>cannot</b> register your OIDplus instance now.');
546
                        echo '</font></p>';
547
                        if ($do_edits) {
548
                                OIDplus::config()->setValue('oobe_registration_done', '1');
549
                        }
550
                        return;
551
                }
552
 
553
                echo '<p>'._L('Privacy level').':</p><select name="reg_privacy" id="reg_privacy">';
554
 
555
                # ---
556
 
557
                echo '<option value="0"';
558
                if (isset($_REQUEST['sent'])) {
559
                        if (isset($_REQUEST['reg_privacy']) && ($_REQUEST['reg_privacy'] == 0)) echo ' selected';
560
                } else {
561
                        if ((OIDplus::config()->getValue('reg_privacy') == 0) || !OIDplus::config()->getValue('oobe_registration_done')) {
562
                                echo ' selected';
563
                        } else {
564
                                echo '';
565
                        }
566
                }
567
                echo '>'._L('0 = Register to directory service and automatically publish RA/OID data at oid-info.com').'</option>';
568
 
569
                # ---
570
 
571
                echo '<option value="1"';
572
                if (isset($_REQUEST['sent'])) {
573
                        if (isset($_REQUEST['reg_privacy']) && ($_REQUEST['reg_privacy'] == 1)) echo ' selected';
574
                } else {
575
                        if ((OIDplus::config()->getValue('reg_privacy') == 1)) {
576
                                echo ' selected';
577
                        } else {
578
                                echo '';
579
                        }
580
                }
581
                echo '>'._L('1 = Only register to directory service').'</option>';
582
 
583
                # ---
584
 
585
                echo '<option value="2"';
586
                if (isset($_REQUEST['sent'])) {
587
                        if (isset($_REQUEST['reg_privacy']) && ($_REQUEST['reg_privacy'] == 2)) echo ' selected';
588
                } else {
589
                        if ((OIDplus::config()->getValue('reg_privacy') == 2)) {
590
                                echo ' selected';
591
                        } else {
592
                                echo '';
593
                        }
594
                }
595
                echo '>'._L('2 = Hide system').'</option>';
596
 
597
                # ---
598
 
599
                echo '</select>';
600
 
601
                $msg = '';
602
                if ($do_edits) {
603
                        try {
604
                                OIDplus::config()->setValue('reg_privacy', isset($_REQUEST['reg_privacy']) ? $_REQUEST['reg_privacy'] : 1);
605
                                OIDplus::config()->setValue('oobe_registration_done', '1');
606
                        } catch (Exception $e) {
607
                                $msg = $e->getMessage();
608
                                $errors_happened = true;
609
                        }
610
                }
611
                echo ' <font color="red"><b>'.$msg.'</b></font>';
612
 
613
                echo '<p>'._L('<i>Privacy information:</i> This setting can always be changed in the administrator login / control panel.').'<br>';
614
                echo _L('<a %1>Click here</a> for more information about privacy related topics.','href="../../../../res/OIDplus/privacy_documentation.html" target="_blank"');
615
                echo '</p>';
616
        }
617
 
1000 daniel-mar 618
        public function getNotifications($user=null): array {
619
                // Interface 1.3.6.1.4.1.37476.2.5.2.3.8
620
                $notifications = array();
621
                if ((!$user || ($user == 'admin')) && OIDplus::authUtils()->isAdminLoggedIn()) {
622
                        if (!function_exists('curl_init')) {
623
                                $title = _L('System registration');
1008 daniel-mar 624
                                $notifications[] = array('ERR', _L('OIDplus plugin "%1" is enabled, but the required PHP extension "%2" is not installed.', '<a '.OIDplus::gui()->link('oidplus:srv_registration').'>'.htmlentities($title).'</a>', 'php_curl'));
1000 daniel-mar 625
                        }
626
                }
627
                return $notifications;
628
        }
629
 
635 daniel-mar 630
}