2015-08-10 23:42:45 +02:00
|
|
|
<?php
|
2015-08-03 11:35:01 +02:00
|
|
|
class Route extends \BaseController {
|
|
|
|
public $_routes;
|
|
|
|
private $_page;
|
2015-08-10 23:42:45 +02:00
|
|
|
|
2015-08-03 11:35:01 +02:00
|
|
|
public function __construct() {
|
|
|
|
$this->_routes = array(
|
|
|
|
'about' => array('x'),
|
|
|
|
'account' => false,
|
|
|
|
'accountnext' => array('s', 'err'),
|
|
|
|
'admin' => false,
|
|
|
|
'blog' => array('f', 'i'),
|
2015-12-15 11:54:49 +01:00
|
|
|
'tag' => array('t', 'i'),
|
2015-08-03 11:35:01 +02:00
|
|
|
'chat' => array('f'),
|
|
|
|
'conf' => false,
|
|
|
|
'contact' => array('f'),
|
|
|
|
'disconnect' => array('err'),
|
2015-09-22 08:18:52 +02:00
|
|
|
'feed' => array('s', 'n'),
|
2015-12-15 11:54:49 +01:00
|
|
|
'node' => array('s', 'n', 'i'),
|
2015-08-19 11:28:28 +02:00
|
|
|
'group' => array('s', 'n', 'i'),
|
2015-08-03 11:35:01 +02:00
|
|
|
'help' => false,
|
|
|
|
'infos' => false,
|
|
|
|
'login' => array('err'),
|
|
|
|
'main' => false,
|
|
|
|
'media' => array('f'),
|
|
|
|
'news' => array('n'),
|
|
|
|
'pods' => false,
|
|
|
|
'profile' => false,
|
|
|
|
'room' => array('r'),
|
|
|
|
'share' => array('url'),
|
|
|
|
'visio' => false
|
|
|
|
);
|
|
|
|
}
|
2015-08-10 23:42:45 +02:00
|
|
|
|
2015-08-03 11:35:01 +02:00
|
|
|
public function find() {
|
2015-11-13 13:37:39 +01:00
|
|
|
$this->fix($_GET, $_SERVER['QUERY_STRING']);
|
|
|
|
|
2015-12-15 11:54:49 +01:00
|
|
|
$gets = array_keys($_GET);
|
|
|
|
$uri = reset($gets);
|
2015-11-13 13:37:39 +01:00
|
|
|
unset($_GET[$uri]);
|
|
|
|
$request = explode('/', $uri);
|
|
|
|
|
|
|
|
$this->_page = array_shift($request);
|
|
|
|
|
|
|
|
if(isset($this->_routes[$this->_page]))
|
|
|
|
$route = $this->_routes[$this->_page];
|
|
|
|
|
|
|
|
if(count($request) && isset($route)) {
|
|
|
|
$i = 0;
|
|
|
|
foreach($route as $key) {
|
|
|
|
if (isset($request[$i])) {
|
|
|
|
$_GET[$key] = $request[$i];
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
2015-11-13 13:37:39 +01:00
|
|
|
$i++;
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(empty($this->_page))
|
|
|
|
$this->_page = 'main';
|
|
|
|
|
|
|
|
if(!isset($this->_routes[$this->_page]))
|
|
|
|
$this->_page = 'notfound';
|
|
|
|
|
|
|
|
return $this->_page;
|
|
|
|
}
|
2015-08-10 23:42:45 +02:00
|
|
|
|
2015-08-03 11:35:01 +02:00
|
|
|
public static function urlize($page, $params = false, $tab = false) {
|
|
|
|
$r = new Route();
|
|
|
|
$routes = $r->_routes;
|
|
|
|
|
|
|
|
if($page === 'root')
|
|
|
|
return BASE_URI;
|
2015-08-10 23:42:45 +02:00
|
|
|
|
|
|
|
if(isset($routes[$page])) {
|
2015-11-13 13:37:39 +01:00
|
|
|
if($tab != false)
|
|
|
|
$tab = '#'.$tab;
|
|
|
|
|
|
|
|
//We construct a classic URL if the rewriting is disabled
|
|
|
|
else {
|
|
|
|
$uri = BASE_URI . '?'. $page;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($params != false && is_array($params)) {
|
|
|
|
foreach($params as $value) {
|
|
|
|
$uri .= '/' . $value ;
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|
2015-11-13 13:37:39 +01:00
|
|
|
} elseif($params != false) {
|
|
|
|
$uri .= '/' . $params;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $uri.$tab;
|
2015-08-03 11:35:01 +02:00
|
|
|
} else {
|
|
|
|
throw new Exception(__('Route not set for the page %s', $page));
|
|
|
|
}
|
|
|
|
}
|
2015-11-13 13:37:39 +01:00
|
|
|
|
|
|
|
private function fix(&$target, $source, $discard = true) {
|
|
|
|
if ($discard)
|
|
|
|
$target = array();
|
|
|
|
|
|
|
|
$source = preg_replace_callback(
|
|
|
|
'/(^|(?<=&))[^=[&]+/',
|
|
|
|
function($key) { return bin2hex(urldecode($key[0])); },
|
|
|
|
$source
|
|
|
|
);
|
|
|
|
|
|
|
|
parse_str($source, $post);
|
|
|
|
foreach($post as $key => $val)
|
|
|
|
$target[ hex2bin($key) ] = $val;
|
|
|
|
}
|
2015-08-03 11:35:01 +02:00
|
|
|
}
|