1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package CleverStyle Framework |
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
5
|
|
|
* @copyright Copyright (c) 2014-2016, Nazar Mokrynskyi |
6
|
|
|
* @license MIT License, see license.txt |
7
|
|
|
*/ |
8
|
|
|
namespace cs\Page; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class includes few methods used for processing CSS, JS and HTML files before putting into cache |
12
|
|
|
* |
13
|
|
|
* This is because CSS and HTML files may contain other includes of other CSS, JS files, images, fonts and so on with absolute and relative paths. |
14
|
|
|
* Methods of this class handle all this includes, applies basic minification to CSS and JS files and produce single resulting file (relative paths to |
15
|
|
|
* files that can't be embedded are converted to absolute). This allows to decrease number of HTTP requests on page and avoid breaking of relative paths for |
16
|
|
|
* fonts, images and other includes after putting them into cache directory as well as minimize contents size by removing comments and other redundant stuff. |
17
|
|
|
*/ |
18
|
|
|
class Includes_processing { |
19
|
|
|
/** |
20
|
|
|
* Do not inline files bigger than 4 KiB |
21
|
|
|
*/ |
22
|
|
|
const MAX_EMBEDDING_SIZE = 4096; |
23
|
|
|
protected static $extension_to_mime = [ |
24
|
|
|
'jpeg' => 'image/jpg', |
25
|
|
|
'jpe' => 'image/jpg', |
26
|
|
|
'jpg' => 'image/jpg', |
27
|
|
|
'gif' => 'image/gif', |
28
|
|
|
'png' => 'image/png', |
29
|
|
|
'svg' => 'image/svg+xml', |
30
|
|
|
'svgz' => 'image/svg+xml', |
31
|
|
|
'woff' => 'application/font-woff', |
32
|
|
|
//'woff2' => 'application/font-woff2' |
33
|
|
|
]; |
34
|
|
|
/** |
35
|
|
|
* Analyses file for images, fonts and css links and include they content into single resulting css file. |
36
|
|
|
* |
37
|
|
|
* Supports next file extensions for possible includes: |
38
|
|
|
* jpeg, jpe, jpg, gif, png, ttf, ttc, svg, svgz, woff, eot, css |
39
|
|
|
* |
40
|
|
|
* @param string $data Content of processed file |
41
|
|
|
* @param string $file Path to file, that includes specified in previous parameter content |
42
|
|
|
* @param string[] $not_embedded_resources Some resources like images and fonts might not be embedded into resulting CSS because of their size |
43
|
|
|
* |
44
|
|
|
* @return string $data |
45
|
|
|
*/ |
46
|
10 |
|
public static function css ($data, $file, &$not_embedded_resources = []) { |
47
|
10 |
|
$dir = dirname($file); |
48
|
|
|
/** |
49
|
|
|
* Remove comments, tabs and new lines |
50
|
|
|
*/ |
51
|
10 |
|
$data = preg_replace('#(/\*.*?\*/)|\t|\n|\r#s', ' ', $data); |
52
|
|
|
/** |
53
|
|
|
* Remove unnecessary spaces |
54
|
|
|
*/ |
55
|
10 |
|
$data = preg_replace('/\s*([,;>{}(])\s*/', '$1', $data); |
56
|
10 |
|
$data = preg_replace('/\s+/', ' ', $data); |
57
|
|
|
/** |
58
|
|
|
* Return spaces required in media queries |
59
|
|
|
*/ |
60
|
10 |
|
$data = preg_replace('/\s(and|or)\(/', ' $1 (', $data); |
61
|
|
|
/** |
62
|
|
|
* Duplicated semicolons |
63
|
|
|
*/ |
64
|
10 |
|
$data = preg_replace('/;+/m', ';', $data); |
65
|
|
|
/** |
66
|
|
|
* Minify rgb colors declarations |
67
|
|
|
*/ |
68
|
10 |
|
$data = preg_replace_callback( |
69
|
10 |
|
'/rgb\(([0-9,.]+)\)/i', |
70
|
|
|
function ($rgb) { |
71
|
6 |
|
$rgb = explode(',', $rgb[1]); |
72
|
|
|
return |
73
|
|
|
'#'. |
74
|
6 |
|
str_pad(dechex($rgb[0]), 2, 0, STR_PAD_LEFT). |
75
|
6 |
|
str_pad(dechex($rgb[1]), 2, 0, STR_PAD_LEFT). |
76
|
6 |
|
str_pad(dechex($rgb[2]), 2, 0, STR_PAD_LEFT); |
77
|
10 |
|
}, |
78
|
|
|
$data |
79
|
|
|
); |
80
|
|
|
/** |
81
|
|
|
* Minify repeated colors declarations |
82
|
|
|
*/ |
83
|
10 |
|
$data = preg_replace('/#([0-9a-f])\1([0-9a-f])\2([0-9a-f])\3/i', '#$1$2$3', $data); |
84
|
|
|
/** |
85
|
|
|
* Remove unnecessary zeros |
86
|
|
|
*/ |
87
|
10 |
|
$data = preg_replace('/(\D)0\.(\d+)/i', '$1.$2', $data); |
88
|
|
|
/** |
89
|
|
|
* Unnecessary spaces around colons (should have whitespace character after, otherwise might be `.c :disabled` and will be handled incorrectly) |
90
|
|
|
*/ |
91
|
10 |
|
$data = preg_replace('/\s*:\s+/', ':', $data); |
92
|
|
|
/** |
93
|
|
|
* Includes processing |
94
|
|
|
*/ |
95
|
10 |
|
$data = preg_replace_callback( |
96
|
10 |
|
'/url\((.*)\)|@import\s*(?:url\()?\s*([\'"].*[\'"])\s*\)??(.*);/U', |
97
|
|
|
function ($match) use ($dir, &$not_embedded_resources) { |
98
|
10 |
|
$path_matched = @$match[2] ?: $match[1]; |
99
|
10 |
|
$path = trim($path_matched, '\'" '); |
100
|
10 |
|
$link = explode('?', $path, 2)[0]; |
101
|
10 |
|
if (!static::is_relative_path_and_exists($link, $dir)) { |
102
|
10 |
|
return $match[0]; |
103
|
|
|
} |
104
|
10 |
|
$extension = file_extension($link); |
105
|
|
|
/** |
106
|
|
|
* Only process CSS imports without media queries, imports with media queries will just be corrected to absolute paths |
107
|
|
|
*/ |
108
|
10 |
|
if ($extension == 'css' && @$match[2] && !trim(@$match[3])) { |
109
|
|
|
/** |
110
|
|
|
* For recursive includes processing, if CSS file includes others CSS files |
111
|
|
|
*/ |
112
|
6 |
|
return static::css(file_get_contents("$dir/$link"), "$dir/$link", $not_embedded_resources); |
113
|
|
|
} |
114
|
10 |
|
$content = file_get_contents("$dir/$link"); |
115
|
10 |
|
if (!isset(static::$extension_to_mime[$extension]) || filesize("$dir/$link") > static::MAX_EMBEDDING_SIZE) { |
116
|
10 |
|
$path_relatively_to_the_root = str_replace(getcwd(), '', realpath("$dir/$link")); |
117
|
10 |
|
$path_relatively_to_the_root .= '?'.substr(md5($content), 0, 5); |
118
|
10 |
|
if (isset(static::$extension_to_mime[$extension]) && strpos($path, '?') === false) { |
119
|
6 |
|
$not_embedded_resources[] = $path_relatively_to_the_root; |
120
|
|
|
} |
121
|
10 |
|
return str_replace($path_matched, "'".str_replace("'", "\\'", $path_relatively_to_the_root)."'", $match[0]); |
122
|
|
|
} |
123
|
6 |
|
$mime_type = static::$extension_to_mime[$extension]; |
124
|
6 |
|
$content = base64_encode($content); |
125
|
6 |
|
return str_replace($path, "data:$mime_type;charset=utf-8;base64,$content", $match[0]); |
126
|
10 |
|
}, |
127
|
|
|
$data |
128
|
|
|
); |
129
|
10 |
|
return trim($data); |
130
|
|
|
} |
131
|
|
|
/** |
132
|
|
|
* Simple and fast JS minification |
133
|
|
|
* |
134
|
|
|
* @param string $data |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
10 |
|
public static function js ($data) { |
139
|
|
|
/** |
140
|
|
|
* Split into array of lines |
141
|
|
|
*/ |
142
|
10 |
|
$data = explode("\n", $data); |
143
|
|
|
/** |
144
|
|
|
* Flag that is `true` when inside comment |
145
|
|
|
*/ |
146
|
10 |
|
$comment = false; |
147
|
|
|
/** |
148
|
|
|
* Set of symbols that are safe to be concatenated without new line with anything else |
149
|
|
|
*/ |
150
|
|
|
$regexp = /** @lang PhpRegExp */ |
151
|
10 |
|
'[:;,.+\-*/{}?><^\'"\[\]=&(]'; |
152
|
10 |
|
$continue_after_position = -1; |
153
|
10 |
|
foreach ($data as $index => &$d) { |
154
|
10 |
|
if ($continue_after_position >= $index) { |
155
|
6 |
|
continue; |
156
|
|
|
} |
157
|
10 |
|
$next_line = isset($data[$index + 1]) ? trim($data[$index + 1]) : ''; |
158
|
|
|
/** |
159
|
|
|
* Remove starting and trailing spaces |
160
|
|
|
*/ |
161
|
10 |
|
$d = trim($d); |
162
|
|
|
/** |
163
|
|
|
* Remove single-line comments |
164
|
|
|
*/ |
165
|
10 |
|
if (mb_strpos($d, '//') === 0) { |
166
|
10 |
|
$d = ''; |
167
|
10 |
|
continue; |
168
|
|
|
} |
169
|
|
|
/** |
170
|
|
|
* Starts with multi-line comment |
171
|
|
|
*/ |
172
|
10 |
|
if (mb_strpos($d, '/*') === 0) { |
173
|
10 |
|
$comment = true; |
174
|
|
|
} |
175
|
10 |
|
if (!$comment) { |
176
|
10 |
|
$backticks_position = strpos($d, '`'); |
177
|
|
|
/** |
178
|
|
|
* Handling template strings can be tricky (since they might be multi-line), so let's fast-forward to the last backticks position and continue |
179
|
|
|
* from there |
180
|
|
|
*/ |
181
|
10 |
|
if ($backticks_position !== false) { |
182
|
6 |
|
$last_item_with_backticks = array_keys( |
183
|
|
|
array_filter( |
184
|
|
|
$data, |
185
|
|
|
function ($d) { |
186
|
6 |
|
return strpos($d, '`') !== false; |
187
|
6 |
|
} |
188
|
|
|
) |
189
|
|
|
); |
190
|
6 |
|
$last_item_with_backticks = array_pop($last_item_with_backticks); |
191
|
6 |
|
if ($last_item_with_backticks > $index) { |
192
|
6 |
|
$continue_after_position = $last_item_with_backticks; |
193
|
6 |
|
continue; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
/** |
197
|
|
|
* Add new line at the end if only needed |
198
|
|
|
*/ |
199
|
|
|
if ( |
200
|
10 |
|
$d && |
201
|
10 |
|
$next_line && |
202
|
10 |
|
!preg_match("#$regexp\$#", $d) && |
203
|
10 |
|
!preg_match("#^$regexp#", $next_line) |
204
|
|
|
) { |
205
|
10 |
|
$d .= "\n"; |
206
|
|
|
} |
207
|
|
|
/** |
208
|
|
|
* Single-line comment |
209
|
|
|
*/ |
210
|
10 |
|
$d = preg_replace('#^\s*//[^\'"]+$#', '', $d); |
211
|
|
|
/** |
212
|
|
|
* If we are not sure - just add new like afterwards |
213
|
|
|
*/ |
214
|
10 |
|
$d = preg_replace('#//.*$#', "\\0\n", $d); |
215
|
|
|
} else { |
216
|
|
|
/** |
217
|
|
|
* End of multi-line comment |
218
|
|
|
*/ |
219
|
10 |
|
if (strpos($d, '*/') !== false) { |
220
|
10 |
|
$d = explode('*/', $d)[1]; |
221
|
10 |
|
$comment = false; |
222
|
|
|
} else { |
223
|
10 |
|
$d = ''; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
} |
227
|
10 |
|
$data = implode('', $data); |
228
|
10 |
|
$data = str_replace('</script>', '<\/script>', $data); |
229
|
10 |
|
return trim($data, ';').';'; |
230
|
|
|
} |
231
|
|
|
/** |
232
|
|
|
* Analyses file for scripts and styles, combines them into resulting files in order to optimize loading process |
233
|
|
|
* (files with combined scripts and styles will be created) |
234
|
|
|
* |
235
|
|
|
* @param string $data Content of processed file |
236
|
|
|
* @param string $file Path to file, that includes specified in previous parameter content |
237
|
|
|
* @param string $base_target_file_path Base filename for resulting combined files |
238
|
|
|
* @param bool $vulcanization Whether to put combined files separately or to make includes built-in (vulcanization) |
239
|
|
|
* @param string[] $not_embedded_resources Resources like images/fonts might not be embedded into resulting CSS because of big size or CSS/JS because of CSP |
240
|
|
|
* |
241
|
|
|
* @return string |
242
|
|
|
*/ |
243
|
8 |
|
public static function html ($data, $file, $base_target_file_path, $vulcanization, &$not_embedded_resources = []) { |
244
|
8 |
|
static::html_process_links_and_styles($data, $file, $base_target_file_path, $vulcanization, $not_embedded_resources); |
245
|
8 |
|
static::html_process_scripts($data, $file, $base_target_file_path, $vulcanization, $not_embedded_resources); |
246
|
|
|
// Removing HTML comments (those that are mostly likely comments, to avoid problems) |
247
|
8 |
|
$data = preg_replace_callback( |
248
|
8 |
|
'/^\s*<!--([^>-].*[^-])?-->/Ums', |
249
|
8 |
|
function ($matches) { |
250
|
8 |
|
return mb_strpos('--', $matches[1]) === false ? '' : $matches[0]; |
251
|
8 |
|
}, |
252
|
|
|
$data |
253
|
|
|
); |
254
|
8 |
|
return preg_replace("/\n+/", "\n", $data); |
255
|
|
|
} |
256
|
|
|
/** |
257
|
|
|
* @param string $data Content of processed file |
258
|
|
|
* @param string $file Path to file, that includes specified in previous parameter content |
259
|
|
|
* @param string $base_target_file_path Base filename for resulting combined files |
260
|
|
|
* @param bool $vulcanization Whether to put combined files separately or to make includes built-in (vulcanization) |
261
|
|
|
* @param string[] $not_embedded_resources Resources like images/fonts might not be embedded into resulting CSS because of big size or CSS/JS because of CSP |
262
|
|
|
*/ |
263
|
8 |
|
protected static function html_process_scripts (&$data, $file, $base_target_file_path, $vulcanization, &$not_embedded_resources) { |
264
|
8 |
|
if (!preg_match_all('/<script(.*)<\/script>/Uims', $data, $scripts)) { |
265
|
8 |
|
return; |
266
|
|
|
} |
267
|
8 |
|
$scripts_content = ''; |
268
|
8 |
|
$scripts_to_replace = []; |
269
|
8 |
|
$dir = dirname($file); |
270
|
8 |
|
foreach ($scripts[1] as $index => $script) { |
271
|
8 |
|
$script = explode('>', $script, 2); |
272
|
8 |
|
if (preg_match('/src\s*=\s*[\'"](.*)[\'"]/Uims', $script[0], $url)) { |
273
|
8 |
|
$url = $url[1]; |
274
|
8 |
|
if (!static::is_relative_path_and_exists($url, $dir)) { |
275
|
4 |
|
continue; |
276
|
|
|
} |
277
|
8 |
|
$scripts_to_replace[] = $scripts[0][$index]; |
278
|
8 |
|
$scripts_content .= file_get_contents("$dir/$url").";\n"; |
279
|
|
|
} else { |
280
|
8 |
|
$scripts_to_replace[] = $scripts[0][$index]; |
281
|
8 |
|
$scripts_content .= "$script[1];\n"; |
282
|
|
|
} |
283
|
|
|
} |
284
|
8 |
|
$scripts_content = static::js($scripts_content); |
285
|
8 |
|
if (!$scripts_to_replace) { |
286
|
4 |
|
return; |
287
|
|
|
} |
288
|
|
|
// Remove all scripts |
289
|
8 |
|
$data = str_replace($scripts_to_replace, '', $data); |
290
|
|
|
/** |
291
|
|
|
* If vulcanization is not used - put contents into separate file, and put link to it, otherwise put minified content back |
292
|
|
|
*/ |
293
|
8 |
|
if (!$vulcanization) { |
294
|
|
|
/** |
295
|
|
|
* md5 to distinguish modifications of the files |
296
|
|
|
*/ |
297
|
2 |
|
$content_md5 = substr(md5($scripts_content), 0, 5); |
298
|
2 |
|
file_put_contents("$base_target_file_path.js", $scripts_content, LOCK_EX | FILE_BINARY); |
299
|
2 |
|
$base_target_file_name = basename($base_target_file_path); |
300
|
|
|
// Add script with combined content file to the end |
301
|
2 |
|
$data .= "<script src=\"$base_target_file_name.js?$content_md5\"></script>"; |
302
|
2 |
|
$not_embedded_resources[] = "$base_target_file_name.js?$content_md5"; |
303
|
|
|
} else { |
304
|
|
|
// Add combined content inline script to the end |
305
|
6 |
|
$data .= "<script>$scripts_content</script>"; |
306
|
|
|
} |
307
|
8 |
|
} |
308
|
|
|
/** |
309
|
|
|
* @param string $data Content of processed file |
310
|
|
|
* @param string $file Path to file, that includes specified in previous parameter content |
311
|
|
|
* @param string $base_target_file_path Base filename for resulting combined files |
312
|
|
|
* @param bool $vulcanization Whether to put combined files separately or to make includes built-in (vulcanization) |
313
|
|
|
* @param string[] $not_embedded_resources Resources like images/fonts might not be embedded into resulting CSS because of big size or CSS/JS because of CSP |
314
|
|
|
*/ |
315
|
8 |
|
protected static function html_process_links_and_styles (&$data, $file, $base_target_file_path, $vulcanization, &$not_embedded_resources) { |
316
|
|
|
// Drop Polymer inclusion, since it is already present |
317
|
8 |
|
$data = str_replace('<link rel="import" href="../polymer/polymer.html">', '', $data); |
318
|
8 |
|
if (!preg_match_all('/<link(.*)>|<style(.*)<\/style>/Uims', $data, $links_and_styles)) { |
319
|
8 |
|
return; |
320
|
|
|
} |
321
|
8 |
|
$dir = dirname($file); |
322
|
8 |
|
foreach ($links_and_styles[1] as $index => $link) { |
323
|
|
|
/** |
324
|
|
|
* Check for custom styles `is="custom-style"` or styles includes `include=".."` - we'll skip them |
325
|
|
|
* Or if content is plain CSS |
326
|
|
|
*/ |
327
|
|
|
if ( |
328
|
8 |
|
preg_match('/^[^>]*(is="custom-style"|include=)[^>]*>/Uim', $links_and_styles[2][$index]) || |
329
|
8 |
|
mb_strpos($links_and_styles[0][$index], '</style>') > 0 |
330
|
|
|
) { |
331
|
8 |
|
$content = explode('>', $links_and_styles[2][$index], 2)[1]; |
332
|
8 |
|
$data = str_replace( |
333
|
|
|
$content, |
334
|
8 |
|
static::css($content, $file, $not_embedded_resources), |
335
|
|
|
$data |
336
|
|
|
); |
337
|
8 |
|
continue; |
338
|
|
|
} |
339
|
8 |
|
if (!static::has_relative_href($link, $url, $dir)) { |
340
|
4 |
|
continue; |
341
|
|
|
} |
342
|
8 |
|
$import = preg_match('/rel\s*=\s*[\'"]import[\'"]/Uim', $link); |
343
|
|
|
/** |
344
|
|
|
* CSS imports are available in Polymer alongside with HTML imports |
345
|
|
|
*/ |
346
|
8 |
|
$css_import = $import && preg_match('/type\s*=\s*[\'"]css[\'"]/Uim', $link); |
347
|
8 |
|
$stylesheet = preg_match('/rel\s*=\s*[\'"]stylesheet[\'"]/Uim', $link); |
348
|
|
|
// TODO: Polymer only supports `style[is=custom-style]`, but no `link`-based counterpart, so we can't provide CSP-compatibility for CSS anyway |
349
|
8 |
|
if ($css_import || $stylesheet) { |
350
|
|
|
/** |
351
|
|
|
* If content is link to CSS file |
352
|
|
|
*/ |
353
|
8 |
|
$css = static::css( |
354
|
8 |
|
file_get_contents("$dir/$url"), |
355
|
8 |
|
"$dir/$url", |
356
|
|
|
$not_embedded_resources |
357
|
|
|
); |
358
|
8 |
|
$data = preg_replace( |
359
|
8 |
|
'/'.$links_and_styles[0][$index].'.*<template>/Uims', |
360
|
8 |
|
"<template><style>$css</style>", |
361
|
|
|
$data |
362
|
|
|
); |
363
|
4 |
|
} elseif ($import) { |
364
|
|
|
/** |
365
|
|
|
* If content is HTML import |
366
|
|
|
*/ |
367
|
4 |
|
$data = str_replace( |
368
|
4 |
|
$links_and_styles[0][$index], |
369
|
4 |
|
static::html( |
370
|
4 |
|
file_get_contents("$dir/$url"), |
371
|
4 |
|
"$dir/$url", |
372
|
8 |
|
"$base_target_file_path-".basename($url, '.html'), |
373
|
|
|
$vulcanization, |
374
|
|
|
$not_embedded_resources |
375
|
|
|
), |
376
|
|
|
$data |
377
|
|
|
); |
378
|
|
|
} |
379
|
|
|
} |
380
|
8 |
|
} |
381
|
|
|
/** |
382
|
|
|
* @param string $link |
383
|
|
|
* @param string $url |
384
|
|
|
* @param string $dir |
385
|
|
|
* |
386
|
|
|
* @return bool |
387
|
|
|
*/ |
388
|
8 |
|
protected static function has_relative_href ($link, &$url, $dir) { |
389
|
|
|
$result = |
390
|
8 |
|
$link && |
391
|
8 |
|
preg_match('/href\s*=\s*[\'"](.*)[\'"]/Uims', $link, $url); |
392
|
8 |
|
if ($result && static::is_relative_path_and_exists($url[1], $dir)) { |
393
|
8 |
|
$url = $url[1]; |
394
|
8 |
|
return true; |
395
|
|
|
} |
396
|
4 |
|
return false; |
397
|
|
|
} |
398
|
|
|
/** |
399
|
|
|
* Simple check for http[s], ftp and absolute links |
400
|
|
|
* |
401
|
|
|
* @param string $path |
402
|
|
|
* @param string $dir |
403
|
|
|
* |
404
|
|
|
* @return bool |
405
|
|
|
*/ |
406
|
10 |
|
protected static function is_relative_path_and_exists ($path, $dir) { |
407
|
10 |
|
return !preg_match('#^(http://|https://|ftp://|/)#i', $path) && file_exists("$dir/$path"); |
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
|