20 function quickcache_debug($s) {
21 static $quickcache_debugline;
23 if ($GLOBALS[
"QUICKCACHE_DEBUG"]) {
24 $quickcache_debugline++;
25 header(
"X-CacheDebug-$quickcache_debugline: $s");
33 function quickcache_key() {
34 if ($GLOBALS[
"QUICKCACHE_CLEANKEYS"]) {
35 $key = eregi_replace(
"[^A-Z,0-9,=]",
"_", quickcache_scriptkey());
36 $key .=
".".eregi_replace(
"[^A-Z,0-9,=]",
"_", quickcache_varkey());
37 if (strlen($key) > 255) {
39 $key = md5(quickcache_scriptkey().quickcache_varkey());
42 $key = md5(quickcache_scriptkey().quickcache_varkey());
44 quickcache_debug(
"Cachekey is set to $key");
53 function quickcache_varkey() {
55 if ($GLOBALS[
"QUICKCACHE_POST"]) {
56 $varkey =
"POST=".serialize($_POST);
58 $varkey .=
"GET=".serialize($_GET);
59 quickcache_debug(
"Cache varkey is set to $varkey");
66 function quickcache_scriptkey() {
68 if ($GLOBALS[
"QUICKCACHE_IGNORE_DOMAIN"]) {
69 $name=$_SERVER[
"PHP_SELF"];
71 $name=$_SERVER[
"SCRIPT_URI"];
77 $name=
"http://".$_SERVER[
"SERVER_NAME"].$_SERVER[
"SCRIPT_NAME"];
80 quickcache_debug(
"Cache scriptkey is set to $name");
86 function quickcache_check() {
87 if (!$GLOBALS[
"QUICKCACHE_ON"]) {
88 quickcache_debug(
"Cache has been disabled!");
94 $GLOBALS[
"quickcache_key"] = quickcache_key();
97 if (quickcache_restore()) {
98 quickcache_debug(
"Cachedata for ".$GLOBALS[
"quickcache_key"].
" found, data restored");
102 quickcache_debug(
"No (valid) cachedata for ".$GLOBALS[
"quickcache_key"]);
111 function quickcache_encoding() {
112 if (headers_sent() || connection_aborted()) {
115 if (strpos($_SERVER[
"HTTP_ACCEPT_ENCODING"],
'x-gzip') !==
false) {
118 if (strpos($_SERVER[
"HTTP_ACCEPT_ENCODING"],
'gzip') !==
false) {
127 function quickcache_init() {
129 if (isset($GLOBALS[
"cachetimeout"])) {
130 $GLOBALS[
"QUICKCACHE_TIME"]=$GLOBALS[
"cachetimeout"];
134 if (!function_exists(
'gzcompress')) {
135 $GLOBALS[
"QUICKCACHE_USE_GZIP"] = 0;
139 if (!$GLOBALS[
"QUICKCACHE_POST"] && (count($_POST) > 0)) {
140 $GLOBALS[
"QUICKCACHE_ON"] = 0;
141 $GLOBALS[
"QUICKCACHE_TIME"] = -1;
145 if ($GLOBALS[
"QUICKCACHE_TIME"] == -1) {
146 $GLOBALS[
"QUICKCACHE_ON"] = 0;
150 header(
"X-Cache: QuickCache v".$GLOBALS[
"QUICKCACHE_VERSION"].
151 " - ".$GLOBALS[
"QUICKCACHE_TYPE"]);
157 function quickcache_gc() {
159 if ($GLOBALS[
"QUICKCACHE_GC"]>0) {
160 mt_srand(time(NULL));
163 if (((mt_rand()%$precision)/$precision) <=
164 ($GLOBALS[
"QUICKCACHE_GC"]/100))
166 quickcache_debug(
"GarbageCollection hit!");
175 function quickcache_start() {
180 quickcache_do_start();
183 if (quickcache_check()) {
185 print quickcache_flush($GLOBALS[
"quickcachedata_gzdata"],
186 $GLOBALS[
"quickcachedata_datasize"],
187 $GLOBALS[
"quickcachedata_datacrc"]);
199 ob_start(
"quickcache_end");
200 ob_implicit_flush(0);
207 function quickcache_end($contents) {
208 quickcache_debug(
"Callback happened");
210 $datasize = strlen($contents);
211 $datacrc = crc32($contents);
213 if ($GLOBALS[
"QUICKCACHE_USE_GZIP"]) {
214 $gzdata = gzcompress($contents, $GLOBALS[
"QUICKCACHE_GZIP_LEVEL"]);
223 if ((!connection_aborted()) &&
224 $GLOBALS[
"QUICKCACHE_ON"] &&
225 ($GLOBALS[
"QUICKCACHE_TIME"] >= 0))
227 quickcache_debug(
"Writing cached data to storage");
229 quickcache_write($gzdata, $datasize, $datacrc);
236 return quickcache_flush($gzdata, $datasize, $datacrc);
246 function quickcache_flush($gzdata, $datasize, $datacrc) {
248 $myETag =
"\"qcd-$datacrc.$datasize\"";
249 header(
"ETag: $myETag");
250 $foundETag = isset($_SERVER[
"HTTP_IF_NONE_MATCH"]) ? stripslashes($_SERVER[
"HTTP_IF_NONE_MATCH"]) :
"";
253 if (strstr($foundETag, $myETag)) {
255 if(stristr($_SERVER[
"SERVER_SOFTWARE"],
"microsoft")) {
258 header(
"Status: 304 Not Modified");
260 if ( $QUICKCACHE_ISCGI ) {
261 header(
'Status: 304 Not Modified');
263 header(
'HTTP/1.0 304');
268 if ($GLOBALS[
"QUICKCACHE_USE_GZIP"]) {
269 $ENCODING = quickcache_encoding();
274 header(
"Content-Encoding: $ENCODING");
275 $ret =
"\x1f\x8b\x08\x00\x00\x00\x00\x00";
276 $ret .= substr($gzdata, 0, strlen($gzdata) - 4);
277 $ret .= pack(
'V',$datacrc);
278 $ret .= pack(
'V',$datasize);
281 $ret = gzuncompress($gzdata);