Subversion Repositories oidplus

Rev

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

Rev Author Line No. Line
139 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
511 daniel-mar 5
 * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
139 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
 
511 daniel-mar 20
if (!defined('INSIDE_OIDPLUS')) die();
21
 
256 daniel-mar 22
class OIDplusPageAdminRegistration extends OIDplusPagePluginAdmin {
139 daniel-mar 23
 
269 daniel-mar 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';
256 daniel-mar 28
 
139 daniel-mar 29
        public function gui($id, &$out, &$handled) {
141 daniel-mar 30
                if ($id === 'oidplus:srv_registration') {
139 daniel-mar 31
                        $handled = true;
360 daniel-mar 32
                        $out['title'] = _L('System registration settings');
241 daniel-mar 33
                        $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : '';
139 daniel-mar 34
 
35
                        if (!OIDplus::authUtils()::isAdminLoggedIn()) {
36
                                $out['icon'] = 'img/error_big.png';
360 daniel-mar 37
                                $out['text'] = '<p>'._L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login')).'</p>';
281 daniel-mar 38
                                return;
39
                        }
40
 
362 daniel-mar 41
                        if (file_exists(__DIR__ . '/info$'.OIDplus::getCurrentLang().'.html')) {
42
                                $info = file_get_contents(__DIR__ . '/info$'.OIDplus::getCurrentLang().'.html');
43
                        } else {
44
                                $info = file_get_contents(__DIR__ . '/info.html');
45
                        }
395 daniel-mar 46
 
386 daniel-mar 47
                        list($html, $js, $css) = extractHtmlContents($info);
48
                        $info = '';
49
                        if (!empty($js))  $info .= "<script>\n$js\n</script>";
50
                        if (!empty($css)) $info .= "<style>\n$css\n</style>";
51
                        $info .= $html;
281 daniel-mar 52
 
362 daniel-mar 53
                        $out['text'] = $info;
54
 
281 daniel-mar 55
                        if (!OIDplus::getPkiStatus()) {
360 daniel-mar 56
                                $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>';
139 daniel-mar 57
                        } else {
360 daniel-mar 58
                                $out['text'] .= '<p><input type="button" onclick="openOidInPanel(\'oidplus:srvreg_status\');" value="'._L('Check status of the registration and collected data').'"></p>';
277 daniel-mar 59
 
281 daniel-mar 60
                                if (OIDplus::baseConfig()->getValue('REGISTRATION_HIDE_SYSTEM', false)) {
360 daniel-mar 61
                                        $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>';
281 daniel-mar 62
                                }
139 daniel-mar 63
 
360 daniel-mar 64
                                $out['text'] .= '<p>'._L('You can adjust your privacy level here').':</p><p><select name="reg_privacy" id="reg_privacy">';
227 daniel-mar 65
 
281 daniel-mar 66
                                # ---
139 daniel-mar 67
 
281 daniel-mar 68
                                $out['text'] .= '<option value="0"';
69
                                if (OIDplus::config()->getValue('reg_privacy') == 0) {
70
                                        $out['text'] .= ' selected';
71
                                } else {
72
                                        $out['text'] .= '';
73
                                }
360 daniel-mar 74
                                $out['text'] .= '>'._L('0 = Register to directory service and automatically publish RA/OID data at oid-info.com').'</option>';
139 daniel-mar 75
 
281 daniel-mar 76
                                # ---
139 daniel-mar 77
 
281 daniel-mar 78
                                $out['text'] .= '<option value="1"';
79
                                if (OIDplus::config()->getValue('reg_privacy') == 1) {
80
                                        $out['text'] .= ' selected';
81
                                } else {
82
                                        $out['text'] .= '';
83
                                }
360 daniel-mar 84
                                $out['text'] .= '>'._L('1 = Only register to directory service').'</option>';
139 daniel-mar 85
 
281 daniel-mar 86
                                # ---
139 daniel-mar 87
 
281 daniel-mar 88
                                $out['text'] .= '<option value="2"';
89
                                if (OIDplus::config()->getValue('reg_privacy') == 2) {
90
                                        $out['text'] .= ' selected';
91
                                } else {
92
                                        $out['text'] .= '';
93
                                }
360 daniel-mar 94
                                $out['text'] .= '>'._L('2 = Hide system').'</option>';
139 daniel-mar 95
 
281 daniel-mar 96
                                # ---
139 daniel-mar 97
 
360 daniel-mar 98
                                $out['text'] .= '</select> <input type="button" value="'._L('Change').'" onclick="crudActionRegPrivacyUpdate()"></p>';
139 daniel-mar 99
 
360 daniel-mar 100
                                $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>';
139 daniel-mar 101
 
360 daniel-mar 102
                                $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>';
139 daniel-mar 103
                        }
104
                }
141 daniel-mar 105
                if ($id === 'oidplus:srvreg_status') {
106
                        $handled = true;
446 daniel-mar 107
                        $out['title'] = _L('Registration live status');
108
                        $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : '';
141 daniel-mar 109
 
446 daniel-mar 110
                        if (!OIDplus::authUtils()::isAdminLoggedIn()) {
111
                                $out['icon'] = 'img/error_big.png';
112
                                $out['text'] = '<p>'._L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login')).'</p>';
113
                                return;
114
                        }
115
 
256 daniel-mar 116
                        $query = self::QUERY_LIVESTATUS_V1;
141 daniel-mar 117
 
118
                        $payload = array(
119
                                "query" => $query, // we must repeat the query because we want to sign it
446 daniel-mar 120
                                "lang" => OIDplus::getCurrentLang(),
227 daniel-mar 121
                                "system_id" => OIDplus::getSystemId(false)
141 daniel-mar 122
                        );
123
 
124
                        $signature = '';
465 daniel-mar 125
                        if (!OIDplus::getPkiStatus() || !@openssl_sign(json_encode($payload), $signature, OIDplus::config()->getValue('oidplus_private_key'))) {
360 daniel-mar 126
                                throw new OIDplusException(_L('Signature failed'));
239 daniel-mar 127
                        }
141 daniel-mar 128
 
129
                        $data = array(
130
                                "payload" => $payload,
131
                                "signature" => base64_encode($signature)
132
                        );
133
 
484 daniel-mar 134
                        if (!function_exists('curl_init')) {
464 daniel-mar 135
                                throw new Exception(_L('The "%1" PHP extension is not installed at your system. Please enable the PHP extension <code>%2</code>.','CURL','php_curl'));
463 daniel-mar 136
                        }
137
 
141 daniel-mar 138
                        $ch = curl_init();
496 daniel-mar 139
                        if (ini_get('curl.cainfo') == '') curl_setopt($ch, CURLOPT_CAINFO, OIDplus::localpath() . '3p/certs/cacert.pem');
141 daniel-mar 140
                        curl_setopt($ch, CURLOPT_URL, 'https://oidplus.viathinksoft.com/reg2/query.php');
141
                        curl_setopt($ch, CURLOPT_POST, 1);
464 daniel-mar 142
                        if (function_exists('gzdeflate')) {
143
                                $compressed = "1";
144
                                $data2 = gzdeflate(json_encode($data));
145
                        } else {
146
                                $compressed = "0";
147
                                $data2 = json_encode($data);
148
                        }
149
                        curl_setopt($ch, CURLOPT_POSTFIELDS, "query=".urlencode($query)."&compressed=$compressed&data=".urlencode(base64_encode($data2)));
141 daniel-mar 150
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
151
                        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
152
                        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
239 daniel-mar 153
                        if (!($res = @curl_exec($ch))) {
360 daniel-mar 154
                                throw new OIDplusException(_L('Communication with ViaThinkSoft server failed: %1',curl_error($ch)));
239 daniel-mar 155
                        }
141 daniel-mar 156
                        curl_close($ch);
157
 
370 daniel-mar 158
                        $json = @json_decode($res, true);
159
 
160
                        if (!$json) {
161
                                $out['icon'] = 'img/error_big.png';
162
                                $out['text'] = _L('JSON reply from ViaThinkSoft decoding error: %1',$res);
163
                                return;
164
                        }
165
 
381 daniel-mar 166
                        if (isset($json['error']) || ($json['status'] < 0)) {
370 daniel-mar 167
                                $out['icon'] = 'img/error_big.png';
168
                                if (isset($json['error'])) {
169
                                        $out['text'] = _L('Received error status code: %1',$json['error']);
170
                                } else {
171
                                        $out['text'] = _L('Received error status code: %1',$json['status']);
172
                                }
173
                                return;
174
                        }
175
 
366 daniel-mar 176
                        $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>' .
372 daniel-mar 177
                                        $json['content'];
141 daniel-mar 178
                }
139 daniel-mar 179
        }
180
 
370 daniel-mar 181
        protected function areWeRegistered() {
182
                // To check if we are registered. Check it "anonymously" (i.e. without revealing our system ID)
183
                $res = file_get_contents('https://oidplus.viathinksoft.com/reg2/query.php?query='.self::QUERY_LISTALLSYSTEMIDS_V1);
184
 
185
                $json = @json_decode($res, true);
186
 
187
                if (!$json) {
188
                        return false; // throw new OIDplusException(_L('JSON reply from ViaThinkSoft decoding error: %1',$res));
189
                }
190
 
381 daniel-mar 191
                if (isset($json['error']) || ($json['status'] < 0)) {
370 daniel-mar 192
                        if (isset($json['error'])) {
193
                                return false; // throw new OIDplusException(_L('Received error status code: %1',$json['error']));
194
                        } else {
195
                                return false; // throw new OIDplusException(_L('Received error status code: %1',$json['status']));
196
                        }
197
                }
198
 
199
                $list = $json['list'];
200
 
201
                return in_array(OIDplus::getSystemId(false), $list);
202
        }
203
 
140 daniel-mar 204
        public function sendRegistrationQuery($privacy_level=null) {
205
                if (is_null($privacy_level)) {
206
                        $privacy_level = OIDplus::config()->getValue('reg_privacy');
207
                }
139 daniel-mar 208
 
496 daniel-mar 209
                $system_url = OIDplus::webpath();
139 daniel-mar 210
 
175 daniel-mar 211
                // It is very important that we set the ping time NOW, because ViaThinkSoft might contact us during the ping,
212
                // and this would cause an endless loop!
213
                OIDplus::config()->setValue('reg_last_ping', time());
277 daniel-mar 214
 
239 daniel-mar 215
                if (!OIDplus::getPkiStatus()) return false;
175 daniel-mar 216
 
139 daniel-mar 217
                if ($privacy_level == 2) {
370 daniel-mar 218
                        // The user wants to unregister,  but we only unregister if we are registered
219
                        if ($this->areWeRegistered()) {
256 daniel-mar 220
                                $query = self::QUERY_UNREGISTER_V1;
139 daniel-mar 221
 
222
                                $payload = array(
223
                                        "query" => $query, // we must repeat the query because we want to sign it
227 daniel-mar 224
                                        "system_id" => OIDplus::getSystemId(false)
139 daniel-mar 225
                                );
226
 
227
                                $signature = '';
465 daniel-mar 228
                                if (!OIDplus::getPkiStatus() || !@openssl_sign(json_encode($payload), $signature, OIDplus::config()->getValue('oidplus_private_key'))) {
360 daniel-mar 229
                                        return false; // throw new OIDplusException(_L('Signature failed'));
239 daniel-mar 230
                                }
139 daniel-mar 231
 
232
                                $data = array(
233
                                        "payload" => $payload,
234
                                        "signature" => base64_encode($signature)
235
                                );
236
 
484 daniel-mar 237
                                if (!function_exists('curl_init')) {
464 daniel-mar 238
                                        return false; // throw new Exception(_L('The "%1" PHP extension is not installed at your system. Please enable the PHP extension <code>%2</code>.','CURL','php_curl'));
463 daniel-mar 239
                                }
240
 
139 daniel-mar 241
                                $ch = curl_init();
496 daniel-mar 242
                                if (ini_get('curl.cainfo') == '') curl_setopt($ch, CURLOPT_CAINFO, OIDplus::localpath() . '3p/certs/cacert.pem');
139 daniel-mar 243
                                curl_setopt($ch, CURLOPT_URL, 'https://oidplus.viathinksoft.com/reg2/query.php');
244
                                curl_setopt($ch, CURLOPT_POST, 1);
464 daniel-mar 245
                                if (function_exists('gzdeflate')) {
246
                                        $compressed = "1";
247
                                        $data2 = gzdeflate(json_encode($data));
248
                                } else {
249
                                        $compressed = "0";
250
                                        $data2 = json_encode($data);
251
                                }
252
                                curl_setopt($ch, CURLOPT_POSTFIELDS, "query=".urlencode($query)."&compressed=$compressed&data=".urlencode(base64_encode($data2)));
139 daniel-mar 253
                                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
254
                                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
255
                                curl_setopt($ch, CURLOPT_AUTOREFERER, true);
239 daniel-mar 256
                                if (!($res = @curl_exec($ch))) {
360 daniel-mar 257
                                        return false; // throw new OIDplusException(_L('Communication with ViaThinkSoft server failed: %1',curl_error($ch)));
239 daniel-mar 258
                                }
139 daniel-mar 259
                                curl_close($ch);
370 daniel-mar 260
 
261
                                $json = @json_decode($res, true);
262
 
263
                                if (!$json) {
264
                                        return false; // throw new OIDplusException(_L('JSON reply from ViaThinkSoft decoding error: %1',$res));
265
                                }
266
 
381 daniel-mar 267
                                if (isset($json['error']) || ($json['status'] < 0)) {
370 daniel-mar 268
                                        if (isset($json['error'])) {
269
                                                return false; // throw new OIDplusException(_L('Received error status code: %1',$json['error']));
270
                                        } else {
271
                                                return false; // throw new OIDplusException(_L('Received error status code: %1',$json['status']));
272
                                        }
273
                                }
139 daniel-mar 274
                        }
275
                } else {
276
                        if ($privacy_level == 0) {
380 daniel-mar 277
                                $adminExportPlugin = OIDplus::getPluginByOid('1.3.6.1.4.1.37476.2.5.2.4.3.400'); // OIDplusPageAdminOIDInfoExport
278
                                if (!is_null($adminExportPlugin)) {
139 daniel-mar 279
                                        ob_start();
280
                                        OIDplusPageAdminOIDInfoExport::outputXML(false); // no online check, because the query should be short (since the query is done while a visitor waits for the response)
281
                                        $oidinfo_xml = ob_get_contents();
282
                                        ob_end_clean();
283
                                } else {
284
                                        $oidinfo_xml = false;
285
                                }
286
                        } else {
287
                                $oidinfo_xml = false;
288
                        }
289
 
256 daniel-mar 290
                        $query = self::QUERY_REGISTER_V1;
139 daniel-mar 291
 
292
                        $root_oids = array();
227 daniel-mar 293
                        foreach (OIDplus::getEnabledObjectTypes() as $ot) {
139 daniel-mar 294
                                if ($ot::ns() == 'oid') {
261 daniel-mar 295
                                        $res = OIDplus::db()->query("select id from ###objects where " .
396 daniel-mar 296
                                                                    "(parent = 'oid:' or " .
394 daniel-mar 297
                                                                    // 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!
443 daniel-mar 298
                                                                    "parent in (select oid from ###asn1id where well_known = ?) or " .
299
                                                                    "parent in (select oid from ###iri where well_known = ?)) and " .
396 daniel-mar 300
                                                                    // We assume hereby that RAs of well-known OIDs (e.g. IANA) will not use OIDplus for allocating OIDs:
443 daniel-mar 301
                                                                    "id not in (select oid from ###asn1id where well_known = ?) and " .
302
                                                                    "id not in (select oid from ###iri where well_known = ?) " .
303
                                                                    "order by ".OIDplus::db()->natOrder('id'), array(true, true, true, true));
236 daniel-mar 304
                                        while ($row = $res->fetch_array()) {
139 daniel-mar 305
                                                $root_oids[] = substr($row['id'],strlen('oid:'));
306
                                        }
307
                                }
308
                        }
309
                        $payload = array(
310
                                "query" => $query, // we must repeat the query because we want to sign it
311
                                "privacy_level" => $privacy_level,
227 daniel-mar 312
                                "system_id" => OIDplus::getSystemId(false),
139 daniel-mar 313
                                "public_key" => OIDplus::config()->getValue('oidplus_public_key'),
314
                                "system_url" => $system_url,
315
                                "hide_system_url" => 0,
316
                                "hide_public_key" => 0,
317
                                "admin_email" => OIDplus::config()->getValue('admin_email'),
257 daniel-mar 318
                                "system_title" => OIDplus::config()->getValue('system_title'),
139 daniel-mar 319
                                "oidinfo_xml" => @base64_encode($oidinfo_xml),
170 daniel-mar 320
                                "root_oids" => $root_oids,
321
                                "system_version" => OIDplus::getVersion(),
322
                                "system_install_type" => OIDplus::getInstallType()
139 daniel-mar 323
                        );
324
 
325
                        $signature = '';
465 daniel-mar 326
                        if (!OIDplus::getPkiStatus() || !@openssl_sign(json_encode($payload), $signature, OIDplus::config()->getValue('oidplus_private_key'))) {
396 daniel-mar 327
                                return false; // throw new OIDplusException(_L('Signature failed'));
239 daniel-mar 328
                        }
139 daniel-mar 329
 
330
                        $data = array(
331
                                "payload" => $payload,
332
                                "signature" => base64_encode($signature)
333
                        );
334
 
463 daniel-mar 335
 
484 daniel-mar 336
                        if (!function_exists('curl_init')) {
464 daniel-mar 337
                                return false; // throw new Exception(_L('The "%1" PHP extension is not installed at your system. Please enable the PHP extension <code>%2</code>.','CURL','php_curl'));
463 daniel-mar 338
                        }
339
 
139 daniel-mar 340
                        $ch = curl_init();
496 daniel-mar 341
                        if (ini_get('curl.cainfo') == '') curl_setopt($ch, CURLOPT_CAINFO, OIDplus::localpath() . '3p/certs/cacert.pem');
139 daniel-mar 342
                        curl_setopt($ch, CURLOPT_URL, 'https://oidplus.viathinksoft.com/reg2/query.php');
343
                        curl_setopt($ch, CURLOPT_POST, 1);
464 daniel-mar 344
                        if (function_exists('gzdeflate')) {
345
                                $compressed = "1";
346
                                $data2 = gzdeflate(json_encode($data));
347
                        } else {
348
                                $compressed = "0";
349
                                $data2 = json_encode($data);
350
                        }
351
                        curl_setopt($ch, CURLOPT_POSTFIELDS, "query=".urlencode($query)."&compressed=$compressed&data=".urlencode(base64_encode($data2)));
139 daniel-mar 352
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
353
                        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
354
                        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
239 daniel-mar 355
                        if (!($res = @curl_exec($ch))) {
360 daniel-mar 356
                                return false; // throw new OIDplusException(_L('Communication with ViaThinkSoft server failed: %1',curl_error($ch)));
239 daniel-mar 357
                        }
139 daniel-mar 358
                        curl_close($ch);
206 daniel-mar 359
 
370 daniel-mar 360
                        $json = @json_decode($res, true);
361
 
362
                        if (!$json) {
363
                                return false; // throw new OIDplusException(_L('JSON reply from ViaThinkSoft decoding error: %1',$res));
364
                        }
365
 
381 daniel-mar 366
                        if (isset($json['error']) || ($json['status'] < 0)) {
367
                                if (isset($json['error'])) {
368
                                        return false; // throw new OIDplusException(_L('Received error status code: %1',$json['error']));
369
                                } else {
370
                                        return false; // throw new OIDplusException(_L('Received error status code: %1',$json['status']));
371
                                }
372
                        } else if ($json['status'] == 99/*Hash conflict*/) {
288 daniel-mar 373
                                OIDplus::logger()->log("[WARN]A!", "Removing SystemID and key pair because there is a hash conflict with another OIDplus system!");
206 daniel-mar 374
 
375
                                // Delete the system ID since we have a conflict with the 31-bit hash!
376
                                OIDplus::config()->setValue('oidplus_private_key', '');
377
                                OIDplus::config()->setValue('oidplus_public_key', '');
378
 
379
                                // Try to generate a new system ID
227 daniel-mar 380
                                OIDplus::getPkiStatus(true);
206 daniel-mar 381
 
370 daniel-mar 382
                                // Enforce a new registration attempt at the next page visit
206 daniel-mar 383
                                // We will not try again here, because that might lead to an endless loop if the VTS server would always return 'HASH_CONFLCIT'
384
                                OIDplus::config()->setValue('reg_last_ping', 0);
385
                        }
139 daniel-mar 386
                }
387
        }
388
 
389
        public function init($html=true) {
463 daniel-mar 390
                // Note: It is important that the default value is '2', otherwise, systems which don't have CURL will fail
391
                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) {
263 daniel-mar 392
                        if (($value != '0') && ($value != '1') && ($value != '2')) {
360 daniel-mar 393
                                throw new OIDplusException(_L('Please enter either 0, 1 or 2.'));
263 daniel-mar 394
                        }
395
                        // Now do a recheck and notify the ViaThinkSoft server
316 daniel-mar 396
                        if (($value == 2) || !OIDplus::baseConfig()->getValue('REGISTRATION_HIDE_SYSTEM', false)) {
397
                                OIDplus::config()->setValue('reg_last_ping', 0);
398
                                $this->sendRegistrationQuery($value);
399
                        }
263 daniel-mar 400
                });
