Subversion Repositories oidplus

Rev

Rev 1130 | Rev 1199 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2023 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.  
  20. namespace ViaThinkSoft\OIDplus;
  21.  
  22. // phpcs:disable PSR1.Files.SideEffects
  23. \defined('INSIDE_OIDPLUS') or die;
  24. // phpcs:enable PSR1.Files.SideEffects
  25.  
  26. class OIDplusPagePublicForgotPassword extends OIDplusPagePluginPublic {
  27.  
  28.         /**
  29.          * @param string $actionID
  30.          * @param array $params
  31.          * @return array
  32.          * @throws OIDplusException
  33.          * @throws OIDplusMailException
  34.          */
  35.         public function action(string $actionID, array $params): array {
  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.  
  44.                         OIDplus::getActiveCaptchaPlugin()->captchaVerify($params, 'captcha');
  45.  
  46.                         OIDplus::logger()->log("[WARN]RA($email)!", "A new password for '$email' was requested (forgot password)");
  47.  
  48.                         $timestamp = time();
  49.                         $activate_url = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL) . '?goto='.urlencode('oidplus:reset_password$'.$email.'$'.$timestamp.'$'.OIDplus::authUtils()->makeAuthKey('reset_password;'.$email.';'.$timestamp));
  50.  
  51.                         $message = $this->getForgotPasswordText($params['email']);
  52.                         $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message);
  53.  
  54.                         OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title').' - Password reset request', $message);
  55.  
  56.                         return array("status" => 0);
  57.  
  58.                 } else if ($actionID == 'reset_password') {
  59.  
  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.  
  72.                         if (!OIDplus::authUtils()->validateAuthKey('reset_password;'.$email.';'.$timestamp, $auth)) {
  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.  
  89.                         OIDplus::logger()->log("[INFO]RA($email)!", "RA '$email' has reset his password (forgot passwort)");
  90.  
  91.                         $ra = new OIDplusRA($email);
  92.                         $ra->change_password($password1);
  93.  
  94.                         return array("status" => 0);
  95.                 } else {
  96.                         return parent::action($actionID, $params);
  97.                 }
  98.         }
  99.  
  100.         /**
  101.          * @param bool $html
  102.          * @return void
  103.          * @throws OIDplusException
  104.          */
  105.         public function init(bool $html=true) {
  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.  
  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) {
  121.                 if (explode('$',$id)[0] == 'oidplus:forgot_password') {
  122.                         $handled = true;
  123.  
  124.                         $out['title'] = _L('Forgot password');
  125.                         $out['icon'] = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/forgot_password_icon.png';
  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();">
  130.                                     '._L('E-Mail').': <input type="text" id="email" value=""/><br><br>
  131.                                     '.OIDplus::getActiveCaptchaPlugin()->captchaGenerate().'
  132.                                     <br>
  133.                                     <input type="submit" value="'._L('Send recovery information').'">
  134.                                   </form>';
  135.  
  136.                         } catch (\Exception $e) {
  137.  
  138.                                 $out['icon'] = 'img/error.png';
  139.                                 $out['text'] = '<p>'._L('Error: %1',htmlentities($e->getMessage())).'</p>';
  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');
  150.                         $out['icon'] = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/reset_password_icon.png';
  151.  
  152.                         if (!OIDplus::authUtils()->validateAuthKey('reset_password;'.$email.';'.$timestamp, $auth)) {
  153.                                 $out['icon'] = 'img/error.png';
  154.                                 $out['text'] = _L('Invalid authorization. Is the URL OK?');
  155.                         } else {
  156.                                 $out['text'] = '<p>'._L('E-Mail-Address: %1','<b>'.$email.'</b>').'</p>
  157.  
  158.                                   <form id="resetPasswordForm" action="javascript:void(0);" onsubmit="return OIDplusPagePublicForgotPassword.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).'"/>
  162.                                     <div><label class="padding_label">'._L('New password').':</label><input type="password" id="password1" value=""/></div>
  163.                                     <div><label class="padding_label">'._L('Repeat').':</label><input type="password" id="password2" value=""/></div>
  164.                                     <br><input type="submit" value="'._L('Change password').'">
  165.                                   </form>';
  166.                         }
  167.                 }
  168.         }
  169.  
  170.         /**
  171.          * @param array $out
  172.          * @return void
  173.          */
  174.         public function publicSitemap(array &$out) {
  175.                 $out[] = 'oidplus:forgot_password';
  176.         }
  177.  
  178.         /**
  179.          * @param array $json
  180.          * @param string|null $ra_email
  181.          * @param bool $nonjs
  182.          * @param string $req_goto
  183.          * @return bool
  184.          */
  185.         public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
  186.                 return false;
  187.         }
  188.  
  189.         /**
  190.          * @param string $email
  191.          * @return string
  192.          * @throws OIDplusException
  193.          */
  194.         private function getForgotPasswordText(string $email): string {
  195.                 $res = OIDplus::db()->query("select * from ###ra where email = ?", array($email));
  196.                 if (!$res->any()) {
  197.                         throw new OIDplusException(_L('This RA does not exist.'));
  198.                 }
  199.  
  200.                 $message = file_get_contents(__DIR__ . '/forgot_password.tpl');
  201.  
  202.                 // Resolve stuff
  203.                 // Note: {{ACTIVATE_URL}} will be resolved in ajax.php
  204.  
  205.                 $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL), $message);
  206.  
  207.                 return str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
  208.         }
  209.  
  210.         /**
  211.          * @param string $request
  212.          * @return array|false
  213.          */
  214.         public function tree_search(string $request) {
  215.                 return false;
  216.         }
  217. }
  218.