You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.2 KiB

  1. <?php
  2. class Route extends \BaseController {
  3. public $_routes;
  4. private $_page;
  5. public function __construct() {
  6. $this->_routes = array(
  7. 'about' => array('x'),
  8. 'account' => false,
  9. 'accountnext' => array('s', 'err'),
  10. 'admin' => false,
  11. 'blog' => array('f', 'i'),
  12. 'chat' => array('f'),
  13. 'conf' => false,
  14. 'contact' => array('f'),
  15. 'disconnect' => array('err'),
  16. 'feed' => array('s', 'n'),
  17. 'node' => array('s', 'n', 'i'),
  18. 'group' => array('s', 'n', 'i'),
  19. 'help' => false,
  20. 'infos' => false,
  21. 'login' => array('err'),
  22. 'main' => false,
  23. 'media' => array('f'),
  24. 'news' => array('n'),
  25. 'pods' => false,
  26. 'profile' => false,
  27. 'room' => array('r'),
  28. 'share' => array('url'),
  29. 'visio' => false
  30. );
  31. }
  32. public function find() {
  33. $this->fix($_GET, $_SERVER['QUERY_STRING']);
  34. $uri = reset(array_keys($_GET));
  35. unset($_GET[$uri]);
  36. $request = explode('/', $uri);
  37. $this->_page = array_shift($request);
  38. if(isset($this->_routes[$this->_page]))
  39. $route = $this->_routes[$this->_page];
  40. if(count($request) && isset($route)) {
  41. $i = 0;
  42. foreach($route as $key) {
  43. if (isset($request[$i])) {
  44. $_GET[$key] = $request[$i];
  45. }
  46. $i++;
  47. }
  48. }
  49. if(empty($this->_page))
  50. $this->_page = 'main';
  51. if(!isset($this->_routes[$this->_page]))
  52. $this->_page = 'notfound';
  53. return $this->_page;
  54. }
  55. public static function urlize($page, $params = false, $tab = false) {
  56. $r = new Route();
  57. $routes = $r->_routes;
  58. if($page === 'root')
  59. return BASE_URI;
  60. if(isset($routes[$page])) {
  61. if($tab != false)
  62. $tab = '#'.$tab;
  63. //We construct a classic URL if the rewriting is disabled
  64. else {
  65. $uri = BASE_URI . '?'. $page;
  66. }
  67. if($params != false && is_array($params)) {
  68. foreach($params as $value) {
  69. $uri .= '/' . $value ;
  70. }
  71. } elseif($params != false) {
  72. $uri .= '/' . $params;
  73. }
  74. return $uri.$tab;
  75. } else {
  76. throw new Exception(__('Route not set for the page %s', $page));
  77. }
  78. }
  79. private function fix(&$target, $source, $discard = true) {
  80. if ($discard)
  81. $target = array();
  82. $source = preg_replace_callback(
  83. '/(^|(?<=&))[^=[&]+/',
  84. function($key) { return bin2hex(urldecode($key[0])); },
  85. $source
  86. );
  87. parse_str($source, $post);
  88. foreach($post as $key => $val)
  89. $target[ hex2bin($key) ] = $val;
  90. }
  91. }