2009-07-13 06:42:48 +02:00
|
|
|
<?php
|
|
|
|
|
2009-07-13 22:28:31 +02:00
|
|
|
/**
|
|
|
|
* This file is designed to be the new 'server' of sites using StaticPublisher.
|
|
|
|
* to use this, you need to modify your .htaccess to point all requests to
|
|
|
|
* static-main.php, rather than main.php. This file also allows for using
|
|
|
|
* static publisher with the subsites module.
|
|
|
|
*
|
|
|
|
* If you are using StaticPublisher+Subsites, set the following in _config.php:
|
|
|
|
* FilesystemPublisher::$domain_based_caching = true;
|
2009-07-14 03:21:04 +02:00
|
|
|
* If you are not using subsites, the host-map.php file will not exist (it is
|
|
|
|
* automatically generated by the Subsites module) and the cache will default
|
|
|
|
* to no subdirectory.
|
2009-07-13 22:28:31 +02:00
|
|
|
*/
|
2009-07-13 06:42:48 +02:00
|
|
|
|
2009-08-03 23:53:30 +02:00
|
|
|
$cacheOn = true;
|
2009-10-15 23:46:36 +02:00
|
|
|
$cacheDebug = false;
|
2009-08-03 23:53:30 +02:00
|
|
|
$hostmapLocation = '../subsites/host-map.php';
|
2010-04-14 05:50:21 +02:00
|
|
|
$homepageMapLocation = '../assets/_homepage-map.php';
|
2009-10-15 23:53:15 +02:00
|
|
|
|
2010-04-12 04:33:08 +02:00
|
|
|
if ($cacheOn && empty($_COOKIE['bypassStaticCache'])) {
|
2010-04-14 05:51:20 +02:00
|
|
|
if (isset($_GET['cacheSubdir']) && !preg_match('/[^a-zA-Z0-9\-_]/', $_GET['cacheSubdir'])) {
|
2010-04-14 06:00:10 +02:00
|
|
|
$cacheDir = $_GET['cacheSubdir'].'/';
|
2010-04-14 05:51:20 +02:00
|
|
|
}
|
|
|
|
else if (file_exists($hostmapLocation)) {
|
2009-08-03 23:53:30 +02:00
|
|
|
include_once $hostmapLocation;
|
|
|
|
$subsiteHostmap['default'] = isset($subsiteHostmap['default']) ? $subsiteHostmap['default'] : '';
|
2010-04-14 05:50:21 +02:00
|
|
|
|
2009-08-03 23:53:30 +02:00
|
|
|
// Look for the host, and find the cache dir
|
|
|
|
$host = str_replace('www.', '', $_SERVER['HTTP_HOST']);
|
|
|
|
$cacheDir = (isset($subsiteHostmap[$host]) ? $subsiteHostmap[$host] : $subsiteHostmap['default']) . '/';
|
|
|
|
} else {
|
|
|
|
$cacheDir = '';
|
|
|
|
}
|
2009-07-13 06:42:48 +02:00
|
|
|
|
2009-08-03 23:53:30 +02:00
|
|
|
// Look for the file in the cachedir
|
2010-04-14 05:50:21 +02:00
|
|
|
$file = trim($_SERVER['REQUEST_URI'], '/');
|
2009-08-03 23:53:30 +02:00
|
|
|
$file = $file ? $file : 'index';
|
2010-04-14 05:50:21 +02:00
|
|
|
|
|
|
|
// Route to the 'correct' index file (if applicable)
|
|
|
|
if ($file == 'index' && file_exists($homepageMapLocation)) {
|
|
|
|
include_once $homepageMapLocation;
|
|
|
|
$file = isset($homepageMap[$_SERVER['HTTP_HOST']]) ? $homepageMap[$_SERVER['HTTP_HOST']] : $file;
|
|
|
|
}
|
2010-10-04 06:32:13 +02:00
|
|
|
|
|
|
|
$file = preg_replace('/[^a-zA-Z0-9\/\-_]/si', '-', $file);
|
2010-04-14 05:50:21 +02:00
|
|
|
|
2009-08-03 23:53:30 +02:00
|
|
|
if (file_exists('../cache/'.$cacheDir.$file.'.html')) {
|
2010-05-25 06:26:15 +02:00
|
|
|
header('X-SilverStripe-Cache: hit at '.@date('r'));
|
2009-08-03 23:53:30 +02:00
|
|
|
echo file_get_contents('../cache/'.$cacheDir.$file.'.html');
|
|
|
|
} elseif (file_exists('../cache/'.$cacheDir.$file.'.php')) {
|
2010-05-25 06:26:15 +02:00
|
|
|
header('X-SilverStripe-Cache: hit at '.@date('r'));
|
2009-08-03 23:53:30 +02:00
|
|
|
include_once '../cache/'.$cacheDir.$file.'.php';
|
2010-04-14 05:50:21 +02:00
|
|
|
if ($cacheDebug) echo "<h1>File was cached</h1>";
|
2009-08-03 23:53:30 +02:00
|
|
|
} else {
|
2010-05-25 06:26:15 +02:00
|
|
|
header('X-SilverStripe-Cache: miss at '.@date('r') . ' on ' . $cacheDir . $file);
|
2009-08-03 23:53:30 +02:00
|
|
|
// No cache hit... fallback!!!
|
|
|
|
include 'main.php';
|
2009-10-15 23:46:36 +02:00
|
|
|
if ($cacheDebug) echo "<h1>File was !NOT! cached</h1>";
|
2009-08-03 23:53:30 +02:00
|
|
|
}
|
2009-07-13 06:42:48 +02:00
|
|
|
} else {
|
|
|
|
include 'main.php';
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|