<?php

   session_start();

   include_once "../pdo_connection.php";

   if(isset($_REQUEST["todo"]) && $_REQUEST["todo"] == "CheckUser") CheckUser();
   if(isset($_REQUEST["todo"]) && $_REQUEST["todo"] == "RememberMe") RememberMe();
   if(isset($_REQUEST["todo"]) && $_REQUEST["todo"] == "LogMeIn") LogMeIn();
   if(isset($_REQUEST["todo"]) && $_REQUEST["todo"] == "LogMeOut") LogMeOut();

   function CheckUser() {

      $sql    = "SELECT user_email, remember_me, TO_SECONDS(ts) AS ts FROM users WHERE user_id = ? LIMIT 1";
      $values = array($_REQUEST["GSB-User"]);
      $stmt   = $GLOBALS["pdo"]->prepare($sql);
      
      $stmt->execute($values);

      if($stmt->rowCount() == 0) echo json_encode(array("Status"=>"ERROR", "Value"=>"No user selected!"));
      else {
          $retData = $stmt->fetch();
          $retData["Status"] = "OK";
          echo json_encode($retData);
      }

   }

   function RememberMe() {

      $rememberMe = isset($_REQUEST["remember"]) ? 1 : 0;

      $sql  = "UPDATE users SET remember_me = ? WHERE user_id = ?";
      $stmt = $GLOBALS["pdo"]->prepare($sql);
      $stmt->execute(array($rememberMe, $_REQUEST["GSB-User"]));

      if($stmt->rowCount() == 0) echo json_encode(array("Status"=>"ERROR", "Value"=>"Not updated!"));
      else echo json_encode(array("Status"=>"OK", "Value"=>$rememberMe));
   }

   function LogMeIn() {
/*
      $sql    = "UPDATE users SET user_password_hash = ? WHERE user_id = ?";
      $values = array(password_hash("dj2510", PASSWORD_DEFAULT), 2);
      $stmt   = $GLOBALS["pdo"]->prepare($sql);
       
      $stmt->execute($values);
*/
      $rememberMe = isset($_REQUEST["remember"]) ? 1 : 0;

      $sql    = "SELECT user_id, user_name, remember_me, TO_SECONDS(ts) AS ts, user_password_hash FROM users WHERE user_email = ? LIMIT 1";
      $values = array($_REQUEST["email"]);
      $stmt   = $GLOBALS["pdo"]->prepare($sql);
       
      $stmt->execute($values);

      if($stmt->rowCount() == 0) $granted = false;
      else {
          $retData = $stmt->fetch();
          $granted = false;
          if(is_numeric($_REQUEST["password"])) {
              if($retData["ts"] == $_REQUEST["password"]) $granted = true;
          } else {
             $granted = password_verify ($_REQUEST["password"], $retData["user_password_hash"]);
          }
      }
      if($granted) {
         $_SESSION["GSB-User"] = $retData["user_id"];
         $_SESSION["GSB-Auto"] = $rememberMe;
         $_SESSION["GSB-Name"] = $retData["user_name"];
         setcookie("GSB-User", $retData["user_id"], time() + 31500000, "/");
         setcookie("GSB-Auto", $rememberMe, time() + 31500000, "/");
         $_COOKIE["GSB-User"] = $retData["user_id"];
         $_COOKIE["GSB-Auto"] = $rememberMe;
         $retData["Status"] = "OK";
         unset($retData["user_password_hash"]);
         echo json_encode($retData);
      } else echo json_encode(array("Status"=>"ERROR", "Value"=>"No user selected!"));

   }

   function LogMeOut() {
       $_SESSION["GSB-User"] = 0;
   }

?>