Subversion Repositories oidplus

Rev

Go to most recent revision | Details | 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
 
22
define('QUERY_REGISTER_V1',         '1.3.6.1.4.1.37476.2.5.2.1.1.1');
23
define('QUERY_UNREGISTER_V1',       '1.3.6.1.4.1.37476.2.5.2.1.2.1');
24
define('QUERY_LISTALLSYSTEMIDS_V1', '1.3.6.1.4.1.37476.2.5.2.1.3.1');
25
 
26
class OIDplusRegistrationWizard extends OIDplusPagePlugin {
27
        public function type() {
28
                return 'admin';
29
        }
30
 
31
        public function priority() {
32
                return 120;
33
        }
34
 
35
        public function action(&$handled) {
36
                // Nothing
37
        }
38
 
39
        public function cfgSetValue($name, $value) {
40
                if ($name == 'reg_privacy') {
41
                        if (($value != '0') && ($value != '1') && ($value != '2')) {
42
                                throw new Exception("Please enter either 0, 1 or 2.");
43
                        }
44
                        OIDplus::config()->setValue('reg_last_ping', 0); // force renewal of ping on the next page load
45
                }
46
        }
47
 
48
        public function gui($id, &$out, &$handled) {
49
                if ($id === 'oidplus:reg_status') {
50
                        $handled = true;
51
                        $out['title'] = 'Registration status';
52
                        $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? 'plugins/adminPages/'.basename(__DIR__).'/icon_big.png' : '';
53
 
54
                        if (!OIDplus::authUtils()::isAdminLoggedIn()) {
55
                                $out['icon'] = 'img/error_big.png';
56
                                $out['text'] .= '<p>You need to <a '.oidplus_link('oidplus:login').'>log in</a> as administrator.</p>';
57
                        } else {
58
                                $out['text'] = '<p>The registration of your OIDplus installation has various advantages: The public key of your system is published, so that users can check the integrity of your data (e.g. signed OID-over-WHOIS requests). You can optionally also enable the automatic publishing of your public OID information to the repository oid-info.com.</p>'.
59
                                               '<p><input type="button" onclick="window.open(\'https://oidplus.viathinksoft.com/reg2/status.php?systemid='.OIDplus::system_id(false).'\',\'_blank\')" value="Check status of the registration and collected data"></p>';
60
 
61
                                if (!function_exists('openssl_sign')) {
62
                                        $out['text'] .= '<p><font color="red">Error: OpenSSL plugin is missing in PHP. You cannot (un)register your OIDplus instance.</font></p>';
63
                                } else {
64
                                        $out['text'] .= '<p>You can adjust your privacy level here:</p><p><select name="reg_privacy" id="reg_privacy">';
65
 
66
                                        # ---
67
 
68
                                        $out['text'] .= '<option value="0"';
69
                                        if (OIDplus::config()->getValue('reg_privacy') == 0) {
70
                                                $out['text'] .= ' selected';
71
                                        } else {
72
                                                $out['text'] .= '';
73
                                        }
74
                                        $out['text'] .= '>0 = Register to directory service and automatically publish RA/OID data at oid-info.com</option>';
75
 
76
                                        # ---
77
 
78
                                        $out['text'] .= '<option value="1"';
79
                                        if (OIDplus::config()->getValue('reg_privacy') == 1) {
80
                                                $out['text'] .= ' selected';
81
                                        } else {
82
                                                $out['text'] .= '';
83
                                        }
84
                                        $out['text'] .= '>1 = Only register to directory service</option>';
85
 
86
                                        # ---
87
 
88
                                        $out['text'] .= '<option value="2"';
89
                                        if (OIDplus::config()->getValue('reg_privacy') == 2) {
90
                                                $out['text'] .= ' selected';
91
                                        } else {
92
                                                $out['text'] .= '';
93
                                        }
94
                                        $out['text'] .= '>2 = Hide system</option>';
95
 
96
                                        # ---
97
 
98
                                        $out['text'] .= '</select> <input type="button" value="Change" onclick="crudActionRegPrivacyUpdate()"></p>';
99
 
100
                                        $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>';
101
                                }
102
 
103
                                $out['text'] .= '<p><i>Privacy information:</i> Please note that removing your system from the directory does not automatically delete already submitted OIDs to oid-info.com. To remove already submitted OIDs at oid-info.com, please contact the <a href="mailto:admin@oid-info.com">OID-Info Webmaster</a>.';
104
                        }
105
                }
106
        }
107
 
108
        public function sendRegistrationQuery() {
109
                $privacy_level = OIDplus::config()->getValue('reg_privacy');
110
 
111
                $system_url = OIDplus::system_url();
112
 
113
                if ($privacy_level == 2) {
114
                        // The user wants to unregister
115
                        // but we only unregister if we are registered. Check this "anonymously" (i.e. without revealing our system ID)
116
                        if (in_array(OIDplus::system_id(false), explode(';',file_get_contents('https://oidplus.viathinksoft.com/reg2/query.php?query='.QUERY_LISTALLSYSTEMIDS_V1)))) {
117
                                $query = QUERY_UNREGISTER_V1;
118
 
119
                                $payload = array(
120
                                        "query" => $query, // we must repeat the query because we want to sign it
121
                                        "system_id" => OIDplus::system_id(false)
122
                                );
123
 
124
                                $signature = '';
125
                                openssl_sign(json_encode($payload), $signature, OIDplus::config()->getValue('oidplus_private_key'));
126
 
127
                                $data = array(
128
                                        "payload" => $payload,
129
                                        "signature" => base64_encode($signature)
130
                                );
131
 
132
                                $ch = curl_init();
133
                                curl_setopt($ch, CURLOPT_URL, 'https://oidplus.viathinksoft.com/reg2/query.php');
134
                                curl_setopt($ch, CURLOPT_POST, 1);
135
                                curl_setopt($ch, CURLOPT_POSTFIELDS, "query=$query&data=".base64_encode(json_encode($data)));
136
                                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
137
                                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
138
                                curl_setopt($ch, CURLOPT_AUTOREFERER, true);
139
                                $res = curl_exec($ch);
140
                                curl_close($ch);
141
                                // die("RES: $res\n");
142
                                // if ($res == 'OK') ...
143
                        }
144
                } else {
145
                        if ($privacy_level == 0) {
146
                                if (class_exists('OIDplusPageAdminOIDInfoExport')) {
147
                                        ob_start();
148
                                        OIDplusPageAdminOIDInfoExport::outputXML(false); // no online check, because the query should be short (since the query is done while a visitor waits for the response)
149
                                        $oidinfo_xml = ob_get_contents();
150
                                        ob_end_clean();
151
                                } else {
152
                                        $oidinfo_xml = false;
153
                                }
154
                        } else {
155
                                $oidinfo_xml = false;
156
                        }
157
 
158
                        $query = QUERY_REGISTER_V1;
159
 
160
                        $root_oids = array();
161
                        foreach (OIDplus::getRegisteredObjectTypes() as $ot) {
162
                                if ($ot::ns() == 'oid') {
163
                                        $res = OIDplus::db()->query("select id from ".OIDPLUS_TABLENAME_PREFIX."objects where " .
164
                                                                    "parent = 'oid:' " .
165
                                                                    "order by ".OIDplus::db()->natOrder('id'));
166
                                        while ($row = OIDplus::db()->fetch_array($res)) {
167
                                                $root_oids[] = substr($row['id'],strlen('oid:'));
168
                                        }
169
                                }
170
                        }
171
                        $payload = array(
172
                                "query" => $query, // we must repeat the query because we want to sign it
173
                                "privacy_level" => $privacy_level,
174
                                "system_id" => OIDplus::system_id(false),
175
                                "public_key" => OIDplus::config()->getValue('oidplus_public_key'),
176
                                "system_url" => $system_url,
177
                                "hide_system_url" => 0,
178
                                "hide_public_key" => 0,
179
                                "admin_email" => OIDplus::config()->getValue('admin_email'),
180
                                "system_title" => OIDplus::config()->systemTitle(),
181
                                "oidinfo_xml" => @base64_encode($oidinfo_xml),
182
                                "root_oids" => $root_oids
183
                        );
184
 
185
                        $signature = '';
186
                        openssl_sign(json_encode($payload), $signature, OIDplus::config()->getValue('oidplus_private_key'));
187
 
188
                        $data = array(
189
                                "payload" => $payload,
190
                                "signature" => base64_encode($signature)
191
                        );
192
 
193
                        $ch = curl_init();
194
                        curl_setopt($ch, CURLOPT_URL, 'https://oidplus.viathinksoft.com/reg2/query.php');
195
                        curl_setopt($ch, CURLOPT_POST, 1);
196
                        curl_setopt($ch, CURLOPT_POSTFIELDS, "query=$query&data=".base64_encode(json_encode($data)));
197
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
198
                        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
199
                        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
200
                        $res = curl_exec($ch);
201
                        curl_close($ch);
202
                        // die("RES: $res\n");
203
                        // if ($res == 'OK') ...
204
                }
205
 
206
                OIDplus::config()->setValue('reg_last_ping', time());
207
        }
208
 
209
        public function init($html=true) {
210
                OIDplus::config()->prepareConfigKey('reg_wizard_done', 'Registration wizard done once?', '0', 1, 0);
211
                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', 0, 1);
212
                OIDplus::config()->prepareConfigKey('reg_ping_interval', 'Registration ping interval (in seconds)', '3600', 0, 0);
213
                OIDplus::config()->prepareConfigKey('reg_last_ping', 'Last ping to ViaThinkSoft directory services', '0', 1, 0);
214
 
215
                if (function_exists('openssl_sign')) {
216
                        // Show registration wizard once
217
 
218
                        if ($html && (OIDplus::config()->getValue('reg_wizard_done') != '1')) {
219
                                if (basename($_SERVER['SCRIPT_NAME']) != 'registration.php') {
220
                                        if ($system_url = OIDplus::system_url()) {
221
                                                header('Location:'.$system_url.'plugins/adminPages/'.basename(__DIR__).'/registration.php');
222
                                        } else {
223
                                                header('Location:plugins/adminPages/'.basename(__DIR__).'/registration.php');
224
                                        }
225
                                        die();
226
                                }
227
                        }
228
 
229
                        // Is it time to register / renew directory entry?
230
 
231
                        if (OIDplus::config()->getValue('reg_wizard_done') == '1') {
232
                                $privacy_level = OIDplus::config()->getValue('reg_privacy');
233
 
234
                                if ((time()-OIDplus::config()->getValue('reg_last_ping') >= OIDplus::config()->getValue('reg_ping_interval'))) {
235
                                        $this->sendRegistrationQuery();
236
                                }
237
                        }
238
                }
239
        }
240
 
241
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
242
                if (file_exists(__DIR__.'/treeicon.png')) {
243
                        $tree_icon = 'plugins/adminPages/'.basename(__DIR__).'/treeicon.png';
244
                } else {
245
                        $tree_icon = null; // default icon (folder)
246
                }
247
 
248
                $json[] = array(
249
                        'id' => 'oidplus:reg_status',
250
                        'icon' => $tree_icon,
251
                        'text' => 'Registration'
252
                );
253
 
254
                return true;
255
        }
256
 
257
        public function tree_search($request) {
258
                return false;
259
        }
260
}
261
 
262
OIDplus::registerPagePlugin(new OIDplusRegistrationWizard());