<?php
//build//3.25//24-09-2013//13220//

// skroty
require_once 'inc/shortcuts.inc.php';

$_cfg = array();

// plik konfiguracyjny
require_once 'inc/config.inc.php';

// poczatek testu wydajnosci
require_once 'lib/Benchmark.php';
$_cfg['timer'] = new Benchmark();
$_cfg['timer']->start();

/**
 * Edito
 * 
 * @package Ideo.Edito
 * @author Jacek
 * @copyright Copyright (c) 2007
 * @version $Id$
 * @access public
 **/
final class Edito
{
	var $provider = null;
	private $providers = array
	(
		'googleAuth' => '@^oauth2callback(?P<path>.*)@',
		'Pdf' => '@^pdf/(?P<path>.*)@',
		'CSSCombine' => '@^gfx/(?P<projekt>.+?)/_thumbs/(?P<file>[0-9a-zA-Z=_/\+-]{1,128}\.css)$@',
		'JSCombine' => '@^gfx/(?P<projekt>.+?)/_thumbs/(?P<file>[0-9a-zA-Z=_/\+-]{1,128}\.js)$@',
		'Thumbs' => '@^gfx/(?P<projekt>.+?)/_thumbs/(?P<path>[0-9a-z\._/-]+)/(?P<file>[a-zA-Z0-9_\.-]{1,255}),(?P<token>.+?)\.(?P<extension>[a-zA-Z]{3,5})$@',
		'ThumbsSecure' => '@^thumbs/((?P<secure>\([0-9a-zA-Z=_/\+-]+?\))/)?(?P<path>[0-9a-z\._/-]+)/(?P<file>[a-zA-Z0-9,_\.-]{1,255})@',
		'AddClick' => '@^addclick/((?P<secure>\([0-9a-zA-Z=_/\+-]+?\))/)?(?P<path>[0-9a-zA-Z=\/]{1,250})[/]?@',
		'FileDownload' => '@^download/((?P<secure>\([0-9a-zA-Z=_/\+-]+?\))/)?(?P<path>[0-9a-z\._/-]+)/(?P<file>[a-z0-9_\.-]{1,128})@',
		'FileShow' => '@^files/((?P<secure>\([0-9a-zA-Z=_/\+-]+?\))/)?(?P<path>[0-9a-z\._/-]+)/(?P<file>[a-zA-Z0-9_\.-]{1,128})@',
		'CaptchaShow' => '@^captcha/(?P<file>[a-zA-Z0-9_\.-]{1,128})@',
		'RssShow' => '@^rss/((?P<lang>[a-z]{2})/)?(?P<path>[0-9a-z\._/-]+)/(?P<id>[a-z0-9-]+)\.xml@',
		
		/* obsluga platnosci elektronicznych */
		'allpayResponse' => '@^dotpay/response\.php@',
		'ecardResponse' => '@^ecard/response\.php@',
		'platnosciplResponse' => '@^platnosci_pl/response\.php@',
		'polcardResponse' => '@^polcard/transaction(?P<status>[a-zA-Z]{1,16})\.php@',
		
		'Newsletter' => '@^newsletter/(?P<lang>[a-z]{2})[/]?((?P<id_wpisu>[0-9a-zA-Z\._/-]+?)/)?(?P<id>[0-9]+)\.html@',
		'Stats' => '@^stat/@',
		'AmfGateway' => '@^AmfGateway/@',
		'Ajax' => '@^ajax/(?P<ajax>.*?)/@',
		'WebServices' => '@^webservice/(?P<service>[A-Za-z0-9_/-]+)\.ews@',
		'SiteMap' => '@^sitemap(?P<map>[A-Za-z0-9_/-]+)?\.xml@',
		'WebSite' => '@^.*$@',
	);
	
	public $config = array();
	public $lang = array();
	
	/**
	 * Edito::__construct()
	 * 
	 * @return 
	 **/
	public function __construct()
	{
	}
	
	/**
	 * Edito::requestUri()
	 * 
	 * @return 
	 **/
	public function requestUri()
	{
		static $requestUri;
		
		if(!isset($requestUri))
		{
			if(empty($_SERVER['REQUEST_URI']))
			{
				$_SERVER['REQUEST_URI'] = $_SERVER['ORIG_PATH_INFO'];
			}
			
			$scriptPath = ltrim(dirname($_SERVER['SCRIPT_NAME']).'/', '/');
			$scriptName = basename($_SERVER['SCRIPT_NAME']);
			$requestUri = ltrim($_SERVER['REQUEST_URI'], '/');
			$requestUri = preg_replace('@^'.$scriptPath.'@', '', $requestUri);
			$requestUri = preg_replace('@^'.$scriptName.'@', '', $requestUri);
			$requestUri = ltrim($requestUri, '/');
			list($requestUri) = explode('?', $requestUri);
		}
		
		//$requestUri = urldecode($requestUri);
		
		return $requestUri;
	}
	
	/**
	 * Edito::setQueryString()
	 * 
	 * @param $args
	 * @return 
	 **/
	public function setQueryString($args)
	{
		foreach($args as $key => $val)
		{
			if(intval($key) !== $key && $val != '')
			{
				$_GET[$key] = urldecode($val);
				$_REQUEST[$key] = urldecode($val);
			}
		}
		
		$separator = '';
		$_SERVER['QUERY_STRING'] = '';
		foreach($_GET as $key => $val)
		{
            $_SERVER['QUERY_STRING'] .= $separator.$key.'='.(is_array($val) ? $val[array_key_first($val)] : $val);
			$separator = '&'; 
		}
	}
	
	/**
	 * Edito::Run()
	 * 
	 * @return 
	 **/
	public function Run()
	{
		global $_cfg;

		foreach($this->providers as $provider => $rule)
		{
			if(preg_match($rule, $this->requestUri(), $matches))
			{
				$this->setQueryString($matches);
				require_once 'providers/'.$provider.'.php';
				$object = new $provider($this);

				break;
			}
		}
	}
	
	/**
	 * Edito::__get()
	 * 
	 * @return 
	 **/	
	public function __get($field)
	{
		$field = mb_strtolower($field);
		
		if(isset($this->{$field}))
			return $this->{$field};
		
		throw new Exception('Field "'.$field.'" does not exists.');
	}
		
	/**
	 * Edito::__set()
	 * 
	 * @return 
	 **/	
	public function __set($field, $value)
	{
		$field = mb_strtolower($field);
		
		if(isset($this->{$field}))
			return $this->{$field} = $value;
		
		throw new Exception('Field "'.$field.'" does not exists.');
	}	
	
	/**
	 * Edito::__isset()
	 * 
	 * @return 
	 **/
	public function __isset($field)
	{
		$field = mb_strtolower($field);
		
		return isset($this->{$field});
	} 
}

// Skrot do obiektu Edito
function Edito()
{
	static $edito;
	
	if(!isset($edito))
	{
		$edito = new Edito();
	}
	
	return $edito;
}

Edito()->Run();

// zerwanie polaczenia z baza danych
db()->disconnect();

/*
try
{
	Edito()->Run();
}
catch($e)
{
	//EditoError::handleException($ex);
}
*/
?>