This repository has been archived by the owner on Jul 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
css.php
426 lines (337 loc) · 12.5 KB
/
css.php
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<?php
/**
* This file is part of Turbine
* http://github.com/SirPepe/Turbine
*
* Copyright Peter Kröner
* Licensed under GNU LGPL 3, see license.txt or http://www.gnu.org/licenses/
*/
/*
* css.php
* Loads Turbine
* @var string $_GET['files'] A list of css files, separated by ;
*/
// Benchmark start time
$start = microtime(true);
// Constants
define('TURBINEVERSION', (file_exists('version.txt')) ? trim(file_get_contents('version.txt')) : 'unknown');
define('TURBINEPATH', dirname($_SERVER['SCRIPT_NAME']));
// Gzip output for faster transfer to client
ini_set('zlib.output_compression', 2048);
ini_set('zlib.output_compression_level', 4);
if(isset($_SERVER['HTTP_ACCEPT_ENCODING']) &&
substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') &&
function_exists('ob_gzhandler') &&
!ini_get('zlib.output_compression') &&
((!ini_get('zlib.output_compression') || intval(ini_get('zlib.output_compression')) == 0))
){
ob_start('ob_gzhandler');
}
else{
ob_start();
}
// A simple function for displaying error messages via css
function turbine_css_error_message($error_message){
return 'html:before { content:"'.$error_message.'" !important; font-family:Verdana, Arial, sans-serif !important;
font-weight:bold !important; color:#000 !important; background:#F4EA9F !important; display:block !important;
border-bottom:1px solid #D5CA6E; padding:8px !important; white-space:pre; }';
}
// Load libraries. Special treatment for the browser class because it tends to be forgotten when cloning git repositories
if(!@include('lib/browser/browser.php')){
echo turbine_css_error_message('Browser library not found! Please download the public version of Turbine or, if you
are using git, clone the browser sub project from http://github.com/SirPepe/Turbine-Browser into lib/browser.');
exit();
}
include('lib/cssmin/cssmin.php');
include('lib/base.php');
include('lib/parser.php');
include('lib/cssp.php');
include('lib/plugin.php');
// Create the Turbine instance
$cssp = new CSSP();
// Get and store browser properties
$browser = new Browser();
$browser->parse();
// Set global path constant SCRIPTPATH for use in the special constant $_SCRIPTPATH
$cssp->global_constants['SCRIPTPATH'] = TURBINEPATH;
// Plugin loading state
$plugins_loaded = false;
// List of available plugins
$plugins_available = array();
// List of used plugins
$plugins_used = array();
// CSSP file title(s)
$css_title = array();
// Process files
if($_GET['files']){
// Split multiple semicolon-separated files into an array
$files = explode(';', $_GET['files']);
// Complete the paths
$num_files = count($files);
for($i=0; $i<$num_files; $i++){
$files[$i] = $cssp->config['css_base_dir'].$files[$i];
}
// Client-side cache: Preparing caching-mechanism using eTags by creating a combined fingerprint of the files involved...
$fingerprint = '';
foreach($files as $file){
if(file_exists($file)){
$fingerprint .= $file.filemtime($file);
}
}
$etag = md5($fingerprint);
// ...and check if client sends eTag to compare it with our eTag-fingerprint
if($cssp->config['debug_level'] == 0 && isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] === $etag){
// Browser already has the file so we tell him nothing changed and exit
header('HTTP/1.1 304 Not Modified');
exit();
}
// Else parse the files and add the rusulting CSS code to $css
$css = '';
foreach($files as $file){
if($file != '' && file_exists($file)){
$fileinfo = pathinfo($file);
// For security reasons do not allow processing of files from above the base dir
if(strpos(realpath($fileinfo['dirname']), realpath($cssp->config['css_base_dir'])) !== 0){
$cssp->report_error('Path of '.$file.' is not in the base directory. File not processed for security reasons.');
continue;
}
// CSSP or CSS?
if($fileinfo['extension'] == 'css'){
// Simply include normal css files in the output. Minify if not debugging and configured to minify
if($cssp->config['debug_level'] == 0 && $cssp->config['minify_css'] == true){
$css .= cssmin::minify(file_get_contents($file));
}
else{
$css .= file_get_contents($file);
}
}
else{
$incache = false; // Server-side cache: Has file already been parsed?
$cachedir = 'cache'; // Cache directory
// Only deal with the cache when not in debug mode
if($cssp->config['debug_level'] == 0){
// Server-side cache: Check if cache-directory has been created
if(!is_dir($cachedir)){
if(!@mkdir($cachedir, 0777)){
$cssp->report_error('The cache directory doesn\'t exist! Please create a directory \"cache\" in '.dirname(realpath(__FILE__)).' and make it writeable.');
}
}
elseif(!is_writable($cachedir)){
if(!@chmod($cachedir, 0777)){
$cssp->report_error('The cache directory '.realpath($cachedir).' is not writeable!');
}
}
// Server-side cache: Create a name for the new cache file
$cachefile = md5(
$browser->platform.
$browser->platform_version.
$browser->platform_type.
$browser->engine.
$browser->engine_version.
$browser->browser.
$browser->browser_version.
realpath($file)
).'.txt';
//Caching mechanism with file locking (don't rebuild cache multiple times)
$css_mtime = filemtime($file);
$cn = $cachedir.'/'.$cachefile;
$attempt = 1;
while(true){
// Server-side cache: Check if a cached version of the file already exists
if(($fc=file_exists($cn)) && filemtime($cn) >= $css_mtime){
$incache = true;
break;
}
elseif($cssp->config['debug_level'] == 0){
$cache_lock = fopen($cn, 'a+');
if(!$fc){
touch($cn, $css_mtime-1);
}
if(flock($cache_lock, LOCK_EX | LOCK_NB)){
//If we locked the file don't stop on user abort
ignore_user_abort(true);
break;
}
else{
fclose($cache_lock);
sleep(1);
//Clearing filemtime cache
clearstatcache();
}
}
else{
break;
}
}
} // End if($cssp->config['debug_level'] == 0)
// Server-side cache: Cached version of the file does not yet exist
if(!$incache){
// Init plugin list
$plugin_list = array();
// Load plugins (if not already loaded)
if(!$plugins_loaded){
$plugindir = 'plugins';
if($handle = opendir($plugindir)){
while(false !== ($pluginfile = readdir($handle))){
if($pluginfile != '.' && $pluginfile != '..' && is_file($plugindir.'/'.$pluginfile) && pathinfo($plugindir.'/'.$pluginfile,PATHINFO_EXTENSION) == 'php' && !function_exists(substr($pluginfile, 0, -4))){
include($plugindir.'/'.$pluginfile); // Include the plugin
$plugins_available[] = substr($pluginfile, 0, -4); // Add the plugin to the list of available plugins
}
}
closedir($handle);
}
$plugins_loaded = true;
}
$cssp->sort_plugins();
// Load the file into cssp
$cssp->load_file($file, true);
// Set global filepath constant for the current file
$filepath = dirname($file);
if($filepath != '.'){
$cssp->global_constants['FILEPATH'] = $filepath;
}
else{
$cssp->global_constants['FILEPATH'] = '';
}
// Get plugin list for the before parse hook
$found = false;
foreach($cssp->code as $line){
if(!$found){
if(preg_match('/^[\s\t]*@turbine/i', $line) == 1){
$found = true;
}
}
else{
preg_match('~^\s+plugins:(.*?)(?://|$)~', $line, $matches);
if(count($matches) == 2){
$matches[1] = rtrim($matches[1], ';'); // Strip semicolons
$plugin_list = array_merge($plugin_list, $cssp->tokenize($matches[1], ','));
break;
}
}
}
$plugins_used = array_unique(array_merge($plugins_used, $plugin_list));
// Get plugin options
$plugin_settings = array();
$found = false;
foreach($cssp->code as $line){
if(!$found){
if(preg_match('/^[\s\t]*@turbine/i', $line) == 1){
$found = true;
}
}
else{
if($line == ''){
break;
}
else{
preg_match('~^\s+([a-zA-Z0-9]+):(.*?)(?://|$)~', $line, $matches);
if(count($matches) == 3){
$plugin_settings_key = trim($matches[1]);
$plugin_settings_val = trim(rtrim($matches[2], ';')); // Dont forget to strip semicolons
$plugin_settings[$plugin_settings_key] = $plugin_settings_val;
}
}
}
}
// Check if there is any plugin in the list that doesn't actually exist
$plugin_diff = array_diff($plugin_list, $plugins_available);
if(!empty($plugin_diff)){
$cssp->report_error('The following plugins are not present in your Turbine installation: '.ucfirst(implode(', ', $plugin_diff)));
}
$cssp->set_indention_char(); // Set the character(s) used for code indention
$cssp->apply_plugins('before_parse', $plugin_list, $cssp->code); // Apply plugins for before parse
$cssp->parse(); // Parse the code
$cssp->apply_plugins('before_compile', $plugin_list, $cssp->parsed); // Apply plugins for before compile
$cssp->compile(); // Do the Turbine magic
$cssp->apply_plugins('before_glue', $plugin_list, $cssp->parsed); // Apply plugins for before glue
// Set compression mode
if(isset($cssp->parsed['global']['@turbine']['compress'][0])){
$compress = (bool) $cssp->parsed['global']['@turbine']['compress'][0];
}
else{
$compress = false;
}
// Add title
if(isset($cssp->parsed['global']['@turbine']['title'])){
$css_title = array_merge($css_title, $cssp->parsed['global']['@turbine']['title']);
}
unset($cssp->parsed['global']['@turbine']); // Remove configuration @-rule
$output = $cssp->glue($compress); // Glue css output
$cssp->apply_plugins('before_output', $plugin_list, $output); // Apply plugins for before output
$cssp->reset(); // Reset the parser
// Add output to cache
if($cssp->config['debug_level'] == 0){
if ($cache_lock) {
fwrite($cache_lock,$output);
fclose($cache_lock);
} else {
file_put_contents($cachedir.'/'.$cachefile, $output);
}
}
}
else{
// Server-side cache: read the cached version of the file
$output = file_get_contents($cachedir.'/'.$cachefile);
}
// Add to final css
$css .= $output;
}
}
// File not found, report error
else{
$cssp->report_error('Style file '.$file.' not found. Is the base path in config.php configured correctly?');
}
} // End foreach($files as $file)
// Show errors
if($cssp->config['debug_level'] > 0 && !empty($cssp->errors)){
$error_message = implode('\\00000A', $cssp->errors);
$css .= turbine_css_error_message($error_message);
}
// Send headers
header('Content-Type: text/css');
if($cssp->config['debug_level'] > 0){
header('Cache-Control: must-revalidate, pre-check=0, no-store, no-cache, max-age=0, post-check=0');
}
else{
if(!$cssp->config['expire_in_future']){
header('Cache-Control: no-cache, must-revalidate');
}
header('Expires: '.gmdate('D, d M Y H:i:s', time() + intval($cssp->config['expire_in_future'])).' GMT');
header('Vary: Accept-Encoding');
header('Content-type: text/css');
header('ETag: '.$etag);
}
// End benchmark
$end = microtime(true);
// Begin header
echo "/*\r\n";
// Output stylesheet title(s)
if(!empty($css_title)){
echo implode("\r\n", $css_title)."\r\n";
}
// You can remove this if you REALLY want to
echo 'Stylesheet generated by Turbine - http://turbine.peterkroener.de/';
// Output debugging info
if($cssp->config['debug_level'] > 0){
$debugginginfo = array(
'Version' => TURBINEVERSION,
'Path' => TURBINEPATH,
'Benchmark' => $end - $start,
'Plugins used' => implode(', ', $plugins_used),
'Browser' => $browser->browser,
'Browser version' => $browser->browser_version,
'Browser engine' => $browser->engine,
'Browser engine version' => $browser->engine_version,
'Platform' => $browser->platform,
'Platform version' => $browser->platform_version,
'Platform type' => $browser->platform_type
);
foreach($debugginginfo as $key => $value){
echo "\r\n$key: $value";
}
}
// Close header, output CSS
echo "\r\n*/\r\n".$css;
} // End if($_GET['files'])
?>