|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class CDNRewriteRequestFilter implements RequestFilter |
|
|
|
|
|
|
4
|
|
|
{ |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Enable rewriting of asset urls |
|
8
|
|
|
* @var bool |
|
9
|
|
|
*/ |
|
10
|
|
|
private static $cdn_rewrite = false; |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* The cdn domain incl. protocol |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
private static $cdn_domain = 'http://cdn.mysite.com'; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Enable rewrite in admin area |
|
20
|
|
|
* @var bool |
|
21
|
|
|
*/ |
|
22
|
|
|
private static $enable_in_backend = false; |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Enable rewrite in dev mode |
|
26
|
|
|
* @var bool |
|
27
|
|
|
*/ |
|
28
|
|
|
private static $enable_in_dev = false; |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* should assets be rewritten? |
|
33
|
|
|
* @var bool |
|
34
|
|
|
*/ |
|
35
|
|
|
private static $rewrite_assets = true; |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* should themes also be rewritten? |
|
39
|
|
|
* @var bool |
|
40
|
|
|
*/ |
|
41
|
|
|
private static $rewrite_themes = false; |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Filter executed before a request processes |
|
45
|
|
|
* |
|
46
|
|
|
* @param SS_HTTPRequest $request Request container object |
|
47
|
|
|
* @param Session $session Request session |
|
48
|
|
|
* @param DataModel $model Current DataModel |
|
49
|
|
|
* @return boolean Whether to continue processing other filters. Null or true will continue processing (optional) |
|
50
|
|
|
*/ |
|
51
|
|
|
public function preRequest(SS_HTTPRequest $request, Session $session, DataModel $model) |
|
52
|
|
|
{ |
|
53
|
|
|
return true; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Filter executed AFTER a request |
|
58
|
|
|
* |
|
59
|
|
|
* @param SS_HTTPRequest $request Request container object |
|
60
|
|
|
* @param SS_HTTPResponse $response Response output object |
|
61
|
|
|
* @param DataModel $model Current DataModel |
|
62
|
|
|
* @return boolean Whether to continue processing other filters. Null or true will continue processing (optional) |
|
63
|
|
|
*/ |
|
64
|
|
|
public function postRequest(SS_HTTPRequest $request, SS_HTTPResponse $response, DataModel $model) |
|
65
|
|
|
{ |
|
66
|
|
|
if (!self::isEnabled()) { |
|
67
|
|
|
return true; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$body = $response->getBody(); |
|
71
|
|
|
$response->setBody(self::replaceCDN($body)); |
|
72
|
|
|
|
|
73
|
|
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Checks if cdn rewrite is enabled |
|
78
|
|
|
* @return bool |
|
79
|
|
|
*/ |
|
80
|
|
|
public static function isEnabled() |
|
81
|
|
|
{ |
|
82
|
|
|
$general = Config::inst()->get('CDNRewriteRequestFilter', 'cdn_rewrite'); |
|
83
|
|
|
$notDev = !Director::isDev() || Config::inst()->get('CDNRewriteRequestFilter', 'enable_in_dev'); |
|
84
|
|
|
$notBackend = !self::isBackend() || Config::inst()->get('CDNRewriteRequestFilter', 'enable_in_backend'); |
|
85
|
|
|
|
|
86
|
|
|
return $general && $notDev && $notBackend; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Helper method to check if we're in backend (LeftAndMain) or frontend |
|
91
|
|
|
* Controller::curr() doesn't return anything, so i cannot check it... |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
|
|
public static function isBackend() |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
|
|
$url = array_key_exists('url', $_GET) ? $_GET['url'] : ''; |
|
97
|
|
|
return !Config::inst()->get('SSViewer', 'theme_enabled') || strpos($url, 'admin') === 1; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* replaces links to assets in src and href attributes to point to a given cdn domain |
|
102
|
|
|
* |
|
103
|
|
|
* @param $body |
|
104
|
|
|
* @return mixed|void |
|
105
|
|
|
*/ |
|
106
|
|
|
public static function replaceCDN($body) |
|
107
|
|
|
{ |
|
108
|
|
|
$cdn = Config::inst()->get('CDNRewriteRequestFilter', 'cdn_domain'); |
|
109
|
|
|
|
|
110
|
|
|
if (Config::inst()->get('CDNRewriteRequestFilter', 'rewrite_assets')) { |
|
111
|
|
|
$body = str_replace('src="assets/', 'src="' . $cdn . '/assets/', $body); |
|
112
|
|
|
$body = str_replace('src="/assets/', 'src="' . $cdn . '/assets/', $body); |
|
113
|
|
|
$body = str_replace('src=\"/assets/', 'src=\"' . $cdn . '/assets/', $body); |
|
114
|
|
|
|
|
115
|
|
|
$body = str_replace('href="/assets/', 'href="' . $cdn . '/assets/', $body); |
|
116
|
|
|
$body = str_replace(Director::absoluteBaseURL() . 'assets/', $cdn . '/assets/', $body); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
if (Config::inst()->get('CDNRewriteRequestFilter', 'rewrite_themes')) { |
|
120
|
|
|
$body = str_replace('src="/themes/', 'src="' . $cdn . '/themes/', $body); |
|
121
|
|
|
$body = str_replace('src="' . Director::absoluteBaseURL() . 'themes/', 'src="' . $cdn . '/themes/', $body); |
|
122
|
|
|
|
|
123
|
|
|
$body = str_replace('href="/themes/', 'href="' . $cdn . '/themes/', $body); |
|
124
|
|
|
$body = str_replace('href="' . Director::absoluteBaseURL() . 'themes/', 'href="' . $cdn . '/themes/', $body); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $body; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.