Subversion Repositories oidplus

Rev

Rev 1050 | Rev 1113 | 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
 
28
        public function action($actionID, $params) {
29
                if ($actionID == 'change_ra_email') {
30
                        if (!OIDplus::config()->getValue('allow_ra_email_change') && !OIDplus::authUtils()->isAdminLoggedIn()) {
31
                                throw new OIDplusException(_L('This functionality has been disabled by the administrator.'));
32
                        }
33
 
34
                        _CheckParamExists($params, 'old_email');
35
                        _CheckParamExists($params, 'new_email');
36
 
37
                        $old_email = $params['old_email'];
38
                        $new_email = $params['new_email'];
39
 
40
                        $ra = new OIDplusRA($old_email);
41
                        if ($ra->isPasswordLess() && !OIDplus::authUtils()->isAdminLoggedIn()) {
42
                                throw new OIDplusException(_L('E-Mail-Address cannot be changed because this user does not have a password'));
43
                        }
44
 
45
                        if (!OIDplus::authUtils()->isRaLoggedIn($old_email) && !OIDplus::authUtils()->isAdminLoggedIn()) {
46
                                throw new OIDplusException(_L('Authentication error. Please log in as admin, or as the RA to update its email address.'));
47
                        }
48
 
49
                        if (!OIDplus::mailUtils()->validMailAddress($new_email)) {
50
                                throw new OIDplusException(_L('eMail address is invalid.'));
51
                        }
52
 
53
                        $res = OIDplus::db()->query("select * from ###ra where email = ?", array($old_email));
790 daniel-mar 54
                        if (!$res->any()) {
635 daniel-mar 55
                                throw new OIDplusException(_L('eMail address does not exist anymore. It was probably already changed.'));
56
                        }
57
 
58
                        $res = OIDplus::db()->query("select * from ###ra where email = ?", array($new_email));
790 daniel-mar 59
                        if ($res->any()) {
635 daniel-mar 60
                                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
                        }
62
 
63
                        if (OIDplus::authUtils()->isAdminLoggedIn()) {
64
                                $ra_was_logged_in = OIDplus::authUtils()->isRaLoggedIn($old_email);
65
 
66
                                $ra = new OIDplusRA($old_email);
67
 
68
                                // Change RA email
69
                                $ra->change_email($new_email);
70
                                OIDplus::logger()->log("[WARN]RA($old_email)!+[INFO]RA($new_email)!+[OK]A!", "Admin changed email address '$old_email' to '$new_email'");
71
 
72
                                // Change objects
73
                                $res = OIDplus::db()->query("select id from ###objects where ra_email = ?", array($old_email));
74
                                while ($row = $res->fetch_array()) {
75
                                        OIDplus::logger()->log("[INFO]OID(".$row['id'].")+SUPOID(".$row['id'].")", "Admin changed email address of RA '$old_email' (owner of ".$row['id'].") to '$new_email'");
76
                                }
77
                                OIDplus::db()->query("update ###objects set ra_email = ? where ra_email = ?", array($new_email, $old_email));
978 daniel-mar 78
                                OIDplusObject::resetObjectInformationCache();
635 daniel-mar 79
 
80
                                // Re-login
81
                                if ($ra_was_logged_in) {
82
                                        OIDplus::authUtils()->raLogout($old_email);
83
                                        OIDplus::authUtils()->raLogin($new_email);
84
                                }
85
 
86
                                return array("status" => 0);
87
                        } else {
88
                                OIDplus::logger()->log("[INFO]RA($old_email)!+RA($new_email)!", "Requested email address change from '$old_email' to '$new_email'");
89
 
90
                                $timestamp = time();
801 daniel-mar 91
                                $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));
635 daniel-mar 92
 
93
                                $message = file_get_contents(__DIR__ . '/change_request_email.tpl');
801 daniel-mar 94
                                $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL), $message);
635 daniel-mar 95
                                $message = str_replace('{{SYSTEM_TITLE}}', OIDplus::config()->getValue('system_title'), $message);
96
                                $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
97
                                $message = str_replace('{{OLD_EMAIL}}', $old_email, $message);
98
                                $message = str_replace('{{NEW_EMAIL}}', $new_email, $message);
99
                                $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message);
100
                                OIDplus::mailUtils()->sendMail($new_email, OIDplus::config()->getValue('system_title').' - Change email request', $message);
101
 
102
                                return array("status" => 0);
103
                        }
104
                }
105
 
