Rev 496 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
117 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
511 | daniel-mar | 5 | * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft |
117 | 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 OIDplusPageRaInvite extends OIDplusPagePluginRa { |
117 | daniel-mar | 23 | |
321 | daniel-mar | 24 | public function action($actionID, $params) { |
25 | if ($actionID == 'invite_ra') { |
||
26 | $email = $params['email']; |
||
117 | daniel-mar | 27 | |
250 | daniel-mar | 28 | if (!OIDplus::mailUtils()->validMailAddress($email)) { |
360 | daniel-mar | 29 | throw new OIDplusException(_L('Invalid email address')); |
117 | daniel-mar | 30 | } |
31 | |||
261 | daniel-mar | 32 | if (OIDplus::baseConfig()->getValue('RECAPTCHA_ENABLED', false)) { |
33 | $secret=OIDplus::baseConfig()->getValue('RECAPTCHA_PRIVATE', ''); |
||
321 | daniel-mar | 34 | $response=$params["captcha"]; |
117 | daniel-mar | 35 | $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}"); |
36 | $captcha_success=json_decode($verify); |
||
37 | if ($captcha_success->success==false) { |
||
360 | daniel-mar | 38 | throw new OIDplusException(_L('CAPTCHA not successfully verified')); |
117 | daniel-mar | 39 | } |
40 | } |
||
41 | |||
42 | $this->inviteSecurityCheck($email); |
||
119 | daniel-mar | 43 | // TODO: should we also log who has invited? |
288 | daniel-mar | 44 | OIDplus::logger()->log("[INFO]RA($email)!", "RA '$email' has been invited"); |
117 | daniel-mar | 45 | |
46 | $timestamp = time(); |
||
496 | daniel-mar | 47 | $activate_url = OIDplus::webpath() . '?goto='.urlencode('oidplus:activate_ra$'.$email.'$'.$timestamp.'$'.OIDplus::authUtils()::makeAuthKey('activate_ra;'.$email.';'.$timestamp)); |
117 | daniel-mar | 48 | |
49 | $message = $this->getInvitationText($email); |
||
50 | $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message); |
||
51 | |||
257 | daniel-mar | 52 | OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title').' - Invitation', $message, OIDplus::config()->getValue('global_cc')); |
117 | daniel-mar | 53 | |
328 | daniel-mar | 54 | return array("status" => 0); |
117 | daniel-mar | 55 | |
321 | daniel-mar | 56 | } else if ($actionID == 'activate_ra') { |
117 | daniel-mar | 57 | |
321 | daniel-mar | 58 | $password1 = $params['password1']; |
59 | $password2 = $params['password2']; |
||
60 | $email = $params['email']; |
||
61 | $auth = $params['auth']; |
||
62 | $timestamp = $params['timestamp']; |
||
117 | daniel-mar | 63 | |
64 | if (!OIDplus::authUtils()::validateAuthKey('activate_ra;'.$email.';'.$timestamp, $auth)) { |
||
360 | daniel-mar | 65 | throw new OIDplusException(_L('Invalid auth key')); |
117 | daniel-mar | 66 | } |
67 | |||
68 | if ((OIDplus::config()->getValue('max_ra_invite_time') > 0) && (time()-$timestamp > OIDplus::config()->getValue('max_ra_invite_time'))) { |
||
360 | daniel-mar | 69 | throw new OIDplusException(_L('Invitation expired!')); |
117 | daniel-mar | 70 | } |
71 | |||
72 | if ($password1 !== $password2) { |
||
360 | daniel-mar | 73 | throw new OIDplusException(_L('Passwords do not match')); |
117 | daniel-mar | 74 | } |
75 | |||
257 | daniel-mar | 76 | if (strlen($password1) < OIDplus::config()->getValue('ra_min_password_length')) { |
360 | daniel-mar | 77 | $minlen = OIDplus::config()->getValue('ra_min_password_length'); |
78 | throw new OIDplusException(_L('Password is too short. Need at least %1 characters',$minlen)); |
||
117 | daniel-mar | 79 | } |
80 | |||
288 | daniel-mar | 81 | OIDplus::logger()->log("[OK]RA($email)!", "RA '$email' has been registered due to invitation"); |
117 | daniel-mar | 82 | |
83 | $ra = new OIDplusRA($email); |
||
84 | $ra->register_ra($password1); |
||
85 | |||
328 | daniel-mar | 86 | return array("status" => 0); |
321 | daniel-mar | 87 | } else { |
360 | daniel-mar | 88 | throw new OIDplusException(_L('Unknown action ID')); |
117 | daniel-mar | 89 | } |
90 | } |
||
91 | |||
92 | public function init($html=true) { |
||
263 | daniel-mar | 93 | OIDplus::config()->prepareConfigKey('max_ra_invite_time', 'Max RA invite time in seconds (0 = infinite)', '0', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
117 | daniel-mar | 94 | if (!is_numeric($value) || ($value < 0)) { |
360 | daniel-mar | 95 | throw new OIDplusException(_L('Please enter a valid value.')); |
117 | daniel-mar | 96 | } |
263 | daniel-mar | 97 | }); |
360 | daniel-mar | 98 | OIDplus::config()->prepareConfigKey('ra_invitation_enabled', 'May RAs be invited? (0=no, 1=yes)', '1', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
155 | daniel-mar | 99 | if (($value != 0) && ($value != 1)) { |
360 | daniel-mar | 100 | throw new OIDplusException(_L('Please enter a valid value (0=no, 1=yes).')); |
155 | daniel-mar | 101 | } |
263 | daniel-mar | 102 | }); |
117 | daniel-mar | 103 | } |
104 | |||
105 | public function gui($id, &$out, &$handled) { |
||
106 | if (explode('$',$id)[0] == 'oidplus:invite_ra') { |
||
107 | $handled = true; |
||
108 | |||
109 | $email = explode('$',$id)[1]; |
||
110 | $origin = explode('$',$id)[2]; |
||
213 | daniel-mar | 111 | |
360 | daniel-mar | 112 | $out['title'] = _L('Invite a Registration Authority'); |
117 | daniel-mar | 113 | |
155 | daniel-mar | 114 | if (!OIDplus::config()->getValue('ra_invitation_enabled')) { |
115 | $out['icon'] = 'img/error_big.png'; |
||
360 | daniel-mar | 116 | $out['text'] = '<p>'._L('Invitations are disabled by the administrator.').'</p>'; |
281 | daniel-mar | 117 | return; |
155 | daniel-mar | 118 | } |
119 | |||
241 | daniel-mar | 120 | $out['icon'] = OIDplus::webpath(__DIR__).'invite_ra_big.png'; |
117 | daniel-mar | 121 | |
122 | try { |
||
123 | $this->inviteSecurityCheck($email); |
||
124 | $cont = $this->getInvitationText($email); |
||
125 | |||
360 | daniel-mar | 126 | $out['text'] .= '<p>'._L('You have chosen to invite %1 as a Registration Authority. If you click "Send", the following email will be sent to %2:','<b>'.$email.'</b>',$email).'</p><p><i>'.nl2br(htmlentities($cont)).'</i></p> |
428 | daniel-mar | 127 | <form id="inviteForm" action="javascript:void(0);" onsubmit="return inviteFormOnSubmit();"> |
117 | daniel-mar | 128 | <input type="hidden" id="email" value="'.htmlentities($email).'"/> |
129 | <input type="hidden" id="origin" value="'.htmlentities($origin).'"/>'. |
||
261 | daniel-mar | 130 | (OIDplus::baseConfig()->getValue('RECAPTCHA_ENABLED', false) ? |
131 | '<script> grecaptcha.render(document.getElementById("g-recaptcha"), { "sitekey" : "'.OIDplus::baseConfig()->getValue('RECAPTCHA_PUBLIC', '').'" }); </script>'. |
||
132 | '<div id="g-recaptcha" class="g-recaptcha" data-sitekey="'.OIDplus::baseConfig()->getValue('RECAPTCHA_PUBLIC', '').'"></div>' : ''). |
||
117 | daniel-mar | 133 | ' <br> |
360 | daniel-mar | 134 | <input type="submit" value="'._L('Send invitation').'"> |
117 | daniel-mar | 135 | </form>'; |
136 | |||
137 | } catch (Exception $e) { |
||
138 | |||
139 | $out['icon'] = 'img/error_big.png'; |
||
360 | daniel-mar | 140 | $out['text'] = _L('Error: %1',$e->getMessage()); |
117 | daniel-mar | 141 | |
142 | } |
||
143 | } else if (explode('$',$id)[0] == 'oidplus:activate_ra') { |
||
144 | $handled = true; |
||
145 | |||
281 | daniel-mar | 146 | $email = explode('$',$id)[1]; |
147 | $timestamp = explode('$',$id)[2]; |
||
148 | $auth = explode('$',$id)[3]; |
||
149 | |||
360 | daniel-mar | 150 | $out['title'] = _L('Register as Registration Authority'); |
155 | daniel-mar | 151 | |
152 | if (!OIDplus::config()->getValue('ra_invitation_enabled')) { |
||
153 | $out['icon'] = 'img/error_big.png'; |
||
360 | daniel-mar | 154 | $out['text'] = '<p>'._L('Invitations are disabled by the administrator.').'</p>'; |
281 | daniel-mar | 155 | return; |
155 | daniel-mar | 156 | } |
157 | |||
241 | daniel-mar | 158 | $out['icon'] = OIDplus::webpath(__DIR__).'activate_ra_big.png'; |
117 | daniel-mar | 159 | |
261 | daniel-mar | 160 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($email)); |
236 | daniel-mar | 161 | if ($res->num_rows() > 0) { |
360 | daniel-mar | 162 | $out['text'] = _L('This RA is already registered and does not need to be invited.'); |
117 | daniel-mar | 163 | } else { |
164 | if (!OIDplus::authUtils()::validateAuthKey('activate_ra;'.$email.';'.$timestamp, $auth)) { |
||
165 | $out['icon'] = 'img/error_big.png'; |
||
360 | daniel-mar | 166 | $out['text'] = _L('Invalid authorization. Is the URL OK?'); |
117 | daniel-mar | 167 | } else { |
168 | // TODO: like in the FreeOID plugin, we could ask here at least for a name for the RA |
||
360 | daniel-mar | 169 | $out['text'] = '<p>'._L('E-Mail-Address').': <b>'.$email.'</b></p> |
117 | daniel-mar | 170 | |
428 | daniel-mar | 171 | <form id="activateRaForm" action="javascript:void(0);" onsubmit="return activateRaFormOnSubmit();"> |
117 | daniel-mar | 172 | <input type="hidden" id="email" value="'.htmlentities($email).'"/> |
173 | <input type="hidden" id="timestamp" value="'.htmlentities($timestamp).'"/> |
||
174 | <input type="hidden" id="auth" value="'.htmlentities($auth).'"/> |
||
360 | daniel-mar | 175 | <div><label class="padding_label">'._L('New password').':</label><input type="password" id="password1" value=""/></div> |
176 | <div><label class="padding_label">'._L('Repeat').':</label><input type="password" id="password2" value=""/></div> |
||
177 | <br><input type="submit" value="'._L('Register').'"> |
||
117 | daniel-mar | 178 | </form>'; |
179 | } |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | |||
184 | public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') { |
||
281 | daniel-mar | 185 | //if (!$ra_email) return false; |
186 | //if (!OIDplus::authUtils()::isRaLoggedIn($ra_email) && !OIDplus::authUtils()::isAdminLoggedIn()) return false; |
||
187 | |||
117 | daniel-mar | 188 | return false; |
189 | } |
||
190 | |||
191 | private function inviteSecurityCheck($email) { |
||
261 | daniel-mar | 192 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($email)); |
236 | daniel-mar | 193 | if ($res->num_rows() > 0) { |
360 | daniel-mar | 194 | throw new OIDplusException(_L('This RA is already registered and does not need to be invited.')); |
117 | daniel-mar | 195 | } |
196 | |||
197 | if (!OIDplus::authUtils()::isAdminLoggedIn()) { |
||
198 | // Check if the RA may invite the user (i.e. the they are the parent of an OID of that person) |
||
199 | $ok = false; |
||
261 | daniel-mar | 200 | $res = OIDplus::db()->query("select parent from ###objects where ra_email = ?", array($email)); |
236 | daniel-mar | 201 | while ($row = $res->fetch_array()) { |
117 | daniel-mar | 202 | $objParent = OIDplusObject::parse($row['parent']); |
360 | daniel-mar | 203 | if (is_null($objParent)) throw new OIDplusException(_L('Type of %1 unknown',$row['parent'])); |
117 | daniel-mar | 204 | if ($objParent->userHasWriteRights()) { |
205 | $ok = true; |
||
206 | } |
||
207 | } |
||
208 | if (!$ok) { |
||
360 | daniel-mar | 209 | throw new OIDplusException(_L('You may not invite this RA. Maybe you need to <a %1>log in</a> again.',OIDplus::gui()->link('oidplus:login'))); |
117 | daniel-mar | 210 | } |
211 | } |
||
212 | } |
||
213 | |||
214 | private function getInvitationText($email) { |
||
215 | $list_of_oids = array(); |
||
261 | daniel-mar | 216 | $res = OIDplus::db()->query("select id from ###objects where ra_email = ?", array($email)); |
236 | daniel-mar | 217 | while ($row = $res->fetch_array()) { |
117 | daniel-mar | 218 | $list_of_oids[] = $row['id']; |
219 | } |
||
220 | |||
221 | $message = file_get_contents(__DIR__ . '/invite_msg.tpl'); |
||
222 | |||
223 | // Resolve stuff |
||
496 | daniel-mar | 224 | $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,false), $message); |
117 | daniel-mar | 225 | $message = str_replace('{{OID_LIST}}', implode("\n", $list_of_oids), $message); |
226 | $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message); |
||
227 | $message = str_replace('{{PARTY}}', OIDplus::authUtils()::isAdminLoggedIn() ? 'the system administrator' : 'a superior Registration Authority', $message); |
||
228 | |||
229 | // {{ACTIVATE_URL}} will be resolved in ajax.php |
||
230 | |||
231 | return $message; |
||
232 | } |
||
233 | |||
234 | public function tree_search($request) { |
||
235 | return false; |
||
236 | } |
||
360 | daniel-mar | 237 | } |