Subversion Repositories oidplus

Rev

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