Subversion Repositories oidplus

Rev

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