-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathboot.php
More file actions
174 lines (144 loc) · 7.75 KB
/
boot.php
File metadata and controls
174 lines (144 loc) · 7.75 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
<?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:
// Parse attributes to check for custom rel attribute
$attributes = !empty($sets[0]['attributes']) ? explode(PHP_EOL, $sets[0]['attributes']) : [];
$hasRelAttribute = false;
$attributesStr = '';
// Efficiently check for rel attribute and build attributes string
if (!empty($attributes)) {
foreach ($attributes as $attr) {
if (preg_match('/^rel\s*=/', $attr)) {
$hasRelAttribute = true;
}
$attributesStr .= ' ' . $attr;
}
}
// Add rel="stylesheet" only if no custom rel is provided
$assetsContent .= '<link ' . ($hasRelAttribute ? '' : 'rel="stylesheet" ') .
'href="' . trim(minify::relativePath($asset)) .
(('yes' == $sets[0]['ignore_browsercache']) ? '?time=' . filemtime($asset) : '') .
'"' . $attributesStr . '>';
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:
// Parse attributes to check for custom rel attribute for minimized files
$attributes = !empty($sets[0]['attributes']) ? explode(PHP_EOL, $sets[0]['attributes']) : [];
$hasRelAttribute = false;
$attributesStr = '';
// Efficiently check for rel attribute and build attributes string
if (!empty($attributes)) {
foreach ($attributes as $attr) {
if (preg_match('/^rel\s*=/', $attr)) {
$hasRelAttribute = true;
}
$attributesStr .= ' ' . $attr;
}
}
// Add rel="stylesheet" only if no custom rel is provided
$content = str_replace($match[0],
'<link ' . ($hasRelAttribute ? '' : 'rel="stylesheet" ') .
'href="' . trim($data) .
(('yes' == $sets[0]['ignore_browsercache']) ? '?time=' . filemtime(ltrim($data, '/')) : '') .
'"' . $attributesStr . '>',
$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);
});
}