Subversion Repositories oidplus

Rev

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