11<?php
22
3- /************************************************************************
4- * CSS and Javascript Combinator 0.5
5- * Copyright 2006 by Niels Leenheer
6- *
7- * Permission is hereby granted, free of charge, to any person obtaining
8- * a copy of this software and associated documentation files (the
9- * "Software"), to deal in the Software without restriction, including
10- * without limitation the rights to use, copy, modify, merge, publish,
11- * distribute, sublicense, and/or sell copies of the Software, and to
12- * permit persons to whom the Software is furnished to do so, subject to
13- * the following conditions:
14- *
15- * The above copyright notice and this permission notice shall be
16- * included in all copies or substantial portions of the Software.
17- *
18- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22- * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23- * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24- * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25- */
26-
27-
28- $ cache = true ;
29- $ write_combined = true ;
30- $ pullfromcache = false ;
31- $ theme = $ _GET ['theme ' ];
32- $ cachedir = dirname (__FILE__ ) . '/cache ' ;
33- $ combinedir = dirname (__FILE__ ) . '/combined ' ;
34- $ cssdir = dirname (__FILE__ ) . '/themes/ ' . $ theme ;
35- $ jsdir = dirname (__FILE__ ) . '/js ' ;
36-
37- // Determine the directory and type we should use
38- switch ($ _GET ['type ' ]) {
39- case 'css ' :
40- $ base = realpath ($ cssdir );
41- break ;
42- case 'javascript ' :
43- $ base = realpath ($ jsdir );
44- break ;
45- default :
46- header ("HTTP/1.0 503 Not Implemented " );
47- exit ;
48- };
49-
50- $ type = $ _GET ['type ' ];
51- //$elements = explode(',', $_GET['files']);
52-
53- include ($ base . '/manifest.php ' );
54-
55- // Determine last modification date of the files
56- $ lastmodified = 0 ;
57- while (list (,$ element ) = each ($ elements )) {
58- $ thisbase = $ base ;
59- $ thiselement = $ element ;
60- if ( strpos ($ thiselement , "../ " ) === 0 ){
61- $ thiselement = str_replace ("../ " ,"" ,$ thiselement );
62- $ thisbase = explode ("/ " , $ thisbase );
63- array_pop ($ thisbase );
64- $ thisbase = implode ("/ " , $ thisbase );
65- }
66- $ path = realpath ($ thisbase . '/ ' . $ thiselement );
67- //echo $path;
68-
69- if (($ type == 'javascript ' && substr ($ path , -3 ) != '.js ' ) ||
70- ($ type == 'css ' && substr ($ path , -4 ) != '.css ' )) {
71- header ("HTTP/1.0 403 Forbidden " );
72- exit ;
73- }
74-
75- if (substr ($ path , 0 , strlen ($ thisbase )) != $ thisbase || !file_exists ($ path )) {
76- header ("HTTP/1.0 404 Not Found " );
77- exit ;
78- }
79-
80- $ lastmodified = max ($ lastmodified , filemtime ($ path ));
81- }
82-
83- // Send Etag hash
84- $ hash = $ lastmodified . '- ' . md5 (implode ('' , $ elements ));
85- header ("Etag: \"" . $ hash . "\"" );
86-
87- if (isset ($ _SERVER ['HTTP_IF_NONE_MATCH ' ]) &&
88- stripslashes ($ _SERVER ['HTTP_IF_NONE_MATCH ' ]) == '" ' . $ hash . '" ' )
89- {
90- // Return visit and no modifications, so do not send anything
91- header ("HTTP/1.0 304 Not Modified " );
92- header ('Content-Length: 0 ' );
93- }
94- else
95- {
96- // First time visit or files were modified
97- if ($ cache )
98- {
99- // Determine supported compression method
100- $ gzip = strstr ($ _SERVER ['HTTP_ACCEPT_ENCODING ' ], 'gzip ' );
101- $ deflate = strstr ($ _SERVER ['HTTP_ACCEPT_ENCODING ' ], 'deflate ' );
102-
103- // Determine used compression method
104- $ encoding = $ gzip ? 'gzip ' : ($ deflate ? 'deflate ' : 'none ' );
105-
106- // Check for buggy versions of Internet Explorer
107- if (!strstr ($ _SERVER ['HTTP_USER_AGENT ' ], 'Opera ' ) &&
108- preg_match ('/^Mozilla\/4\.0 \(compatible; MSIE ([0-9]\.[0-9])/i ' , $ _SERVER ['HTTP_USER_AGENT ' ], $ matches )) {
109- $ version = floatval ($ matches [1 ]);
110-
111- if ($ version < 6 )
112- $ encoding = 'none ' ;
113-
114- if ($ version == 6 && !strstr ($ _SERVER ['HTTP_USER_AGENT ' ], 'EV1 ' ))
115- $ encoding = 'none ' ;
116- }
117-
118- // Try the cache first to see if the combined files were already generated
119- $ cachefile = 'cache- ' . $ hash . '. ' . $ type . ($ encoding != 'none ' ? '. ' . $ encoding : '' );
120-
121-
122- if (file_exists ($ cachedir . '/ ' . $ cachefile ) && $ pullfromcache == true ) {
123- if ($ fp = fopen ($ cachedir . '/ ' . $ cachefile , 'rb ' )) {
124-
125- if ($ encoding != 'none ' ) {
126- header ("Content-Encoding: " . $ encoding );
127- }
128-
129- header ("Content-Type: text/ " . $ type );
130- header ("Content-Length: " . filesize ($ cachedir . '/ ' . $ cachefile ));
131-
132- fpassthru ($ fp );
133- fclose ($ fp );
134- exit ;
135- }
136- }
137-
138- }
139-
140- // Get contents of the files
141- $ contents = '' ;
142- reset ($ elements );
143- while (list (,$ element ) = each ($ elements )) {
144- $ path = realpath ($ base . '/ ' . $ element );
145- $ contents .= "\n\n" . file_get_contents ($ path );
146- }
147-
148- // Write pre gzipped files to disk
149- if ($ write_combined ) {
150-
151- if (!file_exists ($ combinedir )) {
152- mkdir ($ combinedir , 0700 );
153- }
154-
155- $ filename = '' ;
156-
157- //Determine the filename to use
158- switch ($ _GET ['type ' ]) {
159- case 'css ' :
160- $ filename = 'jquery.mobile.css ' ;
161- break ;
162- case 'javascript ' :
163- $ filename = 'jquery.mobile.js ' ;
164- break ;
165- default :
166- header ("HTTP/1.0 503 Not Implemented " );
167- exit ;
168- };
169-
170- if ($ fp = fopen ($ combinedir . '/ ' . $ filename , 'wb ' )) {
171- fwrite ($ fp , $ contents );
172- fclose ($ fp );
173- }
174- }
175-
176- // Send Content-Type
177- header ("Content-Type: text/ " . $ type );
178-
179- if (isset ($ encoding ) && $ encoding != 'none ' )
180- {
181- // Send compressed contents
182- $ contents = gzencode ($ contents , 9 , $ gzip ? FORCE_GZIP : FORCE_DEFLATE );
183- header ("Content-Encoding: " . $ encoding );
184- header ('Content-Length: ' . strlen ($ contents ));
185- echo $ contents ;
186- }
187- else
188- {
189- // Send regular contents
190- header ('Content-Length: ' . strlen ($ contents ));
191- echo $ contents ;
192- }
193-
194- // Store cache
195- if ($ cache ) {
196-
197- if (!file_exists ($ cachedir )) {
198- mkdir ($ cachedir , 0700 );
199- }
200-
201- if ($ fp = fopen ($ cachedir . '/ ' . $ cachefile , 'wb ' )) {
202- fwrite ($ fp , $ contents );
203- fclose ($ fp );
204- }
205- }
206- }
207-
3+ if (!isset ($ type ) || !isset ($ elements ))
4+ {
5+ echo "\$type and \$elements must be specified! " ;
6+ exit ;
7+ }
8+
9+ $ contents = '' ;
10+ reset ($ elements );
11+ while (list (,$ element ) = each ($ elements )) {
12+ $ contents .= "\n\n" . file_get_contents ($ element );
13+ }
14+
15+ header ("Content-Type: " . $ type );
16+ header ("Content-Length: " . strlen ($ contents ));
17+ echo $ contents ;
18+ ?>
0 commit comments