<?php

require_once dirname(__FILE__) . '/se-lib/bootstrap.php';

if (!Request::isHttps()) {
	header("Location: https://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}");
	exit();
}

date_default_timezone_set('Europe/Warsaw');// PHP 5 >= 5.1.0 required by date functions

$errorReportingLevel = E_ALL & ~E_NOTICE;
error_reporting($errorReportingLevel);
ini_set('error_reporting', $errorReportingLevel);

if (file_exists(APP_PATH_ROOT . "/config/.config_{$_SERVER['SERVER_NAME']}.php")) {
	require APP_PATH_ROOT . "/config/.config_{$_SERVER['SERVER_NAME']}.php";
}
if (file_exists(APP_PATH_ROOT . "/.config.php")) include APP_PATH_ROOT . "/.config.php";

require_once APP_PATH_ROOT . "/superedit-SEF.php";
SEF('DEBUG_S');

Lib::loadClass('ApiUser');
Lib::loadClass('Api');

session_start();
session_write_close();

$apiUser = new ApiUser();
// ?LOGIN=LOGOUT
if ('LOGOUT' == V::get('LOGIN', '', $_GET)) {
	$apiUser->logout();
} else {
	$apiUser->auth();
}

$taskPath = Request::getRewriteTaskPath();
IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">taskPath (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($taskPath);echo'</pre>';}

$api = new Api();
$api->setUser($apiUser);
try {
	$api->execute($taskPath);
}
catch (HttpException $e) {
	Http::sendHeaderByCode($e->getCode());
	echo $e->getMessage();
}
catch (Exception $e) {
	Http::sendHeaderByCode(500);
	echo $e->getMessage();
}

IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">PHP_AUTH_USER (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($_SERVER['PHP_AUTH_USER']);echo'</pre>';}
IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">REQUEST_URI (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($_SERVER['REQUEST_URI']);echo'</pre>';}
IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">SCRIPT_NAME (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($_SERVER['SCRIPT_NAME']);echo'</pre>';}
IF(V::get('DBG','',$_GET)){die("\nEOF L." . __LINE__);}

?>
