<?php

require '../header.php';

require_anonymous();

if ( !isset($_REQUEST['username']) || !isset($_REQUEST['password']) || !isset($_REQUEST['remember']) ) redirectPage();

$user = User::getUserFromLogin($_REQUEST['username'], $_REQUEST['password']);

$data = array();

if ( $user === User::LOGININVALID ) {
   $data['status'] =  "invalid";
} else {
   $_SESSION['userid'] = $user->getID();
   $user->saveLastLogin();
   if ( $_REQUEST['remember'] == "1" ) {
      $user->setCookie($_SERVER['REMOTE_ADDR']);
   }
   $data['status'] = "valid";
}

header('Content-Type: application/json');
echo json_encode($data);
exit();
