~ubuntu-branches/ubuntu/raring/extplorer/raring-proposed

« back to all changes in this revision

Viewing changes to fetchscript.php

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Goirand
  • Date: 2010-07-05 19:53:12 UTC
  • Revision ID: james.westby@ubuntu.com-20100705195312-i92s1udelus7gl52
Tags: upstream-2.1.0b6+dfsg
ImportĀ upstreamĀ versionĀ 2.1.0b6+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
* This file is used to send gzipped Javascripts and Stylesheets to the browser
 
4
 
5
* It expects three parameters:
 
6
 
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
 
11
 
12
* @version $Id: fetchscript.php 156 2009-10-26 15:19:27Z soeren $
 
13
* @package eXtplorer
 
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.
 
20
*
 
21
* http://virtuemart.net
 
22
*/
 
23
 
 
24
/**
 
25
 * Initialise GZIP
 
26
 * @author Mambo / Joomla Project
 
27
 */
 
28
function initGzip() {
 
29
        global $do_gzip_compress;
 
30
 
 
31
        $gzip = isset( $_GET['gzip'] ) ? (boolean)$_GET['gzip'] : false;
 
32
 
 
33
        $do_gzip_compress = FALSE;
 
34
        if ($gzip) {
 
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'] : '';
 
38
 
 
39
                $gzip_check     = 0;
 
40
                $zlib_check     = 0;
 
41
                $gz_check               = 0;
 
42
                $zlibO_check    = 0;
 
43
                $sid_check              = 0;
 
44
                if ( strpos( $canZip, 'gzip' ) !== false) {
 
45
                        $gzip_check = 1;
 
46
                }
 
47
                if ( extension_loaded( 'zlib' ) ) {
 
48
                        $zlib_check = 1;
 
49
                }
 
50
                if ( function_exists('ob_gzhandler') ) {
 
51
                        $gz_check = 1;
 
52
                }
 
53
                if ( ini_get('zlib.output_compression') ) {
 
54
                        $zlibO_check = 1;
 
55
                }
 
56
 
 
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' );
 
63
 
 
64
                                return;
 
65
                        }
 
66
                } else if ( $phpver > '4.0' ) {
 
67
                        if ( $gzip_check ) {
 
68
 
 
69
                                if ( $zlib_check ) {
 
70
                                        $do_gzip_compress = TRUE;
 
71
                                        ob_start();
 
72
                                        ob_implicit_flush(0);
 
73
 
 
74
                                        header( 'Content-Encoding: gzip' );
 
75
                                        return;
 
76
                                }
 
77
                        }
 
78
                }
 
79
        }
 
80
        ob_start();
 
81
}
 
82
 
 
83
/**
 
84
* Perform GZIP
 
85
* @author Mambo / Joomla! project
 
86
*/
 
87
function doGzip() {
 
88
        global $do_gzip_compress;
 
89
        if ( $do_gzip_compress ) {
 
90
                /**
 
91
                *Borrowed from php.net!
 
92
                */
 
93
                $gzip_contents = ob_get_contents();
 
94
                ob_end_clean();
 
95
 
 
96
                $gzip_size = strlen($gzip_contents);
 
97
                $gzip_crc = crc32($gzip_contents);
 
98
 
 
99
                $gzip_contents = gzcompress($gzip_contents, 9);
 
100
                $gzip_contents = substr($gzip_contents, 0, strlen($gzip_contents) - 4);
 
101
 
 
102
                echo "\x1f\x8b\x08\x00\x00\x00\x00\x00";
 
103
                echo $gzip_contents;
 
104
                echo pack('V', $gzip_crc);
 
105
                echo pack('V', $gzip_size);
 
106
        } else {
 
107
                ob_end_flush();
 
108
        }
 
109
}
 
110
 
 
111
/**
 
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!
 
115
*/
 
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.'" )';
 
122
        }
 
123
        chdir( dirname( __FILE__ ).'/'.$subdir );
 
124
        $ref = str_replace( dirname( __FILE__ ), '', realpath( $ref ));
 
125
        $ref = str_replace( "\\", '/', $ref );
 
126
        return 'url( "'. substr( $ref, 1 ).'" )';
 
