2012-09-21 07:47:26 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* 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
|
2012-09-25 10:02:26 +02:00
|
|
|
* staticpublisher/main.php, rather than framework/main.php. This file also allows for
|
2012-09-21 09:48:50 +02:00
|
|
|
* using static publisher with the subsites module.
|
2012-09-21 07:47:26 +02:00
|
|
|
*
|
|
|
|
* If you are using StaticPublisher+Subsites, set the following in _config.php:
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* FilesystemPublisher::$domain_based_caching = true;
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* and added main site host mapping in subsites/host-map.php after everytime
|
|
|
|
* a new subsite is created or modified.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2012-09-25 10:02:26 +02:00
|
|
|
* @package staticpublisher
|
2012-09-21 07:47:26 +02:00
|
|
|
*/
|
|
|
|
|
2013-09-18 02:31:55 +02:00
|
|
|
require_once('../framework/core/Constants.php');
|
|
|
|
|
2012-09-21 07:47:26 +02:00
|
|
|
$cacheEnabled = true;
|
|
|
|
$cacheDebug = false;
|
|
|
|
$cacheBaseDir = '../cache/'; // Should point to the same folder as FilesystemPublisher->destFolder
|
|
|
|
|
|
|
|
// Optional settings for FilesystemPublisher::$domain_based_mapping=TRUE
|
|
|
|
$hostmapLocation = '../subsites/host-map.php';
|
|
|
|
|
|
|
|
// Specific to 'homepagefordomain' module
|
|
|
|
$homepageMapLocation = '../assets/_homepage-map.php';
|
|
|
|
|
2012-09-21 09:48:50 +02:00
|
|
|
|
|
|
|
function skipCache() {
|
|
|
|
require_once('../framework/core/Core.php');
|
|
|
|
require_once('../framework/main.php');
|
|
|
|
}
|
|
|
|
|
2012-09-21 07:47:26 +02:00
|
|
|
if (
|
|
|
|
$cacheEnabled
|
|
|
|
&& empty($_COOKIE['bypassStaticCache'])
|
|
|
|
// No GET params other than cache relevant config is passed (e.g. "?stage=Stage"),
|
|
|
|
// which would mean that we have to bypass the cache
|
|
|
|
&& count(array_diff(array_keys($_GET), array('url', 'cacheSubdir'))) == 0
|
|
|
|
// Request is not POST (which would have to be handled dynamically)
|
|
|
|
&& count($_POST) == 0
|
|
|
|
) {
|
|
|
|
// Define system paths (copied from Core.php)
|
|
|
|
if(!defined('BASE_PATH')) {
|
2012-09-25 10:02:26 +02:00
|
|
|
// Assuming that this file is staticpublisher/main.php we can then determine the base path
|
2012-09-21 09:48:50 +02:00
|
|
|
define('BASE_PATH', rtrim(dirname(dirname(__FILE__))), DIRECTORY_SEPARATOR);
|
2012-09-21 07:47:26 +02:00
|
|
|
}
|
|
|
|
if(!defined('BASE_URL')) {
|
|
|
|
// Determine the base URL by comparing SCRIPT_NAME to SCRIPT_FILENAME and getting common elements
|
|
|
|
$path = realpath($_SERVER['SCRIPT_FILENAME']);
|
|
|
|
|
|
|
|
if(substr($path, 0, strlen(BASE_PATH)) == BASE_PATH) {
|
|
|
|
$urlSegmentToRemove = substr($path, 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
|
|
|
|
if (isset($_GET['cacheSubdir']) && !preg_match('/[^a-zA-Z0-9\-_]/', $_GET['cacheSubdir'])) {
|
|
|
|
$cacheDir = $_GET['cacheSubdir'].'/';
|
|
|
|
}
|
|
|
|
// Custom mapping through PHP file (assumed FilesystemPublisher::$domain_based_mapping=TRUE)
|
|
|
|
else if (file_exists($hostmapLocation)) {
|
|
|
|
include_once $hostmapLocation;
|
2012-09-21 09:48:50 +02:00
|
|
|
|
2012-09-21 07:47:26 +02:00
|
|
|
$subsiteHostmap['default'] = isset($subsiteHostmap['default']) ? $subsiteHostmap['default'] : '';
|
|
|
|
$cacheDir = (isset($subsiteHostmap[$host]) ? $subsiteHostmap[$host] : $subsiteHostmap['default']) . '/';
|
|
|
|
}
|
|
|
|
// No subfolder (for FilesystemPublisher::$domain_based_mapping=FALSE)
|
|
|
|
else {
|
|
|
|
$cacheDir = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for the file in the cachedir
|
|
|
|
$file = trim($url, '/');
|
|
|
|
$file = $file ? $file : 'index';
|
|
|
|
|
|
|
|
// Route to the 'correct' index file (if applicable)
|
|
|
|
if ($file == 'index' && file_exists($homepageMapLocation)) {
|
|
|
|
include_once $homepageMapLocation;
|
2012-09-21 09:48:50 +02:00
|
|
|
|
2012-09-21 07:47:26 +02:00
|
|
|
$file = isset($homepageMap[$_SERVER['HTTP_HOST']]) ? $homepageMap[$_SERVER['HTTP_HOST']] : $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode each part of the path individually, in order to support multibyte paths.
|
|
|
|
// SiteTree.URLSegment and hence the static folder and filenames are stored in encoded form,
|
|
|
|
// to avoid filesystem incompatibilities.
|
|
|
|
$file = implode('/', array_map('rawurlencode', explode('/', $file)));
|
|
|
|
// Find file by extension (either *.html or *.php)
|
|
|
|
if (file_exists($cacheBaseDir . $cacheDir . $file . '.html')) {
|
2013-10-04 21:47:36 +02:00
|
|
|
//source: http://css-tricks.com/snippets/php/intelligent-php-cache-control/
|
|
|
|
$filepath = $cacheBaseDir . $cacheDir . $file . '.html';
|
|
|
|
$lastModified=filemtime($filepath);
|
|
|
|
$etagFile = md5_file($filepath);
|
|
|
|
$ifModifiedSince=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false);
|
|
|
|
$etagHeader=(isset($_SERVER['HTTP_IF_NONE_MATCH']) ? trim($_SERVER['HTTP_IF_NONE_MATCH']) : false);
|
|
|
|
|
|
|
|
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $lastModified)." GMT");
|
|
|
|
header("Etag: $etagFile");
|
|
|
|
header('Cache-Control: public');
|
2012-09-21 07:47:26 +02:00
|
|
|
header('X-SilverStripe-Cache: hit at '.@date('r'));
|
2013-10-04 21:47:36 +02:00
|
|
|
|
|
|
|
if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])==$lastModified || $etagHeader == $etagFile) {
|
|
|
|
header("HTTP/1.1 304 Not Modified");
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo file_get_contents($filepath);
|
2012-09-21 09:48:50 +02:00
|
|
|
if ($cacheDebug) {
|
|
|
|
echo "<h1>File was cached</h1>";
|
|
|
|
}
|
|
|
|
|
2012-09-21 07:47:26 +02:00
|
|
|
} elseif (file_exists($cacheBaseDir . $cacheDir . $file . '.php')) {
|
|
|
|
header('X-SilverStripe-Cache: hit at '.@date('r'));
|
2012-09-21 09:48:50 +02:00
|
|
|
|
2012-09-21 07:47:26 +02:00
|
|
|
include_once $cacheBaseDir . $cacheDir . $file . '.php';
|
2012-09-21 09:48:50 +02:00
|
|
|
|
|
|
|
if ($cacheDebug) {
|
|
|
|
echo "<h1>File was cached</h1>";
|
|
|
|
}
|
2012-09-21 07:47:26 +02:00
|
|
|
} else {
|
|
|
|
header('X-SilverStripe-Cache: miss at '.@date('r') . ' on ' . $cacheDir . $file);
|
2012-09-21 09:48:50 +02:00
|
|
|
|
|
|
|
skipCache();
|
|
|
|
|
2012-09-21 07:47:26 +02:00
|
|
|
if ($cacheDebug) echo "<h1>File was NOT cached</h1>";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Fall back to dynamic generation via normal routing if caching has been explicitly disabled
|
2012-09-21 09:48:50 +02:00
|
|
|
skipCache();
|
2012-09-21 07:47:26 +02:00
|
|
|
}
|
|
|
|
|