Advertisement
Guest User

gzhandler.php

a guest
Feb 11th, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.08 KB | None | 0 0
  1. <?php
  2.  
  3. // get config
  4. require 'config.php';
  5.  
  6. // get variables ready
  7. $request = $_SERVER['REQUEST_URI'];
  8.  
  9. $path = parse_url($request, PHP_URL_PATH);
  10.  
  11. // show 404 if anything goes wrong
  12. function show404 () {
  13.     startBuffer();
  14.    
  15.     header('HTTP/1.0 404 Not Found');
  16.     setContentType('html');
  17.    
  18.     readfile(KU_ROOTDIR.'/404.html');
  19.    
  20.     endBuffer();
  21. }
  22.  
  23. // set header type
  24. function setContentType ($type = 'html') {
  25.     $contentTypeStr = 'Content-Type: text/';
  26.    
  27.     switch ($type) {
  28.         case 'css':
  29.             $contentTypeStr .= 'css';
  30.             break;
  31.         case 'js':
  32.             $contentTypeStr .= 'javascript';
  33.             break;
  34.         default:
  35.             $contentTypeStr .= 'html';
  36.     }
  37.    
  38.     header($contentTypeStr);
  39. }
  40.  
  41. // start buffer
  42. function startBuffer () {
  43.     if (/*KU_CUSTOMENABLEGZIP && */!ob_start('ob_gzhandler')) {
  44.         ob_start();
  45.     }
  46. }
  47.  
  48. // end buffer
  49. function endBuffer () {
  50.     ob_end_flush();
  51.     exit();
  52. }
  53.  
  54. // if invalid path
  55. if ($path === false) {
  56.     show404();
  57. }
  58.  
  59. // strip reverse dots and add root to path
  60. $path = KU_ROOTDIR.str_replace('..', '', $path);
  61.  
  62. // change to board index if first page
  63. if (substr($path, -1) == '/') {
  64.     $path .= 'board.html';
  65. }
  66.  
  67. // if file doesn't exists or not the right file type
  68. if (!file_exists($path) || !preg_match('/\.(html|css|js)$/', $path, $match)) {
  69.     show404();
  70. }
  71.  
  72. // reassign type and file mod time
  73. $type = $match[1];
  74. $filetime = filemtime($path);
  75.  
  76. // not modifed
  77. if (
  78.     $type != 'html' &&
  79.     isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
  80.     strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $filetime
  81. ) {
  82.     header('HTTP/1.0 304 Not Modified');
  83.     exit();
  84. }
  85.  
  86. // start gzhandler
  87. startBuffer();
  88.  
  89. // set content type
  90. setContentType($type);
  91.  
  92. // additional headers
  93. if ($type == 'html') {
  94.     header_remove('ETag');
  95.     header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate');
  96.     header('Pragma: no-cache');
  97.     header('Expires: Wed, 11 Jan 1984 05:00:00 GMT');
  98. }
  99. else {
  100.     header('Last-Modified: '.gmdate('D, d M Y H:i:s', $filetime).' GMT');
  101.     header('Expires: '.gmdate('D, d M Y H:i:s', time() + 604800).' GMT');
  102. }
  103.  
  104. // read and exit
  105. readfile($path);
  106. endBuffer();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement