<?php
/*-----------------------------------------------\
| 												 |
|  Author:       Andrey Brykin (Drunya)          |
|  Version:      1.0.0                           |
|  Project:      CMS                             |
|  package       CMS Fapos                       |
|  subpackege    Pather                          |
|  copyright     ©Andrey Brykin 2010-2011        |
\-----------------------------------------------*/

/*-----------------------------------------------\
| 												 |
|  any partial or not partial extension          |
|  CMS Fapos,without the consent of the          |
|  author, is illegal                            |
|------------------------------------------------|
|  Любое распространение                         |
|  CMS Fapos или ее частей,                      |
|  без согласия автора, является не законным     |
\-----------------------------------------------*/




if (file_exists('install')) {
	if (file_exists('sys/settings/config.php')) {
		include_once ('sys/settings/config.php');
		if (!empty($set)) {
			if (!empty($set['db']['name'])) {
				echo 'Before use your site, delete INSTALL dir! <br />Перед использованием удалите папку INSTALL'; die();
			}
		}
	}
	header('Location: install'); die();
}

/**
* include needed files (core)
*/
include_once 'sys/boot.php';

if($_SERVER['REQUEST_URI']=='/')
{
include_once 'sys/inc/modulei.php';
} else {
include_once 'sys/inc/module.php';
}


/**
 * @author      Brykin Andrey
 * @url         http://fapos.net
 * @version     0.9.9
 * @copyright   ©Andrey Brykin
 *
 * Parse url path and get from him requested needed params 
 * (module, action, etc.)
 */
Class Pather {
	
	
	
	function __construct() {
		$redirect = Config::read('redirect');
		if (!empty($redirect)) {
			header('Location: ' . Config::read('redirect') . '');
			die();
		}
		$params = $this->parsePath();
		$data = $this->callAction($params);
	}
	
	
	
	
	function parsePath() {
		$pathParams = array();

		if (!isset($_GET['url'])) {
			if (Config::read('start_mod')) {
				$_GET['url'] = Config::read('start_mod');
				$pathParams = $this->parsePath();
				return $pathParams;
			}
		} else {
			$pathParams = explode('/', $_GET['url']);
			foreach ($pathParams as $key => $value) {
				if (empty($value)) unset($pathParams[$key]);
			}
		}
		
		if (empty($pathParams)) {
			if (Config::read('start_mod')) {
				$_GET['url'] = Config::read('start_mod');
				$pathParams = $this->parsePath();
				return $pathParams;
			}
		} 
		
		//may be i need upgrade this...hz
		if (count($pathParams) == 1 && preg_match('#^\d+$#', $pathParams[0])) {
			$pathParams[2] = (int)$pathParams[0];
			$pathParams[1] = 'index';
			$pathParams[0] = 'pages';
		} else if (count($pathParams) == 0 ) {
			$pathParams[1] = 'index';
			$pathParams[0] = 'pages';
		}
		
		foreach ($pathParams as $key => $val) {
			$pathParams[$key] = trim($val);
		}
		return $pathParams;
	}
	
	
	
	
	function callAction($params) {
		// if we have one argument, we get page if it exists or error
		if (!is_file('modules/' . strtolower($params[0]) . '/index.php')) {
			$mat_id = $this->getHluId($params[0], 'pages');
			if ($mat_id && Config::read('hlu') == 1) {
				$params[2] = $mat_id;
				$params[1] = 'index';
				$params[0] = 'pages';
			} else {
				die("can't find " . h($params[0]) . " Module");
			}
		}
		
		
		include_once 'modules/' . strtolower($params[0]) . '/index.php';
		$module = ucfirst($params[0]) . 'Module';
		if (!class_exists($module))  die("Not found class " . h($module));
		
		if (count($params) == 1) $params[] = 'index';
		$this->module = new $module($params);
		
		
		// Parse two and more arguments
		if (count($params) > 1) {
			// Human Like URL
			if (Config::read('hlu_understanding') || Config::read('hlu')) {
				$mat_id = $this->getHluId($params[1], $params[0]);
				if ($mat_id) {
					$params[2] = $mat_id;
					$params[1] = 'view';
				}
			}
		}
		
		
		// Parse second argument
		if (count($params) > 1) {
			if (preg_match('#^_+#', $params[1])) {
				die('Access to action ' . h($params[1]) . ' is denied');
			}
			if (!method_exists($this->module, $params[1])) {
				die('Action ' . h($params[1]) . ' not found in ' . h($module) . ' Class.');
			}
		}
		

		
		switch (count($params)) {
			case 2:
				$this->module->{$params[1]}();
				break;
			case 3:
				$this->module->{$params[1]}($params[2]);
				break;
			case 4:
				$this->module->{$params[1]}($params[2], $params[3]);
				break;
			case 5:
				$this->module->{$params[1]}($params[2], $params[3], $params[4]);
				break;
			default:
				$this->module->index();
				break;
		}
	}
	
	
	/**
	 * Find relation string->id on Human Like Url
	 *
	 * @param string $string
	 * @param string $module
	 * @return int ID
	 */
	private function getHluId($string, $module) {
		$clean_str = substr($string, 0, strpos($string, '.')); 
		$tmp_dir = R . 'sys/tmp/hlu_' . $module . '/';
		$tmp_file = $tmp_dir . $clean_str . '.dat';
		if (!file_exists($tmp_file) || !is_readable($tmp_file)) return false;
	
		$id = file_get_contents($tmp_file);
		$id = (int)$id;
		return (is_int($id)) ? $id : false;
	}

}


new Pather;



?>
