Subversion Repositories oidplus

Rev

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