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