/htdocs/inc/router.php

https://github.com/toxik/iHazNews · PHP · 114 lines · 82 code · 12 blank · 20 comment · 25 complexity · 82e1da51a07f08d3275210a9900a42f3 MD5 · raw file

  1. <?php
  2. // if the application doesn't reside in root (nexuspro.info), but in a folder (nexuspro.info/example_app)
  3. $basepath = substr($_SERVER['SCRIPT_NAME'],0,-9);
  4. $request_str = trim(substr($_SERVER['REQUEST_URI'],strlen($basepath)),'/');
  5. // process the request
  6. for ($i = 2, $arr = explode('/', $request_str), $n = count($arr); $i < $n; $i+=2) {
  7. if (!strstr($arr[$i],'[')) {
  8. if ($arr[$i][0] != '?')
  9. $_GET[$arr[$i]] = urldecode($arr[$i+1]);
  10. else unset($_GET[$arr[$i]]);
  11. } else
  12. $_GET[strstr($arr[$i],'[',true)][] = $arr[$i+1];
  13. }
  14. // process the module and action
  15. $module = !empty($arr[0]) ? $arr[0] : DEFAULT_MODULE;
  16. $action = !empty($arr[1]) ? $arr[1] : DEFAULT_ACTION;
  17. // maybe we have some ? in our URL.. let's clean it and do a redirect
  18. define(MODULE, $module);
  19. define(ACTION, $action);
  20. if (strstr($request_str,'?')) {
  21. if (strstr($request_str,'/?') === false) {
  22. $request_str = str_replace('?', '/?', $request_str);
  23. redirect('/'.$request_str);
  24. }
  25. $path = '';
  26. foreach ($_GET as $key => $value)
  27. if (!is_array($value))
  28. $path .= '/' . $key . '/' . urlencode($value);
  29. else foreach ($value as $newKey => $newValue)
  30. $path .= '/' . $key . '[]/' . urlencode($newValue);
  31. $uri = $_SERVER['REQUEST_URI'];
  32. $path = $basepath . $module . '/' . $action . $path;
  33. redirect($path);
  34. exit();
  35. }
  36. // sa purificam input-ul
  37. foreach ($_GET as $id => $val)
  38. if (!is_array($val))
  39. $_GET[$id] = strip_tags($val);
  40. foreach ($_POST as $id => $val)
  41. if (!in_array($id, array('text') ) &&
  42. !is_array($val) )
  43. $_POST[$id] = strip_tags($val);
  44. // initialize the main variable
  45. $page = array();
  46. // verify if the specified module exists
  47. if (!file_exists(APP_PATH.'controllers/'.ucwords($module).'.php')) {
  48. header("HTTP/1.0 404 Not Found");
  49. //header("Location: " . $basepath);
  50. exit('This page does not exist! - controller');
  51. }
  52. //echo benchmark().'se incarca modulul <br />';
  53. // instantiate the module
  54. $moduleName = 'Controller_' . ucfirst($module);
  55. $controller = new $moduleName($page);
  56. // call the specified function
  57. // we make sure the Controller's method exists..
  58. if (!method_exists($moduleName,$action)) {
  59. header("HTTP/1.0 404 Not Found");
  60. header("Location: ".$basepath);
  61. exit('This page does not exist! - action');
  62. }
  63. try {
  64. // afisam erorile de sql daca suntem in development
  65. if ($_SERVER['APPLICATION_ENV'] == 'development') {
  66. try {
  67. $controller->$action();
  68. } catch (Zend_Db_Exception $e) {
  69. exit ('Eroare la baza de date:<br />'."\n".$e->getMessage());
  70. }
  71. } else
  72. $controller->$action();
  73. }
  74. catch (Zend_Exception $e) {
  75. header("HTTP/1.0 404 Not Found");
  76. if ($_SERVER['APPLICATION_ENV'] != 'development') {
  77. header("Location: ".$basepath);
  78. } else {
  79. //echo $e->getMessage();
  80. throw $e;
  81. }
  82. exit('This page does not exist! - load action');
  83. }
  84. unset ($controller); // facem curatenie in RAM
  85. //echo benchmark().'s-a executat metoda <br />';
  86. // start the buffer
  87. if (!file_exists(APP_PATH.'views/'.$module.'/'.$action.'.phtml') && !$page['standalone'])
  88. throw new Exception('Eroare interna la incarcarea view-ului din Controller-ul '
  89. .ucwords($module).' pentru Action-ul '.ucwords($action).'!');
  90. // "prindem" continutul
  91. ob_start();
  92. // include the view
  93. if (!$page['standalone'])
  94. require 'views/'.$module.'/'.$action.'.phtml';
  95. $continut = ob_get_clean();
  96. //echo benchmark().'s-a scris in buffer<br />';
  97. // include the template ( unless a flag is set )
  98. if (!$page['single'] && !$page['standalone'])
  99. require 'template/index.php';
  100. else
  101. if (!$page['standalone'])
  102. echo $continut;