<?php
/**
 * Created by PhpStorm.
 * User: jaskokoyn
 * Date: 2/24/2015
 * Time: 11:32 AM
 */

include( '../inc/db.php' );
include( '../inc/functions.php' );

$response   =   array();

if( !isReady($_POST, array( "userEmail" ) ) ){
    $response['status'] = 2;
    die( json_encode($response) );
}

$email      =   secure($_POST['userEmail']);
$expires    =   time() + 86400;
$code       =   get_random_string(16);

$insertForgotQuery    =   $db->prepare("INSERT INTO forgot_log(email,expires,fkey)
                                      VALUES( :email, :expires, :fkey )");
$insertForgotQuery->execute(array(
    ":email"        =>  $email,
    ":expires"      =>  $expires,
    ":fkey"         =>  $code
));

$headers = "From: no-reply@" . $_SERVER['HTTP_HOST'] . "\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message    =   "
<h1>You requested a password change.<h1>
<p>
To change your password please <a href='" . $_SERVER['SERVER_NAME'] . "/#/forgot/" . $code . "'>Click here!</a>
</p>
";
mail($email, "Forgot Password Recovery", $message, $headers);

$response['status'] =   1;
$response['message']    =   "Please check your email for more instructions.";
die(json_encode($response));
