<?php

/*
 MyThoughts Project
 http://git.lutran.fr/main.git
 Copyright (C) 2015 François Lutran

 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('mythoughts');
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';
$sNickName = isset($_GET['nickname'])?$_GET['nickname']:'';
$iApiKey = isset($_GET['api'])?$_GET['api']:'';
$sContent = isset($_POST['content'])?$_POST['content']:'';
$iId = isset($_REQUEST['id'])?$_REQUEST['id']:0;

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

$sResult = '';
if($sAction=='logmein') $sResult = $oMyThoughts->logMeIn($sToken);
elseif($sAction!='' && $bLoggedIn)
{
	switch ($sAction)
	{
		case 'update':
			$sResult = $oMyThoughts->updateThought($sContent, $iId);
			break;
		default:
			$sResult = MyThoughts::getJsonResult(false, MyThoughts::NOT_FOUND);
	}
}
elseif($sAction!='' && !$bLoggedIn)
{
	if($oMyThoughts->checkApiKey($iApiKey))
	{
		switch ($sAction)
		{
			case '':
				//$sResult = $oMyThoughts->apifunction();
				break;
			default:
				$sResult = MyThoughts::getJsonResult(false, MyThoughts::NOT_FOUND);
		}
	}
	elseif($sAction=='register') $sResult = $oMyThoughts->register($sNickName);
	else $sResult = MyThoughts::getJsonResult(false, MyThoughts::UNAUTHORIZED);
}
else $sResult = $oMyThoughts->getPage($bLoggedIn);

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

echo $sResult;

/*

//load classes
session_start();
require_once 'config.php';

//clean sent values
cleanPost($_POST);
cleanPost($_GET);
cleanPost($_REQUEST);

//general
$sPage = (isset($_GET['p']) && $_GET['p']!='')?$_GET['p']:'w';
$sPostToken = isset($_POST['post_token'])?$_POST['post_token']:'';

//logon
$sLogin = (isset($_POST['login']) && $_POST['login']!='Nickname')?$_POST['login']:'';
$sPass = (isset($_POST['pass']) && $_POST['pass']!='Password')?$_POST['pass']:'';
$bRegister = (isset($_POST['register']) && $_POST['register']==1);

//writing pad
$sThought = isset($_POST['thoughts'])?$_POST['thoughts']:'';
$iThoughtId = (isset($_POST['thought_id']) && $_POST['thought_id']!='')?$_POST['thought_id']:0;				//update or insert
$bFinishedWriting = isset($_POST['finished']);

//calendar
$iDay = isset($_GET['d'])?$_GET['d']:date(MyThoughts::URL_DATE_FORMAT);			//d = yyyymmdd
$iCalYear = isset($_GET[Calendar::CAL_YEAR])?$_GET[Calendar::CAL_YEAR]:0;		//cy = yyyy
$iCalMonth = isset($_GET[Calendar::CAL_MONTH])?$_GET[Calendar::CAL_MONTH]:0;	//cm = m

$oMyThougths = new MyThoughts();
$bValidPost = ($sPostToken!='' && $oMyThougths->checkPostToken($sPostToken));

if($bValidPost)
{
	if($bRegister)
	{
		$oMyThougths->register($sLogin, $sPass);
		$sPage = 'r';
	}
	elseif($sLogin!='' && $sPass!='')
	{
		$oMyThougths->logMeIn($sLogin, $sPass);
	}
}

//if loggued in
if(!$oMyThougths->isLogguedIn())
{
	$oMyThougths->logonPage($sLogin);
}
else
{
	$oMyThougths->activateMenu();
	$oMyThougths->setCalendarDate();
	switch($sPage)
	{
		case 'w': //write a thought
			if($bValidPost && $sThought!='' && $sThought!='Talk to me.')
			{
				if($iThoughtId==0)
				{
					$iThoughtId = $oMyThougths->addThought($sThought);
				}
				else
				{
					$oMyThougths->updateThought($iThoughtId, $sThought);
				}
			}
			if($bFinishedWriting)
			{
				$oMyThougths->readingPage();
			}
			else
			{
				$oMyThougths->writingPage($iThoughtId);
			}
			break;
		case 'r': //read a thought (per day)
			if($iDay<=0 || !$oMyThougths->readingPage(strtotime($iDay)))
			{
				$oMyThougths->writingPage();
			}
			break;
		case 's': // go to settings page
			if($bValidPost)
			{
				$asSettings = array_intersect_key($_POST, array_flip($oMyThougths->getSettingsList()));	
				$oMyThougths->setSettings($asSettings);
				$oMyThougths->writingPage();
			}
			else
			{
				$oMyThougths->settingsPage();
			}
			break;
		case 'q': //quit
			$oMyThougths->logMeOut();
	}

	if($iCalYear!=0 && $iCalMonth!=0)
	{
		$oMyThougths->setCalendarDate($iCalYear, $iCalMonth);
	}
}
echo $oMyThougths->getPage();

*/
?>