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