<?php
/**
 * Created by PhpStorm.
 * User: jaskokoyn
 * Date: 2/24/2015
 * Time: 1:28 PM
 */

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

$response   =   array();

if(isset($_SESSION['loggedin']) || !isReady($_POST, array('pass1', 'pass2', 'code'))){
    $response['status'] = 2;
    die( json_encode($response) );
}

$code   =   secure($_POST['code']);
$codeCheckQuery =   $db->prepare("SELECT * FROM forgot_log WHERE fkey = :fkey AND expires > :ctime");
$codeCheckQuery->execute(array(
    ":fkey" =>  $code,
    ":ctime"    =>  time()
));
if($codeCheckQuery->rowCount() == 0){
    $response['status'] = 2;
    die( json_encode($response) );
}
$codeCheckRow   =   $codeCheckQuery->fetch();

$pass1  =   secure($_POST['pass1'], true);
$pass2  =   secure($_POST['pass2'], true);

if($pass1 != $pass2){
    $response['status'] = 2;
    die( json_encode($response) );
}

$userChangeQuery    =   $db->prepare("UPDATE users SET pass = :pass WHERE email = :email");
$userChangeQuery->execute(array(
    ":pass"     =>  $pass1,
    ":email"    =>  $codeCheckRow['email']
));

$response['status'] =   1;
die( json_encode($response) );