aboutsummaryrefslogtreecommitdiff
path: root/scripts/caches.php
blob: 1e3b4820a94fbd9625ddf6df964af29a3b87f17f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php if (!defined('PmWiki')) exit();
/*  Copyright 2006-2020 Patrick R. Michaud (pmichaud@pobox.com)
    This file is part of PmWiki; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published
    by the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.  See pmwiki.php for full details.

    Script maintained by Petko YOTOV www.pmwiki.org/petko
*/

## Browser cache-control.  If this is a cacheable action (e.g., browse,
## diff), then set the Last-Modified header to the time the site was 
## last modified.  If the browser has provided us with a matching 
## If-Modified-Since request header, we can return 304 Not Modified.
SDV($LastModFile,"$WorkDir/.lastmod");
if (!$LastModFile) return;

$LastModTime = @filemtime($LastModFile);
foreach(get_included_files() as $f) 
  { $v = @filemtime($f); if ($v > $LastModTime) $LastModTime = $v; }

if (@$EnableIMSCaching) {
  SDV($IMSCookie, $CookiePrefix.'imstime');
  SDV($IMSCookieExpires, $Now + 60*60*24*30);
  SDV($IMSInvalidators, array('authpw', 'author'));
  $LogoutCookies[] = $IMSCookie;

  if ($IMSCookie) {
    $IMSTime = @$_COOKIE[$IMSCookie];
    if ($IMSTime < $LastModTime
        || array_intersect($IMSInvalidators, array_keys($_POST))) {
      $IMSTime = $Now; 
      pmsetcookie($IMSCookie, $IMSTime, $IMSCookieExpires, '/');
    }
  } else $IMSTime = $LastModTime;

  if (in_array($action, (array)$CacheActions)) {
    $HTTPLastMod = gmdate('D, d M Y H:i:s \G\M\T',$IMSTime);
    $HTTPHeaders[] = "Cache-Control: no-cache";
    $HTTPHeaders[] = "Last-Modified: $HTTPLastMod";
    if (@$_SERVER['HTTP_IF_MODIFIED_SINCE']==$HTTPLastMod) {
      header("HTTP/1.0 304 Not Modified");
      header("Cache-Control: no-cache");
      header("Expires: ");
      header("Last-Modified: $HTTPLastMod");
      exit(); 
    }
  }
}

if ($NoHTMLCache 
    || !@$PageCacheDir
    || count($_POST) > 0
    || count($_GET) > 1
    || (count($_GET) == 1 && !@$_GET['n'])) { $NoHTMLCache |= 1; return; }

mkdirp($PageCacheDir);
if (!file_exists("$PageCacheDir/.htaccess") 
    && $fp = @fopen("$PageCacheDir/.htaccess", "w"))
  { fwrite($fp, $DenyHtaccessContent); fclose($fp); }

SDV($PageCacheFileFmt, "%s/%s,cache");
SDV($PageCacheFile, sprintf($PageCacheFileFmt, $PageCacheDir, $pagename));

if (file_exists($PageCacheFile) && @filemtime($PageCacheFile) < $LastModTime) 
  @unlink($PageCacheFile);