Subversion Repositories oidplus

Rev

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

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