This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
forked from nyeholt/silverstripe-simplecache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontend-cache.php
105 lines (84 loc) · 3.15 KB
/
frontend-cache.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
include_once __DIR__ .'/code/services/SimpleCache.php';
include_once __DIR__.'/code/controllers/FrontendProxy.php';
$framework = dirname(__DIR__) . '/framework';
require_once($framework . '/core/Constants.php');
require_once 'control/injector/Injector.php';
/**
* 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 (substr(strtolower($url), 0, strlen(BASE_URL)) == strtolower(BASE_URL)) $url = substr($url, strlen(BASE_URL));
if (defined('USE_PROXY') && USE_PROXY) {
if (defined('PROXY_CONFIG_FILE')) {
include_once BASE_PATH . '/' . PROXY_CONFIG_FILE;
}
$publisher = null;
if (defined('PROXY_PUBLISHER')) {
$publisher = SimpleCache::get_cache(PROXY_PUBLISHER);
}
$dynamic = null;
if (defined('PROXY_DYNAMIC_PUBLISHER')) {
$dynamic = SimpleCache::get_cache(PROXY_DYNAMIC_PUBLISHER);
}
$cookies = defined('PROXY_BYPASS_COOKIES') ? explode(',', PROXY_BYPASS_COOKIES) : array();
$url_config = isset($PROXY_CACHE_URLS) ? $PROXY_CACHE_URLS : null;
$blacklist = isset($PROXY_CACHE_BLACKLIST) ? $PROXY_CACHE_BLACKLIST : array();
$proxyclass = defined('PROXY_CLASS') ? PROXY_CLASS : 'FrontendProxy';
$proxy = new $proxyclass($publisher, $dynamic, $url_config, $cookies);
$proxy
->setCacheGetVars(defined('CACHE_ALLOW_GET_VARS') && CACHE_ALLOW_GET_VARS)
->setIgnoreGetVars(defined('CACHE_IGNORE_GET_VARS') && CACHE_IGNORE_GET_VARS)
->setBlacklist($blacklist);
if (isset($PROXY_CACHE_REMAP)) {
$proxy->setRemapHosts($PROXY_CACHE_REMAP);
}
if (defined('PROXY_CONTENT_REWRITER')) {
$proxy->setContentRewriter(PROXY_CONTENT_REWRITER);
}
$host = $_SERVER['HTTP_HOST'];
$trimFunc = defined('PROXY_TRIM_FUNC') ? PROXY_TRIM_FUNC : 'trim';
$relativeUrl = $trimFunc($url, '/');
$proxy->checkIfEnabled($host, $relativeUrl);
if ($proxy->urlIsCached($host, $relativeUrl)) {
$proxy->serve($host, $relativeUrl);
} else if ($proxy->canCache($host, $relativeUrl)) {
$proxy->generateCache($host, $relativeUrl);
$proxy->serve($host, $relativeUrl);
} else {
include $framework . '/main.php';
}
// otherwise we're going to fall back to main processing
} else {
include $framework . '/main.php';
}