forked from soerennb/extplorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchscript.php
More file actions
177 lines (152 loc) · 5.42 KB
/
fetchscript.php
File metadata and controls
177 lines (152 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* This file is used to send gzipped Javascripts and Stylesheets to the browser
*
* It expects three parameters:
*
* gzip (can be 1 or 0, for yes or no; default: 0)
* subdir[INDEX] (relative directory from /components/com_virtuemart/js)
* file[INDEX] (filename only)
* where INDEX is the actual number of the file to be included, so you can include multiple scripts at a time
*
* @package eXtplorer
* @copyright Copyright (C) 2006-2022 Soeren Eberhardt. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* VirtueMart is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
*
* http://virtuemart.net
*/
/**
* This function fixes the URLs used in the CSS file
* This is necessary, because this file is (usually) located somewhere else than the CSS file! That makes
* relative URL references point to wrong directories - so we need to fix that!
*/
function cssUrl( $refs ) {
$ref = $refs[1];
$subdir = $GLOBALS['subdir'];
$ref = str_replace( "'", '', stripslashes( $ref ));
$ref = trim( str_replace( '"', '', $ref) );
// Absolute References don't need to be fixed
if( substr( $ref, 0, 4 ) == 'http' ) {
return 'url( "'. $ref.'" )';
}
chdir( dirname( __FILE__ ).'/'.$subdir );
$ref = str_replace( dirname( __FILE__ ), '', realpath( $ref ));
$ref = str_replace( "\\", '/', $ref );
return 'url( "'. substr( $ref, 1 ).'" )';
}
/**
* Checks and sets HTTP headers for conditional HTTP requests
* Borrowed from DokuWiki (/lib/exe/fetch.php)
* @author Simon Willison <swillison@gmail.com>
* @link http://simon.incutio.com/archive/2003/04/23/conditionalGet
*/
function http_conditionalRequest($timestamp){
// A PHP implementation of conditional get, see
// http://fishbowl.pastiche.org/archives/001132.html
$last_modified = gmdate( 'D, d M Y H:i:s', $timestamp ) . ' GMT';
$etag = '"'.md5($last_modified).'"';
// Send the headers
header("Last-Modified: $last_modified");
header("ETag: $etag");
// See if the client has provided the required headers
$if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ?
stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) :
false;
$if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ?
stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) :
false;
if (!$if_modified_since && !$if_none_match) {
return;
}
// At least one of the headers is there - check them
if ($if_none_match && $if_none_match != $etag) {
return; // etag is there but doesn't match
}
if ($if_modified_since && $if_modified_since != $last_modified) {
return; // if-modified-since is there but doesn't match
}
// Nothing has changed since their last request - serve a 304 and exit
header('HTTP/1.0 304 Not Modified');
exit;
}
ob_start('ob_gzhandler') OR ob_start();
$base_dir = dirname( __FILE__ );
$subdirs = @$_GET['subdir'];
if( !is_array( $subdirs ) && !empty( $subdirs )) {
$subdirs = array( $subdirs );
}
$files = @$_GET['file'];
if( !is_array( $files ) && !empty( $files )) {
$files = array( $files );
}
if( empty( $files ) || sizeof($files) != sizeof( $subdirs )) {
header("HTTP/1.0 404 Not Found");
exit;
}
$countFiles = sizeof($files);
$newest_mdate = 0;
for( $i = 0; $i < $countFiles; $i++ ) {
if( empty( $files[$i] )) continue;
$file = $files[$i];
$subdir = $subdirs[$i];
$dir = realpath( $base_dir . '/' . $subdir );
$file = $dir . '/' . basename( $file );
if( !file_exists( $file ) || (!stristr( $dir, $base_dir ) && !stristr( $dir, "/usr/share/javascript") && !stristr( $dir, "/usr/share/yui")) ) {
if( $countFiles == 1 ) {
header("HTTP/1.0 404 Not Found");
exit;
}
continue;
}
$newest_mdate = max( filemtime( $file ), $newest_mdate );
}
// This function quits the page load if the browser has a cached version of the requested script.
// It then returns a 304 Not Modified header
http_conditionalRequest( $newest_mdate );
// here we need to send the script or stylesheet
$processed_files = 0;
for( $i = 0; $i < $countFiles; $i++ ) {
$file = addslashes($files[$i]);// fix by colonelxc
$subdir = $subdirs[$i];
$dir = realpath( $base_dir . '/' . $subdir );
$file = $dir . '/' . basename( $file );
if( !file_exists( $file ) || (!stristr( $dir, $base_dir ) && !stristr( $dir, "/usr/share/javascript") && !stristr( $dir, "/usr/share/yui")) || !is_readable( $file )) {
continue;
}
$fileinfo = pathinfo( $file );
switch ( $fileinfo['extension']) {
case 'css':
$mime_type = 'text/css';
header( 'Content-Type: '.$mime_type.';');
$css = implode( '', file( $file ));
$GLOBALS['subdir'] = $subdir;
$str_css = preg_replace_callback("/url\((.+?)\)/i", 'cssUrl', $css);
echo $str_css;
$processed_files++;
break;
case 'js':
$mime_type = 'application/x-javascript';
header( 'Content-Type: '.$mime_type.';');
readfile( $file );
$processed_files++;
break;
default:
}
}
if( $processed_files == 0 ) {
header("HTTP/1.0 404 Not Found");
exit;
}
// Tell the user agent to cache this script/stylesheet for an hour
$age = 3600;
header( 'Expires: '.gmdate( 'D, d M Y H:i:s', time()+ $age ) . ' GMT' );
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', @filemtime( $file ) ) . ' GMT' );
header( 'Cache-Control: public, max-age='.$age.', must-revalidate, post-check=0, pre-check=0' );
header( 'Pragma: public' );
ob_end_flush();
exit;
?>