summaryrefslogtreecommitdiff
path: root/inc/functions.php
diff options
context:
space:
mode:
authorSavetheinternet <savetheinternet@tinyboard.org>2011-05-22 01:12:57 +1000
committerSavetheinternet <savetheinternet@tinyboard.org>2011-05-22 01:12:57 +1000
commit0137b9646469ef8b4d8d38733da8d48b9d405164 (patch)
tree68d173cc55437a0efa4a94d11bed9db543688346 /inc/functions.php
parent24932a15bfca91e7a711d55554a811a32efd4113 (diff)
PURGE for caching reverse-proxies like Squid or Varnish
Diffstat (limited to 'inc/functions.php')
-rw-r--r--inc/functions.php28
1 files changed, 28 insertions, 0 deletions
diff --git a/inc/functions.php b/inc/functions.php
index 06674749..ec49dfbf 100644
--- a/inc/functions.php
+++ b/inc/functions.php
@@ -218,6 +218,25 @@
} else return false;
}
+ function purge($uri) {
+ global $config;
+ $uri = (str_replace('\\', '/', dirname($_SERVER['REQUEST_URI'])) == '/' ? '/' : str_replace('\\', '/', dirname($_SERVER['REQUEST_URI'])) . '/') . $uri;
+ $request = "PURGE {$uri} HTTP/1.0\r\nHost: {$_SERVER['HTTP_HOST']}\r\nUser-Agent: Tinyboard\r\nConnection: Close\r\n\r\n";
+
+ foreach($config['purge'] as &$purge) {
+ $host = $purge[0];
+ $port = $purge[1];
+ if($fp = fsockopen($host, $port, $errno, $errstr, $config['purge_timeout'])) {
+ fwrite($fp, $request);
+ fclose($fp);
+ } else {
+ // Cannot connect?
+ error('Could not PURGE for ' . $host);
+ }
+
+ }
+ }
+
function file_write($path, $data) {
global $config;
if(preg_match('/^scp:\/\/(.+)$/', $path, $m)) {
@@ -233,6 +252,15 @@
return;
}
+ if(isset($config['purge']) && isset($_SERVER['HTTP_HOST'])) {
+ // Purge cache
+ if(basename($path) == $config['file_index']) {
+ // Index file (/index.html); purge "/" as well
+ purge(dirname($path) . '/');
+ }
+ purge($path);
+ }
+
if(!$fp = fopen($path, 'c'))
error('Unable to open file for writing: ' . $path);