401
                OIDplus::config()->prepareConfigKey('reg_ping_interval', 'Registration ping interval (in seconds)', '3600', OIDplusConfig::PROTECTION_HIDDEN, function($value) {
277 daniel-mar 402
 
263 daniel-mar 403
                });
404
                OIDplus::config()->prepareConfigKey('reg_last_ping', 'Last ping to ViaThinkSoft directory services', '0', OIDplusConfig::PROTECTION_HIDDEN, function($value) {
277 daniel-mar 405
 
263 daniel-mar 406
                });
362 daniel-mar 407
                OIDplus::config()->prepareConfigKey('oobe_registration_done', '"Out Of Box Experience" wizard for OIDplusPageAdminRegistration done once?', '0', OIDplusConfig::PROTECTION_HIDDEN, function($value) {});
277 daniel-mar 408
 
292 daniel-mar 409
                // Is it time to register / renew the directory entry?
294 daniel-mar 410
                // 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.
292 daniel-mar 411
                // 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)
277 daniel-mar 412
 
463 daniel-mar 413
                if (OIDplus::config()->getValue('oobe_registration_done') == '1') {
414
                        if (!OIDplus::baseConfig()->getValue('REGISTRATION_HIDE_SYSTEM', false)) {
415
                                $privacy_level = OIDplus::config()->getValue('reg_privacy');
139 daniel-mar 416
 
463 daniel-mar 417
                                if (PHP_SAPI !== 'cli') { // don't register when called from CLI, otherwise the oidinfo XML can't convert relative links into absolute links
418
                                        if ((time()-OIDplus::config()->getValue('reg_last_ping') >= OIDplus::config()->getValue('reg_ping_interval'))) {
419
                                                $this->sendRegistrationQuery();
420
                                        }
139 daniel-mar 421
                                }
422
                        }
423
                }