127
 
 
128
}
 
129
/**
 
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
 
134
 */
 
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).'"';
 
140
        // Send the headers
 
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']) :
 
146
                false;
 
147
        $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ?
 
148
                stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : 
 
149
                false;
 
150
        if (!$if_modified_since && !$if_none_match) {
 
151
                return;
 
152
        }
 
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
 
156
        }
 
157
        if ($if_modified_since && $if_modified_since != $last_modified) {
 
158
                return; // if-modified-since is there but doesn't match
 
159
        }
 
160
        // Nothing has changed since their last request - serve a 304 and exit
 
161
        header('HTTP/1.0 304 Not Modified');
 
162
        exit;
 
163
}
 
164
 
 
165
initGzip();
 
166
 
 
167
$base_dir = dirname( __FILE__ );
 
168
$subdirs = @$_GET['subdir'];
 
169
if( !is_array( $subdirs ) && !empty( $subdirs )) {
 
170
        $subdirs = array( $subdirs );
 
171
}
 
172
 
 
173
$files = @$_GET['file'];
 
174
if( !is_array( $files ) && !empty( $files )) {
 
175
        $files = array( $files );
 
176
}
 
177
if( empty( $files ) || sizeof($files) != sizeof( $subdirs )) {
 
178
        header("HTTP/1.0 400 Bad Request");
 
179
        echo 'Bad request';
 
180
        exit;
 
181
}
 
182
$countFiles = sizeof($files);
 
183
$newest_mdate = 0;
 
184
 
 
185
for( $i = 0; $i < $countFiles; $i++ ) {
 
186
        if( empty( $files[$i] )) continue;
 
187
        $file = $files[$i];
 
188
        $subdir = $subdirs[$i];
 
189
 
 
190
        $dir = realpath( $base_dir . '/' .      $subdir );
 
191
        $file = $dir . '/' . basename( $file );
 
192
 
 
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");
 
196
                        echo 'Not Found';
 
197
                        exit;
 
198
                }
 
199
                continue;
 
200
        }
 
201
        $newest_mdate = max( filemtime( $file ), $newest_mdate );
 
202
}
 
203
 
 
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 );
 
207
 
 
208
// here we need to send the script or stylesheet
 
209
$processed_files = 0;
 
210
for( $i = 0; $i < $countFiles; $i++ ) {
 
211
        $file = $files[$i];
 
212
        $subdir = $subdirs[$i];
 
213
 
 
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 )) {
 
217
                continue;
 
218
        }
 
219
        $processed_files++;
 
220
        $fileinfo = pathinfo( $file );
 
221
        switch ( $fileinfo['extension']) {
 
222
                case 'css': 
 
223
                        $mime_type = 'text/css'; 
 
224
                        header( 'Content-Type: '.$mime_type.';');
 
225
                        $css = implode( '', file( $file ));
 
226
 
 
227
                        $str_css =      preg_replace("/url\((.+?)\)/ie","cssUrl('\\1', '$subdir')", $css);
 
228
                        echo $str_css;
 
229
 
 
230
                        break;
 
231
 
 
232
                case 'js': 
 
233
                        $mime_type = 'application/x-javascript'; 
 
234
                        header( 'Content-Type: '.$mime_type.';');
 
235
 
 
236
                        readfile( $file );
 
237
 
 
238
                        break;
 
239
 
 
240
                default: 
 
241
                        continue;
 
242
 
 
243
        }
 
244
}
 
245
if( $processed_files == 0 ) {
 
246
        if( !file_exists( $file ) ) {
 
247
                header("HTTP/1.0 404 Not Found");
 
248
                echo 'Not Found';
 
249
                exit;
 
250
        }
 
251
        if( !is_readable( $file ) ) {
 
252
                header("HTTP/1.0 500 Internal Server Error");
 
253
                echo "Could not read ".basename($file)." - bad permissions?";
 
254
                exit;
 
255
        }
 
256
}
 
257
// Tell the user agent to cache this script/stylesheet for an hour
 
258
$age = 3600;
 
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' );
 
263
 
 
264
doGzip();
 
265
 
 
266
exit;
 
267
 
 
268
?>
 
 
b'\\ No newline at end of file'