Subversion Repositories oidplus

Rev

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

Rev Author Line No. Line
635 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
1086 daniel-mar 5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
635 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
 
1050 daniel-mar 20
namespace ViaThinkSoft\OIDplus;
635 daniel-mar 21
 
1086 daniel-mar 22
// phpcs:disable PSR1.Files.SideEffects
23
\defined('INSIDE_OIDPLUS') or die;
24
// phpcs:enable PSR1.Files.SideEffects
25
 
635 daniel-mar 26
class OIDplusPagePublicForgotPassword extends OIDplusPagePluginPublic {
27
 
1116 daniel-mar 28
        /**
29
         * @param string $actionID
30
         * @param array $params
1143 daniel-mar 31
         * @return array
1116 daniel-mar 32
         * @throws OIDplusException
33
         * @throws OIDplusMailException
34
         */
35
        public function action(string $actionID, array $params): array {
635 daniel-mar 36
                if ($actionID == 'forgot_password') {
37
                        _CheckParamExists($params, 'email');
38
                        $email = $params['email'];
39
 
40
                        if (!OIDplus::mailUtils()->validMailAddress($email)) {
41
                                throw new OIDplusException(_L('Invalid email address'));
42
                        }
43
 
702 daniel-mar 44
                        OIDplus::getActiveCaptchaPlugin()->captchaVerify($params, 'captcha');
635 daniel-mar 45
 
1267 daniel-mar 46
                        OIDplus::logger()->log("V2:[WARN]RA(%1)", "A new password for '%1' was requested (forgot password)", $email);
635 daniel-mar 47
 
48
                        $timestamp = time();
1282 daniel-mar 49
                        $activate_url = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL) . '?goto='.urlencode('oidplus:reset_password$'.$email.'$'.$timestamp.'$'.OIDplus::authUtils()->makeAuthKey('93a16dbe-f4fb-11ed-b67e-3c4a92df8582:'.$email.'/'.$timestamp));
635 daniel-mar 50
 
51
                        $message = $this->getForgotPasswordText($params['email']);
52
                        $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message);
53
 
875 daniel-mar 54
                        OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title').' - Password reset request', $message);
635 daniel-mar 55
 
56
                        return array("status" => 0);
57
 
58
                } else if ($actionID == 'reset_password') {
702 daniel-mar 59
 
635 daniel-mar 60
                        _CheckParamExists($params, 'password1');
61
                        _CheckParamExists($params, 'password2');
62
                        _CheckParamExists($params, 'email');
63
                        _CheckParamExists($params, 'auth');
64
                        _CheckParamExists($params, 'timestamp');
65
 
66
                        $password1 = $params['password1'];
67
                        $password2 = $params['password2'];
68
                        $email = $params['email'];
69
                        $auth = $params['auth'];
70
                        $timestamp = $params['timestamp'];
71
 
1282 daniel-mar 72
                        if (!OIDplus::authUtils()->validateAuthKey('93a16dbe-f4fb-11ed-b67e-3c4a92df8582:'.$email.'/'.$timestamp, $auth)) {
635 daniel-mar 73
                                throw new OIDplusException(_L('Invalid auth key'));
74
                        }
75
 
76
                        if ((OIDplus::config()->getValue('max_ra_pwd_reset_time') > 0) && (time()-$timestamp > OIDplus::config()->getValue('max_ra_pwd_reset_time'))) {
77
                                throw new OIDplusException(_L('Invitation expired!'));
78
                        }
79
 
80
                        if ($password1 !== $password2) {
81
                                throw new OIDplusException(_L('Passwords do not match'));
82
                        }
83
 
84
                        if (strlen($password1) < OIDplus::config()->getValue('ra_min_password_length')) {
85
                                $minlen = OIDplus::config()->getValue('ra_min_password_length');
86
                                throw new OIDplusException(_L('Password is too short. Need at least %1 characters',$minlen));
87
                        }
88
 
1267 daniel-mar 89
                        OIDplus::logger()->log("V2:[INFO]RA(%1)", "RA '%1' has reset his password (forgot passwort)", $email);
635 daniel-mar 90
 
91
                        $ra = new OIDplusRA($email);
92
                        $ra->change_password($password1);
93
 
94
                        return array("status" => 0);
95
                } else {
1116 daniel-mar 96
                        return parent::action($actionID, $params);
635 daniel-mar 97
                }
98
        }
99
 
1116 daniel-mar 100
        /**
101
         * @param bool $html
102
         * @return void
103
         * @throws OIDplusException
104
         */
105
        public function init(bool $html=true) {
635 daniel-mar 106
                OIDplus::config()->prepareConfigKey('max_ra_pwd_reset_time', 'Max RA password reset time in seconds (0 = infinite)', '0', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
107
                        if (!is_numeric($value) || ($value < 0)) {
108
                                throw new OIDplusException(_L('Please enter a valid value.'));
109
                        }
110
                });
