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