index.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. function d() {
  3. }
  4. function error($code = 403) {
  5. if($code == 404) {
  6. header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
  7. header("Status: 404 Not Found");
  8. $_SERVER['REDIRECT_STATUS'] = 404;
  9. return $GLOBALS['basedir'] . 'pages/errors/404.php';
  10. } else {
  11. header($_SERVER["SERVER_PROTOCOL"]." 403 Forbidden");
  12. header("Status: 403 Forbidden");
  13. $_SERVER['REDIRECT_STATUS'] = 403;
  14. return $GLOBALS['basedir'] . 'pages/errors/403.php';
  15. }
  16. }
  17. function getRequestPage($request = '') {
  18. if($request == '' or $request == '/') {
  19. return array(array(), array());
  20. }
  21. $rArray = explode('/', $request);
  22. $args = array();
  23. while(!file_exists($file = 'pages/' . implode('/', $rArray) . '.php') and $rArray != NULL) {
  24. array_unshift($args, array_pop($rArray));
  25. }
  26. return array($rArray, $args);
  27. }
  28. include 'libs/utils.php';
  29. $baseurl = trim(full_url($_SERVER), '/') . '/';
  30. $basedir = dirname($_SERVER['SCRIPT_FILENAME']) . '/';
  31. $dirty_req = preg_replace('/(\?.*)/', '', $_SERVER['REQUEST_URI']);
  32. $dn = dirname($_SERVER['SCRIPT_NAME']);
  33. $dirty_req = substr($dirty_req, strlen($dn));
  34. $dirty_req = trim($dirty_req, '/');
  35. if(strlen($dirty_req) > 0) {
  36. $baseurl = substr(trim(preg_replace('/(\?.*)/', '', $baseurl), '/'), 0, -strlen($dirty_req));
  37. } else {
  38. $baseurl = preg_replace('/(\?.*)/', '', $baseurl);
  39. }
  40. if(strlen($dirty_req) > 0) {
  41. if(substr($_SERVER['REQUEST_URI'], -1) === '/') {
  42. $redirect_url = trim(full_url($_SERVER), '/');
  43. header("HTTP/1.1 301 Moved Permanently");
  44. header("Location: $redirect_url");
  45. die;
  46. }
  47. }
  48. $request = $dirty_req;
  49. ob_start();
  50. $r = getRequestPage($request);
  51. $GLOBALS['args'] = $r[1];
  52. if(empty($r[0])) {
  53. if(empty($r[1])) {
  54. include $basedir . 'pages/home.php';
  55. } else {
  56. include error(404);
  57. }
  58. } else {
  59. include $basedir.'pages/'.implode('/', $r[0]).'.php';
  60. }
  61. $html = ob_get_clean();
  62. echo $html;