forked from trigunflame/php-cdn
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.php
231 lines (201 loc) · 5.45 KB
/
index.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
// php-cdn
// dynamic file caching pseudo cdn
/////////////////////////////////////////////////////////////////////////
// cdn root path : http://cdn.com/
// cdn example url : http://cdn.com/path/to/resource.css?d=12345
// maps the uri : /path/to/resource.css?d=12345
// to the origin : http://yoursite.com/path/to/resource.css?d=12345
// caches file to : ./cache/[base64-encoded-uri].css
// returns local cached copy or issues 304 not modified
/////////////////////////////////////////////////////////////////////////
// error_reporting(E_ERROR | E_PARSE);
// the source that we intend to mirror
$f_origin = 'http://cdn.com';
// encode as filename-safe base64
$f_name = strtr(base64_encode($_SERVER['REQUEST_URI']), '+/=', '-_,');
// parse the file extension
$f_ext = strrchr(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '.');
function send_file($f_path, $f_modified)
{
$header = file_get_contents($f_path . ".header");
$header_list = explode("\r\n", $header);
$has_last_modified=false;
$has_location=false;
foreach ($header_list as &$elem) {
if(strpos(strtolower($elem), "transfer-encoding:")===0)
{
continue;
}
if(strpos(strtolower($elem), "content-length:")===0)
{
continue;
}
if(strpos(strtolower($elem), "connection:")===0)
{
continue;
}
if(strpos(strtolower($elem), "last-modified:")===0)
{
$has_last_modified=true;
}
if(strpos(strtolower($elem), "location:")===0)
{
$has_location=true;
}
header($elem, false);
}
if($has_last_modified===false)
{
header('Last-Modified: ' . gmdate('D, d M Y H:i:s \G\M\T', $f_modified));
}
if($has_location===false)
{
if(strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false
&& file_exists($f_path . ".gz") )
{
header("Content-Encoding: gzip");
header("Content-length: " . filesize($f_path . ".gz"));
readfile($f_path . ".gz");
}
else
{
header("Content-length: " . filesize($f_path));
// stream the file
readfile($f_path);
}
}
else
{
die("has location");
}
}
// construct usable file path
$f_path = "./cache/{$f_name}{$f_ext}";
// check the local cache
if (file_exists($f_path)) {
// get last modified time
$f_modified = filemtime($f_path);
// validate the client cache
if (isset( $_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
(strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $f_modified)
) {
// client has a valid cache, issue *304*
header($_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified');
} else {
send_file($f_path, $f_modified);
}
} else {
// http *HEAD* request
// verify that the image exists
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $f_origin . $_SERVER['REQUEST_URI'],
CURLOPT_TIMEOUT => 15,
CURLOPT_CONNECTTIMEOUT => 15,
CURLOPT_FAILONERROR => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_BINARYTRANSFER => 1,
CURLOPT_HEADER => 0,
CURLOPT_NOBODY => 1,
CURLOPT_HTTPGET => 1
// CURLOPT_FOLLOWLOCATION => 1,
));
// we have located the remote file
if (curl_exec($ch) !== false) {
$fp = fopen($f_path, 'a+b');
$file_ok = false;
if(flock($fp, LOCK_EX | LOCK_NB)) {
// empty *possible* contents
ftruncate($fp, 0);
rewind($fp);
// http *GET* request
// and write directly to the file
$ch2 = curl_init();
curl_setopt_array($ch2, array(
CURLOPT_URL => $f_origin . $_SERVER['REQUEST_URI'],
CURLOPT_TIMEOUT => 15,
CURLOPT_CONNECTTIMEOUT => 15,
CURLOPT_FAILONERROR => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_BINARYTRANSFER => 1,
CURLOPT_HEADER => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1
));
if(file_exists(getcwd() . "/ca.crt")) {
curl_setopt($ch2, CURLOPT_CAINFO, getcwd() . "/ca.crt");
}
$response = curl_exec($ch2);
$has_modified = false;
// did the transfer complete?
if ( $response === false) {
// something went wrong, null
// the file just in case >.>
ftruncate($fp, 0);
die(curl_error($response));
}
else
{
list($header, $body) = explode("\r\n\r\n", $response, 2);
fwrite($fp, $body);
$fp_header = fopen($f_path . ".header", 'w');
fwrite($fp_header, $header);
fclose($fp_header);
$file_ok = true;
$header_list = explode("\r\n", $header);
foreach ($header_list as &$elem) {
if(strpos(strtolower($elem), "last-modified:")===0)
{
$e_time = substr($elem, strpos($elem, ":") + 1);
$f_modified = strtotime($e_time);
$has_modified= $f_modified!==false;
break;
}
}
if($f_ext == ".css" || $f_ext == ".js")
{
$fp_gzipped = fopen($f_path . ".gz", 'w');
fwrite($fp_gzipped, gzencode($body));
fclose($fp_gzipped);
}
}
// 1) flush output to the file
// 2) release the file lock
// 3) release the curl socket
fflush($fp);
flock($fp, LOCK_UN);
curl_close($ch2);
if($has_modified===true)
{
touch($f_path, $f_modified);
}
}
else
{
//die("Could not lock ".$f_path);
}
// close the file
fclose($fp);
if($has_modified===false)
{
$f_modified = filemtime($f_path);
}
if($file_ok)
{
send_file($f_path, $f_modified);
}
else
{
// issue *302* for *this* request
header('Location: ' . $f_origin . $_SERVER['REQUEST_URI']);
}
} else {
// the file doesn't exist, issue *404*
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
header('Cache-Control: private');
}
// finished
curl_close($ch);
}
?>