lib.cache.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. # ***** BEGIN LICENSE BLOCK *****
  3. # This file is part of DotClear.
  4. # Copyright (c) 2004 Olivier Meunier and contributors. All rights
  5. # reserved.
  6. #
  7. # DotClear is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # DotClear is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with DotClear; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. #
  21. # ***** END LICENSE BLOCK *****
  22. class cache
  23. {
  24. # Envoyer les en-têtes de cache HTTP en fonction d'une liste de fichiers
  25. function http($files,$mod_ts=array())
  26. {
  27. if (empty($files) || !is_array($files)) {
  28. return false;
  29. }
  30. array_walk($files,create_function('&$v','$v = filemtime($v);'));
  31. $array_ts = array_merge($mod_ts,$files);
  32. rsort($array_ts);
  33. $ts = $array_ts[0];
  34. $since = NULL;
  35. if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
  36. $since = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
  37. $since = preg_replace ('/^(.*)(Mon|Tue|Wed|Thu|Fri|Sat|Sun)(.*)(GMT)(.*)/', '$2$3 GMT', $since);
  38. $since = strtotime($since);
  39. }
  40. $status_mode = preg_match('/cgi/',php_sapi_name());
  41. # Listes des en-têtes communs
  42. $headers[] = 'Last-Modified: '.gmdate('D, d M Y H:i:s',$ts).' GMT';
  43. $headers[] = 'Cache-Control: must-revalidate, max-age=0';
  44. $headers[] = 'Pragma:';
  45. if ($since >= $ts)
  46. {
  47. if ($status_mode) {
  48. header("Status: 304 Not Modified");
  49. } else {
  50. if (version_compare(phpversion(),'4.3.0','>=')) {
  51. header('Not Modified', TRUE, 304);
  52. } else {
  53. header('HTTP/1.x 304 Not Modified');
  54. }
  55. }
  56. foreach ($headers as $v) {
  57. header($v);
  58. }
  59. exit;
  60. }
  61. else
  62. {
  63. header('Date: '.gmdate('D, d M Y H:i:s').' GMT');
  64. foreach ($headers as $v) {
  65. header($v);
  66. }
  67. }
  68. }
  69. # écriture d'un fichier de cache
  70. function writeFile($file,$content)
  71. {
  72. $w = false;
  73. if (file_exists($file)) {
  74. $w = is_writable($file);
  75. } else {
  76. $w = is_writable(dirname($file));
  77. }
  78. if (!$w) {
  79. return false;
  80. }
  81. if (($fp = @fopen($file,'w')) !== false) {
  82. fwrite($fp,$content,strlen($content));
  83. fclose($fp);
  84. }
  85. }
  86. # Récupération d'un fichier en fonction d'une liste de fichiers
  87. function getFile($file,$files)
  88. {
  89. if (empty($files) || !is_array($files) || !file_exists($file)) {
  90. return false;
  91. }
  92. array_walk($files,create_function('&$v','$v = filemtime($v);'));
  93. rsort($files);
  94. $ts = $files[0];
  95. $ftime = filemtime($file);
  96. if ($ts <= $ftime) {
  97. define('DC_CACHE_CONTENT',implode('',file($file)));
  98. }
  99. }
  100. }
  101. ?>