111
        }
112
 
1116 daniel-mar 113
        /**
114
         * @param string $id
115
         * @param array $out
116
         * @param bool $handled
117
         * @return void
118
         * @throws OIDplusException
119
         */
120
        public function gui(string $id, array &$out, bool &$handled) {
635 daniel-mar 121
                if (explode('$',$id)[0] == 'oidplus:forgot_password') {
122
                        $handled = true;
123
 
124
                        $out['title'] = _L('Forgot password');
801 daniel-mar 125
                        $out['icon'] = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/forgot_password_icon.png';
635 daniel-mar 126
 
127
                        try {
128
                                $out['text'] .= '<p>'._L('Please enter the email address of your account, and information about the password reset will be sent to you.').'</p>
129
                                  <form id="forgotPasswordForm" action="javascript:void(0);" onsubmit="return OIDplusPagePublicForgotPassword.forgotPasswordFormOnSubmit();">
702 daniel-mar 130
                                    '._L('E-Mail').': <input type="text" id="email" value=""/><br><br>
131
                                    '.OIDplus::getActiveCaptchaPlugin()->captchaGenerate().'
132
                                    <br>
635 daniel-mar 133
                                    <input type="submit" value="'._L('Send recovery information').'">
134
                                  </form>';
135
 
1050 daniel-mar 136
                        } catch (\Exception $e) {
635 daniel-mar 137
 
1201 daniel-mar 138
                                $htmlmsg = $e instanceof OIDplusException ? $e->getHtmlMessage() : htmlentities($e->getMessage());
1206 daniel-mar 139
                                throw new OIDplusHtmlException(_L('Error: %1',$htmlmsg), $out['title']);
635 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'] = _L('Reset password');
801 daniel-mar 150
                        $out['icon'] = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/reset_password_icon.png';
635 daniel-mar 151
 
152
                        if (!OIDplus::authUtils()->validateAuthKey('reset_password;'.$email.';'.$timestamp, $auth)) {
1206 daniel-mar 153
                                throw new OIDplusException(_L('Invalid authorization. Is the URL OK?'), $out['title']);
635 daniel-mar 154
                        } else {
155
                                $out['text'] = '<p>'._L('E-Mail-Address: %1','<b>'.$email.'</b>').'</p>
156
 
157
                                  <form id="resetPasswordForm" action="javascript:void(0);" onsubmit="return OIDplusPagePublicForgotPassword.resetPasswordFormOnSubmit();">
158
                                    <input type="hidden" id="email" value="'.htmlentities($email).'"/>
159
                                    <input type="hidden" id="timestamp" value="'.htmlentities($timestamp).'"/>
160
                                    <input type="hidden" id="auth" value="'.htmlentities($auth).'"/>
161
                                    <div><label class="padding_label">'._L('New password').':</label><input type="password" id="password1" value=""/></div>
162
                                    <div><label class="padding_label">'._L('Repeat').':</label><input type="password" id="password2" value=""/></div>
163
                                    <br><input type="submit" value="'._L('Change password').'">
164
                                  </form>';
165
                        }
166
                }
167
        }
168
 
1116 daniel-mar 169
        /**
170
         * @param array $out
171
         * @return void
172
         */
173
        public function publicSitemap(array &$out) {
635 daniel-mar 174
                $out[] = 'oidplus:forgot_password';
175
        }
176
 
1116 daniel-mar 177
        /**
178
         * @param array $json
179
         * @param string|null $ra_email
180
         * @param bool $nonjs
181
         * @param string $req_goto
182
         * @return bool
183
         */
184
        public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
635 daniel-mar 185
                return false;
186
        }
187
 
1116 daniel-mar 188
        /**
189
         * @param string $email
190
         * @return string
191
         * @throws OIDplusException
192
         */
193
        private function getForgotPasswordText(string $email): string {
635 daniel-mar 194
                $res = OIDplus::db()->query("select * from ###ra where email = ?", array($email));
790 daniel-mar 195
                if (!$res->any()) {
635 daniel-mar 196
                        throw new OIDplusException(_L('This RA does not exist.'));
197
                }
198
 
199
                $message = file_get_contents(__DIR__ . '/forgot_password.tpl');
200
 
201
                // Resolve stuff
1130 daniel-mar 202
                // Note: {{ACTIVATE_URL}} will be resolved in ajax.php
203
 
801 daniel-mar 204
                $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL), $message);
635 daniel-mar 205
 
1130 daniel-mar 206
                return str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
635 daniel-mar 207
        }
208
 
1116 daniel-mar 209
        /**
210
         * @param string $request
211
         * @return array|false
212
         */
213
        public function tree_search(string $request) {
635 daniel-mar 214
                return false;
215
        }
216
}