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 OIDplusPagePublicForgotPassword extends OIDplusPagePluginPublic {
23
 
24
        public function action($actionID, $params) {
25
                if ($actionID == 'forgot_password') {
26
                        _CheckParamExists($params, 'email');
27
                        $email = $params['email'];
28
 
29
                        if (!OIDplus::mailUtils()->validMailAddress($email)) {
30
                                throw new OIDplusException(_L('Invalid email address'));
31
                        }
32
 
702 daniel-mar 33
                        OIDplus::getActiveCaptchaPlugin()->captchaVerify($params, 'captcha');
635 daniel-mar 34
 
35
                        OIDplus::logger()->log("[WARN]RA($email)!", "A new password for '$email' was requested (forgot password)");
36
 
37
                        $timestamp = time();
38
                        $activate_url = OIDplus::webpath(null,false) . '?goto='.urlencode('oidplus:reset_password$'.$email.'$'.$timestamp.'$'.OIDplus::authUtils()->makeAuthKey('reset_password;'.$email.';'.$timestamp));
39
 
40
                        $message = $this->getForgotPasswordText($params['email']);
41
                        $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message);
42
 
43
                        OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title').' - Password reset request', $message, OIDplus::config()->getValue('global_cc'));
44
 
45
                        return array("status" => 0);
46
 
47
                } else if ($actionID == 'reset_password') {
702 daniel-mar 48
 
635 daniel-mar 49
                        _CheckParamExists($params, 'password1');
50
                        _CheckParamExists($params, 'password2');
51
                        _CheckParamExists($params, 'email');
52
                        _CheckParamExists($params, 'auth');
53
                        _CheckParamExists($params, 'timestamp');
54
 
55
                        $password1 = $params['password1'];
56
                        $password2 = $params['password2'];
57
                        $email = $params['email'];
58
                        $auth = $params['auth'];
59
                        $timestamp = $params['timestamp'];
60
 
61
                        if (!OIDplus::authUtils()->validateAuthKey('reset_password;'.$email.';'.$timestamp, $auth)) {
62
                                throw new OIDplusException(_L('Invalid auth key'));
63
                        }
64
 
65
                        if ((OIDplus::config()->getValue('max_ra_pwd_reset_time') > 0) && (time()-$timestamp > OIDplus::config()->getValue('max_ra_pwd_reset_time'))) {
66
                                throw new OIDplusException(_L('Invitation expired!'));
67
                        }
68
 
69
                        if ($password1 !== $password2) {
70
                                throw new OIDplusException(_L('Passwords do not match'));
71
                        }
72
 
73
                        if (strlen($password1) < OIDplus::config()->getValue('ra_min_password_length')) {
74
                                $minlen = OIDplus::config()->getValue('ra_min_password_length');
75
                                throw new OIDplusException(_L('Password is too short. Need at least %1 characters',$minlen));
76
                        }
77
 
78
                        OIDplus::logger()->log("[INFO]RA($email)!", "RA '$email' has reset his password (forgot passwort)");
79
 
80
                        $ra = new OIDplusRA($email);
81
                        $ra->change_password($password1);
82
 
83
                        return array("status" => 0);
84
                } else {
85
                        throw new OIDplusException(_L('Unknown action ID'));
86
                }
87
        }
88
 
89
        public function init($html=true) {
90
                OIDplus::config()->prepareConfigKey('max_ra_pwd_reset_time', 'Max RA password reset time in seconds (0 = infinite)', '0', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
91
                        if (!is_numeric($value) || ($value < 0)) {
92
                                throw new OIDplusException(_L('Please enter a valid value.'));
93
                        }
94
                });
95
        }
96
 
97
        public function gui($id, &$out, &$handled) {
98
                if (explode('$',$id)[0] == 'oidplus:forgot_password') {
99
                        $handled = true;
100
 
101
                        $out['title'] = _L('Forgot password');
800 daniel-mar 102
                        $out['icon'] = OIDplus::webpath(__DIR__,true).'img/forgot_password_icon.png';
635 daniel-mar 103
 
104
                        try {
105
                                $out['text'] .= '<p>'._L('Please enter the email address of your account, and information about the password reset will be sent to you.').'</p>
106
                                  <form id="forgotPasswordForm" action="javascript:void(0);" onsubmit="return OIDplusPagePublicForgotPassword.forgotPasswordFormOnSubmit();">
702 daniel-mar 107
                                    '._L('E-Mail').': <input type="text" id="email" value=""/><br><br>
108
                                    '.OIDplus::getActiveCaptchaPlugin()->captchaGenerate().'
109
                                    <br>
635 daniel-mar 110
                                    <input type="submit" value="'._L('Send recovery information').'">
111
                                  </form>';
112
 
113
                        } catch (Exception $e) {
114
 
800 daniel-mar 115
                                $out['icon'] = 'img/error.png';
635 daniel-mar 116
                                $out['text'] = '<p>'._L('Error: %1',htmlentities($e->getMessage())).'</p>';
117
 
118
                        }
119
                } else if (explode('$',$id)[0] == 'oidplus:reset_password') {
120
                        $handled = true;
121
 
122
                        $email = explode('$',$id)[1];
123
                        $timestamp = explode('$',$id)[2];
124
                        $auth = explode('$',$id)[3];
125
 
126
                        $out['title'] = _L('Reset password');
800 daniel-mar 127
                        $out['icon'] = OIDplus::webpath(__DIR__,true).'img/reset_password_icon.png';
635 daniel-mar 128
 
129
                        if (!OIDplus::authUtils()->validateAuthKey('reset_password;'.$email.';'.$timestamp, $auth)) {
800 daniel-mar 130
                                $out['icon'] = 'img/error.png';
635 daniel-mar 131
                                $out['text'] = _L('Invalid authorization. Is the URL OK?');
132
                        } else {
133
                                $out['text'] = '<p>'._L('E-Mail-Address: %1','<b>'.$email.'</b>').'</p>
134
 
135
                                  <form id="resetPasswordForm" action="javascript:void(0);" onsubmit="return OIDplusPagePublicForgotPassword.resetPasswordFormOnSubmit();">
136
                                    <input type="hidden" id="email" value="'.htmlentities($email).'"/>
137
                                    <input type="hidden" id="timestamp" value="'.htmlentities($timestamp).'"/>
138
                                    <input type="hidden" id="auth" value="'.htmlentities($auth).'"/>
139
                                    <div><label class="padding_label">'._L('New password').':</label><input type="password" id="password1" value=""/></div>
140
                                    <div><label class="padding_label">'._L('Repeat').':</label><input type="password" id="password2" value=""/></div>
141
                                    <br><input type="submit" value="'._L('Change password').'">
142
                                  </form>';
143
                        }
144
                }
145
        }
146
 
147
        public function publicSitemap(&$out) {
148
                $out[] = 'oidplus:forgot_password';
149
        }
150
 
151
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
152
                return false;
153
        }
154
 
155
        private function getForgotPasswordText($email) {
156
                $res = OIDplus::db()->query("select * from ###ra where email = ?", array($email));
790 daniel-mar 157
                if (!$res->any()) {
635 daniel-mar 158
                        throw new OIDplusException(_L('This RA does not exist.'));
159
                }
160
 
161
                $message = file_get_contents(__DIR__ . '/forgot_password.tpl');
162
 
163
                // Resolve stuff
164
                $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,false), $message);
165
                $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
166
 
167
                // {{ACTIVATE_URL}} will be resolved in ajax.php
168
 
169
                return $message;
170
        }
171
 
172
        public function tree_search($request) {
173
                return false;
174
        }
175
}