Subversion Repositories oidplus

Rev

Rev 241 | 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. if (!defined('IN_OIDPLUS')) die();
  21.  
  22. class OIDplusPagePublicForgotPassword extends OIDplusPagePlugin {
  23.         public function type() {
  24.                 return 'public';
  25.         }
  26.  
  27.         public static function getPluginInformation() {
  28.                 $out = array();
  29.                 $out['name'] = 'Forgot password';
  30.                 $out['author'] = 'ViaThinkSoft';
  31.                 $out['version'] = null;
  32.                 $out['descriptionHTML'] = null;
  33.                 return $out;
  34.         }
  35.  
  36.         public function priority() {
  37.                 return 91;
  38.         }
  39.  
  40.         public function action(&$handled) {
  41.                 if (isset($_POST["action"]) && ($_POST["action"] == "forgot_password")) {
  42.                         $handled = true;
  43.  
  44.                         $email = $_POST['email'];
  45.  
  46.                         if (!OIDplus::mailUtils()->validMailAddress($email)) {
  47.                                 throw new OIDplusException('Invalid email address');
  48.                         }
  49.  
  50.                         if (RECAPTCHA_ENABLED) {
  51.                                 $secret=RECAPTCHA_PRIVATE;
  52.                                 $response=$_POST["captcha"];
  53.                                 $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}");
  54.                                 $captcha_success=json_decode($verify);
  55.                                 if ($captcha_success->success==false) {
  56.                                         throw new OIDplusException('Captcha wrong');
  57.                                 }
  58.                         }
  59.  
  60.                         OIDplus::logger()->log("RA($email)!", "A new password for '$email' was requested (forgot password)");
  61.  
  62.                         $timestamp = time();
  63.                         $activate_url = OIDplus::getSystemUrl() . '?goto='.urlencode('oidplus:reset_password$'.$email.'$'.$timestamp.'$'.OIDplus::authUtils()::makeAuthKey('reset_password;'.$email.';'.$timestamp));
  64.  
  65.                         $message = $this->getForgotPasswordText($_POST['email']);
  66.                         $message = str_replace('{{ACTIVATE_URL}}', $activate_url, $message);
  67.  
  68.                         OIDplus::mailUtils()->sendMail($email, OIDplus::config()->systemTitle().' - Password reset request', $message, OIDplus::config()->globalCC());
  69.  
  70.                         echo json_encode(array("status" => 0));
  71.                 }
  72.  
  73.                 if (isset($_POST["action"]) && ($_POST["action"] == "reset_password")) {
  74.                         $handled = true;
  75.  
  76.                         $password1 = $_POST['password1'];
  77.                         $password2 = $_POST['password2'];
  78.                         $email = $_POST['email'];
  79.                         $auth = $_POST['auth'];
  80.                         $timestamp = $_POST['timestamp'];
  81.  
  82.                         if (!OIDplus::authUtils()::validateAuthKey('reset_password;'.$email.';'.$timestamp, $auth)) {
  83.                                 throw new OIDplusException('Invalid auth key');
  84.                         }
  85.  
  86.                         if ((OIDplus::config()->getValue('max_ra_pwd_reset_time') > 0) && (time()-$timestamp > OIDplus::config()->getValue('max_ra_pwd_reset_time'))) {
  87.                                 throw new OIDplusException('Invitation expired!');
  88.                         }
  89.  
  90.                         if ($password1 !== $password2) {
  91.                                 throw new OIDplusException('Passwords are not equal');
  92.                         }
  93.  
  94.                         if (strlen($password1) < OIDplus::config()->minRaPasswordLength()) {
  95.                                 throw new OIDplusException('Password is too short. Minimum password length: '.OIDplus::config()->minRaPasswordLength());
  96.                         }
  97.  
  98.                         OIDplus::logger()->log("RA($email)!", "RA '$email' has reset his password (forgot passwort)");
  99.  
  100.                         $ra = new OIDplusRA($email);
  101.                         $ra->change_password($password1);
  102.  
  103.                         echo json_encode(array("status" => 0));
  104.                 }
  105.         }
  106.  
  107.         public function init($html=true) {
  108.                 OIDplus::config()->prepareConfigKey('max_ra_pwd_reset_time', 'Max RA password reset time in seconds (0 = infinite)', '0', 0, 1);
  109.         }
  110.  
  111.         public function cfgSetValue($name, $value) {
  112.                 if ($name == 'max_ra_pwd_reset_time') {
  113.                         if (!is_numeric($value) || ($value < 0)) {
  114.                                 throw new OIDplusException("Please enter a valid value.");
  115.                         }
  116.                 }
  117.         }
  118.  
  119.         public function gui($id, &$out, &$handled) {
  120.                 if (explode('$',$id)[0] == 'oidplus:forgot_password') {
  121.                         $handled = true;
  122.  
  123.                         $out['title'] = 'Forgot password';
  124.                         $out['icon'] = OIDplus::webpath(__DIR__).'forgot_password_big.png';
  125.  
  126.                         try {
  127.                                 $out['text'] .= '<p>Please enter the email address of your account, and information about the password reset will be sent to you.</p>
  128.                                   <form id="forgotPasswordForm" onsubmit="return forgotPasswordFormOnSubmit();">
  129.                                     E-Mail: <input type="text" id="email" value=""/><br><br>'.
  130.                                  (RECAPTCHA_ENABLED ? '<script> grecaptcha.render(document.getElementById("g-recaptcha"), { "sitekey" : "'.RECAPTCHA_PUBLIC.'" }); </script>'.
  131.                                                    '<div id="g-recaptcha" class="g-recaptcha" data-sitekey="'.RECAPTCHA_PUBLIC.'"></div>' : '').
  132.                                 ' <br>
  133.                                     <input type="submit" value="Send recovery information">
  134.                                   </form>';
  135.  
  136.                         } catch (Exception $e) {
  137.  
  138.                                 $out['icon'] = 'img/error_big.png';
  139.                                 $out['text'] = '<p>Error: '.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'] = 'Reset password';
  150.                         $out['icon'] = OIDplus::webpath(__DIR__).'reset_password_big.png';
  151.  
  152.                         if (!OIDplus::authUtils()::validateAuthKey('reset_password;'.$email.';'.$timestamp, $auth)) {
  153.                                 $out['icon'] = 'img/error_big.png';
  154.                                 $out['text'] = 'Invalid authorization. Is the URL OK?';
  155.                         } else {
  156.                                 $out['text'] = '<p>E-Mail-Adress: <b>'.$email.'</b></p>
  157.  
  158.                                   <form id="resetPasswordForm" onsubmit="return 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">New password:</label><input type="password" id="password1" value=""/></div>
  163.                                     <div><label class="padding_label">Repeat:</label><input type="password" id="password2" value=""/></div>
  164.                                     <br><input type="submit" value="Change password">
  165.                                   </form>';
  166.                         }
  167.                 }
  168.         }
  169.  
  170.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  171.                 return false;
  172.         }
  173.  
  174.         private function getForgotPasswordText($email) {
  175.                 $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."ra where email = ?", array($email));
  176.                 if ($res->num_rows() == 0) {
  177.                         throw new OIDplusException("This RA does not exist.");
  178.                 }
  179.  
  180.                 $message = file_get_contents(__DIR__ . '/forgot_password.tpl');
  181.  
  182.                 // Resolve stuff
  183.                 $message = str_replace('{{SYSTEM_URL}}', OIDplus::getSystemUrl(), $message);
  184.                 $message = str_replace('{{ADMIN_EMAIL}}', OIDplus::config()->getValue('admin_email'), $message);
  185.  
  186.                 // {{ACTIVATE_URL}} will be resolved in ajax.php
  187.  
  188.                 return $message;
  189.         }
  190.  
  191.         public function tree_search($request) {
  192.                 return false;
  193.         }
  194. }
  195.