<?php 

	require_once(__DIR__.'/includes/sysinit.php'); //Must be called from the index.php of each module. Calls VerifySession()!

	//should be cleared so we don't get parameters from a previous call.

	//SableDebug::debug($_SERVER);

	if (isset($_SESSION['caller']['args'])) 

	{

		unset($_SESSION['caller']['args']);

	}

	/*

		if our encoding (encryption) is enabled (i.e. SABLE_ENABLE_ENCRYPTION===true) then we need to first decode our argument:

	*/	



	if (SABLE_ENABLE_ENCRYPTION===true)

	{

		//when encoded there can only be one argument (should not be any ? or + symbols :P

		if ($_SERVER['argv'][0])

		{

			$urlArg = SableSystem::sableDecode($_SERVER['argv'][0]);

			if (strpos($urlArg, '?')) 

			{

				$dcoded = $_SESSION['caller']['args'] = explode('?', $urlArg); 			

				$dcoded = implode('+', $dcoded);			

			} else $dcoded = $urlArg;

		}

	}

	else $dcoded = isset($_SESSION['revert']['url'])&&(!empty($_SESSION['revert']['url']))?$_SESSION['revert']['url']:$_SERVER['QUERY_STRING'];

	//SableDebug::debug($dcoded);

	//Check for .htaccess "ErrorDocument" pages:

	switch($dcoded)

	{

		case 400:

		{

			$dcoded = SABLE_THEME_WORKING_PATH."400.php";//"400.php";

			$_SESSION['caller']['args'][0] = "400.php";

			break;

		}

		case 401:

		{

			$dcoded = SABLE_THEME_WORKING_PATH."401.php";//"401.php";

			$_SESSION['caller']['args'][0] = "401.php";

			break;

		}		

		case 403:

		{

			$dcoded = SABLE_THEME_WORKING_PATH."403.php";//"403.php";

			$_SESSION['caller']['args'][0] = "403.php";

			break;

		}

		case 404:

		{

			$dcoded = SABLE_THEME_WORKING_PATH."404.php";//"404.php";

			$_SESSION['caller']['args'][0] = "404";

			break;

		}

		case 500:

		{

			$dcoded = SABLE_THEME_WORKING_PATH."500.php";//"500.php";

			$_SESSION['caller']['args'][0] = "500.php";

			break;

		}		

	}



	$maintenanceFile 		= SABLE_THEME_WORKING_PATH."maintenance.php";

	$showUnderMaintenance 	= false;

	//0 = no maintenance, 1 = maintenance logged in, 2 = maintenance not logged in, 3 = maintenance both.



	if ((SABLE_UNDER_MAINTENANCE) && (is_file($maintenanceFile)))

	{

		switch (SABLE_UNDER_MAINTENANCE)

		{

			case 1:

			{

				$loginpage = SABLE_URL_ROOT.'login/logout.php+'.session_id();

				$showUnderMaintenance = SableSystem::loggedIn() && ($dcoded !== $loginpage);

				break;

			}

			case 2: 

			{

				$loginpage = SABLE_URL_ROOT.'login/index.php';

				$showUnderMaintenance = !SableSystem::loggedIn() && ($dcoded !== $loginpage);

				break;

			}

			case 3:

			{

				$showUnderMaintenance = true;

				break;

			}

		}

		if ($showUnderMaintenance)

		{

			$dcoded = $maintenanceFile;//"500.php";

			$_SESSION['caller']['args'][0] = $dcoded;

		}

	}



	/*

		How the URL works.

		------------------

		

		There are different options when reading URL's. 

		1. URL with no parameters (e.g. http://sable.jona.co.za)

		   --> There is no session ID so, you are not logged in and should only ever be for the website's home page.

		   

		2. URL with page reference (e.g. http://sable.jona.co.za/index.php?user/index.php)

		   --> The parameter indicates the page to access.

		   --> If no session id, then not logged in.

		   --> "?" indicates to us a parameter which (in this case) is the page to redirect to.

		   

		3. URL with page ref + parameters (e.g. http://sable.jona.co.za/index.php?user/index.php?0+30+1+1+f43jpb7h5mm4kc74vj161pj873

		   --> This URL holds 2 "?". This must be extracted first because the first "?" tells us the page

		       to redirect to, and the second gives is the parameter list of the page we're going to.

		   --> Parameters are delimted by "+" and can be exploded into an array.

	*/



	if (strpos($dcoded, '?')) $dcoded=str_replace('?', '+', $dcoded);

	if (strpos($dcoded, '+')) 

	{

		$_SESSION['caller']['args'] = explode('+', $dcoded); 

	}

	else 

	{

		//if (!empty($dcoded))

		//{

			$_SESSION['caller']['args'] 	= array();

			$_SESSION['caller']['args'][] 	= $dcoded;	

		//}

	}

	//SableDebug::debug($_SESSION['caller']);



	/*

		check for URL arguments. If we are logged in then the LAST argument will be the session_id and the first argument 

		is the URL with - possibly - its parameters/arguments. This means, therefore that if we have only 1 argument, we 

		cannot be logged in and this will be the URL. If we have 2 arguments then we are logged in.

	*/	

	$url = $_SESSION['caller']['args'][0]; //our first argument	

	/*

		if we are in an active session (i.e. logged in) then we must use the last argument to 

		verify the session validity.

		If not logged in, then the first argument is the URL.

		$_SESSION['caller']['args'] must be called before this check because checkSession() uses

		$_SESSION['caller']['args'][0] as the session id.

	*/



	if (SableSystem::loggedIn()) SableSystem::checkSession();

	if (empty($url)) $url = SABLE_WORKING_PATH.SABLE_THEME_PATH.SABLE_INSTALLED_THEME;	

	//for includes, the path must be an absolute one:

	if (strpos($url, SABLE_URL_ROOT) !== false) $url = str_replace(SABLE_URL_ROOT, SABLE_WORKING_PATH, $url);

	SableSystem::convertURLPathToWorkPath($url);

	//now, finally, check that the absolute path has been provided as a prefix:

	$swp = strpos($url, SABLE_WORKING_PATH);

	if ($swp===false) $url=SABLE_WORKING_PATH.$url;

	//check if the path is clean, and call it:

	$url = substr($url, -1)=='/'?$url.='index.php':$url; //must add file name if it's not there... default is index.php

	if ((substr($url, -4)!=='.php')&&(substr($url, -1)!=='/')) $url = $url.'/';

	$url = str_replace('//', '/', $url); //can do this 'cos this is a working path not an URL path.

	

	//SableDebug::debug($url);

	//build up any useful information regarding our path:

	$_SESSION['current_page'] = SableGeneralFunctions::extractFileName($url); //extractFileName($_SESSION['caller']['args'][0]);

	$_SESSION['current_path'] = SableGeneralFunctions::extractFilePath($url); //extractFilePath($_SESSION['caller']['args'][0]);

	$_SESSION['current_dir']  = SableGeneralFunctions::extractDirName($url);  //extractDirName($_SESSION['current_path']);

	//SableDebug::debug($_SESSION);

	

	//check for specific pages that require Open Graph tags to be defined:

	file_exists(SABLE_THEME_WORKING_PATH.'ogpages.php')?include_once(SABLE_THEME_WORKING_PATH.'ogpages.php'):'';

	require_once('includes/sable_header.php');



	if (SableSystem::loggedIn())

	{

		if ($_SESSION['user']['access_level'] > SABLE_ACCESS_USER)

		{

	?>

        <section class="sableDashboard">

            <div class="sableDashboardMenu"><?php require_once(SABLE_THEME_MENU); ?></div>

            <div class="sableDashboardContent" id="sableDashboardContent">

                <div class="wrapper wrapper-content animated fadeInRight" id="page-wrapper">

                    <div class="row-fluid border-bottom">

						<div class="navbar section no-padding" role="navigation" style="margin:10px 0; background-color:#3cced6;">

							<div class="heading" style="margin-bottom:0;">

								<div class="container" style="width:95%">

									<div class="row">

										<div class="col-sm-12">

											<a class="navbar-minimalize" title="Menu" href="javascript:void(0);">

												<span class="fa fa-2x fa-bars" style="margin-top:10px;"></span>

											</a>

										</div>

									</div>

								</div>

							</div>

						</div>

                    </div> 

					<?php 

                        if (file_exists($url)) require_once($url); else SableGeneralFunctions::divertFromPage('home/', 'Page not found (404)');

                    ?>

            	</div>

            </div>

        </section>

    <?php

		}

		else

		{

			//mobile app layout for Groomers that will use phone to navigate between clients:

	?>

        <section class="sableDashboard">

            <div class="sableDashboardContent" id="sableDashboardContent">

                <div class="animated fadeInRight"> 

					<?php 

                        if (file_exists($url)) require_once($url); else SableGeneralFunctions::divertFromPage('home/', 'Page not found (404)');

                    ?>

            	</div>

            </div>

        </section>

	<?php

	

		}

	} 

	else 

	{

		//SableDebug::debug($url);

		if (file_exists($url)) require_once($url); else SableGeneralFunctions::divertFromPage('/?404', '');

	}

	require_once('includes/sable_footer.php'); 

	unset($_SESSION['og_tags']);