<?php

/* 1st page on load */

//load classes
ob_start();
require_once 'config.php';
$oClassManagement = new ClassManagement();

//Global values
ToolBox::cleanPost($_POST);
ToolBox::cleanPost($_GET);
ToolBox::cleanPost($_REQUEST);
ToolBox::fixGlobalVars(isset($argv)?$argv:array());

//Global variables (reserved names) and default values
$sPage = isset($_GET['p'])?$_GET['p']:'welcome';
$sAction = isset($_GET['a'])?$_GET['a']:'';
$oCode = isset($_REQUEST['code'])?$_REQUEST['code']:false;
$oUser = isset($_REQUEST['user'])?$_REQUEST['user']:'';
$sAuthName = isset($_POST['auth_name'])?$_POST['auth_name']:'';
$sAuthCompany = isset($_POST['auth_company'])?$_POST['auth_company']:'';
$sToken = isset($_GET['auth_token'])?$_GET['auth_token']:'';
$sContent = isset($_POST['content'])?$_POST['content']:'';
$sCategory = isset($_GET['cat'])?$_GET['cat']:'';
$sDescription = isset($_POST['description'])?$_POST['description']:'';
$sLink = isset($_REQUEST['link_escaped'])?$_REQUEST['link_escaped']:'';
$sSearchWords = isset($_GET['keywords'])?$_GET['keywords']:'';
$sMessage = isset($_POST['message'])?$_POST['message']:'';
$sChan = isset($_REQUEST['chan'])?$_REQUEST['chan']:'';
$asAttendees = isset($_REQUEST['attendees'])?$_REQUEST['attendees']:'';
$bFirstConn = isset($_GET['first_conn'])?$_GET['first_conn']:false;
$iMessageId = isset($_GET['message_id'])?$_GET['message_id']:0;
$iProcId = isset($_GET['proc_id'])?$_GET['proc_id']:0;
$iItemId = isset($_GET['id'])?$_GET['id']:0;
$sResult = '';

//Launching application
$oDatabap = new Databap($oClassManagement);

//Auth check
$bUserOk = $oDatabap->logMeIn($sAuthName, $sAuthCompany, $sToken, $sAction);

//if connected
if($bUserOk && $sAction!=Databap::EXT_ACCESS)
{
	//if expected page, relocate
	//$oDatabap->redirectExpectedPage();
	
	//loading data
	if($sAction!='')
	{
		//Public actions
		switch($sAction)
		{
			case 'log_me_out':
				$oDatabap->logMeOut();
				break;
			case 'add_code':
				$sResult = $oDatabap->addCode(array('description'=>$sDescription, 'content'=>$sContent, 'link'=>$sLink));
				break;
			case 'edit_code':
				$sResult = $oDatabap->editCode($oCode, $sContent);
				break;
			case 'read_code':
				if($oCode!==false) $sResult = $oDatabap->getColoredCode($oCode);
				break;
			case 'nude_code':
				if($oCode!==false) $sResult = $oDatabap->getNudeCode($oCode);
				break;
			case 'raw_code':
				if($oCode!==false) $sResult = $oDatabap->getRawCode($oCode);
				break;
			case 'dl_code':
				if($oCode!==false) $sResult = $oDatabap->getSavedCode($oCode);
				break;
			case 'dl_file':
				$sResult = $oDatabap->getFile($iItemId);
				break;
			case 'print_code':
				if($oCode!==false) $sResult = $oDatabap->getPrintCode($oCode);
				break;
			case 'add_procedure':
				$sResult = $oDatabap->addProcedure($_POST);
				break;
			case 'get_procedure':
				$sResult = $oDatabap->getProcedure($iProcId);
				break;
			case 'add_doc':
				$sResult = $oDatabap->addDoc($_POST);
				break;
			case 'get_doc':
				$sResult = $oDatabap->getDoc($iItemId);
				break;
			case 'art_redirect':
				$sResult = $oDatabap->redirectArticle($iItemId);
				break;
			case 'upload_image':
				$sResult = $oDatabap->uploadImage();
				break;
			case 'upload_file':
				$sResult = $oDatabap->uploadDoc();
				break;
			case 'url':
				$sResult = $oDatabap->checkValue(MySqlManager::URL_TABLE, array('phrase'=>$sLink));
				break;
			case 'user_info':
				$sResult = $oDatabap->getUserInfo($oDatabap->getUserId(), true);
				break;
			case 'profile':
				$sResult = $oDatabap->getProfile($oUser);
				break;
			case 'search':
				$sResult = $oDatabap->getResults($sSearchWords);
				break;
			case 'code_block':
				$sResult = $oDatabap->getCodeBlock();
				break;
			case 'join_chan':
				$sResult = $oDatabap->joinChan($sChan, $bFirstConn, $asAttendees);
				break;
			case 'quit_chan':
				$sResult = $oDatabap->quitChan($sChan);
				break;
			case 'disconnect_chat':
				$sResult = $oDatabap->disconnectChat();
				break;
			case 'add_message':
				$sResult = $oDatabap->addChatMessage($sMessage, $sChan);
				break;
			case 'messages':
				$sResult = $oDatabap->getMessages($iMessageId);
				break;
			case 'connected_users':
				$sResult = $oDatabap->getConnectedUsers(true);
				break;
			case 'get_options':
				$sResult = $oDatabap->getOptions();
				break;
			case 'set_options':
				$sResult = $oDatabap->setOptions($_POST, false);
				break;
			case 'list':
				$sResult = $oDatabap->getItemList();
				break;
			case 'css':
				$sResult = $oDatabap->getStyleSheet();
				break;
		}
		
		//Admin actions
		if($oDatabap->getUserClearance()==Databap::CLEARANCE_ADMIN)
		{
			switch($sAction)
			{
				case 'add_user':
					$asInfo = explode('-', strtolower($oUser));
					$oDatabap->addUser($asInfo[0], $asInfo[1], $asInfo[2]);
					break;
				case 'reset_token':
					$sResult = print_r($oDatabap->resetToken(), true);
					break;
				case 'build_index':
					$oDatabap->buildCompleteIndex();
					$sResult = 'Index ok';
					break;
				case 'install':	
					$oMySqlInstall = new MySqlManager();
					$sResult = $oMySqlInstall->getFullInstallQuery();
					break;
			}
		}
	}
	//Loading a page
	else
	{
		$sResult = $oDatabap->getPage($sPage, $_GET);
	}
}
//External Access with token
elseif($bUserOk && $sAction==Databap::EXT_ACCESS)
{
	//Public actions
	switch($sPage)
	{
		case 'rss': //RSS Feed
			$sResult = $oDatabap->getRss($sCategory);
			break;
	}

	//Restricted actions
	if($oDatabap->getUserClearance()==Databap::CLEARANCE_ADMIN)
	{
		switch($sPage)
		{
			case 'sap_blog': //Syncing SAP BW Blog with database & spreading the news on chat
				$sResult = $oDatabap->syncSapBlog();
				break;
		}
	}
}
elseif($sAction!='')
{
	$sResult = Databap::DISCONNECTED;
}
//loading logon
else
{
	//$oDatabap->setExpectedPage($_SERVER['REQUEST_URI']);
	$oPage = new Mask('logon');
	$oPage->setTag('name', $sAuthName);
	$oPage->setTag('company', $sAuthCompany);
	$sResult = $oPage->getMask();
}

//clean unwanted error log
ob_end_clean();
//$sDebug = ob_get_clean();

echo $sResult;
//echo json_encode($sDebug);
?>