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