106
                else if ($actionID == 'activate_new_ra_email') {
107
                        if (!OIDplus::config()->getValue('allow_ra_email_change')) {
108
                                throw new OIDplusException(_L('This functionality has been disabled by the administrator.'));
109
                        }
110
 
111
                        _CheckParamExists($params, 'old_email');
112
                        _CheckParamExists($params, 'new_email');
113
                        _CheckParamExists($params, 'password');
114
                        _CheckParamExists($params, 'auth');
115
                        _CheckParamExists($params, 'timestamp');
116
 
117
                        $old_email = $params['old_email'];
118
                        $new_email = $params['new_email'];
119
                        $password = $params['password'];
120
 
121
                        $auth = $params['auth'];
122
                        $timestamp = $params['timestamp'];
123
 
124
                        $ra_was_logged_in = OIDplus::authUtils()->isRaLoggedIn($old_email);
125
 
126
                        $ra = new OIDplusRA($old_email);
127
                        if ($ra->isPasswordLess() && !OIDplus::authUtils()->isAdminLoggedIn()) {
128
                                throw new OIDplusException(_L('E-Mail-Address cannot be changed because this user does not have a password'));
129
                        }
130
 
131
                        if (!OIDplus::authUtils()->validateAuthKey('activate_new_ra_email;'.$old_email.';'.$new_email.';'.$timestamp, $auth)) {
132
                                throw new OIDplusException(_L('Invalid auth key'));
133
                        }
134
 
135
                        if ((OIDplus::config()->getValue('max_ra_email_change_time') > 0) && (time()-$timestamp > OIDplus::config()->maxEmailChangeTime())) {
136
                                throw new OIDplusException(_L('Activation link expired!'));
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);
158
                        OIDplus::logger()->log("[OK]RA($new_email)!+RA($old_email)!", "RA '$old_email' has changed their email address to '$new_email'");
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()) {
163
                                OIDplus::logger()->log("[INFO]OID(".$row['id'].")+SUPOID(".$row['id'].")", "RA '$old_email' (owner of ".$row['id'].") has changed their email address to '$new_email'");
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 {
185
                        throw new OIDplusException(_L('Unknown action ID'));
186
                }
187
        }
188
 
189
        public function init($html=true) {
190
                OIDplus::config()->prepareConfigKey('max_ra_email_change_time', 'Max RA email change time in seconds (0 = infinite)', '0', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
191
                        if (!is_numeric($value) || ($value < 0)) {
192
                                throw new OIDplusException(_L('Please enter a valid value.'));
193
                        }
194
                });
195
                OIDplus::config()->prepareConfigKey('allow_ra_email_change', 'Allow that RAs change their email address (0/1)', '1', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
196
                        if (($value != '0') && ($value != '1')) {
197
                                throw new OIDplusException(_L('Please enter a valid value (0=no, 1=yes).'));
198
                        }
199
                });
200
        }
201
 
