<?php
  // Inject Configuration
  require_once(__DIR__.'/private/config.php');
  
  
  
  // Initialize arrays to hold errors
  $errors = array(
    'general'  => false,
    'email'    => false,
    'password' => false
  );
  
  
  
  // Detect action to take based on provided parameters
  if (!empty($_REQUEST['email'])) {
    $action = 'send email';
  }
  else if (!empty($_REQUEST['token'])) {
    if (empty($_REQUEST['password']) && empty($_REQUEST['password_confirmation'])) {
      $action = 'prompt for new password';
    }
    else {
      $action = 'change password';
    }
  }
  else {
    $action = 'prompt for email';
  }
  
  
  
  switch ($action) {
  case 'send email':
    $user = User::findByEmail($_REQUEST['email']);
    if (is_null($user)) {
      $errors['email'] = 'An account with that email address could not be found.';
    }
    else {
      $token = Password::createResetToken($user);
      if (!Password::mailResetToken($user->email(), $token)) {
        $errors['email'] = 'An error occurred while sending mail to this address.';
      }
    }
    break;
  
  
  case 'prompt for new password':
    $user = User::findByResetToken($_REQUEST['token']);
    if (!isset($user)) {
      $errors['general'] = 'That password reset link is invalid or expired.';
    }
    break;
  
  
  case 'change password':
    if ($_REQUEST['password'] !== $_REQUEST['password_confirmation']) {
      $errors['password'] = 'Passwords do not match.';
    }
    else if (!Password::resetFromToken($_REQUEST['token'], $_REQUEST['password'])) {
      $errors['general'] = 'An error occurred and your password was not reset. Please try again.';
    }
    break;
  }
?>





<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    
    <title>Password Reset - My Digital Hand</title>
    
    <!-- Bootstrap -->
    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
    
    <!-- CSS -->
    <link href="css/rtptf.css" rel="stylesheet">
    <link href="css/splash.css" rel="stylesheet">
    
    <!-- Font -->
    <link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
    
    <!-- Icon -->
    <link rel="shortcut icon" type="image/png" href="img/ptf-icon.png" />
    
    
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  
  
  
  
  <body>
    <!-- Main Content -->
    <div class="container-fluid">
      <div id="login">
        <!-- Title -->
        <h1>Password Reset</h1>
        
        <p></p>
        
        <!-- General Error Message -->
        <?php if ($errors['general']) { ?>
          <div class="alert alert-danger alert-dismissible">
            <button type="button" class="close" data-dismiss="alert">&times;</button>
            <strong>Error:</strong>
            <?php echo $errors['general'] ?>
          </div>
        
        
        <!-- Email Prompt -->
        <?php } else if ($action === 'prompt for email' || $errors['email']) { ?>
          <p>
            To reset your password, enter the email address associated with your account.
          </p>
          
          <form method="POST" class="form-horizontal">
            <!-- Email Address -->
            <div class="form-group <?php print($errors['email'] ? 'has-error' : '') ?>">
              <label for="email" class="control-label col-sm-3">Email</label>
              <div class="col-sm-9">
                <input type="text" class="form-control" name="email" id="email" autofocus placeholder="Email" value="<?php print($_REQUEST['email'] ? htmlspecialchars($_REQUEST['email']) : '' ) ?>">
              </div>
              
              <?php if ($errors['email']) { ?>
                <div class="col-sm-9 col-sm-offset-3">
                  <p class="text-danger">
                    <?php echo $errors['email'] ?>
                  </p>
                </div>
              <?php } ?>
            </div>
            
            
            <!-- Send Button -->
            <div class="form-group">
              <div class="col-xs-12 text-right">
                <p>
                  <button type="submit" class="btn btn-primary" name="submit" value="submit">Send</button>
                </p>
                <a href="login.php">Click here to return to the sign in page</a>
              </div>
            </div>
          </form>
        
        
        <!-- Token Sent -->
        <?php } else if ($action === 'send email') { ?>
          <div class="alert alert-info">
            An email has been sent to <?php echo $user->email() ?>.<br />
            Please check your email and follow the instructions to reset your password.
          </div>
        
        
        <!-- New Password Prompt -->
        <?php } else if ($action === 'prompt for new password' || $errors['password']) { ?>
          <form method="POST" class="form-horizontal">
            <!-- Desired Password -->
            <div class="form-group <?php print($errors['password'] ? 'has-error' : '') ?>">
              <label for="password" class="control-label col-sm-3">New Password</label>
              <div class="col-sm-9">
                <input type="password" class="form-control" name="password" id="password" autofocus placeholder="Desired password">
              </div>
            </div>
            
            
            <!-- Desired Password Confirmation -->
            <div class="form-group <?php print($errors['password'] ? 'has-error' : '') ?>">
              <label for="password_confirmation" class="control-label col-sm-3">Verify Password</label>
              <div class="col-sm-9">
                <input type="password" class="form-control" name="password_confirmation" id="password_confirmation" placeholder="Retype desired password">
              </div>
              
              <?php if ($errors['password']) { ?>
                <div class="col-sm-9 col-sm-offset-3">
                  <p class="text-danger">
                    <?php echo $errors['password'] ?>
                  </p>
                </div>
              <?php } ?>
            </div>
            
            
            <!-- Submit Button -->
            <div class="form-group">
              <div class="col-xs-12 text-right">
                <p>
                  <input type="hidden" name="token" value="<?php echo $_REQUEST['token'] ?>" />
                  <button type="submit" class="btn btn-primary" name="submit" value="submit">Submit</button>
                </p>
                <a href="login.php">Click here to return to the sign in page</a>
              </div>
            </div>
          </form>
        
        
        <!-- Successful Password Change -->
        <?php } else if ($action === 'change password') { ?>
          <div class="alert alert-info">
            Your password has been successfully changed.
            <a href="login.php">Click here to sign in</a>
          </div>
        <?php } ?>
      </div>
    </div>
    
    
    
    
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="js/jquery-1.11.3.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="bootstrap/js/bootstrap.min.js"></script>
  </body>
</html>