<?php
require_once('user.php');
require_once('common.php');

if ( $session->loggedIn() ) {
  redirect_to("profile.php");
}

require_once('commonheader.php');

?>

<div id="wrap">
  <div id="main">
    <h3>
    My ImageNet Account
    </h3>

    <br clear="all">
    <div class="hr"></div>

    <div id="about_content" class="aboutoverview">

      <ul id="about_menu">
      <?php
      print_accountmenu_notloggedin("recoverusername");
      ?>
      </ul>

      <div id="about_content_border_fixed">

      <h5>Recover Username</h5>

      <?php

      $recover_ok = false;

      $action = $_POST['action'];
      if( $action == "recoverusername" ) {
        $email = trim($_POST['email']);

        $captcha = $_POST['g-recaptcha-response'];
        $captcha_valid = check_captcha($captcha);
        if( !$captcha_valid ) {
          $recaptcha_error = "The reCAPTCHA wasn't entered correctly. Please try again.";
        }

        if( !$email ) {
          $email_error = "This field is required.";
        } else if( !validEmail($email) ) {
          $email_error = "Please enter a valid email address.";
        }

        $check_ok = !isset($email_error) && !isset($recaptcha_error);
        if( $check_ok ) {
          // Look up user by email and send username — always show generic message
          $userid = $userdb->getUserIDByEmail($email);
          if( $userid ) {
            $userinfo = $userdb->getUserInfo($userid);
            $username = $userinfo['username'];
            $msg = sprintf("Hello,\r\n\r\nYou requested your ImageNet username. Your username is: %s\r\n\r\nIf you did not request this email, please ignore it.\r\n\r\nThanks.\r\nImageNet Support Team", $username);
            $subject = "ImageNet: Your username";
            sendmail_fromsupport($email, $subject, $msg);
          }
          // Always show the same message to prevent account enumeration
          $recover_ok = true;
        }
      }

      if( $recover_ok ) {
        echo "If that email address is registered, we have sent the username to it.";
      } else {
        ?>
        Forget your username? Enter your email address and we'll send it to you.
        <p/>
        <form action="" method="post">
        <table>

        <tr>
        <td>Email: </td>
        <td>
        <input type="text" name="email" value="<?php echo htmlspecialchars($email); ?>"></input>
        <div class="signup_field_error"><?php echo $email_error; ?></div>
        </td>
        </tr>

        <?php print_hidden_input('action', 'recoverusername'); ?>

        </table>
        <br/>
        &nbsp We need to verify that you are a human, not a robot.

        <div class="g-recaptcha" data-sitekey="6LdV7wUTAAAAADeN22dATsLQwQAoU4wNRfVK6uEe"></div>
        <div class="signup_field_error"><?php echo $recaptcha_error; ?></div>

        <br/>
        <input type="submit" name="submit" value="Recover"></input>

        </form>
        <?php
      }
      ?>

      </div><!--about_count-->
    </div><!--/about_content_overview-->
  </div><!--/main-->
</div><!--/wrap-->

<br clear="all">
<p>

<?php
require('commonfooter.php');
?>
