| Current Path : /home/balossw/www/temp/common/includes/ |
| Current File : /home/balossw/www/temp/common/includes/url-rewriting.inc.php |
<?php
/*---------------------------------------------------------------------------
File name ............ common/includes/url-rewriting.inc.php
Author ............... Antoine Lemoine
Creation date ........ 22.03.2010
Modification date .... 29.06.2010
---------------------------------------------------------------------------*/
class UrlRewritingRule {
var $from;
var $to;
var $childs;
function UrlRewritingRule($from, $to, $childs=null) {
$this->from = $from;
$this->to = $to;
$this->childs = $childs;
}
}
class UrlRewriting {
var $lang;
var $translations_table;
var $reverse_translations_table;
var $redirect_table;
var $url_rewriting_source;
var $current_url_params;
function UrlRewriting() {
global $global;
$this->url_rewriting_source = isset($_GET['url-rewriting-source']) ? $_GET['url-rewriting-source'] : null;
$this->current_url_params = array();
$this->translations_table = array();
$this->reverse_translations_table = array();
$this->redirect_table = array();
}
function redirect($url, $redirect_type=null) {
global $global;
switch ($redirect_type) {
case 301 : header("Status: 301 Moved Permanently", false, 301); break;
default: break;
}
header("Location: ".(substr($url,0,7)=='http://' ? '' : $global->config['document_base_href']).$url);
exit;
}
function checkLanguage() {
global $global;
if (!isset($global->lang) || !in_array($global->lang,$global->config['languages']['availables'])) {
$global->lang = $global->config['languages']['default'];
$global->page = 'erreur-404';
}
}
function _setParameters($parameters) {
global $global;
$pairs = explode('&',$parameters);
$n = count($pairs);
for ($i=0; $i<$n; $i++) {
$p = explode('=',$pairs[$i]);
switch ($p[0]) {
case 'page' :
$global->page = $p[1];
break;
case 'lang' :
$global->lang = $p[1];
break;
default :
$_GET[$p[0]] = $p[1];
}
$this->current_url_params[$p[0]] = $p[1];
}
}
function getCurrentUrlParams() {
$output = array();
foreach ($this->current_url_params as $key => $value) {
$output[] = $key.'='.$value;
}
return implode('&',$output);
}
function parseCurrentUrl() {
global $global;
if (isset($_GET['minified'])) return;
if (!isset($global->config['url_rewriting'])) return;
$global->page = isset($_GET['page']) ? $_GET['page'] : 'erreur-404';
$global->lang = isset($_GET['lang']) ? $_GET['lang'] : null;
if ((!isset($this->url_rewriting_source) || $this->url_rewriting_source=='') && isset($_GET['page'])) {
$this->current_url_params = $_GET;
}
else if (isset($this->url_rewriting_source)) {
$this->current_url_params = array();
if (isset($this->redirect_table['*'.$this->url_rewriting_source])) {
$this->redirect($this->url($this->redirect_table['*'.$this->url_rewriting_source]),301);
}
$url_elements = explode('/',$this->url_rewriting_source);
do {
$part = implode('/',$url_elements);
if (isset($this->reverse_translations_table[$part])) {
foreach ($this->reverse_translations_table[$part] as $parameters) {
$this->_setParameters($parameters);
}
return;
}
array_pop($url_elements);
} while (count($url_elements)>0);
}
}
function addRedirectRule($from, $to) {
$this->redirect_table['*'.$from] = $to;
}
function addRewriteRules() {
$rules = func_get_args();
foreach ($rules as $rule) {
$this->addRewriteRule($rule,false);
}
$this->parseCurrentUrl();
}
function addRewriteRule($rule, $parse_current_url=true) {
$from_keys = array();
$to_keys = array();
$this->_addRewriteRule($rule,$from_keys,$to_keys);
if ($parse_current_url) $this->parseCurrentUrl();
}
function _addRewriteRule($rule, $from_keys, $to_keys) {
$from_keys[] = $rule->from;
$to_keys[] = substr($rule->to,-1,1)=='/' ? substr($rule->to,0,-1) : $rule->to;
$from_key = implode('&',$from_keys);
$to_key = implode('/',$to_keys);
if (!isset($this->translations_table[$from_key])) {
$this->translations_table[$from_key] = $to_key;
$this->reverse_translations_table[$to_key] = $from_keys;
}
if (isset($rule->childs) && is_array($rule->childs)) {
foreach ($rule->childs as $child_rule) {
$this->_addRewriteRule($child_rule,$from_keys,$to_keys);
}
}
}
function _clean($argument) {
return str_replace('&','&',$argument);
}
function url() {
global $global;
$args = func_get_args();
if (count($args)==1 && is_array($args[0])) $args = $args[0];
if ($global->config['languages']['enabled'] && substr($args[0],0,5)!='lang=') {
$args[0] = 'lang='.$global->lang.'&'.$args[0];
}
$anchor = null;
foreach ($args as $index => $arg) {
$args[$index] = $this->_clean($arg);
if ($arg{0}=='#') {
$anchor = substr($arg,1,strlen($arg));
unset($args[$index]);
}
}
$arg1 = array_shift($args);
$parameters = implode('&',$args);
if ($global->config['url_rewriting']) {
$page_name = null;
if (isset($this->translations_table[$arg1])) {
$page_name = $this->translations_table[$arg1];
} else {
$parameters = $arg1.($parameters!='' ? '&'.$parameters : '');
}
return (isset($page_name) ? $page_name : '').($parameters!='' ? '?'.$parameters : '').(isset($anchor) ? '#'.$anchor : '');
} else {
$parameters = $arg1.($parameters!='' ? '&'.$parameters : '');
return '.'.'?'.$parameters.(isset($anchor) ? '#'.$anchor : '');
}
}
function encoded_url() {
$args = func_get_args();
if (count($args)==1 && is_array($args[0])) $args = $args[0];
return str_replace('&','&',$this->url($args));
}
}
?>