Subversion Repositories oidplus

Rev

Rev 1050 | Rev 1116 | 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.         public function action($actionID, $params) {
  29.                 if ($actionID == 'forgot_password') {
  30.                         _CheckParamExists($params, 'email');
  31.                         $email = $params['email'];
  32.  
  33.                         if (!OIDplus::mailUtils()->validMailAddress($email)) {
  34.                                 throw new OIDplusException(_L('Invalid email address'));
  35.                         }
  36.  
  37.                         OIDplus::getActiveCaptchaPlugin()->captchaVerify($params, 'captcha');
  38.  
  39.                         OIDplus::logger()->log("[WARN]RA($email)!", "A new password for '$email' was requested (forgot password)");
  40.  
  41.                         $timestamp = time();
  42.                         $activate_url = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL) . '?goto='.urlencode('oidplus:reset_password$'.$email.'$'.$timestamp.'$'.OIDplus::authUtils()->makeAuthKey('reset_password;'.$email.';'.$timestamp));
  43.  
  44.                         $message = $this->getForgotPasswordText($params['email']);
  45.                         $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message);
  46.  
  47.                         OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title').' - Password reset request', $message);
  48.  
  49.                         return array("status" => 0);
  50.  
  51.                 } else if ($actionID == 'reset_password') {
  52.  
  53.                         _CheckParamExists($params, 'password1');
  54.                         _CheckParamExists($params, 'password2');
  55.                         _CheckParamExists($params, 'email');
  56.                         _CheckParamExists($params, 'auth');
  57.                         _CheckParamExists($params, 'timestamp');
  58.  
  59.                         $password1 = $params['password1'];
  60.                         $password2 = $params['password2'];
  61.                         $email = $params['email'];
  62.                         $auth = $params['auth'];
  63.                         $timestamp = $params['timestamp'];
  64.  
  65.                         if (!OIDplus::authUtils()->validateAuthKey('reset_password;'.$email.';'.$timestamp, $auth)) {
  66.                                 throw new OIDplusException(_L('Invalid auth key'));
  67.                         }
  68.  
  69.                         if ((OIDplus::config()->getValue('max_ra_pwd_reset_time') > 0) && (time()-$timestamp > OIDplus::config()->getValue('max_ra_pwd_reset_time'))) {
  70.                                 throw new OIDplusException(_L('Invitation expired!'));
  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.  
  82.                         OIDplus::logger()->log("[INFO]RA($email)!", "RA '$email' has reset his password (forgot passwort)");
  83.  
  84.                         $ra = new OIDplusRA($email);
  85.                         $ra->change_password($password1);
  86.  
  87.                         return array("status" => 0);
  88.                 } else {
  89.                         throw new OIDplusException(_L('Unknown action ID'));
  90.                 }
  91.         }
  92.  
  93.         public function init($html=true) {
  94.                 OIDplus::config()->prepareConfigKey('max_ra_pwd_reset_time', 'Max RA password reset time in seconds (0 = infinite)', '0', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  95.                         if (!is_numeric($value) || ($value < 0)) {
  96.                                 throw new OIDplusException(_L('Please enter a valid value.'));
  97.                         }
  98.                 });
  99.         }
  100.  
  101.         public function gui($id, &$out, &$handled) {
  102.                 if (explode('$',$id)[0] == 'oidplus:forgot_password') {
  103.                         $handled = true;
  104.  
  105.                         $out['title'] = _L('Forgot password');
  106.                         $out['icon'] = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/forgot_password_icon.png';
  107.  
  108.                         try {
  109.                                 $out['text'] .= '<p>'._L('Please enter the email address of your account, and information about the password reset will be sent to you.').'</p>
  110.                                   <form id="forgotPasswordForm" action="javascript:void(0);" onsubmit="return OIDplusPagePublicForgotPassword.forgotPasswordFormOnSubmit();">
  111.                                     '._L('E-Mail').': <input type="text" id="email" value=""/><br><br>
  112.                                     '.OIDplus::getActiveCaptchaPlugin()->captchaGenerate().'
  113.                                     <br>
  114.                                     <input type="submit" value="'._L('Send recovery information').'">
  115.                                   </form>';
  116.  
  117.                         } catch (\Exception $e) {
  118.  
  119.                                 $out['icon'] = 'img/error.png';
  120.                                 $out['text'] = '<p>'._L('Error: %1',htmlentities($e->getMessage())).'</p>';
  121.  
  122.                         }
  123.                 } else if (explode('$',$id)[0] == 'oidplus:reset_password') {
  124.                         $handled = true;
  125.  
  126.                         $email = explode('$',$id)[1];
  127.                         $timestamp = explode('$',$id)[2];
  128.                         $auth = explode('$',$id)[3];
  129.  
  130.                         $out['title'] = _L('Reset password');
  131.                         $out['icon'] = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/reset_password_icon.png';
  132.  
  133.                         if (!OIDplus::authUtils()->validateAuthKey('reset_password;'.$email.';'.$timestamp, $auth)) {
  134.                                 $out['icon'] = 'img/error.png';
  135.                                 $out['text'] = _L('Invalid authorization. Is the URL OK?');
  136.                         } else {
  137.                                 $out['text'] = '<p>'._L('E-Mail-Address: %1','<b>'.$email.'</b>').'</p>
  138.  
  139.                                   <form id="resetPasswordForm" action="javascript:void(0);" onsubmit="return OIDplusPagePublicForgotPassword.resetPasswordFormOnSubmit();">
  140.                                     <input type="hidden" id="email" value="'.htmlentities($email).'"/>
  141.                                     <input type="hidden" id="timestamp" value="'.htmlentities($timestamp).'"/>
  142.                                     <input type="hidden" id="auth" value="'.htmlentities($auth).'"/>
  143.                                     <div><label class="padding_label">'._L('New password').':</label><input type="password" id="password1" value=""/></div>
  144.                                     <div><label class="padding_label">'._L('Repeat').':</label><input type="password" id="password2" value=""/></div>
  145.                                     <br><input type="submit" value="'._L('Change password').'">
  146.                                   </form>';
  147.                         }
  148.                 }
  149.         }
  150.  
  151.         public function publicSitemap(&$out) {
  152.                 $out[] = 'oidplus:forgot_password';
  153.         }
  154.  
  155.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  156.                 return false;
  157.         }
  158.  
  159.         private function getForgotPasswordText($email) {
  160.                 $res = OIDplus::db()->query("select * from ###ra where email = ?", array($email));
  161.                 if (!$res->any()) {
  162.                         throw new OIDplusException(_L('This RA does not exist.'));
  163.                 }
  164.  
  165.                 $message = file_get_contents(__DIR__ . '/forgot_password.tpl');
  166.  
  167.                 // Resolve stuff
  168.                 $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL), $message);
  169.                 $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
  170.  
  171.                 // {{ACTIVATE_URL}} will be resolved in ajax.php
  172.  
  173.                 return $message;
  174.         }
  175.  
  176.         public function tree_search($request) {
  177.                 return false;
  178.         }
  179. }
  180.