Просмотр файла libraries/route.php

Размер файла: 4.74Kb
<?php
/**
 * Ant0ha's project
 *
 * @package
 * @author Anton Pisarenko <[email protected]>
 * @copyright Copyright (c) 2006 - 2010, Anton Pisarenko
 * @license http://ant0ha.ru/license.txt
 * @link http://ant0ha.ru
 */

defined('IN_SYSTEM') or die('<b>403<br />Запрет доступа</b>');

//------------------------------------------------------
/**
* Route class
*/
class Route {
	/**
	* Путь к контроллеру
	*/
	public $controller_path = NULL;
	/**
	* Название контроллера
	*/
	public $controller_name = NULL;
	/**
	* Получаемый action
	*/
	public $action = NULL;
	/**
	* Флаг существования контроллера
	*/
	public $controller_exists = TRUE;
	/**
	* Модуль
	*/
	public $module;
	/**
	* Сегменты
	*/
	public $segment1;
	public $segment2;
	public $segment3;

	/**
	* Конструктор
	*/
	public function __construct() {
		$this->route();
	}

	/**
    * Функция определяет контроллер и action
    */
    public function route() {
        # Определение сегментов
        if($this->_check_segments()) {
			# Если нет сегментов, подключаем модуль по умолчанию
			if(empty($this->segment1)) {
				if(file_exists(ROOT .'modules/'. DEFAULT_MODULE .'/controllers/'. DEFAULT_MODULE .'.php')) {
					$this->controller_path = 'modules/'. DEFAULT_MODULE .'/controllers/'. DEFAULT_MODULE .'.php';
					$this->controller_name = DEFAULT_MODULE;
					$this->action = '';
					$this->module = DEFAULT_MODULE;
				}
				else $this->controller_exists = FALSE;
			}
            # Если указаны все 3 сегмента
            if(!empty($this->segment1) && !empty($this->segment2) && !empty($this->segment3)) {
				if(file_exists(ROOT .'modules/'. $this->segment1 .'/controllers/'. $this->segment1 .'_'. $this->segment2 .'.php')) {
					$this->controller_path = 'modules/'. $this->segment1 .'/controllers/'. $this->segment1 .'_'. $this->segment2 .'.php';
					$this->controller_name = $this->segment1 .'_'. $this->segment2;
					$this->action = $this->segment3;
					$this->module = $this->segment1;
				}
				else $this->controller_exists = FALSE;
            }
            # Если указаны 2 сегмента
            elseif(!empty($this->segment1) && !empty($this->segment2)) {
				if(file_exists(ROOT .'modules/'. $this->segment1 .'/controllers/'. $this->segment1 .'_'. $this->segment2 .'.php')) {
					$this->controller_path = 'modules/'. $this->segment1 .'/controllers/'. $this->segment1 .'_'. $this->segment2 .'.php';
					$this->controller_name = $this->segment1 .'_'. $this->segment2;
					$this->action = '';
					$this->module = $this->segment1;
				}
				elseif(file_exists(ROOT .'modules/'. $this->segment1 .'/controllers/'. $this->segment1 .'.php')) {
					$this->controller_path = 'modules/'. $this->segment1 .'/controllers/'. $this->segment1 .'.php';
					$this->controller_name = $this->segment1;
					$this->action = $this->segment2;
					$this->module = $this->segment1;
				}
				else $this->controller_exists = FALSE;
            }
            # Если указан только 1 сегмент
            elseif(!empty($this->segment1)) {
				if(file_exists(ROOT . 'modules/'. $this->segment1 .'/controllers/'. $this->segment1 .'.php')) {
					$this->controller_path = 'modules/'. $this->segment1 .'/controllers/'. $this->segment1 .'.php';
					$this->controller_name = $this->segment1;
					$this->action = '';
					$this->module = $this->segment1;
				}
				else $this->controller_exists = FALSE;
            }

			if($this->controller_exists) {
	          	define('ROUTE_CONTROLLER_PATH', $this->controller_path);
	          	define('ROUTE_CONTROLLER_NAME', $this->controller_name);
	          	define('ROUTE_ACTION', $this->action);
	          	define('ROUTE_MODULE', $this->module);
			}
			else a_error('Контроллер не найден!');
        }
    }

    /**
    * Проверка правильности сегментов
    */
    protected function _check_segments() {
		$check_segments = TRUE;
        if(!empty($_GET['segment1'])) {
                if(preg_match('~^[0-9a-z_-]*$~', $_GET['segment1'])) $this->segment1 = $_GET['segment1'];
                else $check_segments = FALSE;
        }
		if(!empty($_GET['segment2'])) {
                if(preg_match('~^[0-9a-z_-]*$~', $_GET['segment2'])) $this->segment2 = $_GET['segment2'];
                else $check_segments = FALSE;
        }
		if(!empty($_GET['segment3'])) {
                if(preg_match('~^[0-9a-z_-]*$~', $_GET['segment3'])) $this->segment3 = $_GET['segment3'];
                else $check_segments = FALSE;
        }
		if(!$check_segments) a_error('Ошибка регистрации сегментов!');
		return TRUE;
    }
}
?>