<?php
define ('SITE_ROOT', realpath(dirname(__FILE__)));

session_start();

require_once SITE_ROOT.'/core/environment.php';

// Pick config based on environment
switch (CM_ENVIRONMENT) {
	case 'production-rackspace':
		require_once SITE_ROOT.'/core/config/production-rackspace.php';
		break;
	case 'production':
		require_once SITE_ROOT.'/core/config/production.php';
		break;
	case 'development':
		require_once SITE_ROOT.'/core/config/development.php';
		break;
	case 'local':
		require_once SITE_ROOT.'/core/config/local.php';
		break;
	default:
		die('Cannot find configuration based on the environment passed.');
		break;
}

if(config::$maintenance) {
	header('HTTP/1.1 503 Service Temporarily Unavailable');
	header('Status: 503 Service Temporarily Unavailable');
	require_once SITE_ROOT.'/maintenance.html';
	exit;
}

// Allows us to use the "path" param to define the site path.
if(isset($_REQUEST['path']))
{
	$_SERVER['REQUEST_URI'] = $_REQUEST['path'].ltrim($_SERVER['REQUEST_URI'],'/');
}

if(CM_STATIC_HASH)
	define('GIT_HASH', md5(CM_STATIC_HASH));
else
	define('GIT_HASH', md5(microtime(true)));

// This changes our request into something sane for Slim to understand
require_once 'core/subdomain_routing.php';

// Slim Framework is the spine, routes everything
require_once 'slim/Slim.php';

// This global is for showing either the global feed or the personalized feed
if(isset($_GET['global'])) {
	if(filter_var($_GET['global'], FILTER_VALIDATE_BOOLEAN)) {
		$_SESSION['global'] = true;
	}else{
		$_SESSION['global'] = false;
	}
}

// Set based on session or default to true (show personalized feed by default)
if (isset($_SESSION['global']) AND filter_var($_SESSION['global'], FILTER_VALIDATE_BOOLEAN)) {
	$_SESSION['global'] = true;
	define('GLOBAL_FEED', true);
}else{
	$_SESSION['global'] = false;
	define('GLOBAL_FEED', false);
}

// Include our libraries, helpers, etc
require_once 'libraries/Crowdmap/Crowdmap.php';
require_once 'libraries/geoip/geoipcity.inc';
require_once 'libraries/recurly.php';
require_once 'libraries/smart_trim.php';
require_once 'libraries/codebird.php';

require_once 'helpers/user.php';
require_once 'helpers/template.php';
require_once 'helpers/hb.php';
require_once 'helpers/form.php';
require_once 'helpers/web.php'; // Standardizes cURL requests
require_once 'helpers/thirdparty.php';
require_once 'helpers/browser.php';

// Fire up the framework
$app = new Slim();

require_once 'core/set_locale.php';

// Prepare template helper
$t = new t;

require_once 'core/routes.php';

$app->run();