424
        }
425
 
426
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
281 daniel-mar 427
                if (!OIDplus::authUtils()::isAdminLoggedIn()) return false;
292 daniel-mar 428
 
139 daniel-mar 429
                if (file_exists(__DIR__.'/treeicon.png')) {
241 daniel-mar 430
                        $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png';
139 daniel-mar 431
                } else {
432
                        $tree_icon = null; // default icon (folder)
433
                }
434
 
435
                $json[] = array(
141 daniel-mar 436
                        'id' => 'oidplus:srv_registration',
139 daniel-mar 437
                        'icon' => $tree_icon,
360 daniel-mar 438
                        'text' => _L('System registration')
139 daniel-mar 439
                );
440
 
441
                return true;
442
        }
443
 
444
        public function tree_search($request) {
445
                return false;
446
        }
292 daniel-mar 447
 
448
        public function implementsFeature($id) {
370 daniel-mar 449
                if (strtolower($id) == '1.3.6.1.4.1.37476.2.5.2.3.1') return true; // oobeEntry, oobeRequested
292 daniel-mar 450
                return false;
451
        }
452
 
362 daniel-mar 453
        public function oobeRequested(): bool {
454
                // Interface 1.3.6.1.4.1.37476.2.5.2.3.1
370 daniel-mar 455
 
362 daniel-mar 456
                return OIDplus::config()->getValue('oobe_registration_done') == '0';
457
        }
458
 
292 daniel-mar 459
        public function oobeEntry($step, $do_edits, &$errors_happened)/*: void*/ {
460
                // Interface 1.3.6.1.4.1.37476.2.5.2.3.1
461
 
360 daniel-mar 462
                echo '<p><u>'._L('Step %1: System registration and automatic publishing (optional)',$step).'</u></p>';
370 daniel-mar 463
 
362 daniel-mar 464
                if (file_exists(__DIR__ . '/info$'.OIDplus::getCurrentLang().'.html')) {
465
                        $info = file_get_contents(__DIR__ . '/info$'.OIDplus::getCurrentLang().'.html');
466
                } else {
467
                        $info = file_get_contents(__DIR__ . '/info.html');
468
                }
292 daniel-mar 469
 
362 daniel-mar 470
                // make sure the program works even if the user provided HTML is not UTF-8
471
                $info = iconv(mb_detect_encoding($info, mb_detect_order(), true), 'UTF-8//IGNORE', $info);
472
                $bom = pack('H*','EFBBBF');
473
                $info = preg_replace("/^$bom/", '', $info);
292 daniel-mar 474
 
362 daniel-mar 475
                echo $info;
476
 
484 daniel-mar 477
                if (!function_exists('curl_init')) {
360 daniel-mar 478
                        echo '<p><font color="red">';
464 daniel-mar 479
                        echo _L('The "%1" PHP extension is not installed at your system. Please enable the PHP extension <code>%2</code>.','CURL','php_curl').' ';
360 daniel-mar 480
                        echo _L('Therefore, you <b>cannot</b> register your OIDplus instance now.');
481
                        echo '</font></p>';
362 daniel-mar 482
                        if ($do_edits) {
483
                                OIDplus::config()->setValue('oobe_registration_done', '1');
484
                        }
353 daniel-mar 485
                        return;
486
                }
487
 
488
                $testurl = 'https://www.google.com/';
489
                $ch = curl_init();
496 daniel-mar 490
                if (ini_get('curl.cainfo') == '') curl_setopt($ch, CURLOPT_CAINFO, OIDplus::localpath() . '3p/certs/cacert.pem');
353 daniel-mar 491
                curl_setopt($ch, CURLOPT_URL, $testurl);
492
                curl_setopt($ch, CURLOPT_HEADER, TRUE);
493
                curl_setopt($ch, CURLOPT_NOBODY, TRUE);
494
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
495
                curl_exec($ch);
496
                $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
497
                curl_close($ch);
498
                if (!$httpCode) {
360 daniel-mar 499
                        echo '<p><font color="red">';
463 daniel-mar 500
                        echo _L('The "CURL" PHP extension cannot access HTTPS webpages. Therefore, you cannot use this feature. Please download <a href="https://curl.haxx.se/ca/cacert.pem">cacert.pem</a>, place it somewhere and then adjust the setting <code>curl.cainfo</code> in PHP.ini.').' ';
360 daniel-mar 501
                        echo _L('Therefore, you <b>cannot</b> register your OIDplus instance now.');
502
                        echo '</font></p>';
362 daniel-mar 503
                        if ($do_edits) {
504
                                OIDplus::config()->setValue('oobe_registration_done', '1');
505
                        }
353 daniel-mar 506
                        return;
507
                }
508
 
292 daniel-mar 509
                $pki_status = OIDplus::getPkiStatus();
510
 
511
                if (!$pki_status) {
360 daniel-mar 512
                        echo '<p><font color="red">';
463 daniel-mar 513
                        echo _L('Your system could not generate a private/public key pair. (OpenSSL is probably missing on your system).').' ';
360 daniel-mar 514
                        echo _L('Therefore, you <b>cannot</b> register your OIDplus instance now.');
515
                        echo '</font></p>';
362 daniel-mar 516
                        if ($do_edits) {
517
                                OIDplus::config()->setValue('oobe_registration_done', '1');
518
                        }
353 daniel-mar 519
                        return;
520
                }
292 daniel-mar 521
 
360 daniel-mar 522
                echo '<p>'._L('Privacy level').':</p><select name="reg_privacy" id="reg_privacy">';
292 daniel-mar 523
 
353 daniel-mar 524
                # ---
292 daniel-mar 525
 
353 daniel-mar 526
                echo '<option value="0"';
527
                if (isset($_REQUEST['sent'])) {
528
                        if (isset($_REQUEST['reg_privacy']) && ($_REQUEST['reg_privacy'] == 0)) echo ' selected';
529
                } else {
362 daniel-mar 530
                        if ((OIDplus::config()->getValue('reg_privacy') == 0) || !OIDplus::config()->getValue('oobe_registration_done')) {
353 daniel-mar 531
                                echo ' selected';
292 daniel-mar 532
                        } else {
353 daniel-mar 533
                                echo '';
292 daniel-mar 534
                        }
353 daniel-mar 535
                }
360 daniel-mar 536
                echo '>'._L('0 = Register to directory service and automatically publish RA/OID data at oid-info.com').'</option>';
292 daniel-mar 537
 
353 daniel-mar 538
                # ---
292 daniel-mar 539
 
353 daniel-mar 540
                echo '<option value="1"';
541
                if (isset($_REQUEST['sent'])) {
542
                        if (isset($_REQUEST['reg_privacy']) && ($_REQUEST['reg_privacy'] == 1)) echo ' selected';
543
                } else {
544
                        if ((OIDplus::config()->getValue('reg_privacy') == 1)) {
545
                                echo ' selected';
292 daniel-mar 546
                        } else {
353 daniel-mar 547
                                echo '';
292 daniel-mar 548
                        }
353 daniel-mar 549
                }
360 daniel-mar 550
                echo '>'._L('1 = Only register to directory service').'</option>';
292 daniel-mar 551
 
353 daniel-mar 552
                # ---
292 daniel-mar 553
 
353 daniel-mar 554
                echo '<option value="2"';
555
                if (isset($_REQUEST['sent'])) {
556
                        if (isset($_REQUEST['reg_privacy']) && ($_REQUEST['reg_privacy'] == 2)) echo ' selected';
557
                } else {
558
                        if ((OIDplus::config()->getValue('reg_privacy') == 2)) {
559
                                echo ' selected';
292 daniel-mar 560
                        } else {
353 daniel-mar 561
                                echo '';
292 daniel-mar 562
                        }
353 daniel-mar 563
                }
360 daniel-mar 564
                echo '>'._L('2 = Hide system').'</option>';
292 daniel-mar 565
 
353 daniel-mar 566
                # ---
292 daniel-mar 567
 
353 daniel-mar 568
                echo '</select>';
292 daniel-mar 569
 
353 daniel-mar 570
                $msg = '';
571
                if ($do_edits) {
572
                        try {
573
                                OIDplus::config()->setValue('reg_privacy', $_REQUEST['reg_privacy']);
362 daniel-mar 574
                                OIDplus::config()->setValue('oobe_registration_done', '1');
353 daniel-mar 575
                        } catch (Exception $e) {
576
                                $msg = $e->getMessage();
577
                                $errors_happened = true;
292 daniel-mar 578
                        }
353 daniel-mar 579
                }
580
                echo ' <font color="red"><b>'.$msg.'</b></font>';
292 daniel-mar 581
 
360 daniel-mar 582
                echo '<p>'._L('<i>Privacy information:</i> This setting can always be changed in the administrator login / control panel.').'<br>';
583
                echo _L('<a %1>Click here</a> for more information about privacy related topics.','href="../../../res/OIDplus/privacy_documentation.html" target="_blank"');
584
                echo '</p>';
292 daniel-mar 585
        }
586
 
463 daniel-mar 587
}