<?php
/*
+--------------------------------------------------------------------------
|   TanyaCMS - Question and Answer Sofware
|   -----------------------------------------------------------------------
|   by fbschool dev team
|   (c) 2011 - 2012 FBSCHOOL
|   http://www.fbschool.com/tanyacms
|   -----------------------------------------------------------------------
|   This is NOT a freeware, use is subject to license terms
|   Support: 13455n@gmail.com
+--------------------------------------------------------------------------
*/

class core_uri
{
	var $params = array(
		'suffix' => '.html', 
		'sep_value' => '-',  //Assign value separator
		'sep_var' => '__',  //Variable delimiters
		'sep_act' => '/' //Act breaks
	);
	
	//The default controller
	var $default = array(
		'app_dir' => 'home', 
		'controller' => 'main', 
		'action' => 'index'
	);
	
	var $app_dir = '';
	var $request_main = '';
	var $index_script = '';

	public function __construct()
	{
		if (!defined('GET_INDEX_SCRIPT'))
		{
			return false;
		}
		
		if (GET_INDEX_SCRIPT == '')
		{
			$this->index_script = '?/';
		}
		else
		{
			$this->index_script = GET_INDEX_SCRIPT;
		}
		
		if($_SERVER['REQUEST_URI'])
		{
			if($_SERVER['HTTP_X_REWRITE_URL'])
			{
				$request_main = $_SERVER['HTTP_X_REWRITE_URL'];
			}
			else
			{
				$request_main = $_SERVER['REQUEST_URI'];
			}
			
			$requests = explode($this->index_script, $request_main);
			
			if (count($requests) == 1 AND dirname($_SERVER['SCRIPT_NAME']) != '/')
			{
				$request_main = preg_replace('/^' . preg_quote(dirname($_SERVER['SCRIPT_NAME']), '/') . '/i', '', $request_main);
			}
			else if (count($requests) == 2)
			{
				if ($requests[0] != '/')
				{
					$request_main = str_replace($requests[0], '', $request_main);
				}
			
				$request_main = str_replace($this->index_script, '', $request_main);
			}
		}
		else if($_SERVER['QUERY_STRING'])
		{
			$request_main = $_SERVER['QUERY_STRING'];
		}
		
		$request_main = ltrim($request_main, "/\\");
		
		$base_script = basename($_SERVER['SCRIPT_NAME']);
		
		if (strstr($base_script, '.php'))
		{
			$request_main = str_replace($base_script . '/', '', $request_main);
		}
		
		if(count($requests) == 1)
		{
			$request_main = $this->parse_uri($request_main);
		}
		
		$this->request_main = $request_main;
	}
	
	public function parse_uri($request_main)
	{
		if ((get_setting('url_rewrite_enable') == 'Y') && ($request_routes = get_request_route(false)))
		{
			if(!$request_main)
			{
				$request_main = '/';
			}
			
			foreach($request_routes as $key => $val)
			{
				if(preg_match('/^' . $val[0] . '/', $request_main))
				{
					$request_main = preg_replace('/^' . $val[0] . '/', $val[1], $request_main);
					return $request_main;
				}
			}
		}

		return $request_main; 
	}
	
	public function set_rewrite()
	{
		global $__app_dir, $__controller, $__action;
		
		if (!defined('GET_INDEX_SCRIPT'))
		{
			return false;
		}
		
		$request_main = $this->request_main;
		
		if (empty($request_main) or $this->index_script == $request_main)
		{
			$__controller = 'main';
			$__action = 'index';
			
			return $this;
		}
		
		$request = explode($this->params['suffix'], $request_main, 2);	
		
 		if (count($request) == 1)
  		{
  			$request = explode('?', $request_main, 2);
  			
  			if (count($request) == 1)
  			{
  				$request = explode('&', $request_main, 2);
  			}
 		}
 		
		$uri = array(
			'first' => array_shift($request), 
			'last' => ltrim(implode($request), '?')
		);
		
		if ($uri['last'])
		{			
			parse_str(str_replace(array($this->params['sep_value'], $this->params['sep_var']), array('=', '&'), $uri['last']), $query_string);
			
			foreach ($query_string AS $key => $val)
			{
				if (!$_GET[$key])
				{
					$_GET[$key] = $val;
				}
			}
		}
		
		$request = explode($this->params['sep_act'], $uri['first']);
		
		$uri['first'] = array(
			'pattern' => "", 
			'args' => $request
		);
		
		$__app_dir = $this->default['app_dir']; //Application directory
		$__controller = $this->default['controller']; //Controller
		$__action = $this->default['action']; //Action
		
		$args_var_str = '';
		
		// Remove empty values
		foreach ($uri['first']['args'] AS $key => $val)
		{
			if (strstr($val, $this->params['sep_value']) AND !$start_key)
			{
				$start_key = $key;
			}
			else if ($start_key)
			{
				$uri['first']['args'][$start_key] .= $this->params['sep_act'] . $val;
				
				unset($uri['first']['args'][$key]);
			}
		}
				
		$args_count = count($uri['first']['args']);
		
		switch ($args_count)
		{
			default:
				return $this;
			break;
			
			case 1:
				$args_var_str = $uri['first']['args'][0];
			break;
			
			case 2:
				$__app_dir = $uri['first']['args'][0] ? $uri['first']['args'][0] : $this->default['app_dir']; //Application directory
				$args_var_str = $uri['first']['args'][1];
			break;
			
			case 3:
				$args_var_str = $uri['first']['args'][2];
				$__app_dir = $uri['first']['args'][0] ? $uri['first']['args'][0] : $this->default['app_dir']; //Application directory
				
				if (file_exists(ROOT_PATH . 'app/' . $__app_dir . '/' . $uri['first']['args'][1] . '.php'))
				{
					$__controller = $uri['first']['args'][1]; //Controller
				}
				else
				{
					$__controller = $this->default['controller']; //Controller
					$__action = $uri['first']['args'][1]; //Action
				}
			break;
			
			case 4:
				$args_var_str = $uri['first']['args'][3];
				$__app_dir = $uri['first']['args'][0] ? $uri['first']['args'][0] : $this->default['app_dir']; //Application directory
				$__controller = $uri['first']['args'][1] ? $uri['first']['args'][1] : $this->default['controller']; //Controller
				$__action = $uri['first']['args'][2] ? $uri['first']['args'][2] : $this->default['action']; //Action
			break;
		}
		
		$this->app_dir = ROOT_PATH . 'app/' . $__app_dir . '/';
		
		$_GET['c'] = $__controller;
		$_GET['act'] = $__action;
		$_GET['app'] = $__app_dir;
		
		if (! empty($args_var_str))
		{
			if (substr($args_var_str, 0, strlen($this->params['sep_var'])) == $this->params['sep_var'])
			{
				$args_var_str = substr($args_var_str, strlen($this->params['sep_var']));
			}
			 
			if (!strstr($args_var_str,'-'))
			{	
				$_GET['id'] = urldecode($args_var_str);
			}
			
			$uri['last'] = explode($this->params['sep_var'], $args_var_str);

			foreach ($uri['last'] as $val)
			{
				@list($k, $v) = explode($this->params['sep_value'], $val, 2);
				
				if ($k)
				{
					if (! strstr($v, '%'))
					{
						$_GET[$k] = $v;
					}
					else
					{
						$_GET[$k] = urldecode($v);
					}
				}
			}
		}
		
		foreach ($_GET AS $key => $val)
		{
			if (strstr($key, '/'))
			{
				unset($_GET[$key]);
			}
		}
				
		return $this;
	}
}