forked from tractorcow/silverstripe-dynamiccache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache-main.php
89 lines (76 loc) · 2.65 KB
/
cache-main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
* This file acts as the front end buffer between Silverstripe and the dynamic caching
* mechanism.
*
* Your .htaccess will need to be modified to change your framework/main.php reference
* to this (dynamiccache/cache-main.php). Alternatively you may set this to a custom file,
* set the various optional filters ($optInXXX etc) and include this file directly after,
* making sure to set $overrideCacheOptions directly before
*
* Caching will automatically filter via top level domain so that modules that serve
* domain specific content (such as subsites) will silently work.
*
* If a page should not be cached, then
*/
// If flush, bypass caching completely in order to delegate to Silverstripe's flush protection
if(isset($_GET['flush'])) {
require('../framework/main.php');
exit;
}
// Include SilverStripe's core code. This is required to access cache classes
// This is a little less lightweight than the file based cache, but still doesn't
// involve a database hit, but allows for dependency injection
require_once('../framework/core/Core.php');
require_once('code/DynamicCache.php');
// IIS will sometimes generate this.
if(!empty($_SERVER['HTTP_X_ORIGINAL_URL'])) {
$_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_ORIGINAL_URL'];
}
/**
* Figure out the request URL
*/
global $url;
// PHP 5.4's built-in webserver uses this
if (php_sapi_name() == 'cli-server') {
$url = $_SERVER['REQUEST_URI'];
// Querystring args need to be explicitly parsed
if(strpos($url,'?') !== false) {
list($url, $query) = explode('?',$url,2);
parse_str($query, $_GET);
if ($_GET) $_REQUEST = array_merge((array)$_REQUEST, (array)$_GET);
}
// Pass back to the webserver for files that exist
if(file_exists(BASE_PATH . $url) && is_file(BASE_PATH . $url)) return false;
// Apache rewrite rules use this
} else if (isset($_GET['url'])) {
$url = $_GET['url'];
// IIS includes get variables in url
$i = strpos($url, '?');
if($i !== false) {
$url = substr($url, 0, $i);
}
// Lighttpd uses this
} else {
if(strpos($_SERVER['REQUEST_URI'],'?') !== false) {
list($url, $query) = explode('?', $_SERVER['REQUEST_URI'], 2);
parse_str($query, $_GET);
if ($_GET) $_REQUEST = array_merge((array)$_REQUEST, (array)$_GET);
} else {
$url = $_SERVER["REQUEST_URI"];
}
}
// Remove base folders from the URL if webroot is hosted in a subfolder
if(strlen($url) && strlen(BASE_URL)) {
if (substr(strtolower($url), 0, strlen(BASE_URL)) == strtolower(BASE_URL)) {
$url = substr($url, strlen(BASE_URL));
}
}
if(empty($url)) {
$url = '/';
} elseif(substr($url, 0, 1) !== '/') {
$url = "/$url";
}
// Activate caching here
$instance = DynamicCache::inst();
$instance->run($url);