Rev 1267 | Rev 1283 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1267 | Rev 1282 | ||
---|---|---|---|
1 | <?php |
1 | <?php |
2 | 2 | ||
3 | /* |
3 | /* |
4 | * OIDplus 2.0 |
4 | * OIDplus 2.0 |
5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
6 | * |
6 | * |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | * you may not use this file except in compliance with 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 |
9 | * You may obtain a copy of the License at |
10 | * |
10 | * |
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | * |
12 | * |
13 | * Unless required by applicable law or agreed to in writing, software |
13 | * Unless required by applicable law or agreed to in writing, software |
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | * See the License for the specific language governing permissions and |
16 | * See the License for the specific language governing permissions and |
17 | * limitations under the License. |
17 | * limitations under the License. |
18 | */ |
18 | */ |
19 | 19 | ||
20 | namespace ViaThinkSoft\OIDplus; |
20 | namespace ViaThinkSoft\OIDplus; |
21 | 21 | ||
22 | // phpcs:disable PSR1.Files.SideEffects |
22 | // phpcs:disable PSR1.Files.SideEffects |
23 | \defined('INSIDE_OIDPLUS') or die; |
23 | \defined('INSIDE_OIDPLUS') or die; |
24 | // phpcs:enable PSR1.Files.SideEffects |
24 | // phpcs:enable PSR1.Files.SideEffects |
25 | 25 | ||
26 | class OIDplusPageRaChangeEMail extends OIDplusPagePluginRa { |
26 | class OIDplusPageRaChangeEMail extends OIDplusPagePluginRa { |
27 | 27 | ||
28 | /** |
28 | /** |
29 | * @param string $actionID |
29 | * @param string $actionID |
30 | * @param array $params |
30 | * @param array $params |
31 | * @return array |
31 | * @return array |
32 | * @throws OIDplusException |
32 | * @throws OIDplusException |
33 | * @throws OIDplusMailException |
33 | * @throws OIDplusMailException |
34 | */ |
34 | */ |
35 | public function action(string $actionID, array $params): array { |
35 | public function action(string $actionID, array $params): array { |
36 | if ($actionID == 'change_ra_email') { |
36 | if ($actionID == 'change_ra_email') { |
37 | if (!OIDplus::config()->getValue('allow_ra_email_change') && !OIDplus::authUtils()->isAdminLoggedIn()) { |
37 | if (!OIDplus::config()->getValue('allow_ra_email_change') && !OIDplus::authUtils()->isAdminLoggedIn()) { |
38 | throw new OIDplusException(_L('This functionality has been disabled by the administrator.')); |
38 | throw new OIDplusException(_L('This functionality has been disabled by the administrator.')); |
39 | } |
39 | } |
40 | 40 | ||
41 | _CheckParamExists($params, 'old_email'); |
41 | _CheckParamExists($params, 'old_email'); |
42 | _CheckParamExists($params, 'new_email'); |
42 | _CheckParamExists($params, 'new_email'); |
43 | 43 | ||
44 | $old_email = $params['old_email']; |
44 | $old_email = $params['old_email']; |
45 | $new_email = $params['new_email']; |
45 | $new_email = $params['new_email']; |
46 | 46 | ||
47 | $ra = new OIDplusRA($old_email); |
47 | $ra = new OIDplusRA($old_email); |
48 | if ($ra->isPasswordLess() && !OIDplus::authUtils()->isAdminLoggedIn()) { |
48 | if ($ra->isPasswordLess() && !OIDplus::authUtils()->isAdminLoggedIn()) { |
49 | throw new OIDplusException(_L('E-Mail-Address cannot be changed because this user does not have a password')); |
49 | throw new OIDplusException(_L('E-Mail-Address cannot be changed because this user does not have a password')); |
50 | } |
50 | } |
51 | 51 | ||
52 | if (!OIDplus::authUtils()->isRaLoggedIn($old_email) && !OIDplus::authUtils()->isAdminLoggedIn()) { |
52 | if (!OIDplus::authUtils()->isRaLoggedIn($old_email) && !OIDplus::authUtils()->isAdminLoggedIn()) { |
53 | throw new OIDplusException(_L('Authentication error. Please log in as admin, or as the RA to update its email address.'), null, 401); |
53 | throw new OIDplusException(_L('Authentication error. Please log in as admin, or as the RA to update its email address.'), null, 401); |
54 | } |
54 | } |
55 | 55 | ||
56 | if (!OIDplus::mailUtils()->validMailAddress($new_email)) { |
56 | if (!OIDplus::mailUtils()->validMailAddress($new_email)) { |
57 | throw new OIDplusException(_L('eMail address is invalid.')); |
57 | throw new OIDplusException(_L('eMail address is invalid.')); |
58 | } |
58 | } |
59 | 59 | ||
60 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($old_email)); |
60 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($old_email)); |
61 | if (!$res->any()) { |
61 | if (!$res->any()) { |
62 | throw new OIDplusException(_L('eMail address does not exist anymore. It was probably already changed.')); |
62 | throw new OIDplusException(_L('eMail address does not exist anymore. It was probably already changed.')); |
63 | } |
63 | } |
64 | 64 | ||
65 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($new_email)); |
65 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($new_email)); |
66 | if ($res->any()) { |
66 | if ($res->any()) { |
67 | 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.')); |
67 | 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.')); |
68 | } |
68 | } |
69 | 69 | ||
70 | if (OIDplus::authUtils()->isAdminLoggedIn()) { |
70 | if (OIDplus::authUtils()->isAdminLoggedIn()) { |
71 | $ra_was_logged_in = OIDplus::authUtils()->isRaLoggedIn($old_email); |
71 | $ra_was_logged_in = OIDplus::authUtils()->isRaLoggedIn($old_email); |
72 | 72 | ||
73 | $ra = new OIDplusRA($old_email); |
73 | $ra = new OIDplusRA($old_email); |
74 | 74 | ||
75 | // Change RA email |
75 | // Change RA email |
76 | $ra->change_email($new_email); |
76 | $ra->change_email($new_email); |
77 | OIDplus::logger()->log("V2:[WARN]RA(%1)+[INFO]RA(%2)+[OK]A", "Admin changed email address '%1' to '%2'", $old_email, $new_email); |
77 | OIDplus::logger()->log("V2:[WARN]RA(%1)+[INFO]RA(%2)+[OK]A", "Admin changed email address '%1' to '%2'", $old_email, $new_email); |
78 | 78 | ||
79 | // Change objects |
79 | // Change objects |
80 | $res = OIDplus::db()->query("select id from ###objects where ra_email = ?", array($old_email)); |
80 | $res = OIDplus::db()->query("select id from ###objects where ra_email = ?", array($old_email)); |
81 | while ($row = $res->fetch_array()) { |
81 | while ($row = $res->fetch_array()) { |
82 | OIDplus::logger()->log("V2:[INFO]OID(%1)+SUPOID(%1)", "Admin changed email address of RA '%2' (owner of %1) to '%3'", $row['id'], $old_email, $new_email); |
82 | OIDplus::logger()->log("V2:[INFO]OID(%1)+SUPOID(%1)", "Admin changed email address of RA '%2' (owner of %1) to '%3'", $row['id'], $old_email, $new_email); |
83 | } |
83 | } |
84 | OIDplus::db()->query("update ###objects set ra_email = ? where ra_email = ?", array($new_email, $old_email)); |
84 | OIDplus::db()->query("update ###objects set ra_email = ? where ra_email = ?", array($new_email, $old_email)); |
85 | OIDplusObject::resetObjectInformationCache(); |
85 | OIDplusObject::resetObjectInformationCache(); |
86 | 86 | ||
87 | // Re-login |
87 | // Re-login |
88 | if ($ra_was_logged_in) { |
88 | if ($ra_was_logged_in) { |
89 | OIDplus::authUtils()->raLogout($old_email); |
89 | OIDplus::authUtils()->raLogout($old_email); |
90 | OIDplus::authUtils()->raLogin($new_email); |
90 | OIDplus::authUtils()->raLogin($new_email); |
91 | } |
91 | } |
92 | 92 | ||
93 | return array("status" => 0); |
93 | return array("status" => 0); |
94 | } else { |
94 | } else { |
95 | OIDplus::logger()->log("V2:[INFO]RA(%1)+RA(%2)", "Requested email address change from '%1' to '%2'", $old_email, $new_email); |
95 | OIDplus::logger()->log("V2:[INFO]RA(%1)+RA(%2)", "Requested email address change from '%1' to '%2'", $old_email, $new_email); |
96 | 96 | ||
97 | $timestamp = time(); |
97 | $timestamp = time(); |
98 | $activate_url = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL) . '?goto='.urlencode('oidplus:activate_new_ra_email$'.$old_email.'$'.$new_email.'$'.$timestamp.'$'.OIDplus::authUtils()->makeAuthKey('activate_new_ra_email;'.$old_email.';'.$new_email.';'.$timestamp)); |
98 | $activate_url = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL) . '?goto='.urlencode('oidplus:activate_new_ra_email$'.$old_email.'$'.$new_email.'$'.$timestamp.'$'.OIDplus::authUtils()->makeAuthKey('5ef24124-f4fb-11ed-b67e-3c4a92df8582:'.$old_email.'/'.$new_email.'/'.$timestamp)); |
99 | 99 | ||
100 | $message = file_get_contents(__DIR__ . '/change_request_email.tpl'); |
100 | $message = file_get_contents(__DIR__ . '/change_request_email.tpl'); |
101 | $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL), $message); |
101 | $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL), $message); |
102 | $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->getValue('system_title'), $message); |
102 | $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->getValue('system_title'), $message); |
103 | $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message); |
103 | $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message); |
104 | $message = str_replace('{{OLD_EMAIL}}', $old_email, $message); |
104 | $message = str_replace('{{OLD_EMAIL}}', $old_email, $message); |
105 | $message = str_replace('{{NEW_EMAIL}}', $new_email, $message); |
105 | $message = str_replace('{{NEW_EMAIL}}', $new_email, $message); |
106 | $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message); |
106 | $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message); |
107 | OIDplus::mailUtils()->sendMail($new_email, OIDplus::config()->getValue('system_title').' - Change email request', $message); |
107 | OIDplus::mailUtils()->sendMail($new_email, OIDplus::config()->getValue('system_title').' - Change email request', $message); |
108 | 108 | ||
109 | return array("status" => 0); |
109 | return array("status" => 0); |
110 | } |
110 | } |
111 | } |
111 | } |
112 | 112 | ||
113 | else if ($actionID == 'activate_new_ra_email') { |
113 | else if ($actionID == 'activate_new_ra_email') { |
114 | if (!OIDplus::config()->getValue('allow_ra_email_change')) { |
114 | if (!OIDplus::config()->getValue('allow_ra_email_change')) { |
115 | throw new OIDplusException(_L('This functionality has been disabled by the administrator.')); |
115 | throw new OIDplusException(_L('This functionality has been disabled by the administrator.')); |
116 | } |
116 | } |
117 | 117 | ||
118 | _CheckParamExists($params, 'old_email'); |
118 | _CheckParamExists($params, 'old_email'); |
119 | _CheckParamExists($params, 'new_email'); |
119 | _CheckParamExists($params, 'new_email'); |
120 | _CheckParamExists($params, 'password'); |
120 | _CheckParamExists($params, 'password'); |
121 | _CheckParamExists($params, 'auth'); |
121 | _CheckParamExists($params, 'auth'); |
122 | _CheckParamExists($params, 'timestamp'); |
122 | _CheckParamExists($params, 'timestamp'); |
123 | 123 | ||
124 | $old_email = $params['old_email']; |
124 | $old_email = $params['old_email']; |
125 | $new_email = $params['new_email']; |
125 | $new_email = $params['new_email']; |
126 | $password = $params['password']; |
126 | $password = $params['password']; |
127 | 127 | ||
128 | $auth = $params['auth']; |
128 | $auth = $params['auth']; |
129 | $timestamp = $params['timestamp']; |
129 | $timestamp = $params['timestamp']; |
130 | 130 | ||
131 | $ra_was_logged_in = OIDplus::authUtils()->isRaLoggedIn($old_email); |
131 | $ra_was_logged_in = OIDplus::authUtils()->isRaLoggedIn($old_email); |
132 | 132 | ||
133 | $ra = new OIDplusRA($old_email); |
133 | $ra = new OIDplusRA($old_email); |
134 | if ($ra->isPasswordLess() && !OIDplus::authUtils()->isAdminLoggedIn()) { |
134 | if ($ra->isPasswordLess() && !OIDplus::authUtils()->isAdminLoggedIn()) { |
135 | throw new OIDplusException(_L('E-Mail-Address cannot be changed because this user does not have a password')); |
135 | throw new OIDplusException(_L('E-Mail-Address cannot be changed because this user does not have a password')); |
136 | } |
136 | } |
137 | 137 | ||
138 | if (!OIDplus::authUtils()->validateAuthKey('activate_new_ra_email;'.$old_email.';'.$new_email.';'.$timestamp, $auth)) { |
138 | if (!OIDplus::authUtils()->validateAuthKey('5ef24124-f4fb-11ed-b67e-3c4a92df8582:'.$old_email.'/'.$new_email.'/'.$timestamp, $auth)) { |
139 | throw new OIDplusException(_L('Invalid auth key')); |
139 | throw new OIDplusException(_L('Invalid auth key')); |
140 | } |
140 | } |
141 | 141 | ||
142 | if ((OIDplus::config()->getValue('max_ra_email_change_time') > 0) && (time()-$timestamp > OIDplus::config()->getValue('max_ra_email_change_time'))) { |
142 | if ((OIDplus::config()->getValue('max_ra_email_change_time') > 0) && (time()-$timestamp > OIDplus::config()->getValue('max_ra_email_change_time'))) { |
143 | throw new OIDplusException(_L('Activation link expired!')); |
143 | throw new OIDplusException(_L('Activation link expired!')); |
144 | } |
144 | } |
145 | 145 | ||
146 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($old_email)); |
146 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($old_email)); |
147 | if (!$res->any()) { |
147 | if (!$res->any()) { |
148 | throw new OIDplusException(_L('eMail address does not exist anymore. It was probably already changed.')); |
148 | throw new OIDplusException(_L('eMail address does not exist anymore. It was probably already changed.')); |
149 | } |
149 | } |
150 | 150 | ||
151 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($new_email)); |
151 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($new_email)); |
152 | if ($res->any()) { |
152 | if ($res->any()) { |
153 | 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.')); |
153 | 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.')); |
154 | } |
154 | } |
155 | 155 | ||
156 | $ra = new OIDplusRA($old_email); |
156 | $ra = new OIDplusRA($old_email); |
157 | if (!$ra->isPasswordLess()) { |
157 | if (!$ra->isPasswordLess()) { |
158 | if (!$ra->checkPassword($password)) { |
158 | if (!$ra->checkPassword($password)) { |
159 | throw new OIDplusException(_L('Wrong password')); |
159 | throw new OIDplusException(_L('Wrong password')); |
160 | } |
160 | } |
161 | } |
161 | } |
162 | 162 | ||
163 | // Change address of RA |
163 | // Change address of RA |
164 | $ra->change_email($new_email); |
164 | $ra->change_email($new_email); |
165 | OIDplus::logger()->log("V2:[OK]RA(%2)+RA(%1)", "RA '%1' has changed their email address to '%2'", $old_email, $new_email); |
165 | OIDplus::logger()->log("V2:[OK]RA(%2)+RA(%1)", "RA '%1' has changed their email address to '%2'", $old_email, $new_email); |
166 | 166 | ||
167 | // Change objects |
167 | // Change objects |
168 | $res = OIDplus::db()->query("select id from ###objects where ra_email = ?", array($old_email)); |
168 | $res = OIDplus::db()->query("select id from ###objects where ra_email = ?", array($old_email)); |
169 | while ($row = $res->fetch_array()) { |
169 | while ($row = $res->fetch_array()) { |
170 | OIDplus::logger()->log("V2:[INFO]OID(%1)+SUPOID(%1)", "RA '%2' (owner of %1) has changed their email address to '%3'", $row['id'], $old_email, $new_email); |
170 | OIDplus::logger()->log("V2:[INFO]OID(%1)+SUPOID(%1)", "RA '%2' (owner of %1) has changed their email address to '%3'", $row['id'], $old_email, $new_email); |
171 | } |
171 | } |
172 | OIDplus::db()->query("update ###objects set ra_email = ? where ra_email = ?", array($new_email, $old_email)); |
172 | OIDplus::db()->query("update ###objects set ra_email = ? where ra_email = ?", array($new_email, $old_email)); |
173 | OIDplusObject::resetObjectInformationCache(); |
173 | OIDplusObject::resetObjectInformationCache(); |
174 | 174 | ||
175 | // Re-login |
175 | // Re-login |
176 | if ($ra_was_logged_in) { |
176 | if ($ra_was_logged_in) { |
177 | OIDplus::authUtils()->raLogout($old_email); |
177 | OIDplus::authUtils()->raLogout($old_email); |
178 | OIDplus::authUtils()->raLogin($new_email); |
178 | OIDplus::authUtils()->raLogin($new_email); |
179 | } |
179 | } |
180 | 180 | ||
181 | // Send email |
181 | // Send email |
182 | $message = file_get_contents(__DIR__ . '/email_change_confirmation.tpl'); |
182 | $message = file_get_contents(__DIR__ . '/email_change_confirmation.tpl'); |
183 | $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL), $message); |
183 | $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL), $message); |
184 | $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->getValue('system_title'), $message); |
184 | $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->getValue('system_title'), $message); |
185 | $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message); |
185 | $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message); |
186 | $message = str_replace('{{OLD_EMAIL}}', $old_email, $message); |
186 | $message = str_replace('{{OLD_EMAIL}}', $old_email, $message); |
187 | $message = str_replace('{{NEW_EMAIL}}', $new_email, $message); |
187 | $message = str_replace('{{NEW_EMAIL}}', $new_email, $message); |
188 | OIDplus::mailUtils()->sendMail($old_email, OIDplus::config()->getValue('system_title').' - eMail address changed', $message); |
188 | OIDplus::mailUtils()->sendMail($old_email, OIDplus::config()->getValue('system_title').' - eMail address changed', $message); |
189 | 189 | ||
190 | return array("status" => 0); |
190 | return array("status" => 0); |
191 | } else { |
191 | } else { |
192 | return parent::action($actionID, $params); |
192 | return parent::action($actionID, $params); |
193 | } |
193 | } |
194 | } |
194 | } |
195 | 195 | ||
196 | /** |
196 | /** |
197 | * @param bool $html |
197 | * @param bool $html |
198 | * @return void |
198 | * @return void |
199 | * @throws OIDplusException |
199 | * @throws OIDplusException |
200 | */ |
200 | */ |
201 | public function init(bool $html=true) { |
201 | public function init(bool $html=true) { |
202 | OIDplus::config()->prepareConfigKey('max_ra_email_change_time', 'Max RA email change time in seconds (0 = infinite)', '0', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
202 | OIDplus::config()->prepareConfigKey('max_ra_email_change_time', 'Max RA email change time in seconds (0 = infinite)', '0', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
203 | if (!is_numeric($value) || ($value < 0)) { |
203 | if (!is_numeric($value) || ($value < 0)) { |
204 | throw new OIDplusException(_L('Please enter a valid value.')); |
204 | throw new OIDplusException(_L('Please enter a valid value.')); |
205 | } |
205 | } |
206 | }); |
206 | }); |
207 | OIDplus::config()->prepareConfigKey('allow_ra_email_change', 'Allow that RAs change their email address (0/1)', '1', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
207 | OIDplus::config()->prepareConfigKey('allow_ra_email_change', 'Allow that RAs change their email address (0/1)', '1', OIDplusConfig::PROTECTION_EDITABLE, function($value) { |
208 | if (($value != '0') && ($value != '1')) { |
208 | if (($value != '0') && ($value != '1')) { |
209 | throw new OIDplusException(_L('Please enter a valid value (0=no, 1=yes).')); |
209 | throw new OIDplusException(_L('Please enter a valid value (0=no, 1=yes).')); |
210 | } |
210 | } |
211 | }); |
211 | }); |
212 | } |
212 | } |
213 | 213 | ||
214 | /** |
214 | /** |
215 | * @param string $id |
215 | * @param string $id |
216 | * @param array $out |
216 | * @param array $out |
217 | * @param bool $handled |
217 | * @param bool $handled |
218 | * @return void |
218 | * @return void |
219 | * @throws OIDplusException |
219 | * @throws OIDplusException |
220 | */ |
220 | */ |
221 | public function gui(string $id, array &$out, bool &$handled) { |
221 | public function gui(string $id, array &$out, bool &$handled) { |
222 | if (explode('$',$id)[0] == 'oidplus:change_ra_email') { |
222 | if (explode('$',$id)[0] == 'oidplus:change_ra_email') { |
223 | $handled = true; |
223 | $handled = true; |
224 | 224 | ||
225 | $ra_email = explode('$',$id)[1]; |
225 | $ra_email = explode('$',$id)[1]; |
226 | 226 | ||
227 | $out['title'] = _L('Change RA email'); |
227 | $out['title'] = _L('Change RA email'); |
228 | $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : ''; |
228 | $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : ''; |
229 | 229 | ||
230 | if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) { |
230 | if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) { |
231 | throw new OIDplusHtmlException(_L('You need to <a %1>log in</a> as the requested RA %2 or as admin.',OIDplus::gui()->link('oidplus:login$ra$'.$ra_email),'<b>'.htmlentities($ra_email).'</b>'), $out['title'], 401); |
231 | throw new OIDplusHtmlException(_L('You need to <a %1>log in</a> as the requested RA %2 or as admin.',OIDplus::gui()->link('oidplus:login$ra$'.$ra_email),'<b>'.htmlentities($ra_email).'</b>'), $out['title'], 401); |
232 | } |
232 | } |
233 | 233 | ||
234 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($ra_email)); |
234 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($ra_email)); |
235 | if (!$res->any()) { |
235 | if (!$res->any()) { |
236 | throw new OIDplusHtmlException(_L('RA "%1" does not exist','<b>'.htmlentities($ra_email).'</b>'), $out['title']); |
236 | throw new OIDplusHtmlException(_L('RA "%1" does not exist','<b>'.htmlentities($ra_email).'</b>'), $out['title']); |
237 | } |
237 | } |
238 | 238 | ||
239 | if (!OIDplus::config()->getValue('allow_ra_email_change') && !OIDplus::authUtils()->isAdminLoggedIn()) { |
239 | if (!OIDplus::config()->getValue('allow_ra_email_change') && !OIDplus::authUtils()->isAdminLoggedIn()) { |
240 | throw new OIDplusException(_L('This functionality has been disabled by the administrator.'), $out['title']); |
240 | throw new OIDplusException(_L('This functionality has been disabled by the administrator.'), $out['title']); |
241 | } |
241 | } |
242 | 242 | ||
243 | if (OIDplus::authUtils()->isAdminLoggedIn()) { |
243 | if (OIDplus::authUtils()->isAdminLoggedIn()) { |
244 | $ra = new OIDplusRA($ra_email); |
244 | $ra = new OIDplusRA($ra_email); |
245 | if ($ra->isPasswordLess()) { |
245 | if ($ra->isPasswordLess()) { |
246 | $out['text'] .= '<p>'._L('Attention: This user does not have a password because they log in using LDAP or Google OAuth etc.').'</p>'; |
246 | $out['text'] .= '<p>'._L('Attention: This user does not have a password because they log in using LDAP or Google OAuth etc.').'</p>'; |
247 | $out['text'] .= '<p>'._L('If you change the email address, the user cannot log in anymore, because the LDAP/OAuth plugin identifies the user via email address, not OpenID.').'</p>'; |
247 | $out['text'] .= '<p>'._L('If you change the email address, the user cannot log in anymore, because the LDAP/OAuth plugin identifies the user via email address, not OpenID.').'</p>'; |
248 | $out['text'] .= '<p>'._L('If you want to change the email address of the user, please <a %1>define a password</a> for them, so that they can use the regular login method using their new email address.', OIDplus::gui()->link('oidplus:change_ra_password$'.$ra_email)).'</p>'; |
248 | $out['text'] .= '<p>'._L('If you want to change the email address of the user, please <a %1>define a password</a> for them, so that they can use the regular login method using their new email address.', OIDplus::gui()->link('oidplus:change_ra_password$'.$ra_email)).'</p>'; |
249 | } |
249 | } |
250 | 250 | ||
251 | $out['text'] .= '<form id="changeRaEmailForm" action="javascript:void(0);" action="javascript:void(0);" onsubmit="return OIDplusPageRaChangeEMail.changeRaEmailFormOnSubmit(true);">'; |
251 | $out['text'] .= '<form id="changeRaEmailForm" action="javascript:void(0);" action="javascript:void(0);" onsubmit="return OIDplusPageRaChangeEMail.changeRaEmailFormOnSubmit(true);">'; |
252 | $out['text'] .= '<input type="hidden" id="old_email" value="'.htmlentities($ra_email).'"/><br>'; |
252 | $out['text'] .= '<input type="hidden" id="old_email" value="'.htmlentities($ra_email).'"/><br>'; |
253 | $out['text'] .= '<div><label class="padding_label">'._L('Old address').':</label><b>'.htmlentities($ra_email).'</b></div>'; |
253 | $out['text'] .= '<div><label class="padding_label">'._L('Old address').':</label><b>'.htmlentities($ra_email).'</b></div>'; |
254 | $out['text'] .= '<div><label class="padding_label">'._L('New address').':</label><input type="text" id="new_email" value=""/></div>'; |
254 | $out['text'] .= '<div><label class="padding_label">'._L('New address').':</label><input type="text" id="new_email" value=""/></div>'; |
255 | $out['text'] .= '<br><input type="submit" value="'._L('Change password').'"> '._L('(admin does not require email verification)').'</form>'; |
255 | $out['text'] .= '<br><input type="submit" value="'._L('Change password').'"> '._L('(admin does not require email verification)').'</form>'; |
256 | } else { |
256 | } else { |
257 | $ra = new OIDplusRA($ra_email); |
257 | $ra = new OIDplusRA($ra_email); |
258 | if ($ra->isPasswordLess()) { |
258 | if ($ra->isPasswordLess()) { |
259 | $out['icon'] = 'img/error.png'; |
259 | $out['icon'] = 'img/error.png'; |
260 | $out['text'] .= '<p>'._L('Attention: You are logged in without password (via LDAP or Google OAuth etc.).').'</p>'; |
260 | $out['text'] .= '<p>'._L('Attention: You are logged in without password (via LDAP or Google OAuth etc.).').'</p>'; |
261 | $out['text'] .= '<p>'._L('Therefore, you cannot change your email address, otherwise you would lose access to your account!').'</p>'; |
261 | $out['text'] .= '<p>'._L('Therefore, you cannot change your email address, otherwise you would lose access to your account!').'</p>'; |
262 | $out['text'] .= '<p>'._L('If you want to change your email address, then please <a %1>setup a password</a> first, and then use the regular login method to log in using your new email address.', OIDplus::gui()->link('oidplus:change_ra_password$'.$ra_email)).'</p>'; |
262 | $out['text'] .= '<p>'._L('If you want to change your email address, then please <a %1>setup a password</a> first, and then use the regular login method to log in using your new email address.', OIDplus::gui()->link('oidplus:change_ra_password$'.$ra_email)).'</p>'; |
263 | return; // throw new OIDplusHtmlException($out['text'], $out['title']); |
263 | return; // throw new OIDplusHtmlException($out['text'], $out['title']); |
264 | } |
264 | } |
265 | 265 | ||
266 | $out['text'] .= '<form id="changeRaEmailForm" action="javascript:void(0);" action="javascript:void(0);" onsubmit="return OIDplusPageRaChangeEMail.changeRaEmailFormOnSubmit(false);">'; |
266 | $out['text'] .= '<form id="changeRaEmailForm" action="javascript:void(0);" action="javascript:void(0);" onsubmit="return OIDplusPageRaChangeEMail.changeRaEmailFormOnSubmit(false);">'; |
267 | $out['text'] .= '<input type="hidden" id="old_email" value="'.htmlentities($ra_email).'"/><br>'; |
267 | $out['text'] .= '<input type="hidden" id="old_email" value="'.htmlentities($ra_email).'"/><br>'; |
268 | $out['text'] .= '<div><label class="padding_label">'._L('Old address').':</label><b>'.htmlentities($ra_email).'</b></div>'; |
268 | $out['text'] .= '<div><label class="padding_label">'._L('Old address').':</label><b>'.htmlentities($ra_email).'</b></div>'; |
269 | $out['text'] .= '<div><label class="padding_label">'._L('New address').':</label><input type="text" id="new_email" value=""/></div>'; |
269 | $out['text'] .= '<div><label class="padding_label">'._L('New address').':</label><input type="text" id="new_email" value=""/></div>'; |
270 | $out['text'] .= '<br><input type="submit" value="'._L('Send new activation email').'"></form>'; |
270 | $out['text'] .= '<br><input type="submit" value="'._L('Send new activation email').'"></form>'; |
271 | } |
271 | } |
272 | } else if (explode('$',$id)[0] == 'oidplus:activate_new_ra_email') { |
272 | } else if (explode('$',$id)[0] == 'oidplus:activate_new_ra_email') { |
273 | $handled = true; |
273 | $handled = true; |
274 | 274 | ||
275 | $old_email = explode('$',$id)[1]; |
275 | $old_email = explode('$',$id)[1]; |
276 | $new_email = explode('$',$id)[2]; |
276 | $new_email = explode('$',$id)[2]; |
277 | $timestamp = explode('$',$id)[3]; |
277 | $timestamp = explode('$',$id)[3]; |
278 | $auth = explode('$',$id)[4]; |
278 | $auth = explode('$',$id)[4]; |
279 | 279 | ||
280 | $out['title'] = _L('Perform email address change'); |
280 | $out['title'] = _L('Perform email address change'); |
281 | $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : ''; |
281 | $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : ''; |
282 | 282 | ||
283 | if (!OIDplus::config()->getValue('allow_ra_email_change') && !OIDplus::authUtils()->isAdminLoggedIn()) { |
283 | if (!OIDplus::config()->getValue('allow_ra_email_change') && !OIDplus::authUtils()->isAdminLoggedIn()) { |
284 | throw new OIDplusException(_L('This functionality has been disabled by the administrator.'), $out['title']); |
284 | throw new OIDplusException(_L('This functionality has been disabled by the administrator.'), $out['title']); |
285 | } |
285 | } |
286 | 286 | ||
287 | $ra = new OIDplusRA($old_email); |
287 | $ra = new OIDplusRA($old_email); |
288 | if ($ra->isPasswordLess() && !OIDplus::authUtils()->isAdminLoggedIn()) { |
288 | if ($ra->isPasswordLess() && !OIDplus::authUtils()->isAdminLoggedIn()) { |
289 | throw new OIDplusException(_L('E-Mail-Address cannot be changed because this user does not have a password'), $out['title']); |
289 | throw new OIDplusException(_L('E-Mail-Address cannot be changed because this user does not have a password'), $out['title']); |
290 | } |
290 | } |
291 | 291 | ||
292 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($old_email)); |
292 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($old_email)); |
293 | if (!$res->any()) { |
293 | if (!$res->any()) { |
294 | throw new OIDplusException(_L('eMail address does not exist anymore. It was probably already changed.'), $out['title']); |
294 | throw new OIDplusException(_L('eMail address does not exist anymore. It was probably already changed.'), $out['title']); |
295 | } else { |
295 | } else { |
296 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($new_email)); |
296 | $res = OIDplus::db()->query("select * from ###ra where email = ?", array($new_email)); |
297 | if ($res->any()) { |
297 | if ($res->any()) { |
298 | 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.'), $out['title']); |
298 | 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.'), $out['title']); |
299 | } else { |
299 | } else { |
300 | if (!OIDplus::authUtils()->validateAuthKey('activate_new_ra_email;'.$old_email.';'.$new_email.';'.$timestamp, $auth)) { |
300 | if (!OIDplus::authUtils()->validateAuthKey('activate_new_ra_email;'.$old_email.';'.$new_email.';'.$timestamp, $auth)) { |
301 | throw new OIDplusException(_L('Invalid authorization. Is the URL OK?'), $out['title']); |
301 | throw new OIDplusException(_L('Invalid authorization. Is the URL OK?'), $out['title']); |
302 | } else { |
302 | } else { |
303 | $out['text'] = '<p>'._L('Old eMail-Address').': <b>'.$old_email.'</b></p> |
303 | $out['text'] = '<p>'._L('Old eMail-Address').': <b>'.$old_email.'</b></p> |
304 | <p>'._L('New eMail-Address').': <b>'.$new_email.'</b></p> |
304 | <p>'._L('New eMail-Address').': <b>'.$new_email.'</b></p> |
305 | 305 | ||
306 | <form id="activateNewRaEmailForm" action="javascript:void(0);" onsubmit="return OIDplusPageRaChangeEMail.activateNewRaEmailFormOnSubmit();"> |
306 | <form id="activateNewRaEmailForm" action="javascript:void(0);" onsubmit="return OIDplusPageRaChangeEMail.activateNewRaEmailFormOnSubmit();"> |
307 | <input type="hidden" id="old_email" value="'.htmlentities($old_email).'"/> |
307 | <input type="hidden" id="old_email" value="'.htmlentities($old_email).'"/> |
308 | <input type="hidden" id="new_email" value="'.htmlentities($new_email).'"/> |
308 | <input type="hidden" id="new_email" value="'.htmlentities($new_email).'"/> |
309 | <input type="hidden" id="timestamp" value="'.htmlentities($timestamp).'"/> |
309 | <input type="hidden" id="timestamp" value="'.htmlentities($timestamp).'"/> |
310 | <input type="hidden" id="auth" value="'.htmlentities($auth).'"/> |
310 | <input type="hidden" id="auth" value="'.htmlentities($auth).'"/> |
311 | 311 | ||
312 | <div><label class="padding_label">'._L('Please verify your password').':</label><input type="password" id="password" value=""/></div> |
312 | <div><label class="padding_label">'._L('Please verify your password').':</label><input type="password" id="password" value=""/></div> |
313 | <br><input type="submit" value="'._L('Change email address').'"> |
313 | <br><input type="submit" value="'._L('Change email address').'"> |
314 | </form>'; |
314 | </form>'; |
315 | } |
315 | } |
316 | } |
316 | } |
317 | } |
317 | } |
318 | } |
318 | } |
319 | } |
319 | } |
320 | 320 | ||
321 | /** |
321 | /** |
322 | * @param array $json |
322 | * @param array $json |
323 | * @param string|null $ra_email |
323 | * @param string|null $ra_email |
324 | * @param bool $nonjs |
324 | * @param bool $nonjs |
325 | * @param string $req_goto |
325 | * @param string $req_goto |
326 | * @return bool |
326 | * @return bool |
327 | * @throws OIDplusException |
327 | * @throws OIDplusException |
328 | */ |
328 | */ |
329 | public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool { |
329 | public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool { |
330 | if (!$ra_email) return false; |
330 | if (!$ra_email) return false; |
331 | if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) return false; |
331 | if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) return false; |
332 | 332 | ||
333 | if (file_exists(__DIR__.'/img/main_icon16.png')) { |
333 | if (file_exists(__DIR__.'/img/main_icon16.png')) { |
334 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png'; |
334 | $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png'; |
335 | } else { |
335 | } else { |
336 | $tree_icon = null; // default icon (folder) |
336 | $tree_icon = null; // default icon (folder) |
337 | } |
337 | } |
338 | 338 | ||
339 | $json[] = array( |
339 | $json[] = array( |
340 | 'id' => 'oidplus:change_ra_email$'.$ra_email, |
340 | 'id' => 'oidplus:change_ra_email$'.$ra_email, |
341 | 'icon' => $tree_icon, |
341 | 'icon' => $tree_icon, |
342 | 'text' => _L('Change email address') |
342 | 'text' => _L('Change email address') |
343 | ); |
343 | ); |
344 | 344 | ||
345 | return true; |
345 | return true; |
346 | } |
346 | } |
347 | 347 | ||
348 | /** |
348 | /** |
349 | * @param string $request |
349 | * @param string $request |
350 | * @return array|false |
350 | * @return array|false |
351 | */ |
351 | */ |
352 | public function tree_search(string $request) { |
352 | public function tree_search(string $request) { |
353 | return false; |
353 | return false; |
354 | } |
354 | } |
355 | } |
355 | } |
356 | 356 |