caches.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php if (!defined('PmWiki')) exit();
  2. /* Copyright 2006-2020 Patrick R. Michaud (pmichaud@pobox.com)
  3. This file is part of PmWiki; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published
  5. by the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version. See pmwiki.php for full details.
  7. Script maintained by Petko YOTOV www.pmwiki.org/petko
  8. */
  9. ## Browser cache-control. If this is a cacheable action (e.g., browse,
  10. ## diff), then set the Last-Modified header to the time the site was
  11. ## last modified. If the browser has provided us with a matching
  12. ## If-Modified-Since request header, we can return 304 Not Modified.
  13. SDV($LastModFile,"$WorkDir/.lastmod");
  14. if (!$LastModFile) return;
  15. $LastModTime = @filemtime($LastModFile);
  16. foreach(get_included_files() as $f)
  17. { $v = @filemtime($f); if ($v > $LastModTime) $LastModTime = $v; }
  18. if (@$EnableIMSCaching) {
  19. SDV($IMSCookie, $CookiePrefix.'imstime');
  20. SDV($IMSCookieExpires, $Now + 60*60*24*30);
  21. SDV($IMSInvalidators, array('authpw', 'author'));
  22. $LogoutCookies[] = $IMSCookie;
  23. if ($IMSCookie) {
  24. $IMSTime = @$_COOKIE[$IMSCookie];
  25. if ($IMSTime < $LastModTime
  26. || array_intersect($IMSInvalidators, array_keys($_POST))) {
  27. $IMSTime = $Now;
  28. pmsetcookie($IMSCookie, $IMSTime, $IMSCookieExpires, '/');
  29. }
  30. } else $IMSTime = $LastModTime;
  31. if (in_array($action, (array)$CacheActions)) {
  32. $HTTPLastMod = gmdate('D, d M Y H:i:s \G\M\T',$IMSTime);
  33. $HTTPHeaders[] = "Cache-Control: no-cache";
  34. $HTTPHeaders[] = "Last-Modified: $HTTPLastMod";
  35. if (@$_SERVER['HTTP_IF_MODIFIED_SINCE']==$HTTPLastMod) {
  36. header("HTTP/1.0 304 Not Modified");
  37. header("Cache-Control: no-cache");
  38. header("Expires: ");
  39. header("Last-Modified: $HTTPLastMod");
  40. exit();
  41. }
  42. }
  43. }
  44. if ($NoHTMLCache
  45. || !@$PageCacheDir
  46. || count($_POST) > 0
  47. || count($_GET) > 1
  48. || (count($_GET) == 1 && !@$_GET['n'])) { $NoHTMLCache |= 1; return; }
  49. mkdirp($PageCacheDir);
  50. if (!file_exists("$PageCacheDir/.htaccess")
  51. && $fp = @fopen("$PageCacheDir/.htaccess", "w"))
  52. { fwrite($fp, $DenyHtaccessContent); fclose($fp); }
  53. SDV($PageCacheFileFmt, "%s/%s,cache");
  54. SDV($PageCacheFile, sprintf($PageCacheFileFmt, $PageCacheDir, $pagename));
  55. if (file_exists($PageCacheFile) && @filemtime($PageCacheFile) < $LastModTime)
  56. @unlink($PageCacheFile);