Rev 328 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
61 | 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 | |||
256 | daniel-mar | 20 | class OIDplusPageRaChangeEMail extends OIDplusPagePluginRa { |
61 | daniel-mar | 21 | |
321 | daniel-mar | 22 | public function action($actionID, $params) { |
23 | if ($actionID == 'change_ra_email') { |
||
152 | daniel-mar | 24 | if (!OIDplus::config()->getValue('allow_ra_email_change') && !OIDplus::authUtils()::isAdminLoggedIn()) { |
360 | daniel-mar | 25 | throw new OIDplusException(_L('This functionality has been disabled by the administrator.')); |
61 | daniel-mar | 26 | } |
27 | |||
321 | daniel-mar | 28 | $old_email = $params['old_email']; |
29 | $new_email = $params['new_email']; |
||
61 | daniel-mar | 30 | |
31 | if (!OIDplus::authUtils()::isRaLoggedIn($old_email) && !OIDplus::authUtils()::isAdminLoggedIn()) { |
||
360 | daniel-mar | 32 | throw new OIDplusException(_L('Authentication error. Please log in as admin, or as the RA to update its email address.')); |
61 | daniel-mar | 33 | } |
34 | |||
250 | daniel-mar | 35 | if (!OIDplus::mailUtils()->validMailAddress($new_email)) { |
360 | daniel-mar | 36 | throw new OIDplusException(_L('eMail address is invalid.')); |
61 | daniel-mar | 37 | } |
38 | |||
261 | daniel-mar | 39 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($old_email)); |
236 | daniel-mar | 40 | if ($res->num_rows() == 0) { |
360 | daniel-mar | 41 | throw new OIDplusException(_L('eMail address does not exist anymore. It was probably already changed.')); |
61 | daniel-mar | 42 | } |
43 | |||
261 | daniel-mar | 44 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($new_email)); |
236 | daniel-mar | 45 | if ($res->num_rows() > 0) { |
360 | daniel-mar | 46 | throw new OIDplusException(_L('eMail address is already used by another RA. To merge accounts, please contact the superior RA of your objects and request an owner change of your objects.')); |
61 | daniel-mar | 47 | } |
224 | daniel-mar | 48 | |
152 | daniel-mar | 49 | if (OIDplus::authUtils()::isAdminLoggedIn()) { |
288 | daniel-mar | 50 | OIDplus::logger()->log("[WARN]RA($old_email)!+[INFO]RA($new_email)!+[OK]A!", "Admin changed email address '$old_email' to '$new_email'"); |
224 | daniel-mar | 51 | |
152 | daniel-mar | 52 | $ra_was_logged_in = OIDplus::authUtils()::isRaLoggedIn($old_email); |
61 | daniel-mar | 53 | |
152 | daniel-mar | 54 | $ra = new OIDplusRA($old_email); |
55 | $ra->change_email($new_email); |
||
115 | daniel-mar | 56 | |
261 | daniel-mar | 57 | OIDplus::db()->query("update ###objects set ra_email = ? where ra_email = ?", array($new_email, $old_email)); |
61 | daniel-mar | 58 | |
152 | daniel-mar | 59 | if ($ra_was_logged_in) { |
60 | OIDplus::authUtils()->raLogout($old_email); |
||
61 | OIDplus::authUtils()->raLogin($new_email); |
||
62 | } |
||
61 | daniel-mar | 63 | |
328 | daniel-mar | 64 | return array("status" => 0); |
152 | daniel-mar | 65 | } else { |
288 | daniel-mar | 66 | OIDplus::logger()->log("[INFO]RA($old_email)!+RA($new_email)!", "Requested email address change from '$old_email' to '$new_email'"); |
152 | daniel-mar | 67 | |
68 | $timestamp = time(); |
||
227 | daniel-mar | 69 | $activate_url = OIDplus::getSystemUrl() . '?goto='.urlencode('oidplus:activate_new_ra_email$'.$old_email.'$'.$new_email.'$'.$timestamp.'$'.OIDplus::authUtils()::makeAuthKey('activate_new_ra_email;'.$old_email.';'.$new_email.';'.$timestamp)); |
152 | daniel-mar | 70 | |
71 | $message = file_get_contents(__DIR__ . '/change_request_email.tpl'); |
||
227 | daniel-mar | 72 | $message = str_replace('{{SYSTEM_URL}}', OIDplus::getSystemUrl(), $message); |
257 | daniel-mar | 73 | $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->getValue('system_title'), $message); |
152 | daniel-mar | 74 | $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message); |
75 | $message = str_replace('{{OLD_EMAIL}}', $old_email, $message); |
||
76 | $message = str_replace('{{NEW_EMAIL}}', $new_email, $message); |
||
77 | $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message); |
||
257 | daniel-mar | 78 | OIDplus::mailUtils()->sendMail($new_email, OIDplus::config()->getValue('system_title').' - Change email request', $message); |
152 | daniel-mar | 79 | |
328 | daniel-mar | 80 | return array("status" => 0); |
152 | daniel-mar | 81 | } |
107 | daniel-mar | 82 | } |
83 | |||
321 | daniel-mar | 84 | else if ($actionID == 'activate_new_ra_email') { |
61 | daniel-mar | 85 | if (!OIDplus::config()->getValue('allow_ra_email_change')) { |
360 | daniel-mar | 86 | throw new OIDplusException(_L('This functionality has been disabled by the administrator.')); |
61 | daniel-mar | 87 | } |
88 | |||
321 | daniel-mar | 89 | $old_email = $params['old_email']; |
90 | $new_email = $params['new_email']; |
||
91 | $password = $params['password']; |
||
61 | daniel-mar | 92 | |
321 | daniel-mar | 93 | $auth = $params['auth']; |
94 | $timestamp = $params['timestamp']; |
||
61 | daniel-mar | 95 | |
96 | if (!OIDplus::authUtils()::validateAuthKey('activate_new_ra_email;'.$old_email.';'.$new_email.';'.$timestamp, $auth)) { |
||
360 | daniel-mar | 97 | throw new OIDplusException(_L('Invalid auth key')); |
61 | daniel-mar | 98 | } |
99 | |||
100 | if ((OIDplus::config()->getValue('max_ra_email_change_time') > 0) && (time()-$timestamp > OIDplus::config()->maxEmailChangeTime())) { |
||
360 | daniel-mar | 101 | throw new OIDplusException(_L('Activation link expired!')); |
61 | daniel-mar | 102 | } |
103 | |||
261 | daniel-mar | 104 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($old_email)); |
236 | daniel-mar | 105 | if ($res->num_rows() == 0) { |
360 | daniel-mar | 106 | throw new OIDplusException(_L('eMail address does not exist anymore. It was probably already changed.')); |
61 | daniel-mar | 107 | } |
108 | |||
261 | daniel-mar | 109 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($new_email)); |
236 | daniel-mar | 110 | if ($res->num_rows() > 0) { |
360 | daniel-mar | 111 | throw new OIDplusException(_L('eMail address is already used by another RA. To merge accounts, please contact the superior RA of your objects and request an owner change of your objects.')); |
61 | daniel-mar | 112 | } |
113 | |||
114 | $ra = new OIDplusRA($old_email); |
||
115 | if (!$ra->checkPassword($password)) { |
||
360 | daniel-mar | 116 | throw new OIDplusException(_L('Wrong password')); |
61 | daniel-mar | 117 | } |
118 | |||
119 | $ra->change_email($new_email); |
||
120 | |||
261 | daniel-mar | 121 | OIDplus::db()->query("update ###objects set ra_email = ? where ra_email = ?", array($new_email, $old_email)); |
61 | daniel-mar | 122 | |
123 | OIDplus::authUtils()->raLogout($old_email); |
||
124 | OIDplus::authUtils()->raLogin($new_email); |
||
125 | |||
288 | daniel-mar | 126 | OIDplus::logger()->log("[OK]RA($new_email)!+RA($old_email)!", "RA '$old_email' has changed their email address to '$new_email'"); |
115 | daniel-mar | 127 | |
61 | daniel-mar | 128 | $message = file_get_contents(__DIR__ . '/email_change_confirmation.tpl'); |
227 | daniel-mar | 129 | $message = str_replace('{{SYSTEM_URL}}', OIDplus::getSystemUrl(), $message); |
257 | daniel-mar | 130 | $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->getValue('system_title'), $message); |
76 | daniel-mar | 131 | $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message); |
61 | daniel-mar | 132 | $message = str_replace('{{OLD_EMAIL}}', $old_email, $message); |
133 | $message = str_replace('{{NEW_EMAIL}}', $new_email, $message); |
||
257 | daniel-mar | 134 | OIDplus::mailUtils()->sendMail($old_email, OIDplus::config()->getValue('system_title').' - eMail address changed', $message); |
61 | daniel-mar | 135 | |
328 | daniel-mar | 136 | return array("status" => 0); |
321 | daniel-mar | 137 | } else { |
360 | daniel-mar | 138 | throw new OIDplusException(_L('Unknown action ID')); |
61 | daniel-mar | 139 | } |
140 | } |
||
141 | |||
75 | daniel-mar | 142 | public function init($html=true) { |
263 | daniel-mar | 143 | OIDplus::config()->prepareConfigKey('max_ra_email_change_time', 'Max RA email change time in seconds (0 = infinite)', '0', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
144 | if (!is_numeric($value) || ($value < 0)) { |
||
360 | daniel-mar | 145 | throw new OIDplusException(_L('Please enter a valid value.')); |
263 | daniel-mar | 146 | } |
147 | }); |
||
148 | OIDplus::config()->prepareConfigKey('allow_ra_email_change', 'Allow that RAs change their email address (0/1)', '1', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
||
61 | daniel-mar | 149 | if (($value != '0') && ($value != '1')) { |
360 | daniel-mar | 150 | throw new OIDplusException(_L('Please enter a valid value (0=no, 1=yes).')); |
61 | daniel-mar | 151 | } |
263 | daniel-mar | 152 | }); |
61 | daniel-mar | 153 | } |
154 | |||
155 | public function gui($id, &$out, &$handled) { |
||
156 | if (explode('$',$id)[0] == 'oidplus:change_ra_email') { |
||
157 | $handled = true; |
||
281 | daniel-mar | 158 | |
159 | $ra_email = explode('$',$id)[1]; |
||
160 | |||
360 | daniel-mar | 161 | $out['title'] = _L('Change RA email'); |
241 | daniel-mar | 162 | $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : ''; |
61 | daniel-mar | 163 | |
281 | daniel-mar | 164 | if (!OIDplus::authUtils()::isRaLoggedIn($ra_email) && !OIDplus::authUtils()::isAdminLoggedIn()) { |
165 | $out['icon'] = 'img/error_big.png'; |
||
360 | daniel-mar | 166 | $out['text'] = '<p>'._L('You need to <a %1>log in</a> as the requested RA %2 or as admin.',OIDplus::gui()->link('oidplus:login'),'<b>'.htmlentities($ra_email).'</b>').'</p>'; |
281 | daniel-mar | 167 | return; |
168 | } |
||
61 | daniel-mar | 169 | |
261 | daniel-mar | 170 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($ra_email)); |
236 | daniel-mar | 171 | if ($res->num_rows() == 0) { |
61 | daniel-mar | 172 | $out['icon'] = 'img/error_big.png'; |
360 | daniel-mar | 173 | $out['text'] = _L('RA "%1" does not exist','<b>'.htmlentities($ra_email).'</b>'); |
281 | daniel-mar | 174 | return; |
175 | } |
||
360 | daniel-mar | 176 | |
281 | daniel-mar | 177 | if (!OIDplus::config()->getValue('allow_ra_email_change') && !OIDplus::authUtils()::isAdminLoggedIn()) { |
61 | daniel-mar | 178 | $out['icon'] = 'img/error_big.png'; |
360 | daniel-mar | 179 | $out['text'] = '<p>'._L('This functionality has been disabled by the administrator.').'</p>'; |
281 | daniel-mar | 180 | return; |
181 | } |
||
360 | daniel-mar | 182 | |
281 | daniel-mar | 183 | if (OIDplus::authUtils()::isAdminLoggedIn()) { |
184 | $out['text'] .= '<form id="changeRaEmailForm" onsubmit="return changeRaEmailFormOnSubmit(true);">'; |
||
185 | $out['text'] .= '<input type="hidden" id="old_email" value="'.htmlentities($ra_email).'"/><br>'; |
||
360 | daniel-mar | 186 | $out['text'] .= '<div><label class="padding_label">'._L('Old address').':</label><b>'.htmlentities($ra_email).'</b></div>'; |
187 | $out['text'] .= '<div><label class="padding_label">'._L('New address').':</label><input type="text" id="new_email" value=""/></div>'; |
||
188 | $out['text'] .= '<br><input type="submit" value="'._L('Change password').'"> '._L('(admin does not require email verification)').'</form>'; |
||
61 | daniel-mar | 189 | } else { |
281 | daniel-mar | 190 | $out['text'] .= '<form id="changeRaEmailForm" onsubmit="return changeRaEmailFormOnSubmit(false);">'; |
191 | $out['text'] .= '<input type="hidden" id="old_email" value="'.htmlentities($ra_email).'"/><br>'; |
||
360 | daniel-mar | 192 | $out['text'] .= '<div><label class="padding_label">'._L('Old address').':</label><b>'.htmlentities($ra_email).'</b></div>'; |
193 | $out['text'] .= '<div><label class="padding_label">'._L('New address').':</label><input type="text" id="new_email" value=""/></div>'; |
||
194 | $out['text'] .= '<br><input type="submit" value="'._L('Send new activation email').'"></form>'; |
||
61 | daniel-mar | 195 | } |
196 | } else if (explode('$',$id)[0] == 'oidplus:activate_new_ra_email') { |
||
197 | $handled = true; |
||
198 | |||
199 | $old_email = explode('$',$id)[1]; |
||
200 | $new_email = explode('$',$id)[2]; |
||
201 | $timestamp = explode('$',$id)[3]; |
||
202 | $auth = explode('$',$id)[4]; |
||
203 | |||
152 | daniel-mar | 204 | if (!OIDplus::config()->getValue('allow_ra_email_change') && !OIDplus::authUtils()::isAdminLoggedIn()) { |
61 | daniel-mar | 205 | $out['icon'] = 'img/error_big.png'; |
360 | daniel-mar | 206 | $out['text'] = '<p>'._L('This functionality has been disabled by the administrator.').'</p>'; |
281 | daniel-mar | 207 | return; |
208 | } |
||
209 | |||
360 | daniel-mar | 210 | $out['title'] = _L('Perform email address change'); |
281 | daniel-mar | 211 | $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : ''; |
212 | |||
213 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($old_email)); |
||
214 | if ($res->num_rows() == 0) { |
||
215 | $out['icon'] = 'img/error_big.png'; |
||
360 | daniel-mar | 216 | $out['text'] = _L('eMail address does not exist anymore. It was probably already changed.'); |
61 | daniel-mar | 217 | } else { |
281 | daniel-mar | 218 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($new_email)); |
219 | if ($res->num_rows() > 0) { |
||
61 | daniel-mar | 220 | $out['icon'] = 'img/error_big.png'; |
360 | daniel-mar | 221 | $out['text'] = _L('eMail address is already used by another RA. To merge accounts, please contact the superior RA of your objects and request an owner change of your objects.'); |
61 | daniel-mar | 222 | } else { |
281 | daniel-mar | 223 | if (!OIDplus::authUtils()::validateAuthKey('activate_new_ra_email;'.$old_email.';'.$new_email.';'.$timestamp, $auth)) { |
61 | daniel-mar | 224 | $out['icon'] = 'img/error_big.png'; |
360 | daniel-mar | 225 | $out['text'] = _L('Invalid authorization. Is the URL OK?'); |
61 | daniel-mar | 226 | } else { |
360 | daniel-mar | 227 | $out['text'] = '<p>'._L('Old eMail-Address').': <b>'.$old_email.'</b></p> |
228 | <p>'._L('New eMail-Address').': <b>'.$new_email.'</b></p> |
||
61 | daniel-mar | 229 | |
281 | daniel-mar | 230 | <form id="activateNewRaEmailForm" onsubmit="return activateNewRaEmailFormOnSubmit();"> |
231 | <input type="hidden" id="old_email" value="'.htmlentities($old_email).'"/> |
||
232 | <input type="hidden" id="new_email" value="'.htmlentities($new_email).'"/> |
||
233 | <input type="hidden" id="timestamp" value="'.htmlentities($timestamp).'"/> |
||
234 | <input type="hidden" id="auth" value="'.htmlentities($auth).'"/> |
||
61 | daniel-mar | 235 | |
360 | daniel-mar | 236 | <div><label class="padding_label">'._L('Please verify your password').':</label><input type="password" id="password" value=""/></div> |
237 | <br><input type="submit" value="'._L('Change email address').'"> |
||
281 | daniel-mar | 238 | </form>'; |
61 | daniel-mar | 239 | } |
240 | } |
||
241 | } |
||
242 | } |
||
243 | } |
||
244 | |||
106 | daniel-mar | 245 | public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') { |
281 | daniel-mar | 246 | if (!$ra_email) return false; |
247 | if (!OIDplus::authUtils()::isRaLoggedIn($ra_email) && !OIDplus::authUtils()::isAdminLoggedIn()) return false; |
||
360 | daniel-mar | 248 | |
61 | daniel-mar | 249 | if (file_exists(__DIR__.'/treeicon.png')) { |
241 | daniel-mar | 250 | $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png'; |
61 | daniel-mar | 251 | } else { |
252 | $tree_icon = null; // default icon (folder) |
||
253 | } |
||
254 | |||
255 | $json[] = array( |
||
256 | 'id' => 'oidplus:change_ra_email$'.$ra_email, |
||
257 | 'icon' => $tree_icon, |
||
360 | daniel-mar | 258 | 'text' => _L('Change email address') |
61 | daniel-mar | 259 | ); |
104 | daniel-mar | 260 | |
261 | return true; |
||
61 | daniel-mar | 262 | } |
108 | daniel-mar | 263 | |
264 | public function tree_search($request) { |
||
265 | return false; |
||
266 | } |
||
360 | daniel-mar | 267 | } |