"; exit(); } // Read the requested file name from GET parameter $file = @$_GET['file'] or debug_exit('Missing file parameter'); $file = $baseDir.$file; // Check file extension if (!preg_match('/\.((js)|(css))$/i', $file, $file_type)) { debug_exit($file." is not a javascript or css file."); } $file_type = strtolower($file_type[1]); // Check for file existence if (!file_exists($file)) { debug_exit($file." not found."); } // Set Modified Time to be returned by the HTTP response $mtime = filemtime($file); if ($mtime < ($mtimeInst = filemtime('load.php'))) { $mtime = $mtimeInst; } $mtimestr = gmdate("D, d M Y H:i:s", $mtime) . " GMT"; // If the file was not modified since the last browser request, // return a "304 Not Modified" $ifModifiedSince = $_SERVER['HTTP_IF_MODIFIED_SINCE']; if (isset($ifModifiedSince) && $ifModifiedSince == $mtimestr) header_exit('304 Not Modified'); // Let's write the HTTP headers header('Content-Type: '.($file_type==='js'?'application/x-javascript':'text/css').'; charset='.$encoding); header("Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT"); header("Last-Modified: " . $mtimestr); header("Cache-Control: must-revalidate", false); // Is gzip supported by both client and server? $supportedEncodings = $_SERVER['HTTP_ACCEPT_ENCODING']; $gzip_supported = (isset($supportedEncodings) && array_search('gzip', array_map('trim', explode(',' , $supportedEncodings))) !== false && function_exists('gzencode')); // Cached file name: generated by MD5 hash function // Should be robust enough for every need $cached_file = $cachedirPath.md5($file).'.'.$file_type; // Add .gz extension to file name if gzipping is supported by the client if ($gzip_supported) { header("Content-Encoding: gzip"); $cached_file .= '.gz'; } // If the file is in cache... if ((file_exists($cached_file)) && ($mtime < filemtime($cached_file))) { // ...serve it! @readfile($cached_file) or debug_exit("Cannot read file ($cached_file)\\nCheck directory permissions or contact the server administrator"); } else { // else read the source file... $file_contents = @file_get_contents($file) or debug_exit("Cannot read file $file."); // ...Gzip it if needed... if ($gzip_supported) { $file_contents = @gzencode($file_contents, 8) or debug_exit("Gzipping failed"); // ...save it in the cache... $handle = @fopen($cached_file, 'w') or debug_exit("Cache dir ($cachedirPath) is not writable or doesn\'t exist"); fwrite($handle, $file_contents); fclose($handle); } // ... and finally output it. We are done now. echo $file_contents; }