<?php
/*
* @package LabCMS
* @copyright (c) 2006 Alexander Levin aka Loki (loki_angel@mail.ru)
* @support http://labcms.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
include_once LAB_PATH_KERNEL.'/classes/Url.php';

class Url_Flat implements Url
{
	static function parse()
	{
		$init_path=@parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
		if (!$init_path) {
			header('Location:/');
			exit;
		}
		
		$path_info=pathinfo($init_path);
		if ($path_info['dirname']=='\\') $path_info['dirname']='';
		if ($path_info['extension']) {
			$_SERVER['PHP_SELF']=$_REQUEST['url']
			=$_GET['url']=$path_info['dirname']
			.'/'.basename($path_info['basename'], '.'.$path_info['extension']);
			$_REQUEST['data_type']=$_GET['data_type']=$path_info['extension'];
		} else {
			$_SERVER['PHP_SELF']=$_REQUEST['url']=$_GET['url']=($init_path!='/')?rtrim($init_path, '/'):'/';
		}
		
		if ($_GET['url']=='/index') {
			$_SERVER['PHP_SELF']=$_REQUEST['url']=$_GET['url']='/';
		}
		
		if ($_REQUEST['redirect']) {
			if (!User::checkToken($_REQUEST['token'], false)) {
				unset($_GET['redirect'], $_POST['redirect'], $_REQUEST['redirect']);
			}
		}
	}
		
	static function create($params, $action, $type, $href, $redirect, $token)
	{
		if (!is_array($params) && $params) $params=array($params);
		if (!$href) $href=$_SERVER['PHP_SELF'];
		$url=array();
		if ($action) $url["action"]=$action;
		if (count($params) && is_array($params)) {
			foreach ($params as $key=>$val) {
				if (is_int($key)) $key='var'.($key+1);
				$url[$key]=$val;
			}
		}
		if($redirect) {
			$params['redirect']=$redirect;
			if (!$token) $token=User::i()->token;
		}
		if ($token) $url['token']=$token;
		
		if ($href!="/") {
			if (!$type) $href.=LabConfig::$cfg['ext'];
			else $href.=".".$type;
		}

		if (count($url)) {
			$href=$href.'?'.http_build_query($url);
		}
		
		return $href;
	}
	
	static function filter($url)
	{
		return convert_url($url);
	}
}

