<?php
    error_reporting(E_ERROR);
	require_once __DIR__ . '/repository/bootstrap.php';

	require_once 'domains.php';
	/**
	 * Check if the url contains inlognaam, if it does redirect to dealer
	 * http://bookyou.com/active where "active" is dealer inlognaam
	 */
	function dealer_init()
	{
		$pattern = preg_quote(BASEURL, '/');

		// check if we have a url with a possible dealername
		if (!preg_match("/^{$pattern}([^\/]+)(.*)$/i", $_SERVER['REQUEST_URI'], $matches))
			return;

		// get dealer inlognaam
		$inlognaam = $matches[1];

		// initialize db object
		$db = \BookYou::getDb();
		$dbresult = $db->dbExec("SELECT * FROM #__dealergegevens_nl WHERE inlognaam={$db->quoteString($inlognaam)}");

		// no dealer found
		if (empty($dbresult))
			return;

		// append extra slash to fix the url
		if (preg_match('/^.*' . preg_quote($inlognaam, '/') . '$/i', $_SERVER['REQUEST_URI']))
			$_SERVER['REQUEST_URI'] .= '/';

		$dealer = $dbresult[0];

		define('DEALERIDX', $dealer['idx']);
		define('DEALERBASEURL', BASEURL . strtolower($dealer['inlognaam']) . '/');

		require_once LOCALPATH . 'dealer/index.php';
		exit;
	}
	dealer_init();

	// test for images
	if (preg_match('/^.*\.(png|jpg|gif|ico|pdf)(\?.*)?$/i', $_SERVER['REQUEST_URI'])) {
		header('HTTP/1.0 404 not found');
		exit;
	}

	// skip some directories
	if ( empty( $path ) ) $path = $_SERVER['REQUEST_URI'];
	$path = substr( $path, strlen( BASEURL ) );
	$path = explode( '/', $path );

	// actiecode redirect
	if ( strcasecmp( $path[0], 'deal' ) == 0 )
	{
		$_SERVER['REQUEST_URI'] = BASEURL.'site/?actiecode=' . $path[1];
		require_once LOCALPATH.'site/index.php';
		exit;
	}

	// actiecode redirect
	if ( strcasecmp( $path[0], 'free' ) == 0 )
	{
		$_SERVER['REQUEST_URI'] = BASEURL.'site/splash?actiecode=' . $path[1];
		require_once LOCALPATH.'site/index.php';
		exit;
	}

	// redirect naar wordpress
	if ( BY_LIVE && empty( $path[0] ) )
	{
		header( 'Location: http://bookyou.com/' );
		exit;
	}

	$_SERVER['REQUEST_URI'] = BASEURL.'site/';
	require_once LOCALPATH.'site/index.php';
