Subversion Repositories oidplus

Rev

Rev 288 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
104 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
5
 * Copyright 2019 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
 
256 daniel-mar 20
class OIDplusPagePublicForgotPassword extends OIDplusPagePluginPublic {
104 daniel-mar 21
 
321 daniel-mar 22
        public function action($actionID, $params) {
23
                if ($actionID == 'forgot_password') {
24
                        $email = $params['email'];
104 daniel-mar 25
 
250 daniel-mar 26
                        if (!OIDplus::mailUtils()->validMailAddress($email)) {
27
                                throw new OIDplusException('Invalid email address');
104 daniel-mar 28
                        }
29
 
261 daniel-mar 30
                        if (OIDplus::baseConfig()->getValue('RECAPTCHA_ENABLED', false)) {
31
                                $secret=OIDplus::baseConfig()->getValue('RECAPTCHA_PRIVATE', '');
321 daniel-mar 32
                                $response=$params["captcha"];
104 daniel-mar 33
                                $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
34
                                $captcha_success=json_decode($verify);
35
                                if ($captcha_success->success==false) {
250 daniel-mar 36
                                        throw new OIDplusException('Captcha wrong');
104 daniel-mar 37
                                }
38
                        }
39
 
288 daniel-mar 40
                        OIDplus::logger()->log("[WARN]RA($email)!", "A new password for '$email' was requested (forgot password)");
115 daniel-mar 41
 
104 daniel-mar 42
                        $timestamp = time();
227 daniel-mar 43
                        $activate_url = OIDplus::getSystemUrl() . '?goto='.urlencode('oidplus:reset_password$'.$email.'$'.$timestamp.'$'.OIDplus::authUtils()::makeAuthKey('reset_password;'.$email.';'.$timestamp));
104 daniel-mar 44
 
321 daniel-mar 45
                        $message = $this->getForgotPasswordText($params['email']);
104 daniel-mar 46
                        $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message);
47
 
257 daniel-mar 48
                        OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title').' - Password reset request', $message, OIDplus::config()->getValue('global_cc'));
104 daniel-mar 49
 
107 daniel-mar 50
                        echo json_encode(array("status" => 0));
104 daniel-mar 51
 
321 daniel-mar 52
                } else if ($actionID == 'reset_password') {
104 daniel-mar 53
 
321 daniel-mar 54
                        $password1 = $params['password1'];
55
                        $password2 = $params['password2'];
56
                        $email = $params['email'];
57
                        $auth = $params['auth'];
58
                        $timestamp = $params['timestamp'];
104 daniel-mar 59
 
60
                        if (!OIDplus::authUtils()::validateAuthKey('reset_password;'.$email.';'.$timestamp, $auth)) {
250 daniel-mar 61
                                throw new OIDplusException('Invalid auth key');
104 daniel-mar 62
                        }
63
 
64
                        if ((OIDplus::config()->getValue('max_ra_pwd_reset_time') > 0) && (time()-$timestamp > OIDplus::config()->getValue('max_ra_pwd_reset_time'))) {
250 daniel-mar 65
                                throw new OIDplusException('Invitation expired!');
104 daniel-mar 66
                        }
67
 
68
                        if ($password1 !== $password2) {
250 daniel-mar 69
                                throw new OIDplusException('Passwords are not equal');
104 daniel-mar 70
                        }
71
 
257 daniel-mar 72
                        if (strlen($password1) < OIDplus::config()->getValue('ra_min_password_length')) {
73
                                throw new OIDplusException('Password is too short. Minimum password length: '.OIDplus::config()->getValue('ra_min_password_length'));
104 daniel-mar 74
                        }
75
 
288 daniel-mar 76
                        OIDplus::logger()->log("[INFO]RA($email)!", "RA '$email' has reset his password (forgot passwort)");
115 daniel-mar 77
 
104 daniel-mar 78
                        $ra = new OIDplusRA($email);
79
                        $ra->change_password($password1);
80
 
107 daniel-mar 81
                        echo json_encode(array("status" => 0));
321 daniel-mar 82
                } else {
83
                        throw new OIDplusException("Unknown action ID");
104 daniel-mar 84
                }
85
        }
86
 
