forked from TurbineCSS/Turbine
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcss.php
More file actions
341 lines (261 loc) · 9.37 KB
/
css.php
File metadata and controls
341 lines (261 loc) · 9.37 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
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
<?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 ;
* @var string $_GET['config'] Path to the plugin configuration file
*/
// Benchmark start time
$start = microtime(true);
// Constants
define('TURBINEVERSION', trim(file_get_contents('version.txt')));
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();
}
// Load libraries
include('lib/browser/browser.php');
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;
// 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_exists($file)){
// CSSP or CSS?
$fileinfo = pathinfo($file);
if($fileinfo['extension'] == 'css'){
// Simply include normal css files in the output. Minify if not debugging or configured not 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
// 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.
$file
).'.txt';
// Server-side cache: Check if a cached version of the file already exists
if(file_exists($cachedir.'/'.$cachefile) && filemtime($cachedir.'/'.$cachefile) >= filemtime($file)){
$incache = true;
}
// Server-side cache: Cached version of the file does not yet exist
if(!$incache){
// 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);
}
}
closedir($handle);
}
$plugins_loaded = true;
}
// 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
$plugin_list = array();
$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){
$plugin_list = $cssp->tokenize($matches[1], ',');
break;
}
}
}
// Get plugin options
$plugin_settings = array();
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($matches[2]);
if(in_array($plugin_settings_key, $plugin_list)){
$plugin_settings[$plugin_settings_key] = $plugin_settings_val;
}
}
}
}
}
$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;
}
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){
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?');
}
}
// Show errors
if($cssp->config['debug_level'] > 0 && !empty($cssp->errors)){
$error_message = implode('\\00000A', $cssp->errors);
$css = $css.'body: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; }';
}
// 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{
header('Cache-Control: no-cache, must-revalidate');
header('Expires: '.gmdate('D, d M Y H:i:s').' GMT');
header('Content-type: text/css');
header('ETag: '.$etag);
}
// End benchmark
$end = microtime(true);
// Output header line
echo "/*\r\n\tStylesheet 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,
'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\t$key: $value";
}
}
// Close comment, output CSS
echo "\r\n*/\r\n".$css;
}
?>