Subversion Repositories oidplus

Rev

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