<?php
    header("Content-Type:application/json");
    include("../connection.php");
    include("../utils.php");
    
    //Paraeotros obligatorios
    if (!isset($_GET["hash"]) || !isset($_GET["pwd"])){
        header("HTTP/1.1 400");
        echo (json_encode(array("msg"=>"Parametros obligatorios hash,pwd")));
        die();
    }
    
    //Inicializamos las variables
    $hash = $_GET["hash"];
    $pwd = $_GET["pwd"];
    
    //Actualizamos al usuario
    $sql =  "UPDATE users " .
            "SET    pwd = '" . $pwd . "', ".
            "       status = 1, ".
            "       fails = 0, ".
            "       hashrecover = NULL ".
            "WHERE  hashrecover='".$hash."'";
    if (!$db->query($sql))
        badEnd("500", array("msg"=>$db->error));
    
    //Si no se actualizo un usuario se retorna un error
    if ($db->affected_rows == 0){
        badEnd("401", array("msg"=>"Hash incorrecto"));
    }
    
    $out = new stdClass();
    $out->msg = "Contrase���a cambiada satisfactoriamente";
    
    header("HTTP/1.1 200");
    echo (json_encode($out));
    die();
?>