202
        public function gui($id, &$out, &$handled) {
203
                if (explode('$',$id)[0] == 'oidplus:change_ra_email') {
204
                        $handled = true;
205
 
206
                        $ra_email = explode('$',$id)[1];
207
 
208
                        $out['title'] = _L('Change RA email');
801 daniel-mar 209
                        $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
635 daniel-mar 210
 
211
                        if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) {
800 daniel-mar 212
                                $out['icon'] = 'img/error.png';
635 daniel-mar 213
                                $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$ra$'.$ra_email),'<b>'.htmlentities($ra_email).'</b>').'</p>';
214
                                return;
215
                        }
216
 
217
                        $res = OIDplus::db()->query("select * from ###ra where email = ?", array($ra_email));
790 daniel-mar 218
                        if (!$res->any()) {
800 daniel-mar 219
                                $out['icon'] = 'img/error.png';
635 daniel-mar 220
                                $out['text'] = _L('RA "%1" does not exist','<b>'.htmlentities($ra_email).'</b>');
221
                                return;
222
                        }
223
 
224
                        if (!OIDplus::config()->getValue('allow_ra_email_change') && !OIDplus::authUtils()->isAdminLoggedIn()) {
800 daniel-mar 225
                                $out['icon'] = 'img/error.png';
635 daniel-mar 226
                                $out['text'] = '<p>'._L('This functionality has been disabled by the administrator.').'</p>';
227
                                return;
228
                        }
229
 
230
                        if (OIDplus::authUtils()->isAdminLoggedIn()) {
231
                                $ra = new OIDplusRA($ra_email);
232
                                if ($ra->isPasswordLess()) {
233
                                        $out['text'] .= '<p>'._L('Attention: This user does not have a password because they log in using LDAP or Google OAuth etc.').'</p>';
234
                                        $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>';
235
                                        $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>';
236
                                }
237
 
238
                                $out['text'] .= '<form id="changeRaEmailForm" action="javascript:void(0);" action="javascript:void(0);" onsubmit="return OIDplusPageRaChangeEMail.changeRaEmailFormOnSubmit(true);">';
239
                                $out['text'] .= '<input type="hidden" id="old_email" value="'.htmlentities($ra_email).'"/><br>';
240
                                $out['text'] .= '<div><label class="padding_label">'._L('Old address').':</label><b>'.htmlentities($ra_email).'</b></div>';
241
                                $out['text'] .= '<div><label class="padding_label">'._L('New address').':</label><input type="text" id="new_email" value=""/></div>';
242
                                $out['text'] .= '<br><input type="submit" value="'._L('Change password').'"> '._L('(admin does not require email verification)').'</form>';
243
                        } else {
244
                                $ra = new OIDplusRA($ra_email);
245
                                if ($ra->isPasswordLess()) {
800 daniel-mar 246
                                        $out['icon'] = 'img/error.png';
635 daniel-mar 247
                                        $out['text'] .= '<p>'._L('Attention: You are logged in without password (via LDAP or Google OAuth etc.).').'</p>';
248
                                        $out['text'] .= '<p>'._L('Therefore, you cannot change your email address, otherwise you would love access to your account!').'</p>';
249
                                        $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>';
250
                                        return;
251
                                }
252
 
253
                                $out['text'] .= '<form id="changeRaEmailForm" action="javascript:void(0);" action="javascript:void(0);" onsubmit="return OIDplusPageRaChangeEMail.changeRaEmailFormOnSubmit(false);">';
254
                                $out['text'] .= '<input type="hidden" id="old_email" value="'.htmlentities($ra_email).'"/><br>';
255
                                $out['text'] .= '<div><label class="padding_label">'._L('Old address').':</label><b>'.htmlentities($ra_email).'</b></div>';
256
                                $out['text'] .= '<div><label class="padding_label">'._L('New address').':</label><input type="text" id="new_email" value=""/></div>';
257
                                $out['text'] .= '<br><input type="submit" value="'._L('Send new activation email').'"></form>';
258
                        }
259
                } else if (explode('$',$id)[0] == 'oidplus:activate_new_ra_email') {
260
                        $handled = true;
261
 
262
                        $old_email = explode('$',$id)[1];
263
                        $new_email = explode('$',$id)[2];
264
                        $timestamp = explode('$',$id)[3];
265
                        $auth = explode('$',$id)[4];
266
 
267
                        $out['title'] = _L('Perform email address change');
801 daniel-mar 268
                        $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
635 daniel-mar 269
 
270
                        if (!OIDplus::config()->getValue('allow_ra_email_change') && !OIDplus::authUtils()->isAdminLoggedIn()) {
800 daniel-mar 271
                                $out['icon'] = 'img/error.png';
635 daniel-mar 272
                                $out['text'] = '<p>'._L('This functionality has been disabled by the administrator.').'</p>';
273
                                return;
274
                        }
275
 
276
                        $ra = new OIDplusRA($old_email);
277
                        if ($ra->isPasswordLess() && !OIDplus::authUtils()->isAdminLoggedIn()) {
800 daniel-mar 278
                                $out['icon'] = 'img/error.png';
635 daniel-mar 279
                                $out['text'] = '<p>'._L('E-Mail-Address cannot be changed because this user does not have a password').'</p>';
280
                                return;
281
                        }
282
 
283
                        $res = OIDplus::db()->query("select * from ###ra where email = ?", array($old_email));
790 daniel-mar 284
                        if (!$res->any()) {
800 daniel-mar 285
                                $out['icon'] = 'img/error.png';
635 daniel-mar 286
                                $out['text'] = _L('eMail address does not exist anymore. It was probably already changed.');
287
                        } else {
288
                                $res = OIDplus::db()->query("select * from ###ra where email = ?", array($new_email));
790 daniel-mar 289
                                if ($res->any()) {
800 daniel-mar 290
                                        $out['icon'] = 'img/error.png';
635 daniel-mar 291
                                        $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.');
292
                                } else {
293
                                        if (!OIDplus::authUtils()->validateAuthKey('activate_new_ra_email;'.$old_email.';'.$new_email.';'.$timestamp, $auth)) {
800 daniel-mar 294
                                                $out['icon'] = 'img/error.png';
635 daniel-mar 295
                                                $out['text'] = _L('Invalid authorization. Is the URL OK?');
296
                                        } else {
297
                                                $out['text'] = '<p>'._L('Old eMail-Address').': <b>'.$old_email.'</b></p>
298
                                                <p>'._L('New eMail-Address').': <b>'.$new_email.'</b></p>
299
 
300
                                                 <form id="activateNewRaEmailForm" action="javascript:void(0);" onsubmit="return OIDplusPageRaChangeEMail.activateNewRaEmailFormOnSubmit();">
301
                                            <input type="hidden" id="old_email" value="'.htmlentities($old_email).'"/>
302
                                            <input type="hidden" id="new_email" value="'.htmlentities($new_email).'"/>
303
                                            <input type="hidden" id="timestamp" value="'.htmlentities($timestamp).'"/>
304
                                            <input type="hidden" id="auth" value="'.htmlentities($auth).'"/>
305
 
306
                                            <div><label class="padding_label">'._L('Please verify your password').':</label><input type="password" id="password" value=""/></div>
307
                                            <br><input type="submit" value="'._L('Change email address').'">
308
                                          </form>';
309
                                        }
310
                                }
311
                        }
312
                }
313
        }
314
 
315
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
316
                if (!$ra_email) return false;
317
                if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) return false;
318
 
800 daniel-mar 319
                if (file_exists(__DIR__.'/img/main_icon16.png')) {
801 daniel-mar 320
                        $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
635 daniel-mar 321
                } else {
322
                        $tree_icon = null; // default icon (folder)
323
                }
324
 
325
                $json[] = array(
326
                        'id' => 'oidplus:change_ra_email$'.$ra_email,
327
                        'icon' => $tree_icon,
328
                        'text' => _L('Change email address')
329
                );
330
 
331
                return true;
332
        }
333
 
334
        public function tree_search($request) {
335
                return false;
336
        }
337
}