Rev 1086 | Rev 1121 | 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 |
||
1086 | daniel-mar | 5 | * Copyright 2019 - 2023 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 | |||
1050 | daniel-mar | 20 | namespace ViaThinkSoft\OIDplus; |
635 | daniel-mar | 21 | |
1086 | daniel-mar | 22 | // phpcs:disable PSR1.Files.SideEffects |
23 | \defined('INSIDE_OIDPLUS') or die; |
||
24 | // phpcs:enable PSR1.Files.SideEffects |
||
25 | |||
635 | daniel-mar | 26 | class OIDplusPagePublicFreeOID extends OIDplusPagePluginPublic { |
27 | |||
1116 | daniel-mar | 28 | /** |
29 | * @param $with_ns |
||
30 | * @return string |
||
31 | * @throws OIDplusException |
||
32 | */ |
||
635 | daniel-mar | 33 | private static function getFreeRootOid($with_ns) { |
34 | return ($with_ns ? 'oid:' : '').OIDplus::config()->getValue('freeoid_root_oid'); |
||
35 | } |
||
36 | |||
1116 | daniel-mar | 37 | /** |
38 | * @param $email |
||
39 | * @param $getId |
||
40 | * @return bool|mixed|null |
||
41 | * @throws OIDplusException |
||
42 | */ |
||
635 | daniel-mar | 43 | public static function alreadyHasFreeOid($email, $getId = false){ |
44 | $res = OIDplus::db()->query("select id from ###objects where ra_email = ? and id like ? order by ".OIDplus::db()->natOrder('id'), array($email, self::getFreeRootOid(true).'.%')); |
||
45 | while ($row = $res->fetch_array()) { |
||
46 | return $getId ? $row['id'] : true; |
||
47 | } |
||
48 | return $getId ? null : false; |
||
49 | } |
||
50 | |||
1116 | daniel-mar | 51 | /** |
52 | * @param string $actionID |
||
53 | * @param array $params |
||
54 | * @return array|int[] |
||
55 | * @throws OIDplusException |
||
56 | * @throws OIDplusMailException |
||
57 | */ |
||
58 | public function action(string $actionID, array $params): array { |
||
635 | daniel-mar | 59 | if (empty(self::getFreeRootOid(false))) throw new OIDplusException(_L('FreeOID service not available. Please ask your administrator.')); |
60 | |||
61 | if ($actionID == 'request_freeoid') { |
||
62 | _CheckParamExists($params, 'email'); |
||
63 | $email = $params['email']; |
||
64 | |||
65 | if ($already_registered_oid = $this->alreadyHasFreeOid($email, true)) { |
||
66 | throw new OIDplusException(_L('This email address already has a FreeOID registered (%1)', $already_registered_oid)); |
||
67 | } |
||
68 | |||
69 | if (!OIDplus::mailUtils()->validMailAddress($email)) { |
||
70 | throw new OIDplusException(_L('Invalid email address')); |
||
71 | } |
||
72 | |||
702 | daniel-mar | 73 | OIDplus::getActiveCaptchaPlugin()->captchaVerify($params, 'captcha'); |
635 | daniel-mar | 74 | |
75 | $root_oid = self::getFreeRootOid(false); |
||
76 | OIDplus::logger()->log("[INFO]OID(oid:$root_oid)+RA($email)!", "Requested a free OID for email '$email' to be placed into root '$root_oid'"); |
||
77 | |||
78 | $timestamp = time(); |
||
801 | daniel-mar | 79 | $activate_url = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL) . '?goto='.urlencode('oidplus:com.viathinksoft.freeoid.activate_freeoid$'.$email.'$'.$timestamp.'$'.OIDplus::authUtils()->makeAuthKey('com.viathinksoft.freeoid.activate_freeoid;'.$email.';'.$timestamp)); |
635 | daniel-mar | 80 | |
81 | $message = file_get_contents(__DIR__ . '/request_msg.tpl'); |
||
801 | daniel-mar | 82 | $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL), $message); |
635 | daniel-mar | 83 | $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->getValue('system_title'), $message); |
84 | $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message); |
||
85 | $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message); |
||
86 | |||
875 | daniel-mar | 87 | OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title').' - Free OID request', $message); |
635 | daniel-mar | 88 | |
89 | return array("status" => 0); |
||
90 | |||
91 | } else if ($actionID == 'activate_freeoid') { |
||
92 | _CheckParamExists($params, 'email'); |
||
93 | _CheckParamExists($params, 'auth'); |
||
94 | _CheckParamExists($params, 'timestamp'); |
||
95 | |||
96 | $email = $params['email']; |
||
97 | $auth = $params['auth']; |
||
98 | $timestamp = $params['timestamp']; |
||
99 | |||
100 | if (!OIDplus::authUtils()->validateAuthKey('com.viathinksoft.freeoid.activate_freeoid;'.$email.';'.$timestamp, $auth)) { |
||
101 | throw new OIDplusException(_L('Invalid auth key')); |
||
102 | } |
||
103 | |||
104 | if ((OIDplus::config()->getValue('max_ra_invite_time') > 0) && (time()-$timestamp > OIDplus::config()->getValue('max_ra_invite_time'))) { |
||
105 | throw new OIDplusException(_L('Invitation expired!')); |
||
106 | } |
||
107 | |||
108 | // 1. step: Check entered data and add the RA to the database |
||
109 | |||
110 | $ra = new OIDplusRA($email); |
||
111 | if (!$ra->existing()) { |
||
112 | _CheckParamExists($params, 'password1'); |
||
113 | _CheckParamExists($params, 'password2'); |
||
114 | _CheckParamExists($params, 'ra_name'); |
||
702 | daniel-mar | 115 | |
635 | daniel-mar | 116 | $password1 = $params['password1']; |
117 | $password2 = $params['password2']; |
||
118 | $ra_name = $params['ra_name']; |
||
119 | |||
120 | if ($password1 !== $password2) { |
||
121 | throw new OIDplusException(_L('Passwords do not match')); |
||
122 | } |
||
123 | |||
124 | if (strlen($password1) < OIDplus::config()->getValue('ra_min_password_length')) { |
||
125 | $minlen = OIDplus::config()->getValue('ra_min_password_length'); |
||
126 | throw new OIDplusException(_L('Password is too short. Need at least %1 characters',$minlen)); |
||
127 | } |
||
128 | |||
129 | if (empty($ra_name)) { |
||
130 | throw new OIDplusException(_L('Please enter your personal name or the name of your group.')); |
||
131 | } |
||
132 | |||
133 | $ra->register_ra($password1); |
||
134 | $ra->setRaName($ra_name); |
||
135 | } else { |
||
136 | // RA already exists (e.g. was logged in using Google OAuth) |
||
137 | $ra_name = $ra->raName(); |
||
138 | } |
||
139 | |||
140 | // 2. step: Add the new OID to the database |
||
141 | |||
142 | $url = isset($params['url']) ? $params['url'] : ''; |
||
143 | $title = isset($params['title']) ? $params['title'] : ''; |
||
144 | |||
145 | $root_oid = self::getFreeRootOid(false); |
||
146 | $new_oid = OIDplusOid::parse('oid:'.$root_oid)->appendArcs($this->freeoid_max_id()+1)->nodeId(false); |
||
147 | |||
148 | OIDplus::logger()->log("[INFO]OID(oid:$root_oid)+OIDRA(oid:$root_oid)!", "Child OID '$new_oid' added automatically by '$email' (RA Name: '$ra_name')"); |
||
149 | OIDplus::logger()->log("[INFO]OID(oid:$new_oid)+[OK]RA($email)!", "Free OID '$new_oid' activated (RA Name: '$ra_name')"); |
||
150 | |||
151 | if ((!empty($url)) && (substr($url, 0, 4) != 'http')) $url = 'http://'.$url; |
||
152 | |||
153 | $description = ''; // '<p>'.htmlentities($ra_name).'</p>'; |
||
154 | if (!empty($url)) { |
||
155 | $description .= '<p>'._L('More information at %1','<a href="'.htmlentities($url).'">'.htmlentities($url).'</a>').'</p>'; |
||
156 | } |
||
157 | |||
158 | if (empty($title)) $title = $ra_name; |
||
159 | |||
160 | try { |
||
161 | $maxlen = OIDplus::baseConfig()->getValue('LIMITS_MAX_ID_LENGTH')-strlen('oid:'); |
||
162 | if (strlen($new_oid) > $maxlen) { |
||
163 | throw new OIDplusException(_L('The resulting OID %1 is too long (max allowed length: %2)',$new_oid,$maxlen)); |
||
164 | } |
||
165 | |||
166 | OIDplus::db()->query("insert into ###objects (id, ra_email, parent, title, description, confidential, created) values (?, ?, ?, ?, ?, ?, ".OIDplus::db()->sqlDate().")", array('oid:'.$new_oid, $email, self::getFreeRootOid(true), $title, $description, false)); |
||
978 | daniel-mar | 167 | OIDplusObject::resetObjectInformationCache(); |
1050 | daniel-mar | 168 | } catch (\Exception $e) { |
635 | daniel-mar | 169 | $ra->delete(); |
170 | throw $e; |
||
171 | } |
||
172 | |||
173 | // Send delegation report email to admin |
||
174 | |||
175 | $message = "OID delegation report\n"; |
||
176 | $message .= "\n"; |
||
1116 | daniel-mar | 177 | $message .= "OID: ".$new_oid."\n"; |
635 | daniel-mar | 178 | $message .= "\n"; |
179 | $message .= "RA Name: $ra_name\n"; |
||
180 | $message .= "RA eMail: $email\n"; |
||
181 | $message .= "URL for more information: $url\n"; |
||
182 | $message .= "OID Name: $title\n"; |
||
183 | $message .= "\n"; |
||
962 | daniel-mar | 184 | $message .= "More details: ".OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL)."?goto=oid%3A$new_oid\n"; |
635 | daniel-mar | 185 | |
875 | daniel-mar | 186 | OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title')." - OID $new_oid registered", $message); |
635 | daniel-mar | 187 | |
188 | // Send delegation information to user |
||
189 | |||
190 | $message = file_get_contents(__DIR__ . '/allocated_msg.tpl'); |
||
801 | daniel-mar | 191 | $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL), $message); |
635 | daniel-mar | 192 | $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->getValue('system_title'), $message); |
193 | $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message); |
||
194 | $message = str_replace('{{NEW_OID}}', $new_oid, $message); |
||
875 | daniel-mar | 195 | OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title').' - Free OID allocated', $message); |
635 | daniel-mar | 196 | |
197 | return array( |
||
198 | "new_oid" => $new_oid, |
||
199 | "status" => 0 |
||
200 | ); |
||
201 | } else { |
||
1116 | daniel-mar | 202 | return parent::action($actionID, $params); |
635 | daniel-mar | 203 | } |
204 | } |
||
205 | |||
1116 | daniel-mar | 206 | /** |
207 | * @param bool $html |
||
208 | * @return void |
||
209 | * @throws OIDplusException |
||
210 | */ |
||
211 | public function init(bool $html=true) { |
||
635 | daniel-mar | 212 | OIDplus::config()->prepareConfigKey('freeoid_root_oid', 'Root-OID of free OID service (a service where visitors can create their own OID using email verification)', '', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
213 | if (($value != '') && !oid_valid_dotnotation($value,false,false,1)) { |
||
214 | throw new OIDplusException(_L('Please enter a valid OID in dot notation or nothing')); |
||
215 | } |
||
216 | }); |
||
217 | } |
||
218 | |||
1116 | daniel-mar | 219 | /** |
220 | * @param string $id |
||
221 | * @param array $out |
||
222 | * @param bool $handled |
||
223 | * @return void |
||
224 | * @throws OIDplusException |
||
225 | */ |
||
226 | public function gui(string $id, array &$out, bool &$handled) { |
||
635 | daniel-mar | 227 | if (empty(self::getFreeRootOid(false))) return; |
228 | |||
229 | if (explode('$',$id)[0] == 'oidplus:com.viathinksoft.freeoid') { |
||
230 | $handled = true; |
||
231 | |||
232 | $out['title'] = _L('Register a free OID'); |
||
801 | daniel-mar | 233 | $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : ''; |
635 | daniel-mar | 234 | |
235 | // Note: We are using the highest OID instead of the rowcount, because there might be OIDs which could have been deleted in between |
||
236 | $highest_id = $this->freeoid_max_id(); |
||
237 | |||
238 | $out['text'] .= '<p>'._L('Currently <a %1>%2 free OIDs have been</a> registered. Please enter your email below to receive a free OID.',OIDplus::gui()->link(self::getFreeRootOid(true)),$highest_id).'</p>'; |
||
239 | |||
240 | try { |
||
241 | $out['text'] .= ' |
||
242 | <form id="freeOIDForm" action="javascript:void(0);" onsubmit="return OIDplusPagePublicFreeOID.freeOIDFormOnSubmit();"> |
||
702 | daniel-mar | 243 | '._L('E-Mail').': <input type="text" id="email" value=""/><br><br> |
244 | '.OIDplus::getActiveCaptchaPlugin()->captchaGenerate().' |
||
245 | <br> |
||
962 | daniel-mar | 246 | <input type="submit" value="'._L('Request a free OID').'"> |
635 | daniel-mar | 247 | </form>'; |
248 | |||
249 | $obj = OIDplusOid::parse(self::getFreeRootOid(true)); |
||
250 | |||
251 | if (file_exists(__DIR__ . '/tos$'.OIDplus::getCurrentLang().'.html')) { |
||
252 | $tos = file_get_contents(__DIR__ . '/tos$'.OIDplus::getCurrentLang().'.html'); |
||
253 | } else { |
||
254 | $tos = file_get_contents(__DIR__ . '/tos.html'); |
||
255 | } |
||
256 | |||
257 | list($html, $js, $css) = extractHtmlContents($tos); |
||
258 | $tos = ''; |
||
259 | if (!empty($js)) $tos .= "<script>\n$js\n</script>"; |
||
260 | if (!empty($css)) $tos .= "<style>\n$css\n</style>"; |
||
821 | daniel-mar | 261 | $tos .= stripHtmlComments($html); |
635 | daniel-mar | 262 | |
263 | $tos = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $tos); |
||
264 | if ($obj) { |
||
265 | $tos = str_replace('{{ROOT_OID}}', $obj->getDotNotation(), $tos); |
||
266 | $tos = str_replace('{{ROOT_OID_ASN1}}', $obj->getAsn1Notation(), $tos); |
||
267 | $tos = str_replace('{{ROOT_OID_IRI}}', $obj->getIriNotation(), $tos); |
||
268 | } |
||
269 | $out['text'] .= $tos; |
||
960 | daniel-mar | 270 | |
271 | if (OIDplus::config()->getValue('freeoid_root_oid') == '1.3.6.1.4.1.37476.9000') { |
||
272 | $out['text'] .= '<p>'._L('<b>Note:</b> Since September 2022, owners of FreeOID automatically receive a free ISO-7816 compliant <b>Application Identifier</b> (AID) with the format <code>D2:76:00:01:86:F0:(FreeOID):FF:(PIX)</code> (up to 64 bits application specific PIX, depending on the length of the FreeOID number).'); |
||
273 | $out['text'] .= ' - <a '.OIDplus::gui()->link('aid:D276000186F1').'>'._L('More information').'</a></p>'; |
||
274 | } |
||
1050 | daniel-mar | 275 | } catch (\Exception $e) { |
635 | daniel-mar | 276 | $out['text'] = _L('Error: %1',$e->getMessage()); |
277 | } |
||
278 | } else if (explode('$',$id)[0] == 'oidplus:com.viathinksoft.freeoid.activate_freeoid') { |
||
279 | $handled = true; |
||
280 | |||
281 | $email = explode('$',$id)[1]; |
||
282 | $timestamp = explode('$',$id)[2]; |
||
283 | $auth = explode('$',$id)[3]; |
||
284 | |||
285 | $out['title'] = _L('Activate Free OID'); |
||
801 | daniel-mar | 286 | $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : ''; |
635 | daniel-mar | 287 | |
288 | if ($already_registered_oid = $this->alreadyHasFreeOid($email, true)) { |
||
289 | throw new OIDplusException(_L('This email address already has a FreeOID registered (%1)', $already_registered_oid)); |
||
290 | } else { |
||
291 | if (!OIDplus::authUtils()->validateAuthKey('com.viathinksoft.freeoid.activate_freeoid;'.$email.';'.$timestamp, $auth)) { |
||
800 | daniel-mar | 292 | $out['icon'] = 'img/error.png'; |
635 | daniel-mar | 293 | $out['text'] = _L('Invalid authorization. Is the URL OK?'); |
294 | } else { |
||
295 | $ra = new OIDplusRA($email); |
||
296 | $ra_existing = $ra->existing(); |
||
297 | |||
298 | $out['text'] = '<p>'._L('eMail-Address').': <b>'.$email.'</b></p>'; |
||
299 | |||
300 | $out['text'] .= ' <form id="activateFreeOIDForm" action="javascript:void(0);" onsubmit="return OIDplusPagePublicFreeOID.activateFreeOIDFormOnSubmit();">'; |
||
301 | $out['text'] .= ' <input type="hidden" id="email" value="'.htmlentities($email).'"/>'; |
||
302 | $out['text'] .= ' <input type="hidden" id="timestamp" value="'.htmlentities($timestamp).'"/>'; |
||
303 | $out['text'] .= ' <input type="hidden" id="auth" value="'.htmlentities($auth).'"/>'; |
||
304 | |||
305 | if ($ra_existing) { |
||
306 | $out['text'] .= ' '._L('Your personal name or the name of your group').':<br><b>'.htmlentities($ra->raName()).'</b><br><br>'; |
||
307 | } else { |
||
308 | $out['text'] .= ' '._L('Your personal name or the name of your group').':<br><input type="text" id="ra_name" value=""/><br><br>'; // TODO: disable autocomplete |
||
309 | } |
||
310 | $out['text'] .= ' '._L('Title of your OID (usually equal to your name, optional)').':<br><input type="text" id="title" value=""/><br><br>'; |
||
311 | $out['text'] .= ' '._L('URL for more information about your project(s) (optional)').':<br><input type="text" id="url" value=""/><br><br>'; |
||
312 | |||
313 | if (!$ra_existing) { |
||
314 | $out['text'] .= ' <div><label class="padding_label">'._L('Password').':</label><input type="password" id="password1" value=""/></div>'; |
||
315 | $out['text'] .= ' <div><label class="padding_label">'._L('Repeat').':</label><input type="password" id="password2" value=""/></div>'; |
||
316 | } |
||
317 | $out['text'] .= ' <br><input type="submit" value="'._L('Register').'">'; |
||
318 | $out['text'] .= ' </form>'; |
||
319 | } |
||
320 | } |
||
321 | } |
||
322 | } |
||
323 | |||
1116 | daniel-mar | 324 | /** |
325 | * @param array $out |
||
326 | * @return void |
||
327 | * @throws OIDplusException |
||
328 | */ |
||
329 | public function publicSitemap(array &$out) { |
||
635 | daniel-mar | 330 | if (empty(self::getFreeRootOid(false))) return; |
331 | $out[] = 'oidplus:com.viathinksoft.freeoid'; |
||
332 | } |
||
333 | |||
1116 | daniel-mar | 334 | /** |
335 | * @param array $json |
||
336 | * @param string|null $ra_email |
||
337 | * @param bool $nonjs |
||
338 | * @param string $req_goto |
||
339 | * @return bool |
||
340 | * @throws OIDplusException |
||
341 | */ |
||
342 | public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool { |
||
635 | daniel-mar | 343 | if (empty(self::getFreeRootOid(false))) return false; |
344 | |||
800 | daniel-mar | 345 | if (file_exists(__DIR__.'/img/main_icon16.png')) { |
801 | daniel-mar | 346 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png'; |
635 | daniel-mar | 347 | } else { |
348 | $tree_icon = null; // default icon (folder) |
||
349 | } |
||
350 | |||
351 | $json[] = array( |
||
352 | 'id' => 'oidplus:com.viathinksoft.freeoid', |
||
353 | 'icon' => $tree_icon, |
||
354 | 'text' => _L('Register a free OID') |
||
355 | ); |
||
356 | |||
357 | return true; |
||
358 | } |
||
359 | |||
360 | # --- |
||
361 | |||
1116 | daniel-mar | 362 | /** |
363 | * @return int |
||
364 | * @throws OIDplusException |
||
365 | */ |
||
366 | protected static function freeoid_max_id(): int { |
||
635 | daniel-mar | 367 | $res = OIDplus::db()->query("select id from ###objects where id like ? order by ".OIDplus::db()->natOrder('id'), array(self::getFreeRootOid(true).'.%')); |
368 | $highest_id = 0; |
||
369 | while ($row = $res->fetch_array()) { |
||
370 | $arc = substr_count(self::getFreeRootOid(false), '.')+1; |
||
371 | $highest_id = explode('.',$row['id'])[$arc]; |
||
372 | } |
||
373 | return $highest_id; |
||
374 | } |
||
375 | |||
1116 | daniel-mar | 376 | /** |
377 | * @param string $request |
||
378 | * @return array|false |
||
379 | */ |
||
380 | public function tree_search(string $request) { |
||
635 | daniel-mar | 381 | return false; |
382 | } |
||
383 | } |