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;
|
2010-10-13 06:04:32 +02:00
|
|
|
* and added main site host mapping in subsites/host-map.php after everytime a new subsite is created or modified
|
|
|
|
*
|
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
|
|
|
|
2010-10-13 06:04:32 +02:00
|
|
|
$cacheEnabled = true;
|
2009-10-15 23:46:36 +02:00
|
|
|
$cacheDebug = false;
|
2010-10-13 06:04:32 +02:00
|
|
|
$cacheBaseDir = '../cache/'; // Should point to the same folder as FilesystemPublisher->destFolder
|
|
|
|
|
|
|
|
// Optional settings for FilesystemPublisher::$domain_based_mapping=TRUE
|
|
|
|
$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-10-13 06:04:32 +02:00
|
|
|
if ($cacheEnabled && empty($_COOKIE['bypassStaticCache'])) {
|
|
|
|
|
|
|
|
// Define system paths (copied from Core.php)
|
|
|
|
if(!defined('BASE_PATH')) {
|
|
|
|
// Assuming that this file is sapphire/static-main.php we can then determine the base path
|
|
|
|
define('BASE_PATH', rtrim(dirname(dirname(__FILE__))), DIRECTORY_SEPARATOR);
|
|
|
|
}
|
|
|
|
if(!defined('BASE_URL')) {
|
|
|
|
// Determine the base URL by comparing SCRIPT_NAME to SCRIPT_FILENAME and getting the common elements
|
|
|
|
if(substr($_SERVER['SCRIPT_FILENAME'],0,strlen(BASE_PATH)) == BASE_PATH) {
|
|
|
|
$urlSegmentToRemove = substr($_SERVER['SCRIPT_FILENAME'],strlen(BASE_PATH));
|
|
|
|
if(substr($_SERVER['SCRIPT_NAME'],-strlen($urlSegmentToRemove)) == $urlSegmentToRemove) {
|
|
|
|
$baseURL = substr($_SERVER['SCRIPT_NAME'], 0, -strlen($urlSegmentToRemove));
|
|
|
|
define('BASE_URL', rtrim($baseURL, DIRECTORY_SEPARATOR));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$url = $_GET['url'];
|
|
|
|
// Remove base folders from the URL if webroot is hosted in a subfolder
|
|
|
|
if (substr(strtolower($url), 0, strlen(BASE_URL)) == strtolower(BASE_URL)) {
|
|
|
|
$url = substr($url, strlen(BASE_URL));
|
|
|
|
}
|
|
|
|
|
|
|
|
$host = str_replace('www.', '', $_SERVER['HTTP_HOST']);
|
|
|
|
|
|
|
|
// Custom cache dir for debugging purposes
|
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-10-13 06:04:32 +02:00
|
|
|
}
|
|
|
|
// Custom mapping through PHP file (assumed FilesystemPublisher::$domain_based_mapping=TRUE)
|
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'] : '';
|
|
|
|
$cacheDir = (isset($subsiteHostmap[$host]) ? $subsiteHostmap[$host] : $subsiteHostmap['default']) . '/';
|
2010-10-13 06:04:32 +02:00
|
|
|
}
|
|
|
|
// No subfolder (for FilesystemPublisher::$domain_based_mapping=FALSE)
|
|
|
|
else {
|
2009-08-03 23:53:30 +02:00
|
|
|
$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-10-13 06:04:32 +02:00
|
|
|
$file = trim($url, '/');
|
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
|
|
|
|
2010-10-13 06:04:32 +02:00
|
|
|
// Find file by extension (either *.html or *.php)
|
2010-10-04 06:32:13 +02:00
|
|
|
$file = preg_replace('/[^a-zA-Z0-9\/\-_]/si', '-', $file);
|
2010-10-13 06:04:32 +02:00
|
|
|
if (file_exists($cacheBaseDir . $cacheDir . $file . '.html')) {
|
2010-05-25 06:26:15 +02:00
|
|
|
header('X-SilverStripe-Cache: hit at '.@date('r'));
|
2010-10-13 06:04:32 +02:00
|
|
|
echo file_get_contents($cacheBaseDir . $cacheDir . $file . '.html');
|
|
|
|
if ($cacheDebug) echo "<h1>File was cached</h1>";
|
|
|
|
} elseif (file_exists($cacheBaseDir . $cacheDir . $file . '.php')) {
|
2010-05-25 06:26:15 +02:00
|
|
|
header('X-SilverStripe-Cache: hit at '.@date('r'));
|
2010-10-13 06:04:32 +02:00
|
|
|
include_once $cacheBaseDir . $cacheDir . $file . '.php';
|
|
|
|
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);
|
2010-10-13 06:04:32 +02:00
|
|
|
// No cache hit... fallback to dynamic routing
|
2009-08-03 23:53:30 +02:00
|
|
|
include 'main.php';
|
2010-10-13 06:04:32 +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 {
|
2010-10-13 06:04:32 +02:00
|
|
|
// Fall back to dynamic generation via normal routing if caching has been explicitly disabled
|
2009-07-13 06:42:48 +02:00
|
|
|
include 'main.php';
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|