Subversion Repositories oidplus

Rev

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