<?php
$is_bad_request
=false;
$cache
= true;
$doc_root_uri
=
$_SERVER
['DOCUMENT_ROOT'].'/';
$cachedir
=
$doc_root_uri
. '
public
/cache';
$type
=isset(
$_GET
['t'])?(
$_GET
['t']=='j'||
$_GET
['t']=='c'?
$_GET
['t']:''):'';
$base
=isset(
$_GET
['b'])?(
$doc_root_uri
.
str_replace
('.','/',
$_GET
['b'])):
$doc_root_uri
;
$fs
=isset(
$_GET
['fs'])?
str_replace
('.','/',
$_GET
['fs']):'';
$fs
=
str_replace
(',','.'.(
$type
=='j'?'js,':'css,'),
$fs
);
$fs
=
$fs
.(
$type
=='j'?'.js':'.css');
if
(
$type
==''||
$fs
==''){
$is_bad_request
=true;}
if
(
$is_bad_request
){header (
"HTTP/1.0 503 Not Implemented"
);}
$file_type
=
$type
=='j'?'javascript':'css';
$elements
=
explode
(',',preg_replace('/([^?]*).*/', '\1',
$fs
));
$lastmodified
= 0;
while
(list(,
$element
) = each(
$elements
)) {
$path
=
$base
. '/' .
$element
;
if
((
$type
== 'j' &&
substr
(
$path
, -3) != '.js') ||
(
$type
== 'c' &&
substr
(
$path
, -4) != '.css')) {
header (
"HTTP/1.0 403 Forbidden"
);
exit
;
}
if
(
substr
(
$path
, 0,
strlen
(
$base
)) !=
$base
|| !
file_exists
(
$path
)) {
header (
"HTTP/1.0 404 Not Found"
);
exit
;
}
$lastmodified
= max(
$lastmodified
,
filemtime
(
$path
));
}
$hash
=
$lastmodified
. '-' . md5(
$fs
);
header (
"Etag: \""
.
$hash
.
"\""
);
if
(isset(
$_SERVER
['HTTP_IF_NONE_MATCH']) &&
stripslashes
(
$_SERVER
['HTTP_IF_NONE_MATCH']) == '
"' . $hash . '"
')
{
header (
"HTTP/1.0 304 Not Modified"
);
header (
"Content-Type: text/"
.
$file_type
);
header ('Content-Length: 0');
}
else
{
if
(
$cache
)
{
$gzip
=
strstr
(
$_SERVER
['HTTP_ACCEPT_ENCODING'], 'gzip');
$deflate
=
strstr
(
$_SERVER
['HTTP_ACCEPT_ENCODING'], 'deflate');
$encoding
=
$gzip
? 'gzip' : (
$deflate
? 'deflate' : 'none');
if
(!
strstr
(
$_SERVER
['HTTP_USER_AGENT'], 'Opera') &&
preg_match('/^Mozilla\/4\.0 \(compatible; MSIE ([0-9]\.[0-9])/i',
$_SERVER
['HTTP_USER_AGENT'],
$matches
)) {
$version
=
floatval
(
$matches
[1]);
if
(
$version
< 6)
$encoding
= 'none';
if
(
$version
== 6 && !
strstr
(
$_SERVER
['HTTP_USER_AGENT'], 'EV1'))
$encoding
= 'none';
}
$cachefile
= 'cache-' .
$hash
. '.' .
$file_type
. (
$encoding
!= 'none' ? '.' .
$encoding
: '');
if
(
file_exists
(
$cachedir
. '/' .
$cachefile
)) {
if
(
$fp
=
fopen
(
$cachedir
. '/' .
$cachefile
, 'rb')) {
if
(
$encoding
!= 'none') {
header (
"Content-Encoding: "
.
$encoding
);
}
header (
"Content-Type: text/"
.
$file_type
);
header (
"Content-Length: "
.
filesize
(
$cachedir
. '/' .
$cachefile
));
fpassthru
(
$fp
);
fclose(
$fp
);
exit
;
}
}
}
$contents
= '';
reset(
$elements
);
while
(list(,
$element
) = each(
$elements
)) {
$path
=
$base
. '/' .
$element
;
$contents
.=
"\n\n"
.
file_get_contents
(
$path
);
}
header (
"Content-Type: text/"
.
$file_type
);
if
(isset(
$encoding
) &&
$encoding
!= 'none')
{
$contents
= gzencode(
$contents
, 9,
$gzip
? FORCE_GZIP : FORCE_DEFLATE);
header (
"Content-Encoding: "
.
$encoding
);
header ('Content-Length: ' .
strlen
(
$contents
));
echo
$contents
;
}
else
{
header ('Content-Length: ' .
strlen
(
$contents
));
echo
$contents
;
}
if
(
$cache
) {
if
(
$fp
=
fopen
(
$cachedir
. '/' .
$cachefile
, 'wb')) {
fwrite(
$fp
,
$contents
);
fclose(
$fp
);
}
}
}