helper_httpCache.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /** This file is part of KCFinder project
  3. *
  4. * @desc HTTP cache helper class
  5. * @package KCFinder
  6. * @version 3.12
  7. * @author Pavel Tzonkov <sunhater@sunhater.com>
  8. * @copyright 2010-2014 KCFinder Project
  9. * @license http://opensource.org/licenses/GPL-3.0 GPLv3
  10. * @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
  11. * @link http://kcfinder.sunhater.com
  12. */
  13. namespace kcfinder;
  14. class httpCache {
  15. const DEFAULT_TYPE = "text/html";
  16. const DEFAULT_EXPIRE = 604800; // in seconds
  17. /** Cache a file. The $type parameter might define the MIME type of the file
  18. * or path to magic file to autodetect the MIME type. If you skip $type
  19. * parameter the method will try with the default magic file. Autodetection
  20. * of MIME type requires Fileinfo PHP extension used in file::getMimeType()
  21. * @param string $file
  22. * @param string $type
  23. * @param integer $expire
  24. * @param array $headers */
  25. static function file($file, $type=null, $expire=null, array $headers=null) {
  26. $mtime = @filemtime($file);
  27. if ($mtime !== false) self::checkMTime($mtime);
  28. if ($type === null) {
  29. $magic = ((substr($type, 0, 1) == "/") || preg_match('/^[a-z]\:/i', $type))
  30. ? $type : null;
  31. $type = file::getMimeType($file, $magic);
  32. if (!$type) $type = null;
  33. }
  34. self::content(file_get_contents($file), $mtime, $type, $expire, $headers, false);
  35. }
  36. /** Cache the given $content with $mtime modification time.
  37. * @param binary $content
  38. * @param integer $mtime
  39. * @param string $type
  40. * @param integer $expire
  41. * @param array $headers
  42. * @param bool $checkMTime */
  43. static function content($content, $mtime, $type=null, $expire=null, array $headers=null, $checkMTime=true) {
  44. if ($checkMTime) self::checkMTime($mtime);
  45. if ($type === null) $type = self::DEFAULT_TYPE;
  46. if ($expire === null) $expire = self::DEFAULT_EXPIRE;
  47. $size = strlen($content);
  48. $expires = gmdate("D, d M Y H:i:s", time() + $expire) . " GMT";
  49. header("Content-Type: $type");
  50. header("Expires: $expires");
  51. header("Cache-Control: max-age=$expire");
  52. header("Pragma: !invalid");
  53. header("Content-Length: $size");
  54. if ($headers !== null) foreach ($headers as $header) header($header);
  55. echo $content;
  56. }
  57. /** Check if given modification time is newer than client-side one. If not,
  58. * the method will tell the client to get the content from its own cache.
  59. * Afterwards the script process will be terminated. This feature requires
  60. * the PHP to be configured as Apache module.
  61. * @param integer $mtime
  62. * @param mixed $sendHeaders */
  63. static function checkMTime($mtime, $sendHeaders=null) {
  64. header("Last-Modified: " . gmdate("D, d M Y H:i:s", $mtime) . " GMT");
  65. $headers = function_exists("getallheaders")
  66. ? getallheaders()
  67. : (function_exists("apache_request_headers")
  68. ? apache_request_headers()
  69. : false);
  70. if (is_array($headers) && isset($headers['If-Modified-Since'])) {
  71. $client_mtime = explode(';', $headers['If-Modified-Since']);
  72. $client_mtime = @strtotime($client_mtime[0]);
  73. if ($client_mtime >= $mtime) {
  74. header('HTTP/1.1 304 Not Modified');
  75. if (is_array($sendHeaders) && count($sendHeaders))
  76. foreach ($sendHeaders as $header)
  77. header($header);
  78. elseif ($sendHeaders !== null)
  79. header($sendHeaders);
  80. die;
  81. }
  82. }
  83. }
  84. }