Subversion Repositories oidplus

Rev

Rev 261 | 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
5
 * Copyright 2019 Daniel Marschall, ViaThinkSoft
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('IN_OIDPLUS')) die();
21
 
256 daniel-mar 22
class OIDplusPageAdminRegistration extends OIDplusPagePluginAdmin {
139 daniel-mar 23
 
256 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';
28
 
222 daniel-mar 29
        public static function getPluginInformation() {
30
                $out = array();
31
                $out['name'] = 'System registration';
32
                $out['author'] = 'ViaThinkSoft';
33
                $out['version'] = null;
34
                $out['descriptionHTML'] = null;
35
                return $out;
36
        }
37
 
139 daniel-mar 38
        public function priority() {
39
                return 120;
40
        }
41
 
42
        public function action(&$handled) {
43
                // Nothing
44
        }
45
 
46
        public function gui($id, &$out, &$handled) {
141 daniel-mar 47
                if ($id === 'oidplus:srv_registration') {
139 daniel-mar 48
                        $handled = true;
141 daniel-mar 49
                        $out['title'] = 'System registration settings';
241 daniel-mar 50
                        $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : '';
139 daniel-mar 51
 
52
                        if (!OIDplus::authUtils()::isAdminLoggedIn()) {
53
                                $out['icon'] = 'img/error_big.png';
250 daniel-mar 54
                                $out['text'] = '<p>You need to <a '.OIDplus::gui()->link('oidplus:login').'>log in</a> as administrator.</p>';
139 daniel-mar 55
                        } else {
239 daniel-mar 56
                                $out['text'] = file_get_contents(__DIR__ . '/info.tpl');
57
 
58
                                if (!OIDplus::getPkiStatus()) {
59
                                        $out['text'] .= '<p><font color="red">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>';
60
                                } else {
61
                                        $out['text'] .= '<p><input type="button" onclick="openOidInPanel(\'oidplus:srvreg_status\');" value="Check status of the registration and collected data"></p>';
139 daniel-mar 62
 
261 daniel-mar 63
                                        if (OIDplus::baseConfig()->getValue('REGISTRATION_HIDE_SYSTEM', false)) {
239 daniel-mar 64
                                                $out['text'] .= '<p><font color="red"><b>Attention!</b> <code>REGISTRATION_HIDE_SYSTEM</code> is set in the local configuration file! Therefore, this system will not register itself, despire the settings below.</font></p>';
65
                                        }
227 daniel-mar 66
 
139 daniel-mar 67
                                        $out['text'] .= '<p>You can adjust your privacy level here:</p><p><select name="reg_privacy" id="reg_privacy">';
68
 
69
                                        # ---
70
 
71
                                        $out['text'] .= '<option value="0"';
72
                                        if (OIDplus::config()->getValue('reg_privacy') == 0) {
73
                                                $out['text'] .= ' selected';
74
                                        } else {
75
                                                $out['text'] .= '';
76
                                        }
77
                                        $out['text'] .= '>0 = Register to directory service and automatically publish RA/OID data at oid-info.com</option>';
78
 
79
                                        # ---
80
 
81
                                        $out['text'] .= '<option value="1"';
82
                                        if (OIDplus::config()->getValue('reg_privacy') == 1) {
83
                                                $out['text'] .= ' selected';
84
                                        } else {
85
                                                $out['text'] .= '';
86
                                        }
87
                                        $out['text'] .= '>1 = Only register to directory service</option>';
88
 
89
                                        # ---
90
 
91
                                        $out['text'] .= '<option value="2"';
92
                                        if (OIDplus::config()->getValue('reg_privacy') == 2) {
93
                                                $out['text'] .= ' selected';
94
                                        } else {
95
                                                $out['text'] .= '';
96
                                        }
97
                                        $out['text'] .= '>2 = Hide system</option>';
98
 
99
                                        # ---
100
 
101
                                        $out['text'] .= '</select> <input type="button" value="Change" onclick="crudActionRegPrivacyUpdate()"></p>';
102
 
103
                                        $out['text'] .= '<p>After clicking "change", your OIDplus installation will contact the ViaThinkSoft server to adjust (add or remove information) your privacy setting. This may take a few minutes.</p>';
239 daniel-mar 104
 
105
                                        $out['text'] .= '<p><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>.';
139 daniel-mar 106
                                }
107
                        }
108
                }
141 daniel-mar 109
                if ($id === 'oidplus:srvreg_status') {
110
                        $handled = true;
111
 
256 daniel-mar 112
                        $query = self::QUERY_LIVESTATUS_V1;
141 daniel-mar 113
 
114
                        $payload = array(
115
                                "query" => $query, // we must repeat the query because we want to sign it
227 daniel-mar 116
                                "system_id" => OIDplus::getSystemId(false)
141 daniel-mar 117
                        );
118
 
119
                        $signature = '';
239 daniel-mar 120
                        if (!@openssl_sign(json_encode($payload), $signature, OIDplus::config()->getValue('oidplus_private_key'))) {
250 daniel-mar 121
                                throw new OIDplusException("Signature failed");
239 daniel-mar 122
                        }
141 daniel-mar 123
 
124
                        $data = array(
125
                                "payload" => $payload,
126
                                "signature" => base64_encode($signature)
127
                        );
128
 
129
                        $ch = curl_init();
130
                        curl_setopt($ch, CURLOPT_URL, 'https://oidplus.viathinksoft.com/reg2/query.php');
131
                        curl_setopt($ch, CURLOPT_POST, 1);
132
                        curl_setopt($ch, CURLOPT_POSTFIELDS, "query=$query&data=".base64_encode(json_encode($data)));
133
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
134
                        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
135
                        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
239 daniel-mar 136
                        if (!($res = @curl_exec($ch))) {
250 daniel-mar 137
                                throw new OIDplusException("Communication with ViaThinkSoft server failed");
239 daniel-mar 138
                        }
141 daniel-mar 139
                        curl_close($ch);
140
                        // die("RES: $res\n");
141
                        // if ($res == 'OK') ...
142
 
143
                        $out['title'] = 'Registration live status';
250 daniel-mar 144
                        $out['text']  = '<p><a '.OIDplus::gui()->link('oidplus:srv_registration').'><img src="img/arrow_back.png" width="16"> Go back to registration settings</a></p>' .
142 daniel-mar 145
                                        $res;
141 daniel-mar 146
                }
139 daniel-mar 147
        }
148
 
140 daniel-mar 149
        public function sendRegistrationQuery($privacy_level=null) {
150
                if (is_null($privacy_level)) {
151
                        $privacy_level = OIDplus::config()->getValue('reg_privacy');
152
                }
139 daniel-mar 153
 
227 daniel-mar 154
                $system_url = OIDplus::getSystemUrl();
139 daniel-mar 155
 
175 daniel-mar 156
                // It is very important that we set the ping time NOW, because ViaThinkSoft might contact us during the ping,
157
                // and this would cause an endless loop!
158
                OIDplus::config()->setValue('reg_last_ping', time());
239 daniel-mar 159
 
160
                if (!OIDplus::getPkiStatus()) return false;
175 daniel-mar 161
 
139 daniel-mar 162
                if ($privacy_level == 2) {
163
                        // The user wants to unregister
164
                        // but we only unregister if we are registered. Check this "anonymously" (i.e. without revealing our system ID)
256 daniel-mar 165
                        if (in_array(OIDplus::getSystemId(false), explode(';',file_get_contents('https://oidplus.viathinksoft.com/reg2/query.php?query='.self::QUERY_LISTALLSYSTEMIDS_V1)))) {
166
                                $query = self::QUERY_UNREGISTER_V1;
139 daniel-mar 167
 
168
                                $payload = array(
169
                                        "query" => $query, // we must repeat the query because we want to sign it
227 daniel-mar 170
                                        "system_id" => OIDplus::getSystemId(false)
139 daniel-mar 171
                                );
172
 
173
                                $signature = '';
239 daniel-mar 174
                                if (!@openssl_sign(json_encode($payload), $signature, OIDplus::config()->getValue('oidplus_private_key'))) {
250 daniel-mar 175
                                        return false; // throw new OIDplusException("Signature failed");
239 daniel-mar 176
                                }
139 daniel-mar 177
 
178
                                $data = array(
179
                                        "payload" => $payload,
180
                                        "signature" => base64_encode($signature)
181
                                );
182
 
183
                                $ch = curl_init();
184
                                curl_setopt($ch, CURLOPT_URL, 'https://oidplus.viathinksoft.com/reg2/query.php');
185
                                curl_setopt($ch, CURLOPT_POST, 1);
186
                                curl_setopt($ch, CURLOPT_POSTFIELDS, "query=$query&data=".base64_encode(json_encode($data)));
187
                                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
188
                                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
189
                                curl_setopt($ch, CURLOPT_AUTOREFERER, true);
239 daniel-mar 190
                                if (!($res = @curl_exec($ch))) {
250 daniel-mar 191
                                        return false; // throw new OIDplusException("Communication with ViaThinkSoft server failed");
239 daniel-mar 192
                                }
139 daniel-mar 193
                                curl_close($ch);
194
                                // die("RES: $res\n");
195
                                // if ($res == 'OK') ...
196
                        }
197
                } else {
198
                        if ($privacy_level == 0) {
199
                                if (class_exists('OIDplusPageAdminOIDInfoExport')) {
200
                                        ob_start();
201
                                        OIDplusPageAdminOIDInfoExport::outputXML(false); // no online check, because the query should be short (since the query is done while a visitor waits for the response)
202
                                        $oidinfo_xml = ob_get_contents();
203
                                        ob_end_clean();
204
                                } else {
205
                                        $oidinfo_xml = false;
206
                                }
207
                        } else {
208
                                $oidinfo_xml = false;
209
                        }
210
 
256 daniel-mar 211
                        $query = self::QUERY_REGISTER_V1;
139 daniel-mar 212
 
213
                        $root_oids = array();
227 daniel-mar 214
                        foreach (OIDplus::getEnabledObjectTypes() as $ot) {
139 daniel-mar 215
                                if ($ot::ns() == 'oid') {
261 daniel-mar 216
                                        $res = OIDplus::db()->query("select id from ###objects where " .
139 daniel-mar 217
                                                                    "parent = 'oid:' " .
218
                                                                    "order by ".OIDplus::db()->natOrder('id'));
236 daniel-mar 219
                                        while ($row = $res->fetch_array()) {
139 daniel-mar 220
                                                $root_oids[] = substr($row['id'],strlen('oid:'));
221
                                        }
222
                                }
223
                        }
224
                        $payload = array(
225
                                "query" => $query, // we must repeat the query because we want to sign it
226
                                "privacy_level" => $privacy_level,
227 daniel-mar 227
                                "system_id" => OIDplus::getSystemId(false),
139 daniel-mar 228
                                "public_key" => OIDplus::config()->getValue('oidplus_public_key'),
229
                                "system_url" => $system_url,
230
                                "hide_system_url" => 0,
231
                                "hide_public_key" => 0,
232
                                "admin_email" => OIDplus::config()->getValue('admin_email'),
257 daniel-mar 233
                                "system_title" => OIDplus::config()->getValue('system_title'),
139 daniel-mar 234
                                "oidinfo_xml" => @base64_encode($oidinfo_xml),
170 daniel-mar 235
                                "root_oids" => $root_oids,
236
                                "system_version" => OIDplus::getVersion(),
237
                                "system_install_type" => OIDplus::getInstallType()
139 daniel-mar 238
                        );
239
 
240
                        $signature = '';
239 daniel-mar 241
                        if (!@openssl_sign(json_encode($payload), $signature, OIDplus::config()->getValue('oidplus_private_key'))) {
250 daniel-mar 242
                                        return false; // throw new OIDplusException("Signature failed");
239 daniel-mar 243
                        }
139 daniel-mar 244
 
245
                        $data = array(
246
                                "payload" => $payload,
247
                                "signature" => base64_encode($signature)
248
                        );
249
 
250
                        $ch = curl_init();
251
                        curl_setopt($ch, CURLOPT_URL, 'https://oidplus.viathinksoft.com/reg2/query.php');
252
                        curl_setopt($ch, CURLOPT_POST, 1);
253
                        curl_setopt($ch, CURLOPT_POSTFIELDS, "query=$query&data=".base64_encode(json_encode($data)));
254
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
255
                        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
256
                        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
239 daniel-mar 257
                        if (!($res = @curl_exec($ch))) {
250 daniel-mar 258
                                return false; // throw new OIDplusException("Communication with ViaThinkSoft server failed");
239 daniel-mar 259
                        }
139 daniel-mar 260
                        curl_close($ch);
206 daniel-mar 261
 
262
                        if ($res === 'HASH_CONFLICT') {
263
                                OIDplus::logger()->log("A!", "Removing SystemID and key pair because there is a hash conflict with another OIDplus system!");
264
 
265
                                // Delete the system ID since we have a conflict with the 31-bit hash!
266
                                OIDplus::config()->setValue('oidplus_private_key', '');
267
                                OIDplus::config()->setValue('oidplus_public_key', '');
268
 
269
                                // Try to generate a new system ID
227 daniel-mar 270
                                OIDplus::getPkiStatus(true);
206 daniel-mar 271
 
272
                                // Enforce a new registration attempt at the next run
273
                                // We will not try again here, because that might lead to an endless loop if the VTS server would always return 'HASH_CONFLCIT'
274
                                OIDplus::config()->setValue('reg_last_ping', 0);
275
                        }
276
 
139 daniel-mar 277
                        // die("RES: $res\n");
278
                        // if ($res == 'OK') ...
279
                }
280
        }
281
 
282
        public function init($html=true) {
263 daniel-mar 283
                OIDplus::config()->prepareConfigKey('reg_wizard_done', 'Registration wizard done once?', '0', OIDplusConfig::PROTECTION_HIDDEN, function($value) {
239 daniel-mar 284
 
263 daniel-mar 285
                });
286
                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', '0', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
287
                        if (($value != '0') && ($value != '1') && ($value != '2')) {
288
                                throw new OIDplusException("Please enter either 0, 1 or 2.");
289
                        }
290
                        // Now do a recheck and notify the ViaThinkSoft server
291
                        OIDplus::config()->setValue('reg_last_ping', 0);
292
                        $this->sendRegistrationQuery($value);
293
                });