87
        public function init($html=true) {
263 daniel-mar 88
                OIDplus::config()->prepareConfigKey('max_ra_pwd_reset_time', 'Max RA password reset time in seconds (0 = infinite)', '0', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
104 daniel-mar 89
                        if (!is_numeric($value) || ($value < 0)) {
250 daniel-mar 90
                                throw new OIDplusException("Please enter a valid value.");
104 daniel-mar 91
                        }
263 daniel-mar 92
                });
104 daniel-mar 93
        }
94
 
95
        public function gui($id, &$out, &$handled) {
96
                if (explode('$',$id)[0] == 'oidplus:forgot_password') {
97
                        $handled = true;
98
 
99
                        $out['title'] = 'Forgot password';
241 daniel-mar 100
                        $out['icon'] = OIDplus::webpath(__DIR__).'forgot_password_big.png';
104 daniel-mar 101
 
102
                        try {
103
                                $out['text'] .= '<p>Please enter the email address of your account, and information about the password reset will be sent to you.</p>
104
                                  <form id="forgotPasswordForm" onsubmit="return forgotPasswordFormOnSubmit();">
105
                                    E-Mail: <input type="text" id="email" value=""/><br><br>'.
261 daniel-mar 106
                                 (OIDplus::baseConfig()->getValue('RECAPTCHA_ENABLED', false) ?
107
                                 '<script> grecaptcha.render(document.getElementById("g-recaptcha"), { "sitekey" : "'.OIDplus::baseConfig()->getValue('RECAPTCHA_PUBLIC', '').'" }); </script>'.
108
                                 '<div id="g-recaptcha" class="g-recaptcha" data-sitekey="'.OIDplus::baseConfig()->getValue('RECAPTCHA_PUBLIC', '').'"></div>' : '').
104 daniel-mar 109
                                ' <br>
110
                                    <input type="submit" value="Send recovery information">
111
                                  </form>';
112
 
113
                        } catch (Exception $e) {
114
 
115
                                $out['icon'] = 'img/error_big.png';
153 daniel-mar 116
                                $out['text'] = '<p>Error: '.htmlentities($e->getMessage()).'</p>';
104 daniel-mar 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'] = 'Reset password';
241 daniel-mar 127
                        $out['icon'] = OIDplus::webpath(__DIR__).'reset_password_big.png';
104 daniel-mar 128
 
129
                        if (!OIDplus::authUtils()::validateAuthKey('reset_password;'.$email.';'.$timestamp, $auth)) {
130
                                $out['icon'] = 'img/error_big.png';
131
                                $out['text'] = 'Invalid authorization. Is the URL OK?';
132
                        } else {
133
                                $out['text'] = '<p>E-Mail-Adress: <b>'.$email.'</b></p>
134
 
135
                                  <form id="resetPasswordForm" onsubmit="return 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).'"/>
152 daniel-mar 139
                                    <div><label class="padding_label">New password:</label><input type="password" id="password1" value=""/></div>
153 daniel-mar 140
                                    <div><label class="padding_label">Repeat:</label><input type="password" id="password2" value=""/></div>
152 daniel-mar 141
                                    <br><input type="submit" value="Change password">
104 daniel-mar 142
                                  </form>';
143
                        }
144
                }
145
        }
146
 
282 daniel-mar 147
        public function publicSitemap(&$out) {
283 daniel-mar 148
                $out[] = OIDplus::getSystemUrl().'?goto='.urlencode('oidplus:forgot_password');
282 daniel-mar 149
        }
150
 
106 daniel-mar 151
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
104 daniel-mar 152
                return false;
153
        }
154
 
155
        private function getForgotPasswordText($email) {
261 daniel-mar 156
                $res = OIDplus::db()->query("select * from ###ra where email = ?", array($email));
236 daniel-mar 157
                if ($res->num_rows() == 0) {
250 daniel-mar 158
                        throw new OIDplusException("This RA does not exist.");
104 daniel-mar 159
                }
160
 
108 daniel-mar 161
                $message = file_get_contents(__DIR__ . '/forgot_password.tpl');
104 daniel-mar 162
 
163
                // Resolve stuff
227 daniel-mar 164
                $message = str_replace('{{SYSTEM_URL}}', OIDplus::getSystemUrl(), $message);
104 daniel-mar 165
                $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
166
 
107 daniel-mar 167
                // {{ACTIVATE_URL}} will be resolved in ajax.php
104 daniel-mar 168
 
169
                return $message;
170
        }
108 daniel-mar 171
 
172
        public function tree_search($request) {
173
                return false;
174
        }
104 daniel-mar 175
}