3
* This file is used to send gzipped Javascripts and Stylesheets to the browser
5
* It expects three parameters:
7
* gzip (can be 1 or 0, for yes or no; default: 0)
8
* subdir[INDEX] (relative directory from /components/com_virtuemart/js)
9
* file[INDEX] (filename only)
10
* where INDEX is the actual number of the file to be included, so you can include multiple scripts at a time
12
* @version $Id: fetchscript.php 156 2009-10-26 15:19:27Z soeren $
14
* @copyright Copyright (C) 2006-2007 Soeren Eberhardt. All rights reserved.
15
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
16
* VirtueMart is free software. This version may have been modified pursuant
17
* to the GNU General Public License, and as distributed it includes or
18
* is derivative of works licensed under the GNU General Public License or
19
* other free or open source software licenses.
21
* http://virtuemart.net
26
* @author Mambo / Joomla Project
29
global $do_gzip_compress;
31
$gzip = isset( $_GET['gzip'] ) ? (boolean)$_GET['gzip'] : false;
33
$do_gzip_compress = FALSE;
35
$phpver = phpversion();
36
$useragent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '';
37
$canZip = isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ? $_SERVER['HTTP_ACCEPT_ENCODING'] : '';
44
if ( strpos( $canZip, 'gzip' ) !== false) {
47
if ( extension_loaded( 'zlib' ) ) {
50
if ( function_exists('ob_gzhandler') ) {
53
if ( ini_get('zlib.output_compression') ) {
57
if ( $phpver >= '4.0.4pl1' && ( strpos($useragent,'compatible') !== false || strpos($useragent,'Gecko') !== false ) ) {
58
// Check for gzip header or norton internet securities
59
if ( ( $gzip_check || isset( $_SERVER['---------------']) ) && $zlib_check && $gz_check && !$zlibO_check ) {
60
// You cannot specify additional output handlers if
61
// zlib.output_compression is activated here
62
ob_start( 'ob_gzhandler' );
66
} else if ( $phpver > '4.0' ) {
70
$do_gzip_compress = TRUE;
74
header( 'Content-Encoding: gzip' );
85
* @author Mambo / Joomla! project
88
global $do_gzip_compress;
89
if ( $do_gzip_compress ) {
91
*Borrowed from php.net!
93
$gzip_contents = ob_get_contents();
96
$gzip_size = strlen($gzip_contents);
97
$gzip_crc = crc32($gzip_contents);
99
$gzip_contents = gzcompress($gzip_contents, 9);
100
$gzip_contents = substr($gzip_contents, 0, strlen($gzip_contents) - 4);
102
echo "\x1f\x8b\x08\x00\x00\x00\x00\x00";
104
echo pack('V', $gzip_crc);
105
echo pack('V', $gzip_size);
112
* This function fixes the URLs used in the CSS file
113
* This is necessary, because this file is (usually) located somewhere else than the CSS file! That makes
114
* relative URL references point to wrong directories - so we need to fix that!
116
function cssUrl( $ref, $subdir ) {
117
$ref = str_replace( "'", '', stripslashes( $ref ));
118
$ref = trim( str_replace( '"', '', $ref) );
119
// Absolute References don't need to be fixed
120
if( substr( $ref, 0, 4 ) == 'http' ) {
121
return 'url( "'. $ref.'" )';
123
chdir( dirname( __FILE__ ).'/'.$subdir );
124
$ref = str_replace( dirname( __FILE__ ), '', realpath( $ref ));
125
$ref = str_replace( "\\", '/', $ref );
126
return 'url( "'. substr( $ref, 1 ).'" )';
130
* Checks and sets HTTP headers for conditional HTTP requests
131
* Borrowed from DokuWiki (/lib/exe/fetch.php)
132
* @author Simon Willison <swillison@gmail.com>
133
* @link http://simon.incutio.com/archive/2003/04/23/conditionalGet
135
function http_conditionalRequest($timestamp){
136
// A PHP implementation of conditional get, see
137
// http://fishbowl.pastiche.org/archives/001132.html
138
$last_modified = gmdate( 'D, d M Y H:i:s', $timestamp ) . ' GMT';
139
$etag = '"'.md5($last_modified).'"';
141
header("Last-Modified: $last_modified");
142
header("ETag: $etag");
143
// See if the client has provided the required headers
144
$if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ?
145
stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) :
147
$if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ?
148
stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) :
150
if (!$if_modified_since && !$if_none_match) {
153
// At least one of the headers is there - check them
154
if ($if_none_match && $if_none_match != $etag) {
155
return; // etag is there but doesn't match
157
if ($if_modified_since && $if_modified_since != $last_modified) {
158
return; // if-modified-since is there but doesn't match
160
// Nothing has changed since their last request - serve a 304 and exit
161
header('HTTP/1.0 304 Not Modified');
167
$base_dir = dirname( __FILE__ );
168
$subdirs = @$_GET['subdir'];
169
if( !is_array( $subdirs ) && !empty( $subdirs )) {
170
$subdirs = array( $subdirs );
173
$files = @$_GET['file'];
174
if( !is_array( $files ) && !empty( $files )) {
175
$files = array( $files );
177
if( empty( $files ) || sizeof($files) != sizeof( $subdirs )) {
178
header("HTTP/1.0 400 Bad Request");
182
$countFiles = sizeof($files);
185
for( $i = 0; $i < $countFiles; $i++ ) {
186
if( empty( $files[$i] )) continue;
188
$subdir = $subdirs[$i];
190
$dir = realpath( $base_dir . '/' . $subdir );
191
$file = $dir . '/' . basename( $file );
193
if( !file_exists( $file ) || (!stristr( $dir, $base_dir ) && !stristr( $dir, "/usr/share/javascript") && !stristr( $dir, "/usr/share/yui")) ) {
194
if( $countFiles == 1 ) {
195
header("HTTP/1.0 404 Not Found");
201
$newest_mdate = max( filemtime( $file ), $newest_mdate );
204
// This function quits the page load if the browser has a cached version of the requested script.
205
// It then returns a 304 Not Modified header
206
http_conditionalRequest( $newest_mdate );
208
// here we need to send the script or stylesheet
209
$processed_files = 0;
210
for( $i = 0; $i < $countFiles; $i++ ) {
212
$subdir = $subdirs[$i];
214
$dir = realpath( $base_dir . '/' . $subdir );
215
$file = $dir . '/' . basename( $file );
216
if( !file_exists( $file ) || (!stristr( $dir, $base_dir ) && !stristr( $dir, "/usr/share/javascript") && !stristr( $dir, "/usr/share/yui")) || !is_readable( $file )) {
220
$fileinfo = pathinfo( $file );
221
switch ( $fileinfo['extension']) {
223
$mime_type = 'text/css';
224
header( 'Content-Type: '.$mime_type.';');
225
$css = implode( '', file( $file ));
227
$str_css = preg_replace("/url\((.+?)\)/ie","cssUrl('\\1', '$subdir')", $css);
233
$mime_type = 'application/x-javascript';
234
header( 'Content-Type: '.$mime_type.';');
245
if( $processed_files == 0 ) {
246
if( !file_exists( $file ) ) {
247
header("HTTP/1.0 404 Not Found");
251
if( !is_readable( $file ) ) {
252
header("HTTP/1.0 500 Internal Server Error");
253
echo "Could not read ".basename($file)." - bad permissions?";
257
// Tell the user agent to cache this script/stylesheet for an hour
259
header( 'Expires: '.gmdate( 'D, d M Y H:i:s', time()+ $age ) . ' GMT' );
260
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', @filemtime( $file ) ) . ' GMT' );
261
header( 'Cache-Control: public, max-age='.$age.', must-revalidate, post-check=0, pre-check=0' );
262
header( 'Pragma: public' );
b'\\ No newline at end of file'