294
                OIDplus::config()->prepareConfigKey('reg_ping_interval', 'Registration ping interval (in seconds)', '3600', OIDplusConfig::PROTECTION_HIDDEN, function($value) {
295
 
296
                });
297
                OIDplus::config()->prepareConfigKey('reg_last_ping', 'Last ping to ViaThinkSoft directory services', '0', OIDplusConfig::PROTECTION_HIDDEN, function($value) {
298
 
299
                });
300
 
239 daniel-mar 301
                $oobe_done = OIDplus::config()->getValue('reg_wizard_done') == '1';
302
 
303
                if (!$oobe_done) {
304
                        // Show registration/configuration wizard once
305
                        if ($html) {
139 daniel-mar 306
                                if (basename($_SERVER['SCRIPT_NAME']) != 'registration.php') {
241 daniel-mar 307
                                        header('Location:'.OIDplus::webpath(__DIR__).'registration.php');
240 daniel-mar 308
                                        die('Redirecting to registration wizard...');
139 daniel-mar 309
                                }
310
                        }
239 daniel-mar 311
                } else {
312
                        // Is it time to register / renew the directory entry?
313
                        // Note: REGISTRATION_HIDE_SYSTEM is an undocumented constant that can be put in the config.inc.php files of a test system accessing the same database as the productive system that is registered.
314
                        // 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)
139 daniel-mar 315
 
261 daniel-mar 316
                        if (!OIDplus::baseConfig()->getValue('REGISTRATION_HIDE_SYSTEM', false)) {
139 daniel-mar 317
                                $privacy_level = OIDplus::config()->getValue('reg_privacy');
318
 
183 daniel-mar 319
                                if (php_sapi_name() !== 'cli') { // don't register when called from CLI, otherweise the oidinfo XML can't convert relative links into absolute links
320
                                        if ((time()-OIDplus::config()->getValue('reg_last_ping') >= OIDplus::config()->getValue('reg_ping_interval'))) {
321
                                                $this->sendRegistrationQuery();
322
                                        }
139 daniel-mar 323
                                }
324
                        }
325
                }
326
        }
327
 
328
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
329
                if (file_exists(__DIR__.'/treeicon.png')) {
241 daniel-mar 330
                        $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png';
139 daniel-mar 331
                } else {
332
                        $tree_icon = null; // default icon (folder)
333
                }
334
 
335
                $json[] = array(
141 daniel-mar 336
                        'id' => 'oidplus:srv_registration',
139 daniel-mar 337
                        'icon' => $tree_icon,
206 daniel-mar 338
                        'text' => 'System registration'
139 daniel-mar 339
                );
340
 
341
                return true;
342
        }
343
 
344
        public function tree_search($request) {
345
                return false;
346
        }
347
}