Subversion Repositories oidplus

Rev

Rev 496 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2021 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. if (!defined('INSIDE_OIDPLUS')) die();
  21.  
  22. class OIDplusPagePublicForgotPassword extends OIDplusPagePluginPublic {
  23.  
  24.         public function action($actionID, $params) {
  25.                 if ($actionID == 'forgot_password') {
  26.                         $email = $params['email'];
  27.  
  28.                         if (!OIDplus::mailUtils()->validMailAddress($email)) {
  29.                                 throw new OIDplusException(_L('Invalid email address'));
  30.                         }
  31.  
  32.                         if (OIDplus::baseConfig()->getValue('RECAPTCHA_ENABLED', false)) {
  33.                                 $secret=OIDplus::baseConfig()->getValue('RECAPTCHA_PRIVATE', '');
  34.                                 $response=$params["captcha"];
  35.                                 $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
  36.                                 $captcha_success=json_decode($verify);
  37.                                 if ($captcha_success->success==false) {
  38.                                         throw new OIDplusException(_L('CAPTCHA not successfully verified'));
  39.                                 }
  40.                         }
  41.  
  42.                         OIDplus::logger()->log("[WARN]RA($email)!", "A new password for '$email' was requested (forgot password)");
  43.  
  44.                         $timestamp = time();
  45.                         $activate_url = OIDplus::webpath(null,false) . '?goto='.urlencode('oidplus:reset_password$'.$email.'$'.$timestamp.'$'.OIDplus::authUtils()::makeAuthKey('reset_password;'.$email.';'.$timestamp));
  46.  
  47.                         $message = $this->getForgotPasswordText($params['email']);
  48.                         $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message);
  49.  
  50.                         OIDplus::mailUtils()->sendMail($email, OIDplus::config()->getValue('system_title').' - Password reset request', $message, OIDplus::config()->getValue('global_cc'));
  51.  
  52.                         return array("status" => 0);
  53.  
  54.                 } else if ($actionID == 'reset_password') {
  55.  
  56.                         $password1 = $params['password1'];
  57.                         $password2 = $params['password2'];
  58.                         $email = $params['email'];
  59.                         $auth = $params['auth'];
  60.                         $timestamp = $params['timestamp'];
  61.  
  62.                         if (!OIDplus::authUtils()::validateAuthKey('reset_password;'.$email.';'.$timestamp, $auth)) {
  63.                                 throw new OIDplusException(_L('Invalid auth key'));
  64.                         }
  65.  
  66.                         if ((OIDplus::config()->getValue('max_ra_pwd_reset_time') > 0) && (time()-$timestamp > OIDplus::config()->getValue('max_ra_pwd_reset_time'))) {
  67.                                 throw new OIDplusException(_L('Invitation expired!'));
  68.                         }
  69.  
  70.                         if ($password1 !== $password2) {
  71.                                 throw new OIDplusException(_L('Passwords do not match'));
  72.                         }
  73.  
  74.                         if (strlen($password1) < OIDplus::config()->getValue('ra_min_password_length')) {
  75.                                 $minlen = OIDplus::config()->getValue('ra_min_password_length');
  76.                                 throw new OIDplusException(_L('Password is too short. Need at least %1 characters',$minlen));
  77.                         }
  78.  
  79.                         OIDplus::logger()->log("[INFO]RA($email)!", "RA '$email' has reset his password (forgot passwort)");
  80.  
  81.                         $ra = new OIDplusRA($email);
  82.                         $ra->change_password($password1);
  83.  
  84.                         return array("status" => 0);
  85.                 } else {
  86.                         throw new OIDplusException(_L('Unknown action ID'));
  87.                 }
  88.         }
  89.  
  90.         public function init($html=true) {
  91.                 OIDplus::config()->prepareConfigKey('max_ra_pwd_reset_time', 'Max RA password reset time in seconds (0 = infinite)', '0', OIDplusConfig::PROTECTION_EDITABLE, function($value) {
  92.                         if (!is_numeric($value) || ($value < 0)) {
  93.                                 throw new OIDplusException(_L('Please enter a valid value.'));
  94.                         }
  95.                 });
  96.         }
  97.  
  98.         public function gui($id, &$out, &$handled) {
  99.                 if (explode('$',$id)[0] == 'oidplus:forgot_password') {
  100.                         $handled = true;
  101.  
  102.                         $out['title'] = _L('Forgot password');
  103.                         $out['icon'] = OIDplus::webpath(__DIR__).'forgot_password_big.png';
  104.  
  105.                         try {
  106.                                 $out['text'] .= '<p>'._L('Please enter the email address of your account, and information about the password reset will be sent to you.').'</p>
  107.                                   <form id="forgotPasswordForm" action="javascript:void(0);" onsubmit="return forgotPasswordFormOnSubmit();">
  108.                                     '._L('E-Mail').': <input type="text" id="email" value=""/><br><br>'.
  109.                                  (OIDplus::baseConfig()->getValue('RECAPTCHA_ENABLED', false) ?
  110.                                  '<script> grecaptcha.render(document.getElementById("g-recaptcha"), { "sitekey" : "'.OIDplus::baseConfig()->getValue('RECAPTCHA_PUBLIC', '').'" }); </script>'.
  111.                                  '<div id="g-recaptcha" class="g-recaptcha" data-sitekey="'.OIDplus::baseConfig()->getValue('RECAPTCHA_PUBLIC', '').'"></div>' : '').
  112.                                 ' <br>
  113.                                     <input type="submit" value="'._L('Send recovery information').'">
  114.                                   </form>';
  115.  
  116.                         } catch (Exception $e) {
  117.  
  118.                                 $out['icon'] = 'img/error_big.png';
  119.                                 $out['text'] = '<p>'._L('Error: %1',htmlentities($e->getMessage())).'</p>';
  120.  
  121.                         }
  122.                 } else if (explode('$',$id)[0] == 'oidplus:reset_password') {
  123.                         $handled = true;
  124.  
  125.                         $email = explode('$',$id)[1];
  126.                         $timestamp = explode('$',$id)[2];
  127.                         $auth = explode('$',$id)[3];
  128.  
  129.                         $out['title'] = _L('Reset password');
  130.                         $out['icon'] = OIDplus::webpath(__DIR__).'reset_password_big.png';
  131.  
  132.                         if (!OIDplus::authUtils()::validateAuthKey('reset_password;'.$email.';'.$timestamp, $auth)) {
  133.                                 $out['icon'] = 'img/error_big.png';
  134.                                 $out['text'] = _L('Invalid authorization. Is the URL OK?');
  135.                         } else {
  136.                                 $out['text'] = '<p>'._L('E-Mail-Address: %1','<b>'.$email.'</b>').'</p>
  137.  
  138.                                   <form id="resetPasswordForm" action="javascript:void(0);" onsubmit="return resetPasswordFormOnSubmit();">
  139.                                     <input type="hidden" id="email" value="'.htmlentities($email).'"/>
  140.                                     <input type="hidden" id="timestamp" value="'.htmlentities($timestamp).'"/>
  141.                                     <input type="hidden" id="auth" value="'.htmlentities($auth).'"/>
  142.                                     <div><label class="padding_label">'._L('New password').':</label><input type="password" id="password1" value=""/></div>
  143.                                     <div><label class="padding_label">'._L('Repeat').':</label><input type="password" id="password2" value=""/></div>
  144.                                     <br><input type="submit" value="'._L('Change password').'">
  145.                                   </form>';
  146.                         }
  147.                 }
  148.         }
  149.  
  150.         public function publicSitemap(&$out) {
  151.                 $out[] = 'oidplus:forgot_password';
  152.         }
  153.  
  154.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  155.                 return false;
  156.         }
  157.  
  158.         private function getForgotPasswordText($email) {
  159.                 $res = OIDplus::db()->query("select * from ###ra where email = ?", array($email));
  160.                 if ($res->num_rows() == 0) {
  161.                         throw new OIDplusException(_L('This RA does not exist.'));
  162.                 }
  163.  
  164.                 $message = file_get_contents(__DIR__ . '/forgot_password.tpl');
  165.  
  166.                 // Resolve stuff
  167.                 $message = str_replace('{{SYSTEM_URL}}', OIDplus::webpath(null,false), $message);
  168.                 $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
  169.  
  170.                 // {{ACTIVATE_URL}} will be resolved in ajax.php
  171.  
  172.                 return $message;
  173.         }
  174.  
  175.         public function tree_search($request) {
  176.                 return false;
  177.         }
  178. }