<?php

/*
 Pedidor Project
 http://git.lutran.fr/pedidor.git
 Copyright (C) 2015 François Lutran & Clara Floren Zabala

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program. If not, see http://www.gnu.org/licenses
 */

/* Requests Handler */

//Start buffering
ob_start();
require_once '../objects/class_management.php';
$oClassManagement = new ClassManagement('pedidor');
ToolBox::cleanPost($_POST);
ToolBox::cleanPost($_GET);
ToolBox::cleanPost($_REQUEST);
ToolBox::fixGlobalVars(isset($argv)?$argv:array());

//Available variables
$sToken = isset($_GET['token'])?$_GET['token']:'';
$sAction = isset($_REQUEST['a'])?$_REQUEST['a']:'';
$sPage = isset($_GET['p'])?$_GET['p']:'index';
$iApiKey = isset($_GET['api'])?$_GET['api']:'';
$sContent = isset($_POST['content'])?$_POST['content']:'';
$iId = isset($_REQUEST['id'])?$_REQUEST['id']:0;

//Initiate class
$oPedidor = new Pedidor($oClassManagement, __FILE__);
$bLoggedIn = $oPedidor->isLoggedIn();

$sResult = '';
if($sAction=='logmein') $sResult = $oPedidor->logMeIn($sToken);
elseif($sAction!='' && $bLoggedIn)
{
	switch($sAction)
	{
		case '':
			//$sResult = $oPedidor->
			break;
		default:
			$sResult = Pedidor::getJsonResult(false, Pedidor::NOT_FOUND);
	}
}
elseif($sAction!='' && !$bLoggedIn)
{
	if($oPedidor->checkApiKey($iApiKey))
	{
		switch($sAction)
		{
			case '':
				//$sResult = $oPedidor->
				break;
			default:
				$sResult = Pedidor::getJsonResult(false, Pedidor::NOT_FOUND);
		}
	}
	else $sResult = Pedidor::getJsonResult(false, Pedidor::UNAUTHORIZED);
}
else $sResult = $oPedidor->getPage($bLoggedIn);

$sDebug = ob_get_clean();
if(Settings::DEBUG && $sDebug!='') $oPedidor->addUncaughtError($sDebug);

echo $sResult;

?>