mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-09-03 08:17:48 +02:00
BUGFIX: static publisher for a site that resides in a subfolder of webroot
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@103745 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
5b79056be4
commit
47e380da03
@ -1,5 +1,4 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This file is designed to be the new 'server' of sites using StaticPublisher.
|
* 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
|
* to use this, you need to modify your .htaccess to point all requests to
|
||||||
@ -8,33 +7,64 @@
|
|||||||
*
|
*
|
||||||
* If you are using StaticPublisher+Subsites, set the following in _config.php:
|
* If you are using StaticPublisher+Subsites, set the following in _config.php:
|
||||||
* FilesystemPublisher::$domain_based_caching = true;
|
* FilesystemPublisher::$domain_based_caching = true;
|
||||||
|
* 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
|
* 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
|
* automatically generated by the Subsites module) and the cache will default
|
||||||
* to no subdirectory.
|
* to no subdirectory.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$cacheOn = true;
|
$cacheEnabled = true;
|
||||||
$cacheDebug = false;
|
$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';
|
$hostmapLocation = '../subsites/host-map.php';
|
||||||
$homepageMapLocation = '../assets/_homepage-map.php';
|
$homepageMapLocation = '../assets/_homepage-map.php';
|
||||||
|
|
||||||
if ($cacheOn && empty($_COOKIE['bypassStaticCache'])) {
|
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
|
||||||
if (isset($_GET['cacheSubdir']) && !preg_match('/[^a-zA-Z0-9\-_]/', $_GET['cacheSubdir'])) {
|
if (isset($_GET['cacheSubdir']) && !preg_match('/[^a-zA-Z0-9\-_]/', $_GET['cacheSubdir'])) {
|
||||||
$cacheDir = $_GET['cacheSubdir'].'/';
|
$cacheDir = $_GET['cacheSubdir'].'/';
|
||||||
}
|
}
|
||||||
|
// Custom mapping through PHP file (assumed FilesystemPublisher::$domain_based_mapping=TRUE)
|
||||||
else if (file_exists($hostmapLocation)) {
|
else if (file_exists($hostmapLocation)) {
|
||||||
include_once $hostmapLocation;
|
include_once $hostmapLocation;
|
||||||
$subsiteHostmap['default'] = isset($subsiteHostmap['default']) ? $subsiteHostmap['default'] : '';
|
$subsiteHostmap['default'] = isset($subsiteHostmap['default']) ? $subsiteHostmap['default'] : '';
|
||||||
|
|
||||||
// Look for the host, and find the cache dir
|
|
||||||
$host = str_replace('www.', '', $_SERVER['HTTP_HOST']);
|
|
||||||
$cacheDir = (isset($subsiteHostmap[$host]) ? $subsiteHostmap[$host] : $subsiteHostmap['default']) . '/';
|
$cacheDir = (isset($subsiteHostmap[$host]) ? $subsiteHostmap[$host] : $subsiteHostmap['default']) . '/';
|
||||||
} else {
|
}
|
||||||
|
// No subfolder (for FilesystemPublisher::$domain_based_mapping=FALSE)
|
||||||
|
else {
|
||||||
$cacheDir = '';
|
$cacheDir = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Look for the file in the cachedir
|
// Look for the file in the cachedir
|
||||||
$file = trim($_SERVER['REQUEST_URI'], '/');
|
$file = trim($url, '/');
|
||||||
$file = $file ? $file : 'index';
|
$file = $file ? $file : 'index';
|
||||||
|
|
||||||
// Route to the 'correct' index file (if applicable)
|
// Route to the 'correct' index file (if applicable)
|
||||||
@ -43,22 +73,24 @@ if ($cacheOn && empty($_COOKIE['bypassStaticCache'])) {
|
|||||||
$file = isset($homepageMap[$_SERVER['HTTP_HOST']]) ? $homepageMap[$_SERVER['HTTP_HOST']] : $file;
|
$file = isset($homepageMap[$_SERVER['HTTP_HOST']]) ? $homepageMap[$_SERVER['HTTP_HOST']] : $file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Find file by extension (either *.html or *.php)
|
||||||
$file = preg_replace('/[^a-zA-Z0-9\/\-_]/si', '-', $file);
|
$file = preg_replace('/[^a-zA-Z0-9\/\-_]/si', '-', $file);
|
||||||
|
if (file_exists($cacheBaseDir . $cacheDir . $file . '.html')) {
|
||||||
if (file_exists('../cache/'.$cacheDir.$file.'.html')) {
|
|
||||||
header('X-SilverStripe-Cache: hit at '.@date('r'));
|
header('X-SilverStripe-Cache: hit at '.@date('r'));
|
||||||
echo file_get_contents('../cache/'.$cacheDir.$file.'.html');
|
echo file_get_contents($cacheBaseDir . $cacheDir . $file . '.html');
|
||||||
} elseif (file_exists('../cache/'.$cacheDir.$file.'.php')) {
|
if ($cacheDebug) echo "<h1>File was cached</h1>";
|
||||||
|
} elseif (file_exists($cacheBaseDir . $cacheDir . $file . '.php')) {
|
||||||
header('X-SilverStripe-Cache: hit at '.@date('r'));
|
header('X-SilverStripe-Cache: hit at '.@date('r'));
|
||||||
include_once '../cache/'.$cacheDir.$file.'.php';
|
include_once $cacheBaseDir . $cacheDir . $file . '.php';
|
||||||
if ($cacheDebug) echo "<h1>File was cached</h1>";
|
if ($cacheDebug) echo "<h1>File was cached</h1>";
|
||||||
} else {
|
} else {
|
||||||
header('X-SilverStripe-Cache: miss at '.@date('r') . ' on ' . $cacheDir . $file);
|
header('X-SilverStripe-Cache: miss at '.@date('r') . ' on ' . $cacheDir . $file);
|
||||||
// No cache hit... fallback!!!
|
// No cache hit... fallback to dynamic routing
|
||||||
include 'main.php';
|
include 'main.php';
|
||||||
if ($cacheDebug) echo "<h1>File was !NOT! cached</h1>";
|
if ($cacheDebug) echo "<h1>File was NOT cached</h1>";
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// Fall back to dynamic generation via normal routing if caching has been explicitly disabled
|
||||||
include 'main.php';
|
include 'main.php';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user