<?php
define('SYSINFO', 'FRONTEND');

include('inc/config.php');

try
{
	$redirect = true;
	// URl type handling, if the request does not directly called the index.php file.
	if ( mb_strpos($_SERVER['REQUEST_URI'], '/index.php') === false && mb_strpos($_SERVER['REQUEST_URI'], '/?') !== 0 )
	{
		// check the url type, if urltype is URLTYPE_PAGE the page gets rendered
		/**
		 * @var NFYC_Frontend_UrlType $urlTypeInstance
		 */
		$urlTypeInstance = NFY_Mixed_Loader::getCustomizedInstance('NFY_Frontend_UrlType');
		$redirect = ($urlTypeInstance->isUrlValid() === false);
		if ( $redirect === true )
		{
			$urlTypeInstance->handle();
		}
		// If the request is not a URLTYPE_PAGE, the process ends here.
	}

	// Initialization of the frontend framework and redirect to another page if defined.
	$renderInstance = new NFY_Frontend_RenderInit(array('renderType' => NFY_Frontend_HttpHeader::TYPE_PAGE, 'redirect' => $redirect));
}
catch ( Exception $e )
{
	$renderInstance = null;
	switch ( $e->getCode() )
	{
		case 404:
		case 410:
			NFY_Mixed_Base::errorLog(__FILE__ . ': ' . $e->getMessage(), 'webServer');
			break;

		default:
			NFY_Mixed_Base::errorLog(__FILE__ . ': ' . $e->getMessage() . "\nTRACE:\n" . $e->getTraceAsString());
	}
	NFY_Mixed_HttpStatusCodeHandler::dispatchError($e->getCode());
}

$htmlOutput = null;
if ( is_object($renderInstance) )
{
	$htmlOutput = $renderInstance->normalInclude($renderInstance->moddata['template_name']);
}

if ( empty($htmlOutput) )
{
	NFY_Mixed_Base::monitorLog(__FILE__ . ': page without content!');
	NFY_Mixed_HttpStatusCodeHandler::dispatchError(500);
	exit;
}

echo $htmlOutput;
