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.

126 lines
3.8 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. 'grouppublic' => 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. $cd = new \Modl\ConfigDAO();
  35. $config = $cd->get();
  36. if($config->rewrite == true
  37. && isset($_SERVER['HTTP_MOD_REWRITE'])
  38. && $_SERVER['HTTP_MOD_REWRITE']) {
  39. $request = explode('/', $this->fetchGet('query'));
  40. $this->_page = $request[0];
  41. array_shift($request);
  42. } else {
  43. $uri = reset(array_keys($_GET));
  44. unset($_GET[$uri]);
  45. $request = explode('/', $uri);
  46. $this->_page = array_shift($request);
  47. }
  48. if(isset($this->_routes[$this->_page]))
  49. $route = $this->_routes[$this->_page];
  50. if(count($request) && isset($route)) {
  51. $i = 0;
  52. foreach($route as $key) {
  53. if (isset($request[$i])) {
  54. $_GET[$key] = $request[$i];
  55. }
  56. $i++;
  57. }
  58. }
  59. if(empty($this->_page))
  60. $this->_page = 'main';
  61. if(!isset($this->_routes[$this->_page]))
  62. $this->_page = 'notfound';
  63. return $this->_page;
  64. }
  65. public static function urlize($page, $params = false, $tab = false) {
  66. $r = new Route();
  67. $routes = $r->_routes;
  68. $cd = new \Modl\ConfigDAO();
  69. $config = $cd->get();
  70. if($page === 'root')
  71. return BASE_URI;
  72. if(isset($routes[$page])) {
  73. if($tab != false)
  74. $tab = '#'.$tab;
  75. // Here we got a beautiful rewriten URL !
  76. if($config->rewrite == true) {
  77. $uri = BASE_URI . $page;
  78. }
  79. //We construct a classic URL if the rewriting is disabled
  80. else {
  81. $uri = BASE_URI . '?'. $page;
  82. }
  83. if($params != false && is_array($params)) {
  84. foreach($params as $value) {
  85. $uri .= '/' . $value ;
  86. }
  87. } elseif($params != false) {
  88. $uri .= '/' . $params;
  89. }
  90. return $uri.$tab;
  91. } else {
  92. throw new Exception(__('Route not set for the page %s', $page));
  93. }
  94. }
  95. private function fix(&$target, $source, $discard = true) {
  96. if ($discard)
  97. $target = array();
  98. $source = preg_replace_callback(
  99. '/(^|(?<=&))[^=[&]+/',
  100. function($key) { return bin2hex(urldecode($key[0])); },
  101. $source
  102. );
  103. parse_str($source, $post);
  104. foreach($post as $key => $val)
  105. $target[ hex2bin($key) ] = $val;
  106. }
  107. }