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