-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmod.automin.php
574 lines (463 loc) · 14.4 KB
/
mod.automin.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* ExpressionEngine - by EllisLab
*
* @package ExpressionEngine
* @author ExpressionEngine Dev Team
* @copyright Copyright (c) 2003 - 2011, EllisLab, Inc.
* @license http://expressionengine.com/user_guide/license.html
* @link http://expressionengine.com
* @since Version 2.0
* @filesource
*/
// ------------------------------------------------------------------------
/**
* AutoMin Module Front End File
*
* @package ExpressionEngine
* @subpackage Addons
* @category Module
* @author Jesse Bunch
* @link http://getbunch.com/
*/
class Automin {
/**
* Not used, but required by EE.
* @var string
* @author Jesse Bunch
*/
public $return_data;
/**
* Constants for different markup types
* @var string
* @author Jesse Bunch
*/
const MARKUP_TYPE_JS = 'js';
const MARKUP_TYPE_CSS = 'css';
const MARKUP_TYPE_LESS = 'less';
/**
* Constructor
* @author Jesse Bunch
*/
public function __construct() {
$this->EE =& get_instance();
$this->EE->load->model('automin_model');
$this->EE->load->library('minification_library');
$this->EE->load->library('automin_caching_library');
}
/**
* exp:automin:css
* Parses both LESS and CSS files
* @author Jesse Bunch
*/
public function css() {
$this->_write_log('Processing CSS/LESS');
return $this->return_data = $this->_process_markup(
$this->EE->TMPL->tagdata,
self::MARKUP_TYPE_CSS
);
}
/**
* exp:automin:js
* @author Jesse Bunch
*/
public function js() {
$this->_write_log('Processing JS');
return $this->return_data = $this->_process_markup(
$this->EE->TMPL->tagdata,
self::MARKUP_TYPE_JS
);
}
/**
* exp:automin:less
* This has been deprecated in favor of the css method.
* @author Jesse Bunch
*/
public function less() {
$this->_write_log('Processing LESS');
return $this->return_data = $this->_process_markup(
$this->EE->TMPL->tagdata,
self::MARKUP_TYPE_LESS
);
}
/**
* Main processing routine, to be used for all types
* @param $markup
* @param $markup_type One of the MARKUP_TYPE_X values
* @return string The new markup
* @author Jesse Bunch
*/
private function _process_markup($markup, $markup_type) {
// AutoMin disabled? Go no further...
if (!$this->EE->automin_model->is_automin_enabled()) {
return $markup;
}
// Gather information
$markup = $this->EE->TMPL->tagdata;
$filename_array = $this->_extract_filenames($markup, $markup_type);
$filename_array = $this->_prep_filenames($filename_array);
$last_modified = $this->_find_last_modified_timestamp($filename_array);
// File Extension
// LESS files should have a .css extension
$extension = ($markup_type == self::MARKUP_TYPE_LESS) ? self::MARKUP_TYPE_CSS : $markup_type;
$cache_key = $this->EE->automin_caching_library->get_cache_key($markup, $extension);
// Fetch and validate cache
$cache_filename = $this->EE->automin_caching_library->fetch_cache(
$cache_key,
$markup,
$last_modified
);
// Output cache file, if valid
if (FALSE !== $cache_filename) {
$this->_write_log("Cache found and valid");
return $this->_format_output($cache_filename, $last_modified, $markup_type);
}
// Combine files, parse @imports if appropriate
$combined_file_data = $this->_combine_files(
$filename_array,
($markup_type == self::MARKUP_TYPE_CSS
OR $markup_type == self::MARKUP_TYPE_LESS)
);
// If we couldn't read some files, return original tags
if (FALSE === $combined_file_data) {
$this->_write_log("ERROR: One or more of your files couldn't be read.");
return $markup;
}
// Attempt compilation and compression
$data_length_before = strlen($combined_file_data) / 1024;
$combined_file_data = $this->_compile_and_compress(
$combined_file_data,
$markup_type
);
$data_length_after = strlen($combined_file_data) / 1024;
// Log the savings
$data_savings_kb = $data_length_before - $data_length_after;
$data_savings_percent = ($data_savings_kb / $data_length_before) * 100;
$data_savings_message = sprintf(
'(%s Compression) Before: %1.0fkb / After: %1.0fkb / Data reduced by %1.2fkb or %1.2f%%',
strtoupper($markup_type),
$data_length_before,
$data_length_after,
$data_savings_kb,
$data_savings_percent
);
$this->_write_log($data_savings_message);
// If compilation fails, return original tags
if (FALSE === $combined_file_data) {
$this->_write_log("ERROR: Compilation failed. Perhaps you have a syntax error?");
return $markup;
}
// Cache output
$cache_result = $this->EE->automin_caching_library->write_cache($cache_key, $combined_file_data);
// If caching failed, return original tags
if (FALSE === $cache_result) {
$this->_write_log("ERROR: Caching is disabled or we were unable to write to your cache directory.");
return $markup;
}
// Return the markup output
return $this->_format_output($cache_result, $last_modified, $markup_type);
}
/**
* Compress and compile (if necessary) the code.
* @param string $code
* @param string $markup_type One of the MARKUP_TYPE_X values
* @return mixed FALSE if failure, string if success
* @author Jesse Bunch
*/
private function _compile_and_compress($code, $markup_type) {
@ini_set('memory_limit', '50M');
@ini_set('memory_limit', '128M');
@ini_set('memory_limit', '256M');
@ini_set('memory_limit', '512M');
@ini_set('memory_limit', '1024M');
try {
switch($markup_type) {
case self::MARKUP_TYPE_LESS:
// Compile with LESS
require_once('libraries/lessphp/lessc.inc.php');
$less_obj = new lessc();
$code = $less_obj->parse($code);
// Compress CSS
require_once('libraries/class.minify_css_compressor.php');
$code = Minify_CSS_Compressor::process($code);
break;
case self::MARKUP_TYPE_CSS:
// Compress CSS
require_once('libraries/class.minify_css_compressor.php');
$code = Minify_CSS_Compressor::process($code);
break;
case self::MARKUP_TYPE_JS:
// Compile JS
require_once('libraries/class.jsmin.php');
$code = JSMin::minify($code);
// require_once('libraries/class.minify_js_closure.php');
// $code = Minify_JS_ClosureCompiler::minify($code);
break;
}
} catch (Exception $e) {
exit($e->getMessage());
$this->_write_log('Compilation Exception: ' . $e->getMessage());
return FALSE;
}
return $code;
}
/**
* Formats the output into valid markup
* @param string $cache_filename The url path to the cache file.
* @param integer $last_modified Timestamp of the latest-modified file
* @param string $markup_type One of the MARKUP_TYPE_X values
* @return string
* @author Jesse Bunch
*/
private function _format_output($cache_filename, $last_modified, $markup_type) {
$markup_output = '';
// Append modified time to the filename
$cache_filename = "$cache_filename?modified=$last_modified";
// Format attributes
$tag_attributes = $this->_fetch_colon_params('attribute');
$attributes_string = '';
foreach($tag_attributes as $key=>$value) {
$attributes_string .= " $key=\"$value\" ";
}
// Create tag
switch($markup_type) {
case self::MARKUP_TYPE_CSS:
case self::MARKUP_TYPE_LESS:
$markup_output = sprintf(
'<link href="%s" %s>',
$cache_filename,
$attributes_string
);
break;
case self::MARKUP_TYPE_JS:
$markup_output = sprintf(
'<script src="%s" %s></script>',
$cache_filename,
$attributes_string
);
break;
}
return $markup_output;
}
/**
* Returns a string of all the files combined. If a file cannot be read,
* this function will return FALSE.
* @param array $files_array Pass in the output of _prep_filenames
* @return mixed string or FALSE
* @author Jesse Bunch
*/
private function _combine_files($files_array, $should_parse_imports = FALSE) {
$combined_output = '';
foreach ($files_array as $file_array) {
if (!file_exists($file_array['server_path'])
OR !is_readable($file_array['server_path'])) {
return FALSE;
}
// Get file contents
$combined_output .= file_get_contents($file_array['server_path']);
// Parse @imports
if ($should_parse_imports) {
$combined_output = $this->_parse_css_imports(
$combined_output,
$file_array['url_path']
);
}
}
return $combined_output;
}
/**
* Returns the timestamp of the latest modified file
* @param array $files_array Pass in the output of _prep_filenames
* @return int
* @author Jesse Bunch
*/
private function _find_last_modified_timestamp($files_array) {
$last_modified_timestamp = 0;
foreach ($files_array as $file_array) {
if ($file_array['last_modified']
AND $file_array['last_modified'] > $last_modified_timestamp) {
$last_modified_timestamp = $file_array['last_modified'];
}
}
return $last_modified_timestamp;
}
/**
* Gathers information about each file and normalizes
* the filename and path.
* @param array $filenames_array
* @return array
* - url_path
* - server_path
* - last_modified
* @author Jesse Bunch
*/
private function _prep_filenames($filenames_array) {
$information_array = array();
foreach($filenames_array as $index => $filename) {
// Path for URLs
$information_array[$index]['url_path'] = $this->_normalize_file_path(
$filename
);
// Path for reading
$information_array[$index]['server_path'] = $this->_normalize_file_path(
$filename,
'',
TRUE
);
// Last modified
$information_array[$index]['last_modified'] = @filemtime(
$information_array[$index]['server_path']
);
}
return $information_array;
}
/**
* Extracts the filenames from the markup based on the provided
* markup type.
* @param string $markup
* @param string $markup_type Use one of the constants MARKUP_TYPE_X
* @return array (of filenames)
* @author Jesse Bunch
*/
private function _extract_filenames($markup, $markup_type) {
$matches_array;
switch($markup_type) {
case self::MARKUP_TYPE_CSS:
case self::MARKUP_TYPE_LESS:
preg_match_all(
"/href\=\"([A-Za-z0-9\.\/\_\-\?\=\:]+.[css|less])\"/",
$markup,
$matches_array
);
break;
case self::MARKUP_TYPE_JS:
preg_match_all(
"/src\=\"([A-Za-z0-9\.\/\_\-\?\=\:]+.js)\"/",
$markup,
$matches_array
);
break;
}
// Matches?
if (count($matches_array) >= 2) {
return $matches_array[1];
}
return FALSE;
}
/**
* Extracts parameters from the tag param array that are
* considered to be colon parameters. e.g. attribute:param="value"
* @param string $colon_key The "attribute" part
* @return array key/value pairs (param = "value")
* @author Jesse Bunch
*/
private function _fetch_colon_params($colon_key) {
// Get all params
$all_params = $this->EE->TMPL->tagparams;
// Pull out params that start with "custom:"
$colon_params = array();
if (is_array($all_params) && count($all_params)) {
$colon_key_end_index = strlen($colon_key) + 1;
foreach ($all_params as $key => $val) {
if (strncmp($key, $colon_key, $colon_key_end_index-1) == 0) {
$colon_params[substr($key, $colon_key_end_index)] = $val;
}
}
}
return $colon_params;
}
/**
* File paths may be in different formats. This function will take
* any file path and normalize it to on of two formats depending on the
* parameters you pass.
* @param string $file_path The path to normalize.
* @param string $relative_path If $file_path is a relative path, we need
* the path to the relative file. If no path is supplied, the dirname of
* the current URI is used.
* @param bool $include_root If TRUE, the full server path is returned. If
* FALSE, the path returned is relative to the document root.
* @return string
* @author Jesse Bunch
*/
private function _normalize_file_path($file_path, $relative_path='', $include_root = FALSE) {
// If the path is a full URL, return it
// We don't currently fetch remote files
if (0 === stripos($file_path, 'http')
OR 0 === stripos($file_path, '//')) {
return $file_path;
}
// Get the relative path
if (!$relative_path) {
$relative_path = $_SERVER['REQUEST_URI'];
}
// Relative path should leave out the document root
$relative_path = str_replace($_SERVER['DOCUMENT_ROOT'], '', $relative_path);
// Parse the path
$path_parts = pathinfo($relative_path);
$dirname = $path_parts['dirname'].'/';
// If not document-root relative, we must add the URI
// of the calling page to make it document-root relative
if (substr($file_path, 0, 1) != '/') {
$file_path = $dirname.$file_path;
}
// Include full root path?
if ($include_root) {
$file_path = $_SERVER['DOCUMENT_ROOT'] . $file_path;
}
if (version_compare(APP_VER, '2.6', '>=')) {
$this->EE->load->helper('string');
$sFixedslashes = reduce_double_slashes($file_path);
} else {
$sFixedslashes = $this->EE->functions->remove_double_slashes($file_path);
}
return $sFixedslashes;
}
/**
* Reads a file from the filesystem
* @param string $file_path Full server path to the file to read
* @return string
* @author Jesse Bunch
*/
private function _read_file($file_path) {
return @file_get_contents($file_path);
}
/**
* Looks for and parses @imports in the provided string.
* @param string $string
* @param string $relative_path Passed to _normalize_file_path(). See
* that function's documentation for details on this param.
* @return string
* @author Jesse Bunch
*/
private function _parse_css_imports(&$string, $relative_path = '') {
// Get all @imports
$matches = array();
preg_match_all('/\@import\s[url\(]?[\'\"]{1}([A-Za-z0-9\.\/\_\-]+)[\'\"]{1}[\)]?[;]?/', $string, $matches);
$matched_lines = $matches[0];
$matched_filenames = $matches[1];
$count = 0;
// Iterate and parse
foreach($matched_filenames as $filename) {
$filename = $this->_normalize_file_path($filename, $relative_path, TRUE);
// Read the file
$file_data = $this->_read_file($filename);
// If we have data, replace the @import
if ($file_data) {
$string = str_replace($matched_lines[$count], $file_data, $string);
}
$count++;
}
return $string;
}
/**
* Writes the message to the template log
* @param string $message
* @return void
* @author Jesse Bunch
*/
private function _write_log($message) {
$this->EE->TMPL->log_item("AutoMin Module: $message");
}
}
/* End of file mod.automin.php */
/* Location: /system/expressionengine/third_party/automin/mod.automin.php */