-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathboot.php
136 lines (108 loc) · 6.13 KB
/
boot.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
<?php
if (!rex::isBackend()) {
rex_extension::register('CACHE_DELETED', ['minify', 'clearCacheFiles'], rex_extension::LATE);
rex_extension::register('OUTPUT_FILTER', function (rex_extension_point $ep) {
//Start - get php.ini settings
$currentBacktrackLimit = ini_get('pcre.backtrack_limit');
$currentRecursionLimit = ini_get('pcre.recursion_limit');
//End - get php.ini settings
//Start - set new php.ini-settings
ini_set('pcre.backtrack_limit', 1000000);
ini_set('pcre.recursion_limit', 1000000);
//End - set new php.ini-settings
$content = $ep->getSubject();
$whitelistTemplates = rex_addon::get('minify')->getConfig('templates', []);
if (null !== rex_article::getCurrent() && !in_array(rex_article::getCurrent()->getTemplateId(), $whitelistTemplates)) {
preg_match_all("/REX_MINIFY\[type=(.*?)\ set=(.*?)\]/", $content, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
//Start - get set by name and type
$sql = rex_sql::factory();
$sets = $sql->getArray('SELECT `minimize`, `ignore_browsercache`, `assets`, `attributes`, `output` FROM `' . rex::getTable('minify_sets') . '` WHERE `type` = ? AND `name` = ?', [$match[1], $match[2]]);
unset($sql);
//End - get set by name and type
if (!empty($sets)) {
$assets = explode(PHP_EOL, trim($sets[0]['assets']));
if ('no' == $sets[0]['minimize']) {
$assetsContent = '';
foreach ($assets as $asset) {
switch ($match[1]) {
case 'css':
if (minify::isSCSS($asset)) {
$asset = minify::compileFile($asset, 'scss');
} else {
$asset = trim(rex_path::base(substr($asset, 1)));
}
switch ($sets[0]['output']) {
case 'inline':
$assetsContent = '<style ' . ((!empty($sets[0]['attributes'])) ? implode(' ', explode(PHP_EOL, $sets[0]['attributes'])) : '') . '>' . rex_file::get($asset) . '</style>';
break;
default:
$assetsContent .= '<link rel="stylesheet" href="' . trim(minify::relativePath($asset)) . (('yes' == $sets[0]['ignore_browsercache']) ? '?time=' . filemtime($asset) : '') . '" ' . ((!empty($sets[0]['attributes'])) ? implode(' ', explode(PHP_EOL, $sets[0]['attributes'])) : '') . '>';
break;
}
break;
case 'js':
$asset = trim(rex_path::base(substr($asset, 1)));
switch ($sets[0]['output']) {
case 'inline':
$assetsContent .= '<script ' . ((!empty($sets[0]['attributes'])) ? implode(' ', explode(PHP_EOL, $sets[0]['attributes'])) : '') . '>' . rex_file::get($asset) . '</script>';
break;
default:
$assetsContent .= '<script src="' . trim(minify::relativePath($asset)) . (('yes' == $sets[0]['ignore_browsercache']) ? '?time=' . filemtime($asset) : '') . '" ' . ((!empty($sets[0]['attributes'])) ? implode(' ', explode(PHP_EOL, $sets[0]['attributes'])) : '') . '></script>';
break;
}
break;
}
}
$content = str_replace($match[0], $assetsContent, $content);
} else {
$minify = new minify();
foreach ($assets as $asset) {
$minify->addFile($asset, $match[2]);
}
$data = $minify->minify($match[1], $match[2], $sets[0]['output']);
switch ($match[1]) {
case 'css':
switch ($sets[0]['output']) {
case 'inline':
$content = str_replace($match[0], '<style ' . ((!empty($sets[0]['attributes'])) ? implode(' ', explode(PHP_EOL, $sets[0]['attributes'])) : '') . '>' . $data . '</style>', $content);
break;
default:
$content = str_replace($match[0], '<link rel="stylesheet" href="' . trim($data) . (('yes' == $sets[0]['ignore_browsercache']) ? '?time=' . filemtime(ltrim($data, '/')) : '') . '" ' . ((!empty($sets[0]['attributes'])) ? implode(' ', explode(PHP_EOL, $sets[0]['attributes'])) : '') . '>', $content);
break;
}
break;
case 'js':
switch ($sets[0]['output']) {
case 'inline':
$content = str_replace($match[0], '<script ' . ((!empty($sets[0]['attributes'])) ? implode(' ', explode(PHP_EOL, $sets[0]['attributes'])) : '') . '>' . $data . '</script>', $content);
break;
default:
$content = str_replace($match[0], '<script src="' . trim($data) . (('yes' == $sets[0]['ignore_browsercache']) ? '?time=' . filemtime(ltrim($data, '/')) : '') . '" ' . ((!empty($sets[0]['attributes'])) ? implode(' ', explode(PHP_EOL, $sets[0]['attributes'])) : '') . '></script>', $content);
break;
}
break;
}
}
} else {
$content = str_replace($match[0], '', $content);
}
}
//Start - minify html
if ($this->getConfig('minifyhtml')) {
if (rex_addon::get('search_it')->isAvailable()) {
$pattern = '/<!--((?!search_it)[\s\S])*?-->/is';
} else {
$pattern = '/<!--[^\[](.|\s)*?[^\]]-->/is';
}
$content = preg_replace([$pattern, '/[[:blank:]]+/'], ['', ' '], str_replace(["\n", "\r", "\t"], '', $content));
}
//End - minify html
}
//Start - set old php.ini-settings
ini_set('pcre.backtrack_limit', $currentBacktrackLimit);
ini_set('pcre.recursion_limit', $currentRecursionLimit);
//End - set old php.ini-settings
$ep->setSubject($content);
});
}