index.php 1.5 KB

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