@@ -1,28 +1,37 @@ discard block |
||
1 | -<?php if (!defined('BB2_CORE')) die("I said no cheating!"); |
|
1 | +<?php if (!defined('BB2_CORE')) |
|
2 | +{ |
|
3 | + die("I said no cheating!"); |
|
4 | +} |
|
2 | 5 | |
3 | 6 | // Miscellaneous helper functions. |
4 | 7 | |
5 | 8 | // Quick and dirty check for an IPv6 address |
6 | -function is_ipv6($address) { |
|
9 | +function is_ipv6($address) |
|
10 | +{ |
|
7 | 11 | return (strpos($address, ":")) ? TRUE : FALSE; |
8 | 12 | } |
9 | 13 | |
10 | 14 | // stripos() needed because stripos is only present on PHP 5 |
11 | -if (!function_exists('stripos')) { |
|
12 | - function stripos($haystack,$needle,$offset = 0) { |
|
15 | +if (!function_exists('stripos')) |
|
16 | +{ |
|
17 | + function stripos($haystack,$needle,$offset = 0) |
|
18 | + { |
|
13 | 19 | return(strpos(strtolower($haystack),strtolower($needle),$offset)); |
14 | 20 | } |
15 | 21 | } |
16 | 22 | |
17 | 23 | // str_split() needed because str_split is only present on PHP 5 |
18 | -if (!function_exists('str_split')) { |
|
24 | +if (!function_exists('str_split')) |
|
25 | +{ |
|
19 | 26 | function str_split($string, $split_length=1) |
20 | 27 | { |
21 | - if ($split_length < 1) { |
|
28 | + if ($split_length < 1) |
|
29 | + { |
|
22 | 30 | return false; |
23 | 31 | } |
24 | 32 | |
25 | - for ($pos=0, $chunks = array(); $pos < strlen($string); $pos+=$split_length) { |
|
33 | + for ($pos=0, $chunks = array(); $pos < strlen($string); $pos+=$split_length) |
|
34 | + { |
|
26 | 35 | $chunks[] = substr($string, $pos, $split_length); |
27 | 36 | } |
28 | 37 | return $chunks; |
@@ -30,28 +39,39 @@ discard block |
||
30 | 39 | } |
31 | 40 | |
32 | 41 | // Convert a string to mixed-case on word boundaries. |
33 | -function uc_all($string) { |
|
42 | +function uc_all($string) |
|
43 | +{ |
|
34 | 44 | $temp = preg_split('/(\W)/', str_replace("_", "-", $string), -1, PREG_SPLIT_DELIM_CAPTURE); |
35 | - foreach ($temp as $key=>$word) { |
|
45 | + foreach ($temp as $key=>$word) |
|
46 | + { |
|
36 | 47 | $temp[$key] = ucfirst(strtolower($word)); |
37 | 48 | } |
38 | 49 | return join ('', $temp); |
39 | 50 | } |
40 | 51 | |
41 | 52 | // Determine if an IP address resides in a CIDR netblock or netblocks. |
42 | -function match_cidr($addr, $cidr) { |
|
53 | +function match_cidr($addr, $cidr) |
|
54 | +{ |
|
43 | 55 | $output = false; |
44 | 56 | |
45 | - if (is_array($cidr)) { |
|
46 | - foreach ($cidr as $cidrlet) { |
|
47 | - if (match_cidr($addr, $cidrlet)) { |
|
57 | + if (is_array($cidr)) |
|
58 | + { |
|
59 | + foreach ($cidr as $cidrlet) |
|
60 | + { |
|
61 | + if (match_cidr($addr, $cidrlet)) |
|
62 | + { |
|
48 | 63 | $output = true; |
49 | 64 | break; |
50 | 65 | } |
51 | 66 | } |
52 | - } else { |
|
67 | + } |
|
68 | + else |
|
69 | + { |
|
53 | 70 | @list($ip, $mask) = explode('/', $cidr); |
54 | - if (!$mask) $mask = 32; |
|
71 | + if (!$mask) |
|
72 | + { |
|
73 | + $mask = 32; |
|
74 | + } |
|
55 | 75 | $mask = pow(2,32) - pow(2, (32 - $mask)); |
56 | 76 | $output = ((ip2long($addr) & $mask) == (ip2long($ip) & $mask)); |
57 | 77 | } |
@@ -59,19 +79,26 @@ discard block |
||
59 | 79 | } |
60 | 80 | |
61 | 81 | // Determine if an IP address is reserved by RFC 1918. |
62 | -function is_rfc1918($addr) { |
|
82 | +function is_rfc1918($addr) |
|
83 | +{ |
|
63 | 84 | return match_cidr($addr, array("10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16")); |
64 | 85 | } |
65 | 86 | // Obtain all the HTTP headers. |
66 | 87 | // NB: on PHP-CGI we have to fake it out a bit, since we can't get the REAL |
67 | 88 | // headers. Run PHP as Apache 2.0 module if possible for best results. |
68 | -function bb2_load_headers() { |
|
69 | - if (!is_callable('getallheaders')) { |
|
89 | +function bb2_load_headers() |
|
90 | +{ |
|
91 | + if (!is_callable('getallheaders')) |
|
92 | + { |
|
70 | 93 | $headers = array(); |
71 | 94 | foreach ($_SERVER as $h => $v) |
72 | - if (preg_match('/HTTP_(.+)/', $h, $hp)) |
|
95 | + { |
|
96 | + if (preg_match('/HTTP_(.+)/', $h, $hp)) |
|
73 | 97 | $headers[str_replace("_", "-", uc_all($hp[1]))] = $v; |
74 | - } else { |
|
98 | + } |
|
99 | + } |
|
100 | + else |
|
101 | + { |
|
75 | 102 | $headers = getallheaders(); |
76 | 103 | } |
77 | 104 | return $headers; |
@@ -4,7 +4,7 @@ |
||
4 | 4 | |
5 | 5 | // Quick and dirty check for an IPv6 address |
6 | 6 | function is_ipv6($address) { |
7 | - return (strpos($address, ":")) ? TRUE : FALSE; |
|
7 | + return (strpos($address, ":")) ? true : false; |
|
8 | 8 | } |
9 | 9 | |
10 | 10 | // stripos() needed because stripos is only present on PHP 5 |
@@ -9,20 +9,20 @@ discard block |
||
9 | 9 | |
10 | 10 | // stripos() needed because stripos is only present on PHP 5 |
11 | 11 | if (!function_exists('stripos')) { |
12 | - function stripos($haystack,$needle,$offset = 0) { |
|
13 | - return(strpos(strtolower($haystack),strtolower($needle),$offset)); |
|
12 | + function stripos($haystack, $needle, $offset = 0) { |
|
13 | + return(strpos(strtolower($haystack), strtolower($needle), $offset)); |
|
14 | 14 | } |
15 | 15 | } |
16 | 16 | |
17 | 17 | // str_split() needed because str_split is only present on PHP 5 |
18 | 18 | if (!function_exists('str_split')) { |
19 | - function str_split($string, $split_length=1) |
|
19 | + function str_split($string, $split_length = 1) |
|
20 | 20 | { |
21 | 21 | if ($split_length < 1) { |
22 | 22 | return false; |
23 | 23 | } |
24 | 24 | |
25 | - for ($pos=0, $chunks = array(); $pos < strlen($string); $pos+=$split_length) { |
|
25 | + for ($pos = 0, $chunks = array(); $pos < strlen($string); $pos += $split_length) { |
|
26 | 26 | $chunks[] = substr($string, $pos, $split_length); |
27 | 27 | } |
28 | 28 | return $chunks; |
@@ -35,7 +35,7 @@ discard block |
||
35 | 35 | foreach ($temp as $key=>$word) { |
36 | 36 | $temp[$key] = ucfirst(strtolower($word)); |
37 | 37 | } |
38 | - return join ('', $temp); |
|
38 | + return join('', $temp); |
|
39 | 39 | } |
40 | 40 | |
41 | 41 | // Determine if an IP address resides in a CIDR netblock or netblocks. |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | } else { |
53 | 53 | @list($ip, $mask) = explode('/', $cidr); |
54 | 54 | if (!$mask) $mask = 32; |
55 | - $mask = pow(2,32) - pow(2, (32 - $mask)); |
|
55 | + $mask = pow(2, 32) - pow(2, (32 - $mask)); |
|
56 | 56 | $output = ((ip2long($addr) & $mask) == (ip2long($ip) & $mask)); |
57 | 57 | } |
58 | 58 | return $output; |
@@ -1,6 +1,10 @@ discard block |
||
1 | -<?php if (!defined('BB2_CORE')) die('I said no cheating!'); |
|
1 | +<?php if (!defined('BB2_CORE')) |
|
2 | +{ |
|
3 | + die('I said no cheating!'); |
|
4 | +} |
|
2 | 5 | |
3 | -function bb2_blacklist($package) { |
|
6 | +function bb2_blacklist($package) |
|
7 | +{ |
|
4 | 8 | |
5 | 9 | // Blacklisted user agents |
6 | 10 | // These user agent strings occur at the beginning of the line. |
@@ -166,27 +170,35 @@ discard block |
||
166 | 170 | @$ua = $package['headers_mixed']['User-Agent']; |
167 | 171 | @$uri = $package['request_uri']; |
168 | 172 | |
169 | - foreach ($bb2_spambots_0 as $spambot) { |
|
173 | + foreach ($bb2_spambots_0 as $spambot) |
|
174 | + { |
|
170 | 175 | $pos = strpos($ua, $spambot); |
171 | - if ($pos !== FALSE && $pos == 0) { |
|
176 | + if ($pos !== FALSE && $pos == 0) |
|
177 | + { |
|
172 | 178 | return "17f4e8c8"; |
173 | 179 | } |
174 | 180 | } |
175 | 181 | |
176 | - foreach ($bb2_spambots as $spambot) { |
|
177 | - if (strpos($ua, $spambot) !== FALSE) { |
|
182 | + foreach ($bb2_spambots as $spambot) |
|
183 | + { |
|
184 | + if (strpos($ua, $spambot) !== FALSE) |
|
185 | + { |
|
178 | 186 | return "17f4e8c8"; |
179 | 187 | } |
180 | 188 | } |
181 | 189 | |
182 | - foreach ($bb2_spambots_regex as $spambot) { |
|
183 | - if (preg_match($spambot, $ua)) { |
|
190 | + foreach ($bb2_spambots_regex as $spambot) |
|
191 | + { |
|
192 | + if (preg_match($spambot, $ua)) |
|
193 | + { |
|
184 | 194 | return "17f4e8c8"; |
185 | 195 | } |
186 | 196 | } |
187 | 197 | |
188 | - foreach ($bb2_spambots_url as $spambot) { |
|
189 | - if (stripos($uri, $spambot) !== FALSE) { |
|
198 | + foreach ($bb2_spambots_url as $spambot) |
|
199 | + { |
|
200 | + if (stripos($uri, $spambot) !== FALSE) |
|
201 | + { |
|
190 | 202 | return "96c0bd29"; |
191 | 203 | } |
192 | 204 | } |
@@ -168,13 +168,13 @@ discard block |
||
168 | 168 | |
169 | 169 | foreach ($bb2_spambots_0 as $spambot) { |
170 | 170 | $pos = strpos($ua, $spambot); |
171 | - if ($pos !== FALSE && $pos == 0) { |
|
171 | + if ($pos !== false && $pos == 0) { |
|
172 | 172 | return "17f4e8c8"; |
173 | 173 | } |
174 | 174 | } |
175 | 175 | |
176 | 176 | foreach ($bb2_spambots as $spambot) { |
177 | - if (strpos($ua, $spambot) !== FALSE) { |
|
177 | + if (strpos($ua, $spambot) !== false) { |
|
178 | 178 | return "17f4e8c8"; |
179 | 179 | } |
180 | 180 | } |
@@ -186,10 +186,10 @@ discard block |
||
186 | 186 | } |
187 | 187 | |
188 | 188 | foreach ($bb2_spambots_url as $spambot) { |
189 | - if (stripos($uri, $spambot) !== FALSE) { |
|
189 | + if (stripos($uri, $spambot) !== false) { |
|
190 | 190 | return "96c0bd29"; |
191 | 191 | } |
192 | 192 | } |
193 | 193 | |
194 | - return FALSE; |
|
194 | + return false; |
|
195 | 195 | } |
@@ -5,160 +5,160 @@ |
||
5 | 5 | // Blacklisted user agents |
6 | 6 | // These user agent strings occur at the beginning of the line. |
7 | 7 | $bb2_spambots_0 = array( |
8 | - "-", // brute force password attempts, malicious botnet |
|
9 | - "8484 Boston Project", // video poker/porn spam |
|
10 | - "ArchiveTeam", // ignores robots.txt and hammers server |
|
11 | - "adwords", // referrer spam |
|
12 | - "autoemailspider", // spam harvester |
|
13 | - "blogsearchbot-martin", // from honeypot |
|
14 | - "BrowserEmulator/", // open proxy software |
|
15 | - "CherryPicker", // spam harvester |
|
16 | - "core-project/", // FrontPage extension exploits |
|
17 | - "Diamond", // delivers spyware/adware |
|
18 | - "Digger", // spam harvester |
|
19 | - "ecollector", // spam harvester |
|
20 | - "EmailCollector", // spam harvester |
|
21 | - "Email Siphon", // spam harvester |
|
22 | - "EmailSiphon", // spam harvester |
|
23 | - "Forum Poster", // forum spambot |
|
24 | - "grub crawler", // misc comment/email spam |
|
25 | - "HttpProxy", // misc comment/email spam |
|
26 | - "Internet Explorer", // XMLRPC exploits seen |
|
27 | - "ISC Systems iRc", // spam harvester |
|
28 | - "Jakarta Commons", // customised spambots |
|
29 | - "Java 1.", // unidentified robots |
|
30 | - "Java/1.", // unidentified robots |
|
31 | - "libwww-perl", // unidentified robots |
|
32 | - "LWP", // unidentified robots |
|
33 | - "lwp", // unidentified robots |
|
34 | - "Microsoft Internet Explorer/", // too old; assumed robot |
|
35 | - "Microsoft URL", // unidentified robots |
|
36 | - "Missigua", // spam harvester |
|
37 | - "MJ12bot/v1.0.8", // malicious botnet |
|
38 | - "Morfeus", // vulnerability scanner |
|
39 | - "Movable Type", // customised spambots |
|
8 | + "-", // brute force password attempts, malicious botnet |
|
9 | + "8484 Boston Project", // video poker/porn spam |
|
10 | + "ArchiveTeam", // ignores robots.txt and hammers server |
|
11 | + "adwords", // referrer spam |
|
12 | + "autoemailspider", // spam harvester |
|
13 | + "blogsearchbot-martin", // from honeypot |
|
14 | + "BrowserEmulator/", // open proxy software |
|
15 | + "CherryPicker", // spam harvester |
|
16 | + "core-project/", // FrontPage extension exploits |
|
17 | + "Diamond", // delivers spyware/adware |
|
18 | + "Digger", // spam harvester |
|
19 | + "ecollector", // spam harvester |
|
20 | + "EmailCollector", // spam harvester |
|
21 | + "Email Siphon", // spam harvester |
|
22 | + "EmailSiphon", // spam harvester |
|
23 | + "Forum Poster", // forum spambot |
|
24 | + "grub crawler", // misc comment/email spam |
|
25 | + "HttpProxy", // misc comment/email spam |
|
26 | + "Internet Explorer", // XMLRPC exploits seen |
|
27 | + "ISC Systems iRc", // spam harvester |
|
28 | + "Jakarta Commons", // customised spambots |
|
29 | + "Java 1.", // unidentified robots |
|
30 | + "Java/1.", // unidentified robots |
|
31 | + "libwww-perl", // unidentified robots |
|
32 | + "LWP", // unidentified robots |
|
33 | + "lwp", // unidentified robots |
|
34 | + "Microsoft Internet Explorer/", // too old; assumed robot |
|
35 | + "Microsoft URL", // unidentified robots |
|
36 | + "Missigua", // spam harvester |
|
37 | + "MJ12bot/v1.0.8", // malicious botnet |
|
38 | + "Morfeus", // vulnerability scanner |
|
39 | + "Movable Type", // customised spambots |
|
40 | 40 | // msnbot is using this fake user agent string now |
41 | 41 | //"Mozilla ", // malicious software |
42 | - "Mozilla/0", // malicious software |
|
43 | - "Mozilla/1", // malicious software |
|
44 | - "Mozilla/2", // malicious software |
|
45 | - "Mozilla/3", // malicious software |
|
46 | - "Mozilla/4.0(", // from honeypot |
|
47 | - "Mozilla/4.0+(compatible;+", // suspicious harvester |
|
48 | - "Mozilla/4.0 (Hydra)", // brute force tool |
|
49 | - "MSIE", // malicious software |
|
50 | - "MVAClient", // automated hacking attempts |
|
51 | - "Nessus", // vulnerability scanner |
|
52 | - "NutchCVS", // unidentified robots |
|
53 | - "Nutscrape/", // misc comment spam |
|
54 | - "OmniExplorer", // spam harvester |
|
55 | - "Opera/9.64(", // comment spam bot |
|
56 | - "PMAFind", // vulnerability scanner |
|
57 | - "psycheclone", // spam harvester |
|
58 | - "PussyCat ", // misc comment spam |
|
59 | - "PycURL", // misc comment spam |
|
60 | - "Python-urllib", // commonly abused |
|
61 | - "revolt", // vulnerability scanner |
|
42 | + "Mozilla/0", // malicious software |
|
43 | + "Mozilla/1", // malicious software |
|
44 | + "Mozilla/2", // malicious software |
|
45 | + "Mozilla/3", // malicious software |
|
46 | + "Mozilla/4.0(", // from honeypot |
|
47 | + "Mozilla/4.0+(compatible;+", // suspicious harvester |
|
48 | + "Mozilla/4.0 (Hydra)", // brute force tool |
|
49 | + "MSIE", // malicious software |
|
50 | + "MVAClient", // automated hacking attempts |
|
51 | + "Nessus", // vulnerability scanner |
|
52 | + "NutchCVS", // unidentified robots |
|
53 | + "Nutscrape/", // misc comment spam |
|
54 | + "OmniExplorer", // spam harvester |
|
55 | + "Opera/9.64(", // comment spam bot |
|
56 | + "PMAFind", // vulnerability scanner |
|
57 | + "psycheclone", // spam harvester |
|
58 | + "PussyCat ", // misc comment spam |
|
59 | + "PycURL", // misc comment spam |
|
60 | + "Python-urllib", // commonly abused |
|
61 | + "revolt", // vulnerability scanner |
|
62 | 62 | // WP 2.5 now has Flash; FIXME |
63 | 63 | // "Shockwave Flash", // spam harvester |
64 | - "sqlmap/", // SQL injection |
|
65 | - "Super Happy Fun ", // spam harvester |
|
66 | - "TrackBack/", // trackback spam |
|
67 | - "user", // suspicious harvester |
|
68 | - "User Agent: ", // spam harvester |
|
69 | - "User-Agent: ", // spam harvester |
|
70 | - "w3af", // vulnerability scanner |
|
71 | - "WebSite-X Suite", // misc comment spam |
|
72 | - "Winnie Poh", // Automated Coppermine hacks |
|
73 | - "Wordpress", // malicious software |
|
74 | - "\"", // malicious software |
|
64 | + "sqlmap/", // SQL injection |
|
65 | + "Super Happy Fun ", // spam harvester |
|
66 | + "TrackBack/", // trackback spam |
|
67 | + "user", // suspicious harvester |
|
68 | + "User Agent: ", // spam harvester |
|
69 | + "User-Agent: ", // spam harvester |
|
70 | + "w3af", // vulnerability scanner |
|
71 | + "WebSite-X Suite", // misc comment spam |
|
72 | + "Winnie Poh", // Automated Coppermine hacks |
|
73 | + "Wordpress", // malicious software |
|
74 | + "\"", // malicious software |
|
75 | 75 | ); |
76 | 76 | |
77 | 77 | // These user agent strings occur anywhere within the line. |
78 | 78 | $bb2_spambots = array( |
79 | - "\r", // A really dumb bot |
|
80 | - "<sc", // XSS exploit attempts |
|
81 | - "; Widows ", // misc comment/email spam |
|
82 | - ": ;", // shellshock |
|
83 | - ":;", // shellshock |
|
84 | - "a href=", // referrer spam |
|
85 | - "ArchiveBot", // ignores robots.txt and hammers server |
|
86 | - "Bad Behavior Test", // Add this to your user-agent to test BB |
|
87 | - "compatible ; MSIE", // misc comment/email spam |
|
88 | - "compatible-", // misc comment/email spam |
|
89 | - "DTS Agent", // misc comment/email spam |
|
90 | - "Email Extractor", // spam harvester |
|
91 | - "Firebird/", // too old; assumed robot |
|
92 | - "Gecko/2525", // revisit this in 500 years |
|
93 | - "grub-client", // search engine ignores robots.txt |
|
94 | - "hanzoweb", // very badly behaved crawler |
|
95 | - "Havij", // SQL injection tool |
|
96 | - "Indy Library", // misc comment/email spam |
|
97 | - "Ming Mong", // brute force tool |
|
98 | - "MSIE 7.0; Windows NT 5.2", // Cyveillance |
|
99 | - "Murzillo compatible", // comment spam bot |
|
100 | - ".NET CLR 1)", // free poker, etc. |
|
101 | - ".NET CLR1", // spam harvester |
|
102 | - "Netsparker", // vulnerability scanner |
|
103 | - "Nikto/", // vulnerability scanner |
|
104 | - "Perman Surfer", // old and very broken harvester |
|
105 | - "POE-Component-Client", // free poker, etc. |
|
106 | - "Teh Forest Lobster", // brute force tool |
|
107 | - "Turing Machine", // www.anonymizer.com abuse |
|
108 | - "Ubuntu/9.25", // comment spam bot |
|
109 | - "unspecified.mail", // stealth harvesters |
|
110 | - "User-agent: ", // spam harvester/splogger |
|
111 | - "WebaltBot", // spam harvester |
|
112 | - "WISEbot", // spam harvester |
|
113 | - "WISEnutbot", // spam harvester |
|
114 | - "Win95", // too old; assumed robot |
|
115 | - "Win98", // too old; assumed robot |
|
116 | - "WinME", // too old; assumed robot |
|
117 | - "Win 9x 4.90", // too old; assumed robot |
|
118 | - "Windows 3", // too old; assumed robot |
|
119 | - "Windows 95", // too old; assumed robot |
|
120 | - "Windows 98", // too old; assumed robot |
|
121 | - "Windows NT 4", // too old; assumed robot |
|
122 | - "Windows NT;", // too old; assumed robot |
|
79 | + "\r", // A really dumb bot |
|
80 | + "<sc", // XSS exploit attempts |
|
81 | + "; Widows ", // misc comment/email spam |
|
82 | + ": ;", // shellshock |
|
83 | + ":;", // shellshock |
|
84 | + "a href=", // referrer spam |
|
85 | + "ArchiveBot", // ignores robots.txt and hammers server |
|
86 | + "Bad Behavior Test", // Add this to your user-agent to test BB |
|
87 | + "compatible ; MSIE", // misc comment/email spam |
|
88 | + "compatible-", // misc comment/email spam |
|
89 | + "DTS Agent", // misc comment/email spam |
|
90 | + "Email Extractor", // spam harvester |
|
91 | + "Firebird/", // too old; assumed robot |
|
92 | + "Gecko/2525", // revisit this in 500 years |
|
93 | + "grub-client", // search engine ignores robots.txt |
|
94 | + "hanzoweb", // very badly behaved crawler |
|
95 | + "Havij", // SQL injection tool |
|
96 | + "Indy Library", // misc comment/email spam |
|
97 | + "Ming Mong", // brute force tool |
|
98 | + "MSIE 7.0; Windows NT 5.2", // Cyveillance |
|
99 | + "Murzillo compatible", // comment spam bot |
|
100 | + ".NET CLR 1)", // free poker, etc. |
|
101 | + ".NET CLR1", // spam harvester |
|
102 | + "Netsparker", // vulnerability scanner |
|
103 | + "Nikto/", // vulnerability scanner |
|
104 | + "Perman Surfer", // old and very broken harvester |
|
105 | + "POE-Component-Client", // free poker, etc. |
|
106 | + "Teh Forest Lobster", // brute force tool |
|
107 | + "Turing Machine", // www.anonymizer.com abuse |
|
108 | + "Ubuntu/9.25", // comment spam bot |
|
109 | + "unspecified.mail", // stealth harvesters |
|
110 | + "User-agent: ", // spam harvester/splogger |
|
111 | + "WebaltBot", // spam harvester |
|
112 | + "WISEbot", // spam harvester |
|
113 | + "WISEnutbot", // spam harvester |
|
114 | + "Win95", // too old; assumed robot |
|
115 | + "Win98", // too old; assumed robot |
|
116 | + "WinME", // too old; assumed robot |
|
117 | + "Win 9x 4.90", // too old; assumed robot |
|
118 | + "Windows 3", // too old; assumed robot |
|
119 | + "Windows 95", // too old; assumed robot |
|
120 | + "Windows 98", // too old; assumed robot |
|
121 | + "Windows NT 4", // too old; assumed robot |
|
122 | + "Windows NT;", // too old; assumed robot |
|
123 | 123 | #"Windows NT 4.0;)", // wikispam bot |
124 | - "Windows NT 5.0;)", // wikispam bot |
|
125 | - "Windows NT 5.1;)", // wikispam bot |
|
126 | - "Windows XP 5", // spam harvester |
|
127 | - "WordPress/4.01", // pingback spam |
|
128 | - "Xedant Human Emulator",// spammer script engine |
|
129 | - "ZmEu", // exploit scanner |
|
130 | - "\\\\)", // spam harvester |
|
124 | + "Windows NT 5.0;)", // wikispam bot |
|
125 | + "Windows NT 5.1;)", // wikispam bot |
|
126 | + "Windows XP 5", // spam harvester |
|
127 | + "WordPress/4.01", // pingback spam |
|
128 | + "Xedant Human Emulator", // spammer script engine |
|
129 | + "ZmEu", // exploit scanner |
|
130 | + "\\\\)", // spam harvester |
|
131 | 131 | ); |
132 | 132 | |
133 | 133 | // These are regular expression matches. |
134 | 134 | $bb2_spambots_regex = array( |
135 | - "/^[A-Z]{10}$/", // misc email spam |
|
135 | + "/^[A-Z]{10}$/", // misc email spam |
|
136 | 136 | "/[bcdfghjklmnpqrstvwxz ]{8,}/", |
137 | 137 | // "/(;\){1,2}$/", // misc spammers/harvesters |
138 | - "/MSIE.*Windows XP/", // misc comment spam |
|
139 | - "/MSIE [2345]/", // too old; assumed robot |
|
138 | + "/MSIE.*Windows XP/", // misc comment spam |
|
139 | + "/MSIE [2345]/", // too old; assumed robot |
|
140 | 140 | ); |
141 | 141 | |
142 | 142 | // Blacklisted URL strings |
143 | 143 | // These strings are considered case-insensitive. |
144 | 144 | $bb2_spambots_url = array( |
145 | - "0x31303235343830303536", // Havij |
|
146 | - "../", // path traversal |
|
147 | - "..\\", // path traversal |
|
148 | - "%60information_schema%60", // SQL injection probe |
|
149 | - "+%2F*%21", // SQL injection probe |
|
150 | - "%27--", // SQL injection |
|
151 | - "%27 --", // SQL injection |
|
152 | - "%27%23", // SQL injection |
|
153 | - "%27 %23", // SQL injection |
|
154 | - "benchmark%28", // SQL injection probe |
|
155 | - "insert+into+", // SQL injection |
|
156 | - "r3dm0v3", // SQL injection probe |
|
157 | - "select+1+from", // SQL injection probe |
|
158 | - "union+all+select", // SQL injection probe |
|
159 | - "union+select", // SQL injection probe |
|
160 | - "waitfor+delay+", // SQL injection probe |
|
161 | - "w00tw00t", // vulnerability scanner |
|
145 | + "0x31303235343830303536", // Havij |
|
146 | + "../", // path traversal |
|
147 | + "..\\", // path traversal |
|
148 | + "%60information_schema%60", // SQL injection probe |
|
149 | + "+%2F*%21", // SQL injection probe |
|
150 | + "%27--", // SQL injection |
|
151 | + "%27 --", // SQL injection |
|
152 | + "%27%23", // SQL injection |
|
153 | + "%27 %23", // SQL injection |
|
154 | + "benchmark%28", // SQL injection probe |
|
155 | + "insert+into+", // SQL injection |
|
156 | + "r3dm0v3", // SQL injection probe |
|
157 | + "select+1+from", // SQL injection probe |
|
158 | + "union+all+select", // SQL injection probe |
|
159 | + "union+select", // SQL injection probe |
|
160 | + "waitfor+delay+", // SQL injection probe |
|
161 | + "w00tw00t", // vulnerability scanner |
|
162 | 162 | ); |
163 | 163 | |
164 | 164 | // Do not edit below this line. |
@@ -1,10 +1,15 @@ |
||
1 | -<?php if (!defined('BB2_CORE')) die('I said no cheating!'); |
|
1 | +<?php if (!defined('BB2_CORE')) |
|
2 | +{ |
|
3 | + die('I said no cheating!'); |
|
4 | +} |
|
2 | 5 | |
3 | 6 | function bb2_movabletype($package) |
4 | 7 | { |
5 | 8 | // Is it a trackback? |
6 | - if (strcasecmp($package['request_method'], "POST")) { |
|
7 | - if (strcmp($package['headers_mixed']['Range'], "bytes=0-99999")) { |
|
9 | + if (strcasecmp($package['request_method'], "POST")) |
|
10 | + { |
|
11 | + if (strcmp($package['headers_mixed']['Range'], "bytes=0-99999")) |
|
12 | + { |
|
8 | 13 | return "7d12528e"; |
9 | 14 | } |
10 | 15 | } |
@@ -1,11 +1,18 @@ discard block |
||
1 | -<?php if (!defined('BB2_CORE')) die('I said no cheating!'); |
|
1 | +<?php if (!defined('BB2_CORE')) |
|
2 | +{ |
|
3 | + die('I said no cheating!'); |
|
4 | +} |
|
2 | 5 | |
3 | 6 | // Look up address on various blackhole lists. |
4 | 7 | // These should not be used for GET requests under any circumstances! |
5 | 8 | // FIXME: Note that this code is no longer in use |
6 | -function bb2_blackhole($package) { |
|
9 | +function bb2_blackhole($package) |
|
10 | +{ |
|
7 | 11 | // Can't use IPv6 addresses yet |
8 | - if (@is_ipv6($package['ip'])) return false; |
|
12 | + if (@is_ipv6($package['ip'])) |
|
13 | + { |
|
14 | + return false; |
|
15 | + } |
|
9 | 16 | |
10 | 17 | // Workaround for "MySQL server has gone away" |
11 | 18 | bb2_db_query("SET @@session.wait_timeout = 90"); |
@@ -29,12 +36,15 @@ discard block |
||
29 | 36 | // Check the blackhole lists |
30 | 37 | $ip = $package['ip']; |
31 | 38 | $find = implode('.', array_reverse(explode('.', $ip))); |
32 | - foreach ($bb2_blackhole_lists as $dnsbl) { |
|
39 | + foreach ($bb2_blackhole_lists as $dnsbl) |
|
40 | + { |
|
33 | 41 | $result = gethostbynamel($find . "." . $dnsbl . "."); |
34 | - if (!empty($result)) { |
|
42 | + if (!empty($result)) |
|
43 | + { |
|
35 | 44 | // Got a match and it isn't on the exception list |
36 | 45 | $result = @array_diff($result, $bb2_blackhole_exceptions[$dnsbl]); |
37 | - if (!empty($result)) { |
|
46 | + if (!empty($result)) |
|
47 | + { |
|
38 | 48 | return '136673cd'; |
39 | 49 | } |
40 | 50 | } |
@@ -42,24 +52,34 @@ discard block |
||
42 | 52 | return false; |
43 | 53 | } |
44 | 54 | |
45 | -function bb2_httpbl($settings, $package) { |
|
55 | +function bb2_httpbl($settings, $package) |
|
56 | +{ |
|
46 | 57 | // Can't use IPv6 addresses yet |
47 | - if (@is_ipv6($package['ip'])) return false; |
|
58 | + if (@is_ipv6($package['ip'])) |
|
59 | + { |
|
60 | + return false; |
|
61 | + } |
|
48 | 62 | |
49 | - if (@!$settings['httpbl_key']) return false; |
|
63 | + if (@!$settings['httpbl_key']) |
|
64 | + { |
|
65 | + return false; |
|
66 | + } |
|
50 | 67 | |
51 | 68 | // Workaround for "MySQL server has gone away" |
52 | 69 | bb2_db_query("SET @@session.wait_timeout = 90"); |
53 | 70 | |
54 | 71 | $find = implode('.', array_reverse(explode('.', $package['ip']))); |
55 | 72 | $result = gethostbynamel($settings['httpbl_key'].".${find}.dnsbl.httpbl.org."); |
56 | - if (!empty($result)) { |
|
73 | + if (!empty($result)) |
|
74 | + { |
|
57 | 75 | $ip = explode('.', $result[0]); |
58 | - if ($ip[0] == 127 && ($ip[3] & 7) && $ip[2] >= $settings['httpbl_threat'] && $ip[1] <= $settings['httpbl_maxage']) { |
|
76 | + if ($ip[0] == 127 && ($ip[3] & 7) && $ip[2] >= $settings['httpbl_threat'] && $ip[1] <= $settings['httpbl_maxage']) |
|
77 | + { |
|
59 | 78 | return '2b021b1f'; |
60 | 79 | } |
61 | 80 | // Check if search engine |
62 | - if ($ip[3] == 0) { |
|
81 | + if ($ip[3] == 0) |
|
82 | + { |
|
63 | 83 | return 1; |
64 | 84 | } |
65 | 85 | } |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | |
13 | 13 | // Only conservative lists |
14 | 14 | $bb2_blackhole_lists = array( |
15 | - "sbl-xbl.spamhaus.org", // All around nasties |
|
15 | + "sbl-xbl.spamhaus.org", // All around nasties |
|
16 | 16 | // "dnsbl.sorbs.net", // Old useless data. |
17 | 17 | // "list.dsbl.org", // Old useless data. |
18 | 18 | // "dnsbl.ioerror.us", // Bad Behavior Blackhole |
@@ -20,8 +20,8 @@ discard block |
||
20 | 20 | |
21 | 21 | // Things that shouldn't be blocked, from aggregate lists |
22 | 22 | $bb2_blackhole_exceptions = array( |
23 | - "sbl-xbl.spamhaus.org" => array("127.0.0.4"), // CBL is problematic |
|
24 | - "dnsbl.sorbs.net" => array("127.0.0.10",), // Dynamic IPs only |
|
23 | + "sbl-xbl.spamhaus.org" => array("127.0.0.4"), // CBL is problematic |
|
24 | + "dnsbl.sorbs.net" => array("127.0.0.10",), // Dynamic IPs only |
|
25 | 25 | "list.dsbl.org" => array(), |
26 | 26 | "dnsbl.ioerror.us" => array(), |
27 | 27 | ); |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | bb2_db_query("SET @@session.wait_timeout = 90"); |
53 | 53 | |
54 | 54 | $find = implode('.', array_reverse(explode('.', $package['ip']))); |
55 | - $result = gethostbynamel($settings['httpbl_key'].".${find}.dnsbl.httpbl.org."); |
|
55 | + $result = gethostbynamel($settings['httpbl_key'] . ".${find}.dnsbl.httpbl.org."); |
|
56 | 56 | if (!empty($result)) { |
57 | 57 | $ip = explode('.', $result[0]); |
58 | 58 | if ($ip[0] == 127 && ($ip[3] & 7) && $ip[2] >= $settings['httpbl_threat'] && $ip[1] <= $settings['httpbl_maxage']) { |
@@ -1,4 +1,7 @@ |
||
1 | -<?php if (!defined('BB2_CORE')) die('I said no cheating!'); |
|
1 | +<?php if (!defined('BB2_CORE')) |
|
2 | +{ |
|
3 | + die('I said no cheating!'); |
|
4 | +} |
|
2 | 5 | |
3 | 6 | // Analyze requests claiming to be from CloudFlare |
4 | 7 |
@@ -1,4 +1,7 @@ discard block |
||
1 | -<?php if (!defined('BB2_CWD')) die("I said no cheating!"); |
|
1 | +<?php if (!defined('BB2_CWD')) |
|
2 | +{ |
|
3 | + die("I said no cheating!"); |
|
4 | +} |
|
2 | 5 | |
3 | 6 | // Bad Behavior browser screener |
4 | 7 | |
@@ -49,10 +52,12 @@ discard block |
||
49 | 52 | |
50 | 53 | // Set up a simple cookie |
51 | 54 | $screener = array(time(), $package['ip']); |
52 | - if (isset($package['headers_mixed']['X-Forwarded-For'])) { |
|
55 | + if (isset($package['headers_mixed']['X-Forwarded-For'])) |
|
56 | + { |
|
53 | 57 | array_push($screener, $package['headers_mixed']['X-Forwarded-For']); |
54 | 58 | } |
55 | - if (isset($package['headers_mixed']['Client-Ip'])) { |
|
59 | + if (isset($package['headers_mixed']['Client-Ip'])) |
|
60 | + { |
|
56 | 61 | array_push($screener, $package['headers_mixed']['Client-Ip']); |
57 | 62 | } |
58 | 63 |
@@ -1,4 +1,7 @@ discard block |
||
1 | -<?php if (!defined('BB2_CORE')) die('I said no cheating!'); |
|
1 | +<?php if (!defined('BB2_CORE')) |
|
2 | +{ |
|
3 | + die('I said no cheating!'); |
|
4 | +} |
|
2 | 5 | |
3 | 6 | // Analyze user agents claiming to be Konqueror |
4 | 7 | |
@@ -6,8 +9,10 @@ discard block |
||
6 | 9 | { |
7 | 10 | // CafeKelsa is a dev project at Yahoo which indexes job listings for |
8 | 11 | // Yahoo! HotJobs. It identifies as Konqueror so we skip these checks. |
9 | - if (stripos($package['headers_mixed']['User-Agent'], "YahooSeeker/CafeKelsa") === FALSE || match_cidr($package['ip'], "209.73.160.0/19") === FALSE) { |
|
10 | - if (!array_key_exists('Accept', $package['headers_mixed'])) { |
|
12 | + if (stripos($package['headers_mixed']['User-Agent'], "YahooSeeker/CafeKelsa") === FALSE || match_cidr($package['ip'], "209.73.160.0/19") === FALSE) |
|
13 | + { |
|
14 | + if (!array_key_exists('Accept', $package['headers_mixed'])) |
|
15 | + { |
|
11 | 16 | return "17566707"; |
12 | 17 | } |
13 | 18 | } |
@@ -18,7 +23,8 @@ discard block |
||
18 | 23 | |
19 | 24 | function bb2_lynx($package) |
20 | 25 | { |
21 | - if (!array_key_exists('Accept', $package['headers_mixed'])) { |
|
26 | + if (!array_key_exists('Accept', $package['headers_mixed'])) |
|
27 | + { |
|
22 | 28 | return "17566707"; |
23 | 29 | } |
24 | 30 | return false; |
@@ -32,8 +38,10 @@ discard block |
||
32 | 38 | // Google Desktop fixed it, but apparently some old versions are |
33 | 39 | // still out there. :( |
34 | 40 | // Always check accept header for Mozilla user agents |
35 | - if (strpos($package['headers_mixed']['User-Agent'], "Google Desktop") === FALSE && strpos($package['headers_mixed']['User-Agent'], "PLAYSTATION 3") === FALSE) { |
|
36 | - if (!array_key_exists('Accept', $package['headers_mixed'])) { |
|
41 | + if (strpos($package['headers_mixed']['User-Agent'], "Google Desktop") === FALSE && strpos($package['headers_mixed']['User-Agent'], "PLAYSTATION 3") === FALSE) |
|
42 | + { |
|
43 | + if (!array_key_exists('Accept', $package['headers_mixed'])) |
|
44 | + { |
|
37 | 45 | return "17566707"; |
38 | 46 | } |
39 | 47 | } |
@@ -44,19 +52,22 @@ discard block |
||
44 | 52 | |
45 | 53 | function bb2_msie($package) |
46 | 54 | { |
47 | - if (!array_key_exists('Accept', $package['headers_mixed'])) { |
|
55 | + if (!array_key_exists('Accept', $package['headers_mixed'])) |
|
56 | + { |
|
48 | 57 | return "17566707"; |
49 | 58 | } |
50 | 59 | |
51 | 60 | // MSIE does NOT send "Windows ME" or "Windows XP" in the user agent |
52 | - if (strpos($package['headers_mixed']['User-Agent'], "Windows ME") !== FALSE || strpos($package['headers_mixed']['User-Agent'], "Windows XP") !== FALSE || strpos($package['headers_mixed']['User-Agent'], "Windows 2000") !== FALSE || strpos($package['headers_mixed']['User-Agent'], "Win32") !== FALSE) { |
|
61 | + if (strpos($package['headers_mixed']['User-Agent'], "Windows ME") !== FALSE || strpos($package['headers_mixed']['User-Agent'], "Windows XP") !== FALSE || strpos($package['headers_mixed']['User-Agent'], "Windows 2000") !== FALSE || strpos($package['headers_mixed']['User-Agent'], "Win32") !== FALSE) |
|
62 | + { |
|
53 | 63 | return "a1084bad"; |
54 | 64 | } |
55 | 65 | |
56 | 66 | // MSIE does NOT send Connection: TE but Akamai does |
57 | 67 | // Bypass this test when Akamai detected |
58 | 68 | // The latest version of IE for Windows CE also uses Connection: TE |
59 | - if (!array_key_exists('Akamai-Origin-Hop', $package['headers_mixed']) && strpos($package['headers_mixed']['User-Agent'], "IEMobile") === FALSE && @preg_match('/\bTE\b/i', $package['headers_mixed']['Connection'])) { |
|
69 | + if (!array_key_exists('Akamai-Origin-Hop', $package['headers_mixed']) && strpos($package['headers_mixed']['User-Agent'], "IEMobile") === FALSE && @preg_match('/\bTE\b/i', $package['headers_mixed']['Connection'])) |
|
70 | + { |
|
60 | 71 | return "2b90f772"; |
61 | 72 | } |
62 | 73 | |
@@ -67,7 +78,8 @@ discard block |
||
67 | 78 | |
68 | 79 | function bb2_opera($package) |
69 | 80 | { |
70 | - if (!array_key_exists('Accept', $package['headers_mixed'])) { |
|
81 | + if (!array_key_exists('Accept', $package['headers_mixed'])) |
|
82 | + { |
|
71 | 83 | return "17566707"; |
72 | 84 | } |
73 | 85 | return false; |
@@ -78,7 +90,8 @@ discard block |
||
78 | 90 | function bb2_safari($package) |
79 | 91 | { |
80 | 92 | // Bypass this test when Android is detected |
81 | - if (!array_key_exists('Accept', $package['headers_mixed']) && strpos($package['headers_mixed']['User-Agent'], "Android") === FALSE) { |
|
93 | + if (!array_key_exists('Accept', $package['headers_mixed']) && strpos($package['headers_mixed']['User-Agent'], "Android") === FALSE) |
|
94 | + { |
|
82 | 95 | return "17566707"; |
83 | 96 | } |
84 | 97 | return false; |
@@ -6,7 +6,7 @@ discard block |
||
6 | 6 | { |
7 | 7 | // CafeKelsa is a dev project at Yahoo which indexes job listings for |
8 | 8 | // Yahoo! HotJobs. It identifies as Konqueror so we skip these checks. |
9 | - if (stripos($package['headers_mixed']['User-Agent'], "YahooSeeker/CafeKelsa") === FALSE || match_cidr($package['ip'], "209.73.160.0/19") === FALSE) { |
|
9 | + if (stripos($package['headers_mixed']['User-Agent'], "YahooSeeker/CafeKelsa") === false || match_cidr($package['ip'], "209.73.160.0/19") === false) { |
|
10 | 10 | if (!array_key_exists('Accept', $package['headers_mixed'])) { |
11 | 11 | return "17566707"; |
12 | 12 | } |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | // Google Desktop fixed it, but apparently some old versions are |
33 | 33 | // still out there. :( |
34 | 34 | // Always check accept header for Mozilla user agents |
35 | - if (strpos($package['headers_mixed']['User-Agent'], "Google Desktop") === FALSE && strpos($package['headers_mixed']['User-Agent'], "PLAYSTATION 3") === FALSE) { |
|
35 | + if (strpos($package['headers_mixed']['User-Agent'], "Google Desktop") === false && strpos($package['headers_mixed']['User-Agent'], "PLAYSTATION 3") === false) { |
|
36 | 36 | if (!array_key_exists('Accept', $package['headers_mixed'])) { |
37 | 37 | return "17566707"; |
38 | 38 | } |
@@ -49,14 +49,14 @@ discard block |
||
49 | 49 | } |
50 | 50 | |
51 | 51 | // MSIE does NOT send "Windows ME" or "Windows XP" in the user agent |
52 | - if (strpos($package['headers_mixed']['User-Agent'], "Windows ME") !== FALSE || strpos($package['headers_mixed']['User-Agent'], "Windows XP") !== FALSE || strpos($package['headers_mixed']['User-Agent'], "Windows 2000") !== FALSE || strpos($package['headers_mixed']['User-Agent'], "Win32") !== FALSE) { |
|
52 | + if (strpos($package['headers_mixed']['User-Agent'], "Windows ME") !== false || strpos($package['headers_mixed']['User-Agent'], "Windows XP") !== false || strpos($package['headers_mixed']['User-Agent'], "Windows 2000") !== false || strpos($package['headers_mixed']['User-Agent'], "Win32") !== false) { |
|
53 | 53 | return "a1084bad"; |
54 | 54 | } |
55 | 55 | |
56 | 56 | // MSIE does NOT send Connection: TE but Akamai does |
57 | 57 | // Bypass this test when Akamai detected |
58 | 58 | // The latest version of IE for Windows CE also uses Connection: TE |
59 | - if (!array_key_exists('Akamai-Origin-Hop', $package['headers_mixed']) && strpos($package['headers_mixed']['User-Agent'], "IEMobile") === FALSE && @preg_match('/\bTE\b/i', $package['headers_mixed']['Connection'])) { |
|
59 | + if (!array_key_exists('Akamai-Origin-Hop', $package['headers_mixed']) && strpos($package['headers_mixed']['User-Agent'], "IEMobile") === false && @preg_match('/\bTE\b/i', $package['headers_mixed']['Connection'])) { |
|
60 | 60 | return "2b90f772"; |
61 | 61 | } |
62 | 62 | |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | function bb2_safari($package) |
79 | 79 | { |
80 | 80 | // Bypass this test when Android is detected |
81 | - if (!array_key_exists('Accept', $package['headers_mixed']) && strpos($package['headers_mixed']['User-Agent'], "Android") === FALSE) { |
|
81 | + if (!array_key_exists('Accept', $package['headers_mixed']) && strpos($package['headers_mixed']['User-Agent'], "Android") === false) { |
|
82 | 82 | return "17566707"; |
83 | 83 | } |
84 | 84 | return false; |
@@ -24,7 +24,8 @@ |
||
24 | 24 | * @param string $input_css |
25 | 25 | * @return string |
26 | 26 | */ |
27 | -function CSSmin($input_css) { |
|
27 | +function CSSmin($input_css) |
|
28 | +{ |
|
28 | 29 | $compressor = new \tubalmartin\CssMin\Minifier(); |
29 | 30 | |
30 | 31 | // Split long lines in the output approximately every 1000 chars. |
@@ -24,872 +24,872 @@ |
||
24 | 24 | |
25 | 25 | class Minifier |
26 | 26 | { |
27 | - const QUERY_FRACTION = '_CSSMIN_QF_'; |
|
28 | - const COMMENT_TOKEN = '_CSSMIN_CMT_%d_'; |
|
29 | - const COMMENT_TOKEN_START = '_CSSMIN_CMT_'; |
|
30 | - const RULE_BODY_TOKEN = '_CSSMIN_RBT_%d_'; |
|
31 | - const PRESERVED_TOKEN = '_CSSMIN_PTK_%d_'; |
|
27 | + const QUERY_FRACTION = '_CSSMIN_QF_'; |
|
28 | + const COMMENT_TOKEN = '_CSSMIN_CMT_%d_'; |
|
29 | + const COMMENT_TOKEN_START = '_CSSMIN_CMT_'; |
|
30 | + const RULE_BODY_TOKEN = '_CSSMIN_RBT_%d_'; |
|
31 | + const PRESERVED_TOKEN = '_CSSMIN_PTK_%d_'; |
|
32 | 32 | |
33 | - // Token lists |
|
34 | - private $comments = array(); |
|
35 | - private $ruleBodies = array(); |
|
36 | - private $preservedTokens = array(); |
|
33 | + // Token lists |
|
34 | + private $comments = array(); |
|
35 | + private $ruleBodies = array(); |
|
36 | + private $preservedTokens = array(); |
|
37 | 37 | |
38 | - // Output options |
|
39 | - private $keepImportantComments = true; |
|
40 | - private $keepSourceMapComment = false; |
|
41 | - private $linebreakPosition = 0; |
|
38 | + // Output options |
|
39 | + private $keepImportantComments = true; |
|
40 | + private $keepSourceMapComment = false; |
|
41 | + private $linebreakPosition = 0; |
|
42 | 42 | |
43 | - // PHP ini limits |
|
44 | - private $raisePhpLimits; |
|
45 | - private $memoryLimit; |
|
46 | - private $maxExecutionTime = 60; // 1 min |
|
47 | - private $pcreBacktrackLimit; |
|
48 | - private $pcreRecursionLimit; |
|
43 | + // PHP ini limits |
|
44 | + private $raisePhpLimits; |
|
45 | + private $memoryLimit; |
|
46 | + private $maxExecutionTime = 60; // 1 min |
|
47 | + private $pcreBacktrackLimit; |
|
48 | + private $pcreRecursionLimit; |
|
49 | 49 | |
50 | - // Color maps |
|
51 | - private $hexToNamedColorsMap; |
|
52 | - private $namedToHexColorsMap; |
|
50 | + // Color maps |
|
51 | + private $hexToNamedColorsMap; |
|
52 | + private $namedToHexColorsMap; |
|
53 | 53 | |
54 | - // Regexes |
|
55 | - private $numRegex; |
|
56 | - private $charsetRegex = '/@charset [^;]+;/Si'; |
|
57 | - private $importRegex = '/@import [^;]+;/Si'; |
|
58 | - private $namespaceRegex = '/@namespace [^;]+;/Si'; |
|
59 | - private $namedToHexColorsRegex; |
|
60 | - private $shortenOneZeroesRegex; |
|
61 | - private $shortenTwoZeroesRegex; |
|
62 | - private $shortenThreeZeroesRegex; |
|
63 | - private $shortenFourZeroesRegex; |
|
64 | - private $unitsGroupRegex = '(?:ch|cm|em|ex|gd|in|mm|px|pt|pc|q|rem|vh|vmax|vmin|vw|%)'; |
|
65 | - |
|
66 | - /** |
|
67 | - * @param bool|int $raisePhpLimits If true, PHP settings will be raised if needed |
|
68 | - */ |
|
69 | - public function __construct($raisePhpLimits = true) |
|
70 | - { |
|
71 | - $this->raisePhpLimits = (bool) $raisePhpLimits; |
|
72 | - $this->memoryLimit = 128 * 1048576; // 128MB in bytes |
|
73 | - $this->pcreBacktrackLimit = 1000 * 1000; |
|
74 | - $this->pcreRecursionLimit = 500 * 1000; |
|
75 | - $this->hexToNamedColorsMap = Colors::getHexToNamedMap(); |
|
76 | - $this->namedToHexColorsMap = Colors::getNamedToHexMap(); |
|
77 | - $this->namedToHexColorsRegex = sprintf( |
|
78 | - '/([:,( ])(%s)( |,|\)|;|$)/Si', |
|
79 | - implode('|', array_keys($this->namedToHexColorsMap)) |
|
80 | - ); |
|
81 | - $this->numRegex = sprintf('-?\d*\.?\d+%s?', $this->unitsGroupRegex); |
|
82 | - $this->setShortenZeroValuesRegexes(); |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Parses & minifies the given input CSS string |
|
87 | - * @param string $css |
|
88 | - * @return string |
|
89 | - */ |
|
90 | - public function run($css = '') |
|
91 | - { |
|
92 | - if (empty($css) || !is_string($css)) { |
|
93 | - return ''; |
|
94 | - } |
|
95 | - |
|
96 | - $this->resetRunProperties(); |
|
97 | - |
|
98 | - if ($this->raisePhpLimits) { |
|
99 | - $this->doRaisePhpLimits(); |
|
100 | - } |
|
101 | - |
|
102 | - return $this->minify($css); |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Sets whether to keep or remove sourcemap special comment. |
|
107 | - * Sourcemap comments are removed by default. |
|
108 | - * @param bool $keepSourceMapComment |
|
109 | - */ |
|
110 | - public function keepSourceMapComment($keepSourceMapComment = true) |
|
111 | - { |
|
112 | - $this->keepSourceMapComment = (bool) $keepSourceMapComment; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Sets whether to keep or remove important comments. |
|
117 | - * Important comments outside of a declaration block are kept by default. |
|
118 | - * @param bool $removeImportantComments |
|
119 | - */ |
|
120 | - public function removeImportantComments($removeImportantComments = true) |
|
121 | - { |
|
122 | - $this->keepImportantComments = !(bool) $removeImportantComments; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Sets the approximate column after which long lines will be splitted in the output |
|
127 | - * with a linebreak. |
|
128 | - * @param int $position |
|
129 | - */ |
|
130 | - public function setLineBreakPosition($position) |
|
131 | - { |
|
132 | - $this->linebreakPosition = (int) $position; |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Sets the memory limit for this script |
|
137 | - * @param int|string $limit |
|
138 | - */ |
|
139 | - public function setMemoryLimit($limit) |
|
140 | - { |
|
141 | - $this->memoryLimit = Utils::normalizeInt($limit); |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Sets the maximum execution time for this script |
|
146 | - * @param int|string $seconds |
|
147 | - */ |
|
148 | - public function setMaxExecutionTime($seconds) |
|
149 | - { |
|
150 | - $this->maxExecutionTime = (int) $seconds; |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Sets the PCRE backtrack limit for this script |
|
155 | - * @param int $limit |
|
156 | - */ |
|
157 | - public function setPcreBacktrackLimit($limit) |
|
158 | - { |
|
159 | - $this->pcreBacktrackLimit = (int) $limit; |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Sets the PCRE recursion limit for this script |
|
164 | - * @param int $limit |
|
165 | - */ |
|
166 | - public function setPcreRecursionLimit($limit) |
|
167 | - { |
|
168 | - $this->pcreRecursionLimit = (int) $limit; |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * Builds regular expressions needed for shortening zero values |
|
173 | - */ |
|
174 | - private function setShortenZeroValuesRegexes() |
|
175 | - { |
|
176 | - $zeroRegex = '0'. $this->unitsGroupRegex; |
|
177 | - $numOrPosRegex = '('. $this->numRegex .'|top|left|bottom|right|center) '; |
|
178 | - $oneZeroSafeProperties = array( |
|
179 | - '(?:line-)?height', |
|
180 | - '(?:(?:min|max)-)?width', |
|
181 | - 'top', |
|
182 | - 'left', |
|
183 | - 'background-position', |
|
184 | - 'bottom', |
|
185 | - 'right', |
|
186 | - 'border(?:-(?:top|left|bottom|right))?(?:-width)?', |
|
187 | - 'border-(?:(?:top|bottom)-(?:left|right)-)?radius', |
|
188 | - 'column-(?:gap|width)', |
|
189 | - 'margin(?:-(?:top|left|bottom|right))?', |
|
190 | - 'outline-width', |
|
191 | - 'padding(?:-(?:top|left|bottom|right))?' |
|
192 | - ); |
|
193 | - |
|
194 | - // First zero regex |
|
195 | - $regex = '/(^|;)('. implode('|', $oneZeroSafeProperties) .'):%s/Si'; |
|
196 | - $this->shortenOneZeroesRegex = sprintf($regex, $zeroRegex); |
|
197 | - |
|
198 | - // Multiple zeroes regexes |
|
199 | - $regex = '/(^|;)(margin|padding|border-(?:width|radius)|background-position):%s/Si'; |
|
200 | - $this->shortenTwoZeroesRegex = sprintf($regex, $numOrPosRegex . $zeroRegex); |
|
201 | - $this->shortenThreeZeroesRegex = sprintf($regex, $numOrPosRegex . $numOrPosRegex . $zeroRegex); |
|
202 | - $this->shortenFourZeroesRegex = sprintf($regex, $numOrPosRegex . $numOrPosRegex . $numOrPosRegex . $zeroRegex); |
|
203 | - } |
|
204 | - |
|
205 | - /** |
|
206 | - * Resets properties whose value may change between runs |
|
207 | - */ |
|
208 | - private function resetRunProperties() |
|
209 | - { |
|
210 | - $this->comments = array(); |
|
211 | - $this->ruleBodies = array(); |
|
212 | - $this->preservedTokens = array(); |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * Tries to configure PHP to use at least the suggested minimum settings |
|
217 | - * @return void |
|
218 | - */ |
|
219 | - private function doRaisePhpLimits() |
|
220 | - { |
|
221 | - $phpLimits = array( |
|
222 | - 'memory_limit' => $this->memoryLimit, |
|
223 | - 'max_execution_time' => $this->maxExecutionTime, |
|
224 | - 'pcre.backtrack_limit' => $this->pcreBacktrackLimit, |
|
225 | - 'pcre.recursion_limit' => $this->pcreRecursionLimit |
|
226 | - ); |
|
227 | - |
|
228 | - // If current settings are higher respect them. |
|
229 | - foreach ($phpLimits as $name => $suggested) { |
|
230 | - $current = Utils::normalizeInt(ini_get($name)); |
|
231 | - |
|
232 | - if ($current >= $suggested) { |
|
233 | - continue; |
|
234 | - } |
|
235 | - |
|
236 | - // memoryLimit exception: allow -1 for "no memory limit". |
|
237 | - if ($name === 'memory_limit' && $current === -1) { |
|
238 | - continue; |
|
239 | - } |
|
240 | - |
|
241 | - // maxExecutionTime exception: allow 0 for "no memory limit". |
|
242 | - if ($name === 'max_execution_time' && $current === 0) { |
|
243 | - continue; |
|
244 | - } |
|
245 | - |
|
246 | - ini_set($name, $suggested); |
|
247 | - } |
|
248 | - } |
|
249 | - |
|
250 | - /** |
|
251 | - * Registers a preserved token |
|
252 | - * @param string $token |
|
253 | - * @return string The token ID string |
|
254 | - */ |
|
255 | - private function registerPreservedToken($token) |
|
256 | - { |
|
257 | - $tokenId = sprintf(self::PRESERVED_TOKEN, count($this->preservedTokens)); |
|
258 | - $this->preservedTokens[$tokenId] = $token; |
|
259 | - return $tokenId; |
|
260 | - } |
|
261 | - |
|
262 | - /** |
|
263 | - * Registers a candidate comment token |
|
264 | - * @param string $comment |
|
265 | - * @return string The comment token ID string |
|
266 | - */ |
|
267 | - private function registerCommentToken($comment) |
|
268 | - { |
|
269 | - $tokenId = sprintf(self::COMMENT_TOKEN, count($this->comments)); |
|
270 | - $this->comments[$tokenId] = $comment; |
|
271 | - return $tokenId; |
|
272 | - } |
|
273 | - |
|
274 | - /** |
|
275 | - * Registers a rule body token |
|
276 | - * @param string $body the minified rule body |
|
277 | - * @return string The rule body token ID string |
|
278 | - */ |
|
279 | - private function registerRuleBodyToken($body) |
|
280 | - { |
|
281 | - if (empty($body)) { |
|
282 | - return ''; |
|
283 | - } |
|
284 | - |
|
285 | - $tokenId = sprintf(self::RULE_BODY_TOKEN, count($this->ruleBodies)); |
|
286 | - $this->ruleBodies[$tokenId] = $body; |
|
287 | - return $tokenId; |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * Parses & minifies the given input CSS string |
|
292 | - * @param string $css |
|
293 | - * @return string |
|
294 | - */ |
|
295 | - private function minify($css) |
|
296 | - { |
|
297 | - // Process data urls |
|
298 | - $css = $this->processDataUrls($css); |
|
299 | - |
|
300 | - // Process comments |
|
301 | - $css = preg_replace_callback( |
|
302 | - '/(?<!\\\\)\/\*(.*?)\*(?<!\\\\)\//Ss', |
|
303 | - array($this, 'processCommentsCallback'), |
|
304 | - $css |
|
305 | - ); |
|
306 | - |
|
307 | - // IE7: Process Microsoft matrix filters (whitespaces between Matrix parameters). Can contain strings inside. |
|
308 | - $css = preg_replace_callback( |
|
309 | - '/filter:\s*progid:DXImageTransform\.Microsoft\.Matrix\(([^)]+)\)/Ss', |
|
310 | - array($this, 'processOldIeSpecificMatrixDefinitionCallback'), |
|
311 | - $css |
|
312 | - ); |
|
313 | - |
|
314 | - // Process quoted unquotable attribute selectors to unquote them. Covers most common cases. |
|
315 | - // Likelyhood of a quoted attribute selector being a substring in a string: Very very low. |
|
316 | - $css = preg_replace( |
|
317 | - '/\[\s*([a-z][a-z-]+)\s*([\*\|\^\$~]?=)\s*[\'"](-?[a-z_][a-z0-9-_]+)[\'"]\s*\]/Ssi', |
|
318 | - '[$1$2$3]', |
|
319 | - $css |
|
320 | - ); |
|
321 | - |
|
322 | - // Process strings so their content doesn't get accidentally minified |
|
323 | - $css = preg_replace_callback( |
|
324 | - '/(?:"(?:[^\\\\"]|\\\\.|\\\\)*")|'."(?:'(?:[^\\\\']|\\\\.|\\\\)*')/S", |
|
325 | - array($this, 'processStringsCallback'), |
|
326 | - $css |
|
327 | - ); |
|
328 | - |
|
329 | - // Normalize all whitespace strings to single spaces. Easier to work with that way. |
|
330 | - $css = preg_replace('/\s+/S', ' ', $css); |
|
331 | - |
|
332 | - // Process import At-rules with unquoted URLs so URI reserved characters such as a semicolon may be used safely. |
|
333 | - $css = preg_replace_callback( |
|
334 | - '/@import url\(([^\'"]+?)\)( |;)/Si', |
|
335 | - array($this, 'processImportUnquotedUrlAtRulesCallback'), |
|
336 | - $css |
|
337 | - ); |
|
54 | + // Regexes |
|
55 | + private $numRegex; |
|
56 | + private $charsetRegex = '/@charset [^;]+;/Si'; |
|
57 | + private $importRegex = '/@import [^;]+;/Si'; |
|
58 | + private $namespaceRegex = '/@namespace [^;]+;/Si'; |
|
59 | + private $namedToHexColorsRegex; |
|
60 | + private $shortenOneZeroesRegex; |
|
61 | + private $shortenTwoZeroesRegex; |
|
62 | + private $shortenThreeZeroesRegex; |
|
63 | + private $shortenFourZeroesRegex; |
|
64 | + private $unitsGroupRegex = '(?:ch|cm|em|ex|gd|in|mm|px|pt|pc|q|rem|vh|vmax|vmin|vw|%)'; |
|
65 | + |
|
66 | + /** |
|
67 | + * @param bool|int $raisePhpLimits If true, PHP settings will be raised if needed |
|
68 | + */ |
|
69 | + public function __construct($raisePhpLimits = true) |
|
70 | + { |
|
71 | + $this->raisePhpLimits = (bool) $raisePhpLimits; |
|
72 | + $this->memoryLimit = 128 * 1048576; // 128MB in bytes |
|
73 | + $this->pcreBacktrackLimit = 1000 * 1000; |
|
74 | + $this->pcreRecursionLimit = 500 * 1000; |
|
75 | + $this->hexToNamedColorsMap = Colors::getHexToNamedMap(); |
|
76 | + $this->namedToHexColorsMap = Colors::getNamedToHexMap(); |
|
77 | + $this->namedToHexColorsRegex = sprintf( |
|
78 | + '/([:,( ])(%s)( |,|\)|;|$)/Si', |
|
79 | + implode('|', array_keys($this->namedToHexColorsMap)) |
|
80 | + ); |
|
81 | + $this->numRegex = sprintf('-?\d*\.?\d+%s?', $this->unitsGroupRegex); |
|
82 | + $this->setShortenZeroValuesRegexes(); |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Parses & minifies the given input CSS string |
|
87 | + * @param string $css |
|
88 | + * @return string |
|
89 | + */ |
|
90 | + public function run($css = '') |
|
91 | + { |
|
92 | + if (empty($css) || !is_string($css)) { |
|
93 | + return ''; |
|
94 | + } |
|
95 | + |
|
96 | + $this->resetRunProperties(); |
|
97 | + |
|
98 | + if ($this->raisePhpLimits) { |
|
99 | + $this->doRaisePhpLimits(); |
|
100 | + } |
|
101 | + |
|
102 | + return $this->minify($css); |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Sets whether to keep or remove sourcemap special comment. |
|
107 | + * Sourcemap comments are removed by default. |
|
108 | + * @param bool $keepSourceMapComment |
|
109 | + */ |
|
110 | + public function keepSourceMapComment($keepSourceMapComment = true) |
|
111 | + { |
|
112 | + $this->keepSourceMapComment = (bool) $keepSourceMapComment; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Sets whether to keep or remove important comments. |
|
117 | + * Important comments outside of a declaration block are kept by default. |
|
118 | + * @param bool $removeImportantComments |
|
119 | + */ |
|
120 | + public function removeImportantComments($removeImportantComments = true) |
|
121 | + { |
|
122 | + $this->keepImportantComments = !(bool) $removeImportantComments; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Sets the approximate column after which long lines will be splitted in the output |
|
127 | + * with a linebreak. |
|
128 | + * @param int $position |
|
129 | + */ |
|
130 | + public function setLineBreakPosition($position) |
|
131 | + { |
|
132 | + $this->linebreakPosition = (int) $position; |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Sets the memory limit for this script |
|
137 | + * @param int|string $limit |
|
138 | + */ |
|
139 | + public function setMemoryLimit($limit) |
|
140 | + { |
|
141 | + $this->memoryLimit = Utils::normalizeInt($limit); |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Sets the maximum execution time for this script |
|
146 | + * @param int|string $seconds |
|
147 | + */ |
|
148 | + public function setMaxExecutionTime($seconds) |
|
149 | + { |
|
150 | + $this->maxExecutionTime = (int) $seconds; |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Sets the PCRE backtrack limit for this script |
|
155 | + * @param int $limit |
|
156 | + */ |
|
157 | + public function setPcreBacktrackLimit($limit) |
|
158 | + { |
|
159 | + $this->pcreBacktrackLimit = (int) $limit; |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Sets the PCRE recursion limit for this script |
|
164 | + * @param int $limit |
|
165 | + */ |
|
166 | + public function setPcreRecursionLimit($limit) |
|
167 | + { |
|
168 | + $this->pcreRecursionLimit = (int) $limit; |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * Builds regular expressions needed for shortening zero values |
|
173 | + */ |
|
174 | + private function setShortenZeroValuesRegexes() |
|
175 | + { |
|
176 | + $zeroRegex = '0'. $this->unitsGroupRegex; |
|
177 | + $numOrPosRegex = '('. $this->numRegex .'|top|left|bottom|right|center) '; |
|
178 | + $oneZeroSafeProperties = array( |
|
179 | + '(?:line-)?height', |
|
180 | + '(?:(?:min|max)-)?width', |
|
181 | + 'top', |
|
182 | + 'left', |
|
183 | + 'background-position', |
|
184 | + 'bottom', |
|
185 | + 'right', |
|
186 | + 'border(?:-(?:top|left|bottom|right))?(?:-width)?', |
|
187 | + 'border-(?:(?:top|bottom)-(?:left|right)-)?radius', |
|
188 | + 'column-(?:gap|width)', |
|
189 | + 'margin(?:-(?:top|left|bottom|right))?', |
|
190 | + 'outline-width', |
|
191 | + 'padding(?:-(?:top|left|bottom|right))?' |
|
192 | + ); |
|
193 | + |
|
194 | + // First zero regex |
|
195 | + $regex = '/(^|;)('. implode('|', $oneZeroSafeProperties) .'):%s/Si'; |
|
196 | + $this->shortenOneZeroesRegex = sprintf($regex, $zeroRegex); |
|
197 | + |
|
198 | + // Multiple zeroes regexes |
|
199 | + $regex = '/(^|;)(margin|padding|border-(?:width|radius)|background-position):%s/Si'; |
|
200 | + $this->shortenTwoZeroesRegex = sprintf($regex, $numOrPosRegex . $zeroRegex); |
|
201 | + $this->shortenThreeZeroesRegex = sprintf($regex, $numOrPosRegex . $numOrPosRegex . $zeroRegex); |
|
202 | + $this->shortenFourZeroesRegex = sprintf($regex, $numOrPosRegex . $numOrPosRegex . $numOrPosRegex . $zeroRegex); |
|
203 | + } |
|
204 | + |
|
205 | + /** |
|
206 | + * Resets properties whose value may change between runs |
|
207 | + */ |
|
208 | + private function resetRunProperties() |
|
209 | + { |
|
210 | + $this->comments = array(); |
|
211 | + $this->ruleBodies = array(); |
|
212 | + $this->preservedTokens = array(); |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * Tries to configure PHP to use at least the suggested minimum settings |
|
217 | + * @return void |
|
218 | + */ |
|
219 | + private function doRaisePhpLimits() |
|
220 | + { |
|
221 | + $phpLimits = array( |
|
222 | + 'memory_limit' => $this->memoryLimit, |
|
223 | + 'max_execution_time' => $this->maxExecutionTime, |
|
224 | + 'pcre.backtrack_limit' => $this->pcreBacktrackLimit, |
|
225 | + 'pcre.recursion_limit' => $this->pcreRecursionLimit |
|
226 | + ); |
|
227 | + |
|
228 | + // If current settings are higher respect them. |
|
229 | + foreach ($phpLimits as $name => $suggested) { |
|
230 | + $current = Utils::normalizeInt(ini_get($name)); |
|
231 | + |
|
232 | + if ($current >= $suggested) { |
|
233 | + continue; |
|
234 | + } |
|
235 | + |
|
236 | + // memoryLimit exception: allow -1 for "no memory limit". |
|
237 | + if ($name === 'memory_limit' && $current === -1) { |
|
238 | + continue; |
|
239 | + } |
|
240 | + |
|
241 | + // maxExecutionTime exception: allow 0 for "no memory limit". |
|
242 | + if ($name === 'max_execution_time' && $current === 0) { |
|
243 | + continue; |
|
244 | + } |
|
245 | + |
|
246 | + ini_set($name, $suggested); |
|
247 | + } |
|
248 | + } |
|
249 | + |
|
250 | + /** |
|
251 | + * Registers a preserved token |
|
252 | + * @param string $token |
|
253 | + * @return string The token ID string |
|
254 | + */ |
|
255 | + private function registerPreservedToken($token) |
|
256 | + { |
|
257 | + $tokenId = sprintf(self::PRESERVED_TOKEN, count($this->preservedTokens)); |
|
258 | + $this->preservedTokens[$tokenId] = $token; |
|
259 | + return $tokenId; |
|
260 | + } |
|
261 | + |
|
262 | + /** |
|
263 | + * Registers a candidate comment token |
|
264 | + * @param string $comment |
|
265 | + * @return string The comment token ID string |
|
266 | + */ |
|
267 | + private function registerCommentToken($comment) |
|
268 | + { |
|
269 | + $tokenId = sprintf(self::COMMENT_TOKEN, count($this->comments)); |
|
270 | + $this->comments[$tokenId] = $comment; |
|
271 | + return $tokenId; |
|
272 | + } |
|
273 | + |
|
274 | + /** |
|
275 | + * Registers a rule body token |
|
276 | + * @param string $body the minified rule body |
|
277 | + * @return string The rule body token ID string |
|
278 | + */ |
|
279 | + private function registerRuleBodyToken($body) |
|
280 | + { |
|
281 | + if (empty($body)) { |
|
282 | + return ''; |
|
283 | + } |
|
284 | + |
|
285 | + $tokenId = sprintf(self::RULE_BODY_TOKEN, count($this->ruleBodies)); |
|
286 | + $this->ruleBodies[$tokenId] = $body; |
|
287 | + return $tokenId; |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * Parses & minifies the given input CSS string |
|
292 | + * @param string $css |
|
293 | + * @return string |
|
294 | + */ |
|
295 | + private function minify($css) |
|
296 | + { |
|
297 | + // Process data urls |
|
298 | + $css = $this->processDataUrls($css); |
|
299 | + |
|
300 | + // Process comments |
|
301 | + $css = preg_replace_callback( |
|
302 | + '/(?<!\\\\)\/\*(.*?)\*(?<!\\\\)\//Ss', |
|
303 | + array($this, 'processCommentsCallback'), |
|
304 | + $css |
|
305 | + ); |
|
306 | + |
|
307 | + // IE7: Process Microsoft matrix filters (whitespaces between Matrix parameters). Can contain strings inside. |
|
308 | + $css = preg_replace_callback( |
|
309 | + '/filter:\s*progid:DXImageTransform\.Microsoft\.Matrix\(([^)]+)\)/Ss', |
|
310 | + array($this, 'processOldIeSpecificMatrixDefinitionCallback'), |
|
311 | + $css |
|
312 | + ); |
|
313 | + |
|
314 | + // Process quoted unquotable attribute selectors to unquote them. Covers most common cases. |
|
315 | + // Likelyhood of a quoted attribute selector being a substring in a string: Very very low. |
|
316 | + $css = preg_replace( |
|
317 | + '/\[\s*([a-z][a-z-]+)\s*([\*\|\^\$~]?=)\s*[\'"](-?[a-z_][a-z0-9-_]+)[\'"]\s*\]/Ssi', |
|
318 | + '[$1$2$3]', |
|
319 | + $css |
|
320 | + ); |
|
321 | + |
|
322 | + // Process strings so their content doesn't get accidentally minified |
|
323 | + $css = preg_replace_callback( |
|
324 | + '/(?:"(?:[^\\\\"]|\\\\.|\\\\)*")|'."(?:'(?:[^\\\\']|\\\\.|\\\\)*')/S", |
|
325 | + array($this, 'processStringsCallback'), |
|
326 | + $css |
|
327 | + ); |
|
328 | + |
|
329 | + // Normalize all whitespace strings to single spaces. Easier to work with that way. |
|
330 | + $css = preg_replace('/\s+/S', ' ', $css); |
|
331 | + |
|
332 | + // Process import At-rules with unquoted URLs so URI reserved characters such as a semicolon may be used safely. |
|
333 | + $css = preg_replace_callback( |
|
334 | + '/@import url\(([^\'"]+?)\)( |;)/Si', |
|
335 | + array($this, 'processImportUnquotedUrlAtRulesCallback'), |
|
336 | + $css |
|
337 | + ); |
|
338 | 338 | |
339 | - // Process comments |
|
340 | - $css = $this->processComments($css); |
|
339 | + // Process comments |
|
340 | + $css = $this->processComments($css); |
|
341 | 341 | |
342 | - // Process rule bodies |
|
343 | - $css = $this->processRuleBodies($css); |
|
342 | + // Process rule bodies |
|
343 | + $css = $this->processRuleBodies($css); |
|
344 | 344 | |
345 | - // Process at-rules and selectors |
|
346 | - $css = $this->processAtRulesAndSelectors($css); |
|
347 | - |
|
348 | - // Restore preserved rule bodies before splitting |
|
349 | - $css = strtr($css, $this->ruleBodies); |
|
350 | - |
|
351 | - // Split long lines in output if required |
|
352 | - $css = $this->processLongLineSplitting($css); |
|
353 | - |
|
354 | - // Restore preserved comments and strings |
|
355 | - $css = strtr($css, $this->preservedTokens); |
|
356 | - |
|
357 | - return trim($css); |
|
358 | - } |
|
359 | - |
|
360 | - /** |
|
361 | - * Searches & replaces all data urls with tokens before we start compressing, |
|
362 | - * to avoid performance issues running some of the subsequent regexes against large string chunks. |
|
363 | - * @param string $css |
|
364 | - * @return string |
|
365 | - */ |
|
366 | - private function processDataUrls($css) |
|
367 | - { |
|
368 | - $ret = ''; |
|
369 | - $searchOffset = $substrOffset = 0; |
|
370 | - |
|
371 | - // Since we need to account for non-base64 data urls, we need to handle |
|
372 | - // ' and ) being part of the data string. |
|
373 | - while (preg_match('/url\(\s*(["\']?)data:/Si', $css, $m, PREG_OFFSET_CAPTURE, $searchOffset)) { |
|
374 | - $matchStartIndex = $m[0][1]; |
|
375 | - $dataStartIndex = $matchStartIndex + 4; // url( length |
|
376 | - $searchOffset = $matchStartIndex + strlen($m[0][0]); |
|
377 | - $terminator = $m[1][0]; // ', " or empty (not quoted) |
|
378 | - $terminatorRegex = '/(?<!\\\\)'. (strlen($terminator) === 0 ? '' : $terminator.'\s*') .'(\))/S'; |
|
345 | + // Process at-rules and selectors |
|
346 | + $css = $this->processAtRulesAndSelectors($css); |
|
347 | + |
|
348 | + // Restore preserved rule bodies before splitting |
|
349 | + $css = strtr($css, $this->ruleBodies); |
|
350 | + |
|
351 | + // Split long lines in output if required |
|
352 | + $css = $this->processLongLineSplitting($css); |
|
353 | + |
|
354 | + // Restore preserved comments and strings |
|
355 | + $css = strtr($css, $this->preservedTokens); |
|
356 | + |
|
357 | + return trim($css); |
|
358 | + } |
|
359 | + |
|
360 | + /** |
|
361 | + * Searches & replaces all data urls with tokens before we start compressing, |
|
362 | + * to avoid performance issues running some of the subsequent regexes against large string chunks. |
|
363 | + * @param string $css |
|
364 | + * @return string |
|
365 | + */ |
|
366 | + private function processDataUrls($css) |
|
367 | + { |
|
368 | + $ret = ''; |
|
369 | + $searchOffset = $substrOffset = 0; |
|
370 | + |
|
371 | + // Since we need to account for non-base64 data urls, we need to handle |
|
372 | + // ' and ) being part of the data string. |
|
373 | + while (preg_match('/url\(\s*(["\']?)data:/Si', $css, $m, PREG_OFFSET_CAPTURE, $searchOffset)) { |
|
374 | + $matchStartIndex = $m[0][1]; |
|
375 | + $dataStartIndex = $matchStartIndex + 4; // url( length |
|
376 | + $searchOffset = $matchStartIndex + strlen($m[0][0]); |
|
377 | + $terminator = $m[1][0]; // ', " or empty (not quoted) |
|
378 | + $terminatorRegex = '/(?<!\\\\)'. (strlen($terminator) === 0 ? '' : $terminator.'\s*') .'(\))/S'; |
|
379 | 379 | |
380 | - $ret .= substr($css, $substrOffset, $matchStartIndex - $substrOffset); |
|
381 | - |
|
382 | - // Terminator found |
|
383 | - if (preg_match($terminatorRegex, $css, $matches, PREG_OFFSET_CAPTURE, $searchOffset)) { |
|
384 | - $matchEndIndex = $matches[1][1]; |
|
385 | - $searchOffset = $matchEndIndex + 1; |
|
386 | - $token = substr($css, $dataStartIndex, $matchEndIndex - $dataStartIndex); |
|
387 | - |
|
388 | - // Remove all spaces only for base64 encoded URLs. |
|
389 | - if (stripos($token, 'base64,') !== false) { |
|
390 | - $token = preg_replace('/\s+/S', '', $token); |
|
391 | - } |
|
392 | - |
|
393 | - $ret .= 'url('. $this->registerPreservedToken(trim($token)) .')'; |
|
394 | - // No end terminator found, re-add the whole match. Should we throw/warn here? |
|
395 | - } else { |
|
396 | - $ret .= substr($css, $matchStartIndex, $searchOffset - $matchStartIndex); |
|
397 | - } |
|
398 | - |
|
399 | - $substrOffset = $searchOffset; |
|
400 | - } |
|
401 | - |
|
402 | - $ret .= substr($css, $substrOffset); |
|
403 | - |
|
404 | - return $ret; |
|
405 | - } |
|
406 | - |
|
407 | - /** |
|
408 | - * Registers all comments found as candidates to be preserved. |
|
409 | - * @param array $matches |
|
410 | - * @return string |
|
411 | - */ |
|
412 | - private function processCommentsCallback($matches) |
|
413 | - { |
|
414 | - return '/*'. $this->registerCommentToken($matches[1]) .'*/'; |
|
415 | - } |
|
416 | - |
|
417 | - /** |
|
418 | - * Preserves old IE Matrix string definition |
|
419 | - * @param array $matches |
|
420 | - * @return string |
|
421 | - */ |
|
422 | - private function processOldIeSpecificMatrixDefinitionCallback($matches) |
|
423 | - { |
|
424 | - return 'filter:progid:DXImageTransform.Microsoft.Matrix('. $this->registerPreservedToken($matches[1]) .')'; |
|
425 | - } |
|
426 | - |
|
427 | - /** |
|
428 | - * Preserves strings found |
|
429 | - * @param array $matches |
|
430 | - * @return string |
|
431 | - */ |
|
432 | - private function processStringsCallback($matches) |
|
433 | - { |
|
434 | - $match = $matches[0]; |
|
435 | - $quote = substr($match, 0, 1); |
|
436 | - $match = substr($match, 1, -1); |
|
437 | - |
|
438 | - // maybe the string contains a comment-like substring? |
|
439 | - // one, maybe more? put'em back then |
|
440 | - if (strpos($match, self::COMMENT_TOKEN_START) !== false) { |
|
441 | - $match = strtr($match, $this->comments); |
|
442 | - } |
|
443 | - |
|
444 | - // minify alpha opacity in filter strings |
|
445 | - $match = str_ireplace('progid:DXImageTransform.Microsoft.Alpha(Opacity=', 'alpha(opacity=', $match); |
|
446 | - |
|
447 | - return $quote . $this->registerPreservedToken($match) . $quote; |
|
448 | - } |
|
449 | - |
|
450 | - /** |
|
451 | - * Searches & replaces all import at-rule unquoted urls with tokens so URI reserved characters such as a semicolon |
|
452 | - * may be used safely in a URL. |
|
453 | - * @param array $matches |
|
454 | - * @return string |
|
455 | - */ |
|
456 | - private function processImportUnquotedUrlAtRulesCallback($matches) |
|
457 | - { |
|
458 | - return '@import url('. $this->registerPreservedToken($matches[1]) .')'. $matches[2]; |
|
459 | - } |
|
460 | - |
|
461 | - /** |
|
462 | - * Preserves or removes comments found. |
|
463 | - * @param string $css |
|
464 | - * @return string |
|
465 | - */ |
|
466 | - private function processComments($css) |
|
467 | - { |
|
468 | - foreach ($this->comments as $commentId => $comment) { |
|
469 | - $commentIdString = '/*'. $commentId .'*/'; |
|
380 | + $ret .= substr($css, $substrOffset, $matchStartIndex - $substrOffset); |
|
381 | + |
|
382 | + // Terminator found |
|
383 | + if (preg_match($terminatorRegex, $css, $matches, PREG_OFFSET_CAPTURE, $searchOffset)) { |
|
384 | + $matchEndIndex = $matches[1][1]; |
|
385 | + $searchOffset = $matchEndIndex + 1; |
|
386 | + $token = substr($css, $dataStartIndex, $matchEndIndex - $dataStartIndex); |
|
387 | + |
|
388 | + // Remove all spaces only for base64 encoded URLs. |
|
389 | + if (stripos($token, 'base64,') !== false) { |
|
390 | + $token = preg_replace('/\s+/S', '', $token); |
|
391 | + } |
|
392 | + |
|
393 | + $ret .= 'url('. $this->registerPreservedToken(trim($token)) .')'; |
|
394 | + // No end terminator found, re-add the whole match. Should we throw/warn here? |
|
395 | + } else { |
|
396 | + $ret .= substr($css, $matchStartIndex, $searchOffset - $matchStartIndex); |
|
397 | + } |
|
398 | + |
|
399 | + $substrOffset = $searchOffset; |
|
400 | + } |
|
401 | + |
|
402 | + $ret .= substr($css, $substrOffset); |
|
403 | + |
|
404 | + return $ret; |
|
405 | + } |
|
406 | + |
|
407 | + /** |
|
408 | + * Registers all comments found as candidates to be preserved. |
|
409 | + * @param array $matches |
|
410 | + * @return string |
|
411 | + */ |
|
412 | + private function processCommentsCallback($matches) |
|
413 | + { |
|
414 | + return '/*'. $this->registerCommentToken($matches[1]) .'*/'; |
|
415 | + } |
|
416 | + |
|
417 | + /** |
|
418 | + * Preserves old IE Matrix string definition |
|
419 | + * @param array $matches |
|
420 | + * @return string |
|
421 | + */ |
|
422 | + private function processOldIeSpecificMatrixDefinitionCallback($matches) |
|
423 | + { |
|
424 | + return 'filter:progid:DXImageTransform.Microsoft.Matrix('. $this->registerPreservedToken($matches[1]) .')'; |
|
425 | + } |
|
426 | + |
|
427 | + /** |
|
428 | + * Preserves strings found |
|
429 | + * @param array $matches |
|
430 | + * @return string |
|
431 | + */ |
|
432 | + private function processStringsCallback($matches) |
|
433 | + { |
|
434 | + $match = $matches[0]; |
|
435 | + $quote = substr($match, 0, 1); |
|
436 | + $match = substr($match, 1, -1); |
|
437 | + |
|
438 | + // maybe the string contains a comment-like substring? |
|
439 | + // one, maybe more? put'em back then |
|
440 | + if (strpos($match, self::COMMENT_TOKEN_START) !== false) { |
|
441 | + $match = strtr($match, $this->comments); |
|
442 | + } |
|
443 | + |
|
444 | + // minify alpha opacity in filter strings |
|
445 | + $match = str_ireplace('progid:DXImageTransform.Microsoft.Alpha(Opacity=', 'alpha(opacity=', $match); |
|
446 | + |
|
447 | + return $quote . $this->registerPreservedToken($match) . $quote; |
|
448 | + } |
|
449 | + |
|
450 | + /** |
|
451 | + * Searches & replaces all import at-rule unquoted urls with tokens so URI reserved characters such as a semicolon |
|
452 | + * may be used safely in a URL. |
|
453 | + * @param array $matches |
|
454 | + * @return string |
|
455 | + */ |
|
456 | + private function processImportUnquotedUrlAtRulesCallback($matches) |
|
457 | + { |
|
458 | + return '@import url('. $this->registerPreservedToken($matches[1]) .')'. $matches[2]; |
|
459 | + } |
|
460 | + |
|
461 | + /** |
|
462 | + * Preserves or removes comments found. |
|
463 | + * @param string $css |
|
464 | + * @return string |
|
465 | + */ |
|
466 | + private function processComments($css) |
|
467 | + { |
|
468 | + foreach ($this->comments as $commentId => $comment) { |
|
469 | + $commentIdString = '/*'. $commentId .'*/'; |
|
470 | 470 | |
471 | - // ! in the first position of the comment means preserve |
|
472 | - // so push to the preserved tokens keeping the ! |
|
473 | - if ($this->keepImportantComments && strpos($comment, '!') === 0) { |
|
474 | - $preservedTokenId = $this->registerPreservedToken($comment); |
|
475 | - // Put new lines before and after /*! important comments |
|
476 | - $css = str_replace($commentIdString, "\n/*$preservedTokenId*/\n", $css); |
|
477 | - continue; |
|
478 | - } |
|
479 | - |
|
480 | - // # sourceMappingURL= in the first position of the comment means sourcemap |
|
481 | - // so push to the preserved tokens if {$this->keepSourceMapComment} is truthy. |
|
482 | - if ($this->keepSourceMapComment && strpos($comment, '# sourceMappingURL=') === 0) { |
|
483 | - $preservedTokenId = $this->registerPreservedToken($comment); |
|
484 | - // Add new line before the sourcemap comment |
|
485 | - $css = str_replace($commentIdString, "\n/*$preservedTokenId*/", $css); |
|
486 | - continue; |
|
487 | - } |
|
488 | - |
|
489 | - // Keep empty comments after child selectors (IE7 hack) |
|
490 | - // e.g. html >/**/ body |
|
491 | - if (strlen($comment) === 0 && strpos($css, '>/*'.$commentId) !== false) { |
|
492 | - $css = str_replace($commentId, $this->registerPreservedToken(''), $css); |
|
493 | - continue; |
|
494 | - } |
|
495 | - |
|
496 | - // in all other cases kill the comment |
|
497 | - $css = str_replace($commentIdString, '', $css); |
|
498 | - } |
|
499 | - |
|
500 | - // Normalize whitespace again |
|
501 | - $css = preg_replace('/ +/S', ' ', $css); |
|
502 | - |
|
503 | - return $css; |
|
504 | - } |
|
505 | - |
|
506 | - /** |
|
507 | - * Finds, minifies & preserves all rule bodies. |
|
508 | - * @param string $css the whole stylesheet. |
|
509 | - * @return string |
|
510 | - */ |
|
511 | - private function processRuleBodies($css) |
|
512 | - { |
|
513 | - $ret = ''; |
|
514 | - $searchOffset = $substrOffset = 0; |
|
515 | - |
|
516 | - while (($blockStartPos = strpos($css, '{', $searchOffset)) !== false) { |
|
517 | - $blockEndPos = strpos($css, '}', $blockStartPos); |
|
518 | - $nextBlockStartPos = strpos($css, '{', $blockStartPos + 1); |
|
519 | - $ret .= substr($css, $substrOffset, $blockStartPos - $substrOffset); |
|
520 | - |
|
521 | - if ($nextBlockStartPos !== false && $nextBlockStartPos < $blockEndPos) { |
|
522 | - $ret .= substr($css, $blockStartPos, $nextBlockStartPos - $blockStartPos); |
|
523 | - $searchOffset = $nextBlockStartPos; |
|
524 | - } else { |
|
525 | - $ruleBody = substr($css, $blockStartPos + 1, $blockEndPos - $blockStartPos - 1); |
|
526 | - $ruleBodyToken = $this->registerRuleBodyToken($this->processRuleBody($ruleBody)); |
|
527 | - $ret .= '{'. $ruleBodyToken .'}'; |
|
528 | - $searchOffset = $blockEndPos + 1; |
|
529 | - } |
|
530 | - |
|
531 | - $substrOffset = $searchOffset; |
|
532 | - } |
|
533 | - |
|
534 | - $ret .= substr($css, $substrOffset); |
|
535 | - |
|
536 | - return $ret; |
|
537 | - } |
|
538 | - |
|
539 | - /** |
|
540 | - * Compresses non-group rule bodies. |
|
541 | - * @param string $body The rule body without curly braces |
|
542 | - * @return string |
|
543 | - */ |
|
544 | - private function processRuleBody($body) |
|
545 | - { |
|
546 | - $body = trim($body); |
|
547 | - |
|
548 | - // Remove spaces before the things that should not have spaces before them. |
|
549 | - $body = preg_replace('/ ([:=,)*\/;\n])/S', '$1', $body); |
|
550 | - |
|
551 | - // Remove the spaces after the things that should not have spaces after them. |
|
552 | - $body = preg_replace('/([:=,(*\/!;\n]) /S', '$1', $body); |
|
471 | + // ! in the first position of the comment means preserve |
|
472 | + // so push to the preserved tokens keeping the ! |
|
473 | + if ($this->keepImportantComments && strpos($comment, '!') === 0) { |
|
474 | + $preservedTokenId = $this->registerPreservedToken($comment); |
|
475 | + // Put new lines before and after /*! important comments |
|
476 | + $css = str_replace($commentIdString, "\n/*$preservedTokenId*/\n", $css); |
|
477 | + continue; |
|
478 | + } |
|
479 | + |
|
480 | + // # sourceMappingURL= in the first position of the comment means sourcemap |
|
481 | + // so push to the preserved tokens if {$this->keepSourceMapComment} is truthy. |
|
482 | + if ($this->keepSourceMapComment && strpos($comment, '# sourceMappingURL=') === 0) { |
|
483 | + $preservedTokenId = $this->registerPreservedToken($comment); |
|
484 | + // Add new line before the sourcemap comment |
|
485 | + $css = str_replace($commentIdString, "\n/*$preservedTokenId*/", $css); |
|
486 | + continue; |
|
487 | + } |
|
488 | + |
|
489 | + // Keep empty comments after child selectors (IE7 hack) |
|
490 | + // e.g. html >/**/ body |
|
491 | + if (strlen($comment) === 0 && strpos($css, '>/*'.$commentId) !== false) { |
|
492 | + $css = str_replace($commentId, $this->registerPreservedToken(''), $css); |
|
493 | + continue; |
|
494 | + } |
|
495 | + |
|
496 | + // in all other cases kill the comment |
|
497 | + $css = str_replace($commentIdString, '', $css); |
|
498 | + } |
|
499 | + |
|
500 | + // Normalize whitespace again |
|
501 | + $css = preg_replace('/ +/S', ' ', $css); |
|
502 | + |
|
503 | + return $css; |
|
504 | + } |
|
505 | + |
|
506 | + /** |
|
507 | + * Finds, minifies & preserves all rule bodies. |
|
508 | + * @param string $css the whole stylesheet. |
|
509 | + * @return string |
|
510 | + */ |
|
511 | + private function processRuleBodies($css) |
|
512 | + { |
|
513 | + $ret = ''; |
|
514 | + $searchOffset = $substrOffset = 0; |
|
515 | + |
|
516 | + while (($blockStartPos = strpos($css, '{', $searchOffset)) !== false) { |
|
517 | + $blockEndPos = strpos($css, '}', $blockStartPos); |
|
518 | + $nextBlockStartPos = strpos($css, '{', $blockStartPos + 1); |
|
519 | + $ret .= substr($css, $substrOffset, $blockStartPos - $substrOffset); |
|
520 | + |
|
521 | + if ($nextBlockStartPos !== false && $nextBlockStartPos < $blockEndPos) { |
|
522 | + $ret .= substr($css, $blockStartPos, $nextBlockStartPos - $blockStartPos); |
|
523 | + $searchOffset = $nextBlockStartPos; |
|
524 | + } else { |
|
525 | + $ruleBody = substr($css, $blockStartPos + 1, $blockEndPos - $blockStartPos - 1); |
|
526 | + $ruleBodyToken = $this->registerRuleBodyToken($this->processRuleBody($ruleBody)); |
|
527 | + $ret .= '{'. $ruleBodyToken .'}'; |
|
528 | + $searchOffset = $blockEndPos + 1; |
|
529 | + } |
|
530 | + |
|
531 | + $substrOffset = $searchOffset; |
|
532 | + } |
|
533 | + |
|
534 | + $ret .= substr($css, $substrOffset); |
|
535 | + |
|
536 | + return $ret; |
|
537 | + } |
|
538 | + |
|
539 | + /** |
|
540 | + * Compresses non-group rule bodies. |
|
541 | + * @param string $body The rule body without curly braces |
|
542 | + * @return string |
|
543 | + */ |
|
544 | + private function processRuleBody($body) |
|
545 | + { |
|
546 | + $body = trim($body); |
|
547 | + |
|
548 | + // Remove spaces before the things that should not have spaces before them. |
|
549 | + $body = preg_replace('/ ([:=,)*\/;\n])/S', '$1', $body); |
|
550 | + |
|
551 | + // Remove the spaces after the things that should not have spaces after them. |
|
552 | + $body = preg_replace('/([:=,(*\/!;\n]) /S', '$1', $body); |
|
553 | 553 | |
554 | - // Replace multiple semi-colons in a row by a single one |
|
555 | - $body = preg_replace('/;;+/S', ';', $body); |
|
556 | - |
|
557 | - // Remove semicolon before closing brace except when: |
|
558 | - // - The last property is prefixed with a `*` (lte IE7 hack) to avoid issues on Symbian S60 3.x browsers. |
|
559 | - if (!preg_match('/\*[a-z0-9-]+:[^;]+;$/Si', $body)) { |
|
560 | - $body = rtrim($body, ';'); |
|
561 | - } |
|
562 | - |
|
563 | - // Remove important comments inside a rule body (because they make no sense here). |
|
564 | - if (strpos($body, '/*') !== false) { |
|
565 | - $body = preg_replace('/\n?\/\*[A-Z0-9_]+\*\/\n?/S', '', $body); |
|
566 | - } |
|
554 | + // Replace multiple semi-colons in a row by a single one |
|
555 | + $body = preg_replace('/;;+/S', ';', $body); |
|
556 | + |
|
557 | + // Remove semicolon before closing brace except when: |
|
558 | + // - The last property is prefixed with a `*` (lte IE7 hack) to avoid issues on Symbian S60 3.x browsers. |
|
559 | + if (!preg_match('/\*[a-z0-9-]+:[^;]+;$/Si', $body)) { |
|
560 | + $body = rtrim($body, ';'); |
|
561 | + } |
|
562 | + |
|
563 | + // Remove important comments inside a rule body (because they make no sense here). |
|
564 | + if (strpos($body, '/*') !== false) { |
|
565 | + $body = preg_replace('/\n?\/\*[A-Z0-9_]+\*\/\n?/S', '', $body); |
|
566 | + } |
|
567 | 567 | |
568 | - // Empty rule body? Exit :) |
|
569 | - if (empty($body)) { |
|
570 | - return ''; |
|
571 | - } |
|
572 | - |
|
573 | - // Shorten font-weight values |
|
574 | - $body = preg_replace( |
|
575 | - array('/(font-weight:)bold\b/Si', '/(font-weight:)normal\b/Si'), |
|
576 | - array('${1}700', '${1}400'), |
|
577 | - $body |
|
578 | - ); |
|
579 | - |
|
580 | - // Shorten background property |
|
581 | - $body = preg_replace('/(background:)(?:none|transparent)( !|;|$)/Si', '${1}0 0$2', $body); |
|
582 | - |
|
583 | - // Shorten opacity IE filter |
|
584 | - $body = str_ireplace('progid:DXImageTransform.Microsoft.Alpha(Opacity=', 'alpha(opacity=', $body); |
|
585 | - |
|
586 | - // Shorten colors from rgb(51,102,153) to #336699, rgb(100%,0%,0%) to #ff0000 (sRGB color space) |
|
587 | - // Shorten colors from hsl(0, 100%, 50%) to #ff0000 (sRGB color space) |
|
588 | - // This makes it more likely that it'll get further compressed in the next step. |
|
589 | - $body = preg_replace_callback( |
|
590 | - '/(rgb|hsl)\(([0-9,.% -]+)\)(.|$)/Si', |
|
591 | - array($this, 'shortenHslAndRgbToHexCallback'), |
|
592 | - $body |
|
593 | - ); |
|
594 | - |
|
595 | - // Shorten colors from #AABBCC to #ABC or shorter color name: |
|
596 | - // - Look for hex colors which don't have a "=" in front of them (to avoid MSIE filters) |
|
597 | - $body = preg_replace_callback( |
|
598 | - '/(?<!=)#([0-9a-f]{3,6})( |,|\)|;|$)/Si', |
|
599 | - array($this, 'shortenHexColorsCallback'), |
|
600 | - $body |
|
601 | - ); |
|
602 | - |
|
603 | - // Shorten long named colors with a shorter HEX counterpart: white -> #fff. |
|
604 | - // Run at least 2 times to cover most cases |
|
605 | - $body = preg_replace_callback( |
|
606 | - array($this->namedToHexColorsRegex, $this->namedToHexColorsRegex), |
|
607 | - array($this, 'shortenNamedColorsCallback'), |
|
608 | - $body |
|
609 | - ); |
|
610 | - |
|
611 | - // Replace positive sign from numbers before the leading space is removed. |
|
612 | - // +1.2em to 1.2em, +.8px to .8px, +2% to 2% |
|
613 | - $body = preg_replace('/([ :,(])\+(\.?\d+)/S', '$1$2', $body); |
|
614 | - |
|
615 | - // shorten ms to s |
|
616 | - $body = preg_replace_callback('/([ :,(])(-?)(\d{3,})ms/Si', function ($matches) { |
|
617 | - return $matches[1] . $matches[2] . ((int) $matches[3] / 1000) .'s'; |
|
618 | - }, $body); |
|
619 | - |
|
620 | - // Remove leading zeros from integer and float numbers. |
|
621 | - // 000.6 to .6, -0.8 to -.8, 0050 to 50, -01.05 to -1.05 |
|
622 | - $body = preg_replace('/([ :,(])(-?)0+([1-9]?\.?\d+)/S', '$1$2$3', $body); |
|
623 | - |
|
624 | - // Remove trailing zeros from float numbers. |
|
625 | - // -6.0100em to -6.01em, .0100 to .01, 1.200px to 1.2px |
|
626 | - $body = preg_replace('/([ :,(])(-?\d?\.\d+?)0+([^\d])/S', '$1$2$3', $body); |
|
627 | - |
|
628 | - // Remove trailing .0 -> -9.0 to -9 |
|
629 | - $body = preg_replace('/([ :,(])(-?\d+)\.0([^\d])/S', '$1$2$3', $body); |
|
630 | - |
|
631 | - // Replace 0 length numbers with 0 |
|
632 | - $body = preg_replace('/([ :,(])-?\.?0+([^\d])/S', '${1}0$2', $body); |
|
633 | - |
|
634 | - // Shorten zero values for safe properties only |
|
635 | - $body = preg_replace( |
|
636 | - array( |
|
637 | - $this->shortenOneZeroesRegex, |
|
638 | - $this->shortenTwoZeroesRegex, |
|
639 | - $this->shortenThreeZeroesRegex, |
|
640 | - $this->shortenFourZeroesRegex |
|
641 | - ), |
|
642 | - array( |
|
643 | - '$1$2:0', |
|
644 | - '$1$2:$3 0', |
|
645 | - '$1$2:$3 $4 0', |
|
646 | - '$1$2:$3 $4 $5 0' |
|
647 | - ), |
|
648 | - $body |
|
649 | - ); |
|
650 | - |
|
651 | - // Replace 0 0 0; or 0 0 0 0; with 0 0 for background-position property. |
|
652 | - $body = preg_replace('/(background-position):0(?: 0){2,3}( !|;|$)/Si', '$1:0 0$2', $body); |
|
653 | - |
|
654 | - // Shorten suitable shorthand properties with repeated values |
|
655 | - $body = preg_replace( |
|
656 | - array( |
|
657 | - '/(margin|padding|border-(?:width|radius)):('.$this->numRegex.')(?: \2)+( !|;|$)/Si', |
|
658 | - '/(border-(?:style|color)):([#a-z0-9]+)(?: \2)+( !|;|$)/Si' |
|
659 | - ), |
|
660 | - '$1:$2$3', |
|
661 | - $body |
|
662 | - ); |
|
663 | - $body = preg_replace( |
|
664 | - array( |
|
665 | - '/(margin|padding|border-(?:width|radius)):'. |
|
666 | - '('.$this->numRegex.') ('.$this->numRegex.') \2 \3( !|;|$)/Si', |
|
667 | - '/(border-(?:style|color)):([#a-z0-9]+) ([#a-z0-9]+) \2 \3( !|;|$)/Si' |
|
668 | - ), |
|
669 | - '$1:$2 $3$4', |
|
670 | - $body |
|
671 | - ); |
|
672 | - $body = preg_replace( |
|
673 | - array( |
|
674 | - '/(margin|padding|border-(?:width|radius)):'. |
|
675 | - '('.$this->numRegex.') ('.$this->numRegex.') ('.$this->numRegex.') \3( !|;|$)/Si', |
|
676 | - '/(border-(?:style|color)):([#a-z0-9]+) ([#a-z0-9]+) ([#a-z0-9]+) \3( !|;|$)/Si' |
|
677 | - ), |
|
678 | - '$1:$2 $3 $4$5', |
|
679 | - $body |
|
680 | - ); |
|
681 | - |
|
682 | - // Lowercase some common functions that can be values |
|
683 | - $body = preg_replace_callback( |
|
684 | - '/(?:attr|blur|brightness|circle|contrast|cubic-bezier|drop-shadow|ellipse|from|grayscale|'. |
|
685 | - 'hsla?|hue-rotate|inset|invert|local|minmax|opacity|perspective|polygon|rgba?|rect|repeat|saturate|sepia|'. |
|
686 | - 'steps|to|url|var|-webkit-gradient|'. |
|
687 | - '(?:-(?:atsc|khtml|moz|ms|o|wap|webkit)-)?(?:calc|(?:repeating-)?(?:linear|radial)-gradient))\(/Si', |
|
688 | - array($this, 'strtolowerCallback'), |
|
689 | - $body |
|
690 | - ); |
|
691 | - |
|
692 | - // Lowercase all uppercase properties |
|
693 | - $body = preg_replace_callback('/(?:^|;)[A-Z-]+:/S', array($this, 'strtolowerCallback'), $body); |
|
694 | - |
|
695 | - return $body; |
|
696 | - } |
|
697 | - |
|
698 | - /** |
|
699 | - * Compresses At-rules and selectors. |
|
700 | - * @param string $css the whole stylesheet with rule bodies tokenized. |
|
701 | - * @return string |
|
702 | - */ |
|
703 | - private function processAtRulesAndSelectors($css) |
|
704 | - { |
|
705 | - $charset = ''; |
|
706 | - $imports = ''; |
|
707 | - $namespaces = ''; |
|
568 | + // Empty rule body? Exit :) |
|
569 | + if (empty($body)) { |
|
570 | + return ''; |
|
571 | + } |
|
572 | + |
|
573 | + // Shorten font-weight values |
|
574 | + $body = preg_replace( |
|
575 | + array('/(font-weight:)bold\b/Si', '/(font-weight:)normal\b/Si'), |
|
576 | + array('${1}700', '${1}400'), |
|
577 | + $body |
|
578 | + ); |
|
579 | + |
|
580 | + // Shorten background property |
|
581 | + $body = preg_replace('/(background:)(?:none|transparent)( !|;|$)/Si', '${1}0 0$2', $body); |
|
582 | + |
|
583 | + // Shorten opacity IE filter |
|
584 | + $body = str_ireplace('progid:DXImageTransform.Microsoft.Alpha(Opacity=', 'alpha(opacity=', $body); |
|
585 | + |
|
586 | + // Shorten colors from rgb(51,102,153) to #336699, rgb(100%,0%,0%) to #ff0000 (sRGB color space) |
|
587 | + // Shorten colors from hsl(0, 100%, 50%) to #ff0000 (sRGB color space) |
|
588 | + // This makes it more likely that it'll get further compressed in the next step. |
|
589 | + $body = preg_replace_callback( |
|
590 | + '/(rgb|hsl)\(([0-9,.% -]+)\)(.|$)/Si', |
|
591 | + array($this, 'shortenHslAndRgbToHexCallback'), |
|
592 | + $body |
|
593 | + ); |
|
594 | + |
|
595 | + // Shorten colors from #AABBCC to #ABC or shorter color name: |
|
596 | + // - Look for hex colors which don't have a "=" in front of them (to avoid MSIE filters) |
|
597 | + $body = preg_replace_callback( |
|
598 | + '/(?<!=)#([0-9a-f]{3,6})( |,|\)|;|$)/Si', |
|
599 | + array($this, 'shortenHexColorsCallback'), |
|
600 | + $body |
|
601 | + ); |
|
602 | + |
|
603 | + // Shorten long named colors with a shorter HEX counterpart: white -> #fff. |
|
604 | + // Run at least 2 times to cover most cases |
|
605 | + $body = preg_replace_callback( |
|
606 | + array($this->namedToHexColorsRegex, $this->namedToHexColorsRegex), |
|
607 | + array($this, 'shortenNamedColorsCallback'), |
|
608 | + $body |
|
609 | + ); |
|
610 | + |
|
611 | + // Replace positive sign from numbers before the leading space is removed. |
|
612 | + // +1.2em to 1.2em, +.8px to .8px, +2% to 2% |
|
613 | + $body = preg_replace('/([ :,(])\+(\.?\d+)/S', '$1$2', $body); |
|
614 | + |
|
615 | + // shorten ms to s |
|
616 | + $body = preg_replace_callback('/([ :,(])(-?)(\d{3,})ms/Si', function ($matches) { |
|
617 | + return $matches[1] . $matches[2] . ((int) $matches[3] / 1000) .'s'; |
|
618 | + }, $body); |
|
619 | + |
|
620 | + // Remove leading zeros from integer and float numbers. |
|
621 | + // 000.6 to .6, -0.8 to -.8, 0050 to 50, -01.05 to -1.05 |
|
622 | + $body = preg_replace('/([ :,(])(-?)0+([1-9]?\.?\d+)/S', '$1$2$3', $body); |
|
623 | + |
|
624 | + // Remove trailing zeros from float numbers. |
|
625 | + // -6.0100em to -6.01em, .0100 to .01, 1.200px to 1.2px |
|
626 | + $body = preg_replace('/([ :,(])(-?\d?\.\d+?)0+([^\d])/S', '$1$2$3', $body); |
|
627 | + |
|
628 | + // Remove trailing .0 -> -9.0 to -9 |
|
629 | + $body = preg_replace('/([ :,(])(-?\d+)\.0([^\d])/S', '$1$2$3', $body); |
|
630 | + |
|
631 | + // Replace 0 length numbers with 0 |
|
632 | + $body = preg_replace('/([ :,(])-?\.?0+([^\d])/S', '${1}0$2', $body); |
|
633 | + |
|
634 | + // Shorten zero values for safe properties only |
|
635 | + $body = preg_replace( |
|
636 | + array( |
|
637 | + $this->shortenOneZeroesRegex, |
|
638 | + $this->shortenTwoZeroesRegex, |
|
639 | + $this->shortenThreeZeroesRegex, |
|
640 | + $this->shortenFourZeroesRegex |
|
641 | + ), |
|
642 | + array( |
|
643 | + '$1$2:0', |
|
644 | + '$1$2:$3 0', |
|
645 | + '$1$2:$3 $4 0', |
|
646 | + '$1$2:$3 $4 $5 0' |
|
647 | + ), |
|
648 | + $body |
|
649 | + ); |
|
650 | + |
|
651 | + // Replace 0 0 0; or 0 0 0 0; with 0 0 for background-position property. |
|
652 | + $body = preg_replace('/(background-position):0(?: 0){2,3}( !|;|$)/Si', '$1:0 0$2', $body); |
|
653 | + |
|
654 | + // Shorten suitable shorthand properties with repeated values |
|
655 | + $body = preg_replace( |
|
656 | + array( |
|
657 | + '/(margin|padding|border-(?:width|radius)):('.$this->numRegex.')(?: \2)+( !|;|$)/Si', |
|
658 | + '/(border-(?:style|color)):([#a-z0-9]+)(?: \2)+( !|;|$)/Si' |
|
659 | + ), |
|
660 | + '$1:$2$3', |
|
661 | + $body |
|
662 | + ); |
|
663 | + $body = preg_replace( |
|
664 | + array( |
|
665 | + '/(margin|padding|border-(?:width|radius)):'. |
|
666 | + '('.$this->numRegex.') ('.$this->numRegex.') \2 \3( !|;|$)/Si', |
|
667 | + '/(border-(?:style|color)):([#a-z0-9]+) ([#a-z0-9]+) \2 \3( !|;|$)/Si' |
|
668 | + ), |
|
669 | + '$1:$2 $3$4', |
|
670 | + $body |
|
671 | + ); |
|
672 | + $body = preg_replace( |
|
673 | + array( |
|
674 | + '/(margin|padding|border-(?:width|radius)):'. |
|
675 | + '('.$this->numRegex.') ('.$this->numRegex.') ('.$this->numRegex.') \3( !|;|$)/Si', |
|
676 | + '/(border-(?:style|color)):([#a-z0-9]+) ([#a-z0-9]+) ([#a-z0-9]+) \3( !|;|$)/Si' |
|
677 | + ), |
|
678 | + '$1:$2 $3 $4$5', |
|
679 | + $body |
|
680 | + ); |
|
681 | + |
|
682 | + // Lowercase some common functions that can be values |
|
683 | + $body = preg_replace_callback( |
|
684 | + '/(?:attr|blur|brightness|circle|contrast|cubic-bezier|drop-shadow|ellipse|from|grayscale|'. |
|
685 | + 'hsla?|hue-rotate|inset|invert|local|minmax|opacity|perspective|polygon|rgba?|rect|repeat|saturate|sepia|'. |
|
686 | + 'steps|to|url|var|-webkit-gradient|'. |
|
687 | + '(?:-(?:atsc|khtml|moz|ms|o|wap|webkit)-)?(?:calc|(?:repeating-)?(?:linear|radial)-gradient))\(/Si', |
|
688 | + array($this, 'strtolowerCallback'), |
|
689 | + $body |
|
690 | + ); |
|
691 | + |
|
692 | + // Lowercase all uppercase properties |
|
693 | + $body = preg_replace_callback('/(?:^|;)[A-Z-]+:/S', array($this, 'strtolowerCallback'), $body); |
|
694 | + |
|
695 | + return $body; |
|
696 | + } |
|
697 | + |
|
698 | + /** |
|
699 | + * Compresses At-rules and selectors. |
|
700 | + * @param string $css the whole stylesheet with rule bodies tokenized. |
|
701 | + * @return string |
|
702 | + */ |
|
703 | + private function processAtRulesAndSelectors($css) |
|
704 | + { |
|
705 | + $charset = ''; |
|
706 | + $imports = ''; |
|
707 | + $namespaces = ''; |
|
708 | 708 | |
709 | - // Remove spaces before the things that should not have spaces before them. |
|
710 | - $css = preg_replace('/ ([@{};>+)\]~=,\/\n])/S', '$1', $css); |
|
709 | + // Remove spaces before the things that should not have spaces before them. |
|
710 | + $css = preg_replace('/ ([@{};>+)\]~=,\/\n])/S', '$1', $css); |
|
711 | 711 | |
712 | - // Remove the spaces after the things that should not have spaces after them. |
|
713 | - $css = preg_replace('/([{}:;>+(\[~=,\/\n]) /S', '$1', $css); |
|
712 | + // Remove the spaces after the things that should not have spaces after them. |
|
713 | + $css = preg_replace('/([{}:;>+(\[~=,\/\n]) /S', '$1', $css); |
|
714 | 714 | |
715 | - // Shorten shortable double colon (CSS3) pseudo-elements to single colon (CSS2) |
|
716 | - $css = preg_replace('/::(before|after|first-(?:line|letter))(\{|,)/Si', ':$1$2', $css); |
|
717 | - |
|
718 | - // Retain space for special IE6 cases |
|
719 | - $css = preg_replace_callback('/:first-(line|letter)(\{|,)/Si', function ($matches) { |
|
720 | - return ':first-'. strtolower($matches[1]) .' '. $matches[2]; |
|
721 | - }, $css); |
|
722 | - |
|
723 | - // Find a fraction that may used in some @media queries such as: (min-aspect-ratio: 1/1) |
|
724 | - // Add token to add the "/" back in later |
|
725 | - $css = preg_replace('/\(([a-z-]+):([0-9]+)\/([0-9]+)\)/Si', '($1:$2'. self::QUERY_FRACTION .'$3)', $css); |
|
726 | - |
|
727 | - // Remove empty rule blocks up to 2 levels deep. |
|
728 | - $css = preg_replace(array_fill(0, 2, '/(\{)[^{};\/\n]+\{\}/S'), '$1', $css); |
|
729 | - $css = preg_replace('/[^{};\/\n]+\{\}/S', '', $css); |
|
730 | - |
|
731 | - // Two important comments next to each other? Remove extra newline. |
|
732 | - if ($this->keepImportantComments) { |
|
733 | - $css = str_replace("\n\n", "\n", $css); |
|
734 | - } |
|
715 | + // Shorten shortable double colon (CSS3) pseudo-elements to single colon (CSS2) |
|
716 | + $css = preg_replace('/::(before|after|first-(?:line|letter))(\{|,)/Si', ':$1$2', $css); |
|
717 | + |
|
718 | + // Retain space for special IE6 cases |
|
719 | + $css = preg_replace_callback('/:first-(line|letter)(\{|,)/Si', function ($matches) { |
|
720 | + return ':first-'. strtolower($matches[1]) .' '. $matches[2]; |
|
721 | + }, $css); |
|
722 | + |
|
723 | + // Find a fraction that may used in some @media queries such as: (min-aspect-ratio: 1/1) |
|
724 | + // Add token to add the "/" back in later |
|
725 | + $css = preg_replace('/\(([a-z-]+):([0-9]+)\/([0-9]+)\)/Si', '($1:$2'. self::QUERY_FRACTION .'$3)', $css); |
|
726 | + |
|
727 | + // Remove empty rule blocks up to 2 levels deep. |
|
728 | + $css = preg_replace(array_fill(0, 2, '/(\{)[^{};\/\n]+\{\}/S'), '$1', $css); |
|
729 | + $css = preg_replace('/[^{};\/\n]+\{\}/S', '', $css); |
|
730 | + |
|
731 | + // Two important comments next to each other? Remove extra newline. |
|
732 | + if ($this->keepImportantComments) { |
|
733 | + $css = str_replace("\n\n", "\n", $css); |
|
734 | + } |
|
735 | 735 | |
736 | - // Restore fraction |
|
737 | - $css = str_replace(self::QUERY_FRACTION, '/', $css); |
|
738 | - |
|
739 | - // Lowercase some popular @directives |
|
740 | - $css = preg_replace_callback( |
|
741 | - '/(?<!\\\\)@(?:charset|document|font-face|import|(?:-(?:atsc|khtml|moz|ms|o|wap|webkit)-)?keyframes|media|'. |
|
742 | - 'namespace|page|supports|viewport)/Si', |
|
743 | - array($this, 'strtolowerCallback'), |
|
744 | - $css |
|
745 | - ); |
|
746 | - |
|
747 | - // Lowercase some popular media types |
|
748 | - $css = preg_replace_callback( |
|
749 | - '/[ ,](?:all|aural|braille|handheld|print|projection|screen|tty|tv|embossed|speech)[ ,;{]/Si', |
|
750 | - array($this, 'strtolowerCallback'), |
|
751 | - $css |
|
752 | - ); |
|
753 | - |
|
754 | - // Lowercase some common pseudo-classes & pseudo-elements |
|
755 | - $css = preg_replace_callback( |
|
756 | - '/(?<!\\\\):(?:active|after|before|checked|default|disabled|empty|enabled|first-(?:child|of-type)|'. |
|
757 | - 'focus(?:-within)?|hover|indeterminate|in-range|invalid|lang\(|last-(?:child|of-type)|left|link|not\(|'. |
|
758 | - 'nth-(?:child|of-type)\(|nth-last-(?:child|of-type)\(|only-(?:child|of-type)|optional|out-of-range|'. |
|
759 | - 'read-(?:only|write)|required|right|root|:selection|target|valid|visited)/Si', |
|
760 | - array($this, 'strtolowerCallback'), |
|
761 | - $css |
|
762 | - ); |
|
736 | + // Restore fraction |
|
737 | + $css = str_replace(self::QUERY_FRACTION, '/', $css); |
|
738 | + |
|
739 | + // Lowercase some popular @directives |
|
740 | + $css = preg_replace_callback( |
|
741 | + '/(?<!\\\\)@(?:charset|document|font-face|import|(?:-(?:atsc|khtml|moz|ms|o|wap|webkit)-)?keyframes|media|'. |
|
742 | + 'namespace|page|supports|viewport)/Si', |
|
743 | + array($this, 'strtolowerCallback'), |
|
744 | + $css |
|
745 | + ); |
|
746 | + |
|
747 | + // Lowercase some popular media types |
|
748 | + $css = preg_replace_callback( |
|
749 | + '/[ ,](?:all|aural|braille|handheld|print|projection|screen|tty|tv|embossed|speech)[ ,;{]/Si', |
|
750 | + array($this, 'strtolowerCallback'), |
|
751 | + $css |
|
752 | + ); |
|
753 | + |
|
754 | + // Lowercase some common pseudo-classes & pseudo-elements |
|
755 | + $css = preg_replace_callback( |
|
756 | + '/(?<!\\\\):(?:active|after|before|checked|default|disabled|empty|enabled|first-(?:child|of-type)|'. |
|
757 | + 'focus(?:-within)?|hover|indeterminate|in-range|invalid|lang\(|last-(?:child|of-type)|left|link|not\(|'. |
|
758 | + 'nth-(?:child|of-type)\(|nth-last-(?:child|of-type)\(|only-(?:child|of-type)|optional|out-of-range|'. |
|
759 | + 'read-(?:only|write)|required|right|root|:selection|target|valid|visited)/Si', |
|
760 | + array($this, 'strtolowerCallback'), |
|
761 | + $css |
|
762 | + ); |
|
763 | 763 | |
764 | - // @charset handling |
|
765 | - if (preg_match($this->charsetRegex, $css, $matches)) { |
|
766 | - // Keep the first @charset at-rule found |
|
767 | - $charset = $matches[0]; |
|
768 | - // Delete all @charset at-rules |
|
769 | - $css = preg_replace($this->charsetRegex, '', $css); |
|
770 | - } |
|
771 | - |
|
772 | - // @import handling |
|
773 | - $css = preg_replace_callback($this->importRegex, function ($matches) use (&$imports) { |
|
774 | - // Keep all @import at-rules found for later |
|
775 | - $imports .= $matches[0]; |
|
776 | - // Delete all @import at-rules |
|
777 | - return ''; |
|
778 | - }, $css); |
|
779 | - |
|
780 | - // @namespace handling |
|
781 | - $css = preg_replace_callback($this->namespaceRegex, function ($matches) use (&$namespaces) { |
|
782 | - // Keep all @namespace at-rules found for later |
|
783 | - $namespaces .= $matches[0]; |
|
784 | - // Delete all @namespace at-rules |
|
785 | - return ''; |
|
786 | - }, $css); |
|
764 | + // @charset handling |
|
765 | + if (preg_match($this->charsetRegex, $css, $matches)) { |
|
766 | + // Keep the first @charset at-rule found |
|
767 | + $charset = $matches[0]; |
|
768 | + // Delete all @charset at-rules |
|
769 | + $css = preg_replace($this->charsetRegex, '', $css); |
|
770 | + } |
|
771 | + |
|
772 | + // @import handling |
|
773 | + $css = preg_replace_callback($this->importRegex, function ($matches) use (&$imports) { |
|
774 | + // Keep all @import at-rules found for later |
|
775 | + $imports .= $matches[0]; |
|
776 | + // Delete all @import at-rules |
|
777 | + return ''; |
|
778 | + }, $css); |
|
779 | + |
|
780 | + // @namespace handling |
|
781 | + $css = preg_replace_callback($this->namespaceRegex, function ($matches) use (&$namespaces) { |
|
782 | + // Keep all @namespace at-rules found for later |
|
783 | + $namespaces .= $matches[0]; |
|
784 | + // Delete all @namespace at-rules |
|
785 | + return ''; |
|
786 | + }, $css); |
|
787 | 787 | |
788 | - // Order critical at-rules: |
|
789 | - // 1. @charset first |
|
790 | - // 2. @imports below @charset |
|
791 | - // 3. @namespaces below @imports |
|
792 | - $css = $charset . $imports . $namespaces . $css; |
|
793 | - |
|
794 | - return $css; |
|
795 | - } |
|
796 | - |
|
797 | - /** |
|
798 | - * Splits long lines after a specific column. |
|
799 | - * |
|
800 | - * Some source control tools don't like it when files containing lines longer |
|
801 | - * than, say 8000 characters, are checked in. The linebreak option is used in |
|
802 | - * that case to split long lines after a specific column. |
|
803 | - * |
|
804 | - * @param string $css the whole stylesheet. |
|
805 | - * @return string |
|
806 | - */ |
|
807 | - private function processLongLineSplitting($css) |
|
808 | - { |
|
809 | - if ($this->linebreakPosition > 0) { |
|
810 | - $l = strlen($css); |
|
811 | - $offset = $this->linebreakPosition; |
|
812 | - while (preg_match('/(?<!\\\\)\}(?!\n)/S', $css, $matches, PREG_OFFSET_CAPTURE, $offset)) { |
|
813 | - $matchIndex = $matches[0][1]; |
|
814 | - $css = substr_replace($css, "\n", $matchIndex + 1, 0); |
|
815 | - $offset = $matchIndex + 2 + $this->linebreakPosition; |
|
816 | - $l += 1; |
|
817 | - if ($offset > $l) { |
|
818 | - break; |
|
819 | - } |
|
820 | - } |
|
821 | - } |
|
822 | - |
|
823 | - return $css; |
|
824 | - } |
|
825 | - |
|
826 | - /** |
|
827 | - * Converts hsl() & rgb() colors to HEX format. |
|
828 | - * @param $matches |
|
829 | - * @return string |
|
830 | - */ |
|
831 | - private function shortenHslAndRgbToHexCallback($matches) |
|
832 | - { |
|
833 | - $type = $matches[1]; |
|
834 | - $values = explode(',', $matches[2]); |
|
835 | - $terminator = $matches[3]; |
|
788 | + // Order critical at-rules: |
|
789 | + // 1. @charset first |
|
790 | + // 2. @imports below @charset |
|
791 | + // 3. @namespaces below @imports |
|
792 | + $css = $charset . $imports . $namespaces . $css; |
|
793 | + |
|
794 | + return $css; |
|
795 | + } |
|
796 | + |
|
797 | + /** |
|
798 | + * Splits long lines after a specific column. |
|
799 | + * |
|
800 | + * Some source control tools don't like it when files containing lines longer |
|
801 | + * than, say 8000 characters, are checked in. The linebreak option is used in |
|
802 | + * that case to split long lines after a specific column. |
|
803 | + * |
|
804 | + * @param string $css the whole stylesheet. |
|
805 | + * @return string |
|
806 | + */ |
|
807 | + private function processLongLineSplitting($css) |
|
808 | + { |
|
809 | + if ($this->linebreakPosition > 0) { |
|
810 | + $l = strlen($css); |
|
811 | + $offset = $this->linebreakPosition; |
|
812 | + while (preg_match('/(?<!\\\\)\}(?!\n)/S', $css, $matches, PREG_OFFSET_CAPTURE, $offset)) { |
|
813 | + $matchIndex = $matches[0][1]; |
|
814 | + $css = substr_replace($css, "\n", $matchIndex + 1, 0); |
|
815 | + $offset = $matchIndex + 2 + $this->linebreakPosition; |
|
816 | + $l += 1; |
|
817 | + if ($offset > $l) { |
|
818 | + break; |
|
819 | + } |
|
820 | + } |
|
821 | + } |
|
822 | + |
|
823 | + return $css; |
|
824 | + } |
|
825 | + |
|
826 | + /** |
|
827 | + * Converts hsl() & rgb() colors to HEX format. |
|
828 | + * @param $matches |
|
829 | + * @return string |
|
830 | + */ |
|
831 | + private function shortenHslAndRgbToHexCallback($matches) |
|
832 | + { |
|
833 | + $type = $matches[1]; |
|
834 | + $values = explode(',', $matches[2]); |
|
835 | + $terminator = $matches[3]; |
|
836 | 836 | |
837 | - if ($type === 'hsl') { |
|
838 | - $values = Utils::hslToRgb($values); |
|
839 | - } |
|
837 | + if ($type === 'hsl') { |
|
838 | + $values = Utils::hslToRgb($values); |
|
839 | + } |
|
840 | 840 | |
841 | - $hexColors = Utils::rgbToHex($values); |
|
842 | - |
|
843 | - // Restore space after rgb() or hsl() function in some cases such as: |
|
844 | - // background-image: linear-gradient(to bottom, rgb(210,180,140) 10%, rgb(255,0,0) 90%); |
|
845 | - if (!empty($terminator) && !preg_match('/[ ,);]/S', $terminator)) { |
|
846 | - $terminator = ' '. $terminator; |
|
847 | - } |
|
848 | - |
|
849 | - return '#'. implode('', $hexColors) . $terminator; |
|
850 | - } |
|
851 | - |
|
852 | - /** |
|
853 | - * Compresses HEX color values of the form #AABBCC to #ABC or short color name. |
|
854 | - * @param $matches |
|
855 | - * @return string |
|
856 | - */ |
|
857 | - private function shortenHexColorsCallback($matches) |
|
858 | - { |
|
859 | - $hex = $matches[1]; |
|
841 | + $hexColors = Utils::rgbToHex($values); |
|
842 | + |
|
843 | + // Restore space after rgb() or hsl() function in some cases such as: |
|
844 | + // background-image: linear-gradient(to bottom, rgb(210,180,140) 10%, rgb(255,0,0) 90%); |
|
845 | + if (!empty($terminator) && !preg_match('/[ ,);]/S', $terminator)) { |
|
846 | + $terminator = ' '. $terminator; |
|
847 | + } |
|
848 | + |
|
849 | + return '#'. implode('', $hexColors) . $terminator; |
|
850 | + } |
|
851 | + |
|
852 | + /** |
|
853 | + * Compresses HEX color values of the form #AABBCC to #ABC or short color name. |
|
854 | + * @param $matches |
|
855 | + * @return string |
|
856 | + */ |
|
857 | + private function shortenHexColorsCallback($matches) |
|
858 | + { |
|
859 | + $hex = $matches[1]; |
|
860 | 860 | |
861 | - // Shorten suitable 6 chars HEX colors |
|
862 | - if (strlen($hex) === 6 && preg_match('/^([0-9a-f])\1([0-9a-f])\2([0-9a-f])\3$/Si', $hex, $m)) { |
|
863 | - $hex = $m[1] . $m[2] . $m[3]; |
|
864 | - } |
|
861 | + // Shorten suitable 6 chars HEX colors |
|
862 | + if (strlen($hex) === 6 && preg_match('/^([0-9a-f])\1([0-9a-f])\2([0-9a-f])\3$/Si', $hex, $m)) { |
|
863 | + $hex = $m[1] . $m[2] . $m[3]; |
|
864 | + } |
|
865 | 865 | |
866 | - // Lowercase |
|
867 | - $hex = '#'. strtolower($hex); |
|
868 | - |
|
869 | - // Replace Hex colors with shorter color names |
|
870 | - $color = array_key_exists($hex, $this->hexToNamedColorsMap) ? $this->hexToNamedColorsMap[$hex] : $hex; |
|
871 | - |
|
872 | - return $color . $matches[2]; |
|
873 | - } |
|
874 | - |
|
875 | - /** |
|
876 | - * Shortens all named colors with a shorter HEX counterpart for a set of safe properties |
|
877 | - * e.g. white -> #fff |
|
878 | - * @param array $matches |
|
879 | - * @return string |
|
880 | - */ |
|
881 | - private function shortenNamedColorsCallback($matches) |
|
882 | - { |
|
883 | - return $matches[1] . $this->namedToHexColorsMap[strtolower($matches[2])] . $matches[3]; |
|
884 | - } |
|
885 | - |
|
886 | - /** |
|
887 | - * Makes a string lowercase |
|
888 | - * @param array $matches |
|
889 | - * @return string |
|
890 | - */ |
|
891 | - private function strtolowerCallback($matches) |
|
892 | - { |
|
893 | - return strtolower($matches[0]); |
|
894 | - } |
|
866 | + // Lowercase |
|
867 | + $hex = '#'. strtolower($hex); |
|
868 | + |
|
869 | + // Replace Hex colors with shorter color names |
|
870 | + $color = array_key_exists($hex, $this->hexToNamedColorsMap) ? $this->hexToNamedColorsMap[$hex] : $hex; |
|
871 | + |
|
872 | + return $color . $matches[2]; |
|
873 | + } |
|
874 | + |
|
875 | + /** |
|
876 | + * Shortens all named colors with a shorter HEX counterpart for a set of safe properties |
|
877 | + * e.g. white -> #fff |
|
878 | + * @param array $matches |
|
879 | + * @return string |
|
880 | + */ |
|
881 | + private function shortenNamedColorsCallback($matches) |
|
882 | + { |
|
883 | + return $matches[1] . $this->namedToHexColorsMap[strtolower($matches[2])] . $matches[3]; |
|
884 | + } |
|
885 | + |
|
886 | + /** |
|
887 | + * Makes a string lowercase |
|
888 | + * @param array $matches |
|
889 | + * @return string |
|
890 | + */ |
|
891 | + private function strtolowerCallback($matches) |
|
892 | + { |
|
893 | + return strtolower($matches[0]); |
|
894 | + } |
|
895 | 895 | } |
@@ -89,13 +89,15 @@ discard block |
||
89 | 89 | */ |
90 | 90 | public function run($css = '') |
91 | 91 | { |
92 | - if (empty($css) || !is_string($css)) { |
|
92 | + if (empty($css) || !is_string($css)) |
|
93 | + { |
|
93 | 94 | return ''; |
94 | 95 | } |
95 | 96 | |
96 | 97 | $this->resetRunProperties(); |
97 | 98 | |
98 | - if ($this->raisePhpLimits) { |
|
99 | + if ($this->raisePhpLimits) |
|
100 | + { |
|
99 | 101 | $this->doRaisePhpLimits(); |
100 | 102 | } |
101 | 103 | |
@@ -226,20 +228,24 @@ discard block |
||
226 | 228 | ); |
227 | 229 | |
228 | 230 | // If current settings are higher respect them. |
229 | - foreach ($phpLimits as $name => $suggested) { |
|
231 | + foreach ($phpLimits as $name => $suggested) |
|
232 | + { |
|
230 | 233 | $current = Utils::normalizeInt(ini_get($name)); |
231 | 234 | |
232 | - if ($current >= $suggested) { |
|
235 | + if ($current >= $suggested) |
|
236 | + { |
|
233 | 237 | continue; |
234 | 238 | } |
235 | 239 | |
236 | 240 | // memoryLimit exception: allow -1 for "no memory limit". |
237 | - if ($name === 'memory_limit' && $current === -1) { |
|
241 | + if ($name === 'memory_limit' && $current === -1) |
|
242 | + { |
|
238 | 243 | continue; |
239 | 244 | } |
240 | 245 | |
241 | 246 | // maxExecutionTime exception: allow 0 for "no memory limit". |
242 | - if ($name === 'max_execution_time' && $current === 0) { |
|
247 | + if ($name === 'max_execution_time' && $current === 0) |
|
248 | + { |
|
243 | 249 | continue; |
244 | 250 | } |
245 | 251 | |
@@ -278,7 +284,8 @@ discard block |
||
278 | 284 | */ |
279 | 285 | private function registerRuleBodyToken($body) |
280 | 286 | { |
281 | - if (empty($body)) { |
|
287 | + if (empty($body)) |
|
288 | + { |
|
282 | 289 | return ''; |
283 | 290 | } |
284 | 291 | |
@@ -370,7 +377,8 @@ discard block |
||
370 | 377 | |
371 | 378 | // Since we need to account for non-base64 data urls, we need to handle |
372 | 379 | // ' and ) being part of the data string. |
373 | - while (preg_match('/url\(\s*(["\']?)data:/Si', $css, $m, PREG_OFFSET_CAPTURE, $searchOffset)) { |
|
380 | + while (preg_match('/url\(\s*(["\']?)data:/Si', $css, $m, PREG_OFFSET_CAPTURE, $searchOffset)) |
|
381 | + { |
|
374 | 382 | $matchStartIndex = $m[0][1]; |
375 | 383 | $dataStartIndex = $matchStartIndex + 4; // url( length |
376 | 384 | $searchOffset = $matchStartIndex + strlen($m[0][0]); |
@@ -380,19 +388,23 @@ discard block |
||
380 | 388 | $ret .= substr($css, $substrOffset, $matchStartIndex - $substrOffset); |
381 | 389 | |
382 | 390 | // Terminator found |
383 | - if (preg_match($terminatorRegex, $css, $matches, PREG_OFFSET_CAPTURE, $searchOffset)) { |
|
391 | + if (preg_match($terminatorRegex, $css, $matches, PREG_OFFSET_CAPTURE, $searchOffset)) |
|
392 | + { |
|
384 | 393 | $matchEndIndex = $matches[1][1]; |
385 | 394 | $searchOffset = $matchEndIndex + 1; |
386 | 395 | $token = substr($css, $dataStartIndex, $matchEndIndex - $dataStartIndex); |
387 | 396 | |
388 | 397 | // Remove all spaces only for base64 encoded URLs. |
389 | - if (stripos($token, 'base64,') !== false) { |
|
398 | + if (stripos($token, 'base64,') !== false) |
|
399 | + { |
|
390 | 400 | $token = preg_replace('/\s+/S', '', $token); |
391 | 401 | } |
392 | 402 | |
393 | 403 | $ret .= 'url('. $this->registerPreservedToken(trim($token)) .')'; |
394 | 404 | // No end terminator found, re-add the whole match. Should we throw/warn here? |
395 | - } else { |
|
405 | + } |
|
406 | + else |
|
407 | + { |
|
396 | 408 | $ret .= substr($css, $matchStartIndex, $searchOffset - $matchStartIndex); |
397 | 409 | } |
398 | 410 | |
@@ -437,7 +449,8 @@ discard block |
||
437 | 449 | |
438 | 450 | // maybe the string contains a comment-like substring? |
439 | 451 | // one, maybe more? put'em back then |
440 | - if (strpos($match, self::COMMENT_TOKEN_START) !== false) { |
|
452 | + if (strpos($match, self::COMMENT_TOKEN_START) !== false) |
|
453 | + { |
|
441 | 454 | $match = strtr($match, $this->comments); |
442 | 455 | } |
443 | 456 | |
@@ -465,12 +478,14 @@ discard block |
||
465 | 478 | */ |
466 | 479 | private function processComments($css) |
467 | 480 | { |
468 | - foreach ($this->comments as $commentId => $comment) { |
|
481 | + foreach ($this->comments as $commentId => $comment) |
|
482 | + { |
|
469 | 483 | $commentIdString = '/*'. $commentId .'*/'; |
470 | 484 | |
471 | 485 | // ! in the first position of the comment means preserve |
472 | 486 | // so push to the preserved tokens keeping the ! |
473 | - if ($this->keepImportantComments && strpos($comment, '!') === 0) { |
|
487 | + if ($this->keepImportantComments && strpos($comment, '!') === 0) |
|
488 | + { |
|
474 | 489 | $preservedTokenId = $this->registerPreservedToken($comment); |
475 | 490 | // Put new lines before and after /*! important comments |
476 | 491 | $css = str_replace($commentIdString, "\n/*$preservedTokenId*/\n", $css); |
@@ -479,7 +494,8 @@ discard block |
||
479 | 494 | |
480 | 495 | // # sourceMappingURL= in the first position of the comment means sourcemap |
481 | 496 | // so push to the preserved tokens if {$this->keepSourceMapComment} is truthy. |
482 | - if ($this->keepSourceMapComment && strpos($comment, '# sourceMappingURL=') === 0) { |
|
497 | + if ($this->keepSourceMapComment && strpos($comment, '# sourceMappingURL=') === 0) |
|
498 | + { |
|
483 | 499 | $preservedTokenId = $this->registerPreservedToken($comment); |
484 | 500 | // Add new line before the sourcemap comment |
485 | 501 | $css = str_replace($commentIdString, "\n/*$preservedTokenId*/", $css); |
@@ -488,7 +504,8 @@ discard block |
||
488 | 504 | |
489 | 505 | // Keep empty comments after child selectors (IE7 hack) |
490 | 506 | // e.g. html >/**/ body |
491 | - if (strlen($comment) === 0 && strpos($css, '>/*'.$commentId) !== false) { |
|
507 | + if (strlen($comment) === 0 && strpos($css, '>/*'.$commentId) !== false) |
|
508 | + { |
|
492 | 509 | $css = str_replace($commentId, $this->registerPreservedToken(''), $css); |
493 | 510 | continue; |
494 | 511 | } |
@@ -513,15 +530,19 @@ discard block |
||
513 | 530 | $ret = ''; |
514 | 531 | $searchOffset = $substrOffset = 0; |
515 | 532 | |
516 | - while (($blockStartPos = strpos($css, '{', $searchOffset)) !== false) { |
|
533 | + while (($blockStartPos = strpos($css, '{', $searchOffset)) !== false) |
|
534 | + { |
|
517 | 535 | $blockEndPos = strpos($css, '}', $blockStartPos); |
518 | 536 | $nextBlockStartPos = strpos($css, '{', $blockStartPos + 1); |
519 | 537 | $ret .= substr($css, $substrOffset, $blockStartPos - $substrOffset); |
520 | 538 | |
521 | - if ($nextBlockStartPos !== false && $nextBlockStartPos < $blockEndPos) { |
|
539 | + if ($nextBlockStartPos !== false && $nextBlockStartPos < $blockEndPos) |
|
540 | + { |
|
522 | 541 | $ret .= substr($css, $blockStartPos, $nextBlockStartPos - $blockStartPos); |
523 | 542 | $searchOffset = $nextBlockStartPos; |
524 | - } else { |
|
543 | + } |
|
544 | + else |
|
545 | + { |
|
525 | 546 | $ruleBody = substr($css, $blockStartPos + 1, $blockEndPos - $blockStartPos - 1); |
526 | 547 | $ruleBodyToken = $this->registerRuleBodyToken($this->processRuleBody($ruleBody)); |
527 | 548 | $ret .= '{'. $ruleBodyToken .'}'; |
@@ -556,17 +577,20 @@ discard block |
||
556 | 577 | |
557 | 578 | // Remove semicolon before closing brace except when: |
558 | 579 | // - The last property is prefixed with a `*` (lte IE7 hack) to avoid issues on Symbian S60 3.x browsers. |
559 | - if (!preg_match('/\*[a-z0-9-]+:[^;]+;$/Si', $body)) { |
|
580 | + if (!preg_match('/\*[a-z0-9-]+:[^;]+;$/Si', $body)) |
|
581 | + { |
|
560 | 582 | $body = rtrim($body, ';'); |
561 | 583 | } |
562 | 584 | |
563 | 585 | // Remove important comments inside a rule body (because they make no sense here). |
564 | - if (strpos($body, '/*') !== false) { |
|
586 | + if (strpos($body, '/*') !== false) |
|
587 | + { |
|
565 | 588 | $body = preg_replace('/\n?\/\*[A-Z0-9_]+\*\/\n?/S', '', $body); |
566 | 589 | } |
567 | 590 | |
568 | 591 | // Empty rule body? Exit :) |
569 | - if (empty($body)) { |
|
592 | + if (empty($body)) |
|
593 | + { |
|
570 | 594 | return ''; |
571 | 595 | } |
572 | 596 | |
@@ -729,7 +753,8 @@ discard block |
||
729 | 753 | $css = preg_replace('/[^{};\/\n]+\{\}/S', '', $css); |
730 | 754 | |
731 | 755 | // Two important comments next to each other? Remove extra newline. |
732 | - if ($this->keepImportantComments) { |
|
756 | + if ($this->keepImportantComments) |
|
757 | + { |
|
733 | 758 | $css = str_replace("\n\n", "\n", $css); |
734 | 759 | } |
735 | 760 | |
@@ -762,7 +787,8 @@ discard block |
||
762 | 787 | ); |
763 | 788 | |
764 | 789 | // @charset handling |
765 | - if (preg_match($this->charsetRegex, $css, $matches)) { |
|
790 | + if (preg_match($this->charsetRegex, $css, $matches)) |
|
791 | + { |
|
766 | 792 | // Keep the first @charset at-rule found |
767 | 793 | $charset = $matches[0]; |
768 | 794 | // Delete all @charset at-rules |
@@ -806,15 +832,18 @@ discard block |
||
806 | 832 | */ |
807 | 833 | private function processLongLineSplitting($css) |
808 | 834 | { |
809 | - if ($this->linebreakPosition > 0) { |
|
835 | + if ($this->linebreakPosition > 0) |
|
836 | + { |
|
810 | 837 | $l = strlen($css); |
811 | 838 | $offset = $this->linebreakPosition; |
812 | - while (preg_match('/(?<!\\\\)\}(?!\n)/S', $css, $matches, PREG_OFFSET_CAPTURE, $offset)) { |
|
839 | + while (preg_match('/(?<!\\\\)\}(?!\n)/S', $css, $matches, PREG_OFFSET_CAPTURE, $offset)) |
|
840 | + { |
|
813 | 841 | $matchIndex = $matches[0][1]; |
814 | 842 | $css = substr_replace($css, "\n", $matchIndex + 1, 0); |
815 | 843 | $offset = $matchIndex + 2 + $this->linebreakPosition; |
816 | 844 | $l += 1; |
817 | - if ($offset > $l) { |
|
845 | + if ($offset > $l) |
|
846 | + { |
|
818 | 847 | break; |
819 | 848 | } |
820 | 849 | } |
@@ -834,7 +863,8 @@ discard block |
||
834 | 863 | $values = explode(',', $matches[2]); |
835 | 864 | $terminator = $matches[3]; |
836 | 865 | |
837 | - if ($type === 'hsl') { |
|
866 | + if ($type === 'hsl') |
|
867 | + { |
|
838 | 868 | $values = Utils::hslToRgb($values); |
839 | 869 | } |
840 | 870 | |
@@ -842,7 +872,8 @@ discard block |
||
842 | 872 | |
843 | 873 | // Restore space after rgb() or hsl() function in some cases such as: |
844 | 874 | // background-image: linear-gradient(to bottom, rgb(210,180,140) 10%, rgb(255,0,0) 90%); |
845 | - if (!empty($terminator) && !preg_match('/[ ,);]/S', $terminator)) { |
|
875 | + if (!empty($terminator) && !preg_match('/[ ,);]/S', $terminator)) |
|
876 | + { |
|
846 | 877 | $terminator = ' '. $terminator; |
847 | 878 | } |
848 | 879 | |
@@ -859,7 +890,8 @@ discard block |
||
859 | 890 | $hex = $matches[1]; |
860 | 891 | |
861 | 892 | // Shorten suitable 6 chars HEX colors |
862 | - if (strlen($hex) === 6 && preg_match('/^([0-9a-f])\1([0-9a-f])\2([0-9a-f])\3$/Si', $hex, $m)) { |
|
893 | + if (strlen($hex) === 6 && preg_match('/^([0-9a-f])\1([0-9a-f])\2([0-9a-f])\3$/Si', $hex, $m)) |
|
894 | + { |
|
863 | 895 | $hex = $m[1] . $m[2] . $m[3]; |
864 | 896 | } |
865 | 897 |
@@ -173,8 +173,8 @@ discard block |
||
173 | 173 | */ |
174 | 174 | private function setShortenZeroValuesRegexes() |
175 | 175 | { |
176 | - $zeroRegex = '0'. $this->unitsGroupRegex; |
|
177 | - $numOrPosRegex = '('. $this->numRegex .'|top|left|bottom|right|center) '; |
|
176 | + $zeroRegex = '0' . $this->unitsGroupRegex; |
|
177 | + $numOrPosRegex = '(' . $this->numRegex . '|top|left|bottom|right|center) '; |
|
178 | 178 | $oneZeroSafeProperties = array( |
179 | 179 | '(?:line-)?height', |
180 | 180 | '(?:(?:min|max)-)?width', |
@@ -192,7 +192,7 @@ discard block |
||
192 | 192 | ); |
193 | 193 | |
194 | 194 | // First zero regex |
195 | - $regex = '/(^|;)('. implode('|', $oneZeroSafeProperties) .'):%s/Si'; |
|
195 | + $regex = '/(^|;)(' . implode('|', $oneZeroSafeProperties) . '):%s/Si'; |
|
196 | 196 | $this->shortenOneZeroesRegex = sprintf($regex, $zeroRegex); |
197 | 197 | |
198 | 198 | // Multiple zeroes regexes |
@@ -321,7 +321,7 @@ discard block |
||
321 | 321 | |
322 | 322 | // Process strings so their content doesn't get accidentally minified |
323 | 323 | $css = preg_replace_callback( |
324 | - '/(?:"(?:[^\\\\"]|\\\\.|\\\\)*")|'."(?:'(?:[^\\\\']|\\\\.|\\\\)*')/S", |
|
324 | + '/(?:"(?:[^\\\\"]|\\\\.|\\\\)*")|' . "(?:'(?:[^\\\\']|\\\\.|\\\\)*')/S", |
|
325 | 325 | array($this, 'processStringsCallback'), |
326 | 326 | $css |
327 | 327 | ); |
@@ -375,7 +375,7 @@ discard block |
||
375 | 375 | $dataStartIndex = $matchStartIndex + 4; // url( length |
376 | 376 | $searchOffset = $matchStartIndex + strlen($m[0][0]); |
377 | 377 | $terminator = $m[1][0]; // ', " or empty (not quoted) |
378 | - $terminatorRegex = '/(?<!\\\\)'. (strlen($terminator) === 0 ? '' : $terminator.'\s*') .'(\))/S'; |
|
378 | + $terminatorRegex = '/(?<!\\\\)' . (strlen($terminator) === 0 ? '' : $terminator . '\s*') . '(\))/S'; |
|
379 | 379 | |
380 | 380 | $ret .= substr($css, $substrOffset, $matchStartIndex - $substrOffset); |
381 | 381 | |
@@ -390,7 +390,7 @@ discard block |
||
390 | 390 | $token = preg_replace('/\s+/S', '', $token); |
391 | 391 | } |
392 | 392 | |
393 | - $ret .= 'url('. $this->registerPreservedToken(trim($token)) .')'; |
|
393 | + $ret .= 'url(' . $this->registerPreservedToken(trim($token)) . ')'; |
|
394 | 394 | // No end terminator found, re-add the whole match. Should we throw/warn here? |
395 | 395 | } else { |
396 | 396 | $ret .= substr($css, $matchStartIndex, $searchOffset - $matchStartIndex); |
@@ -411,7 +411,7 @@ discard block |
||
411 | 411 | */ |
412 | 412 | private function processCommentsCallback($matches) |
413 | 413 | { |
414 | - return '/*'. $this->registerCommentToken($matches[1]) .'*/'; |
|
414 | + return '/*' . $this->registerCommentToken($matches[1]) . '*/'; |
|
415 | 415 | } |
416 | 416 | |
417 | 417 | /** |
@@ -421,7 +421,7 @@ discard block |
||
421 | 421 | */ |
422 | 422 | private function processOldIeSpecificMatrixDefinitionCallback($matches) |
423 | 423 | { |
424 | - return 'filter:progid:DXImageTransform.Microsoft.Matrix('. $this->registerPreservedToken($matches[1]) .')'; |
|
424 | + return 'filter:progid:DXImageTransform.Microsoft.Matrix(' . $this->registerPreservedToken($matches[1]) . ')'; |
|
425 | 425 | } |
426 | 426 | |
427 | 427 | /** |
@@ -455,7 +455,7 @@ discard block |
||
455 | 455 | */ |
456 | 456 | private function processImportUnquotedUrlAtRulesCallback($matches) |
457 | 457 | { |
458 | - return '@import url('. $this->registerPreservedToken($matches[1]) .')'. $matches[2]; |
|
458 | + return '@import url(' . $this->registerPreservedToken($matches[1]) . ')' . $matches[2]; |
|
459 | 459 | } |
460 | 460 | |
461 | 461 | /** |
@@ -466,7 +466,7 @@ discard block |
||
466 | 466 | private function processComments($css) |
467 | 467 | { |
468 | 468 | foreach ($this->comments as $commentId => $comment) { |
469 | - $commentIdString = '/*'. $commentId .'*/'; |
|
469 | + $commentIdString = '/*' . $commentId . '*/'; |
|
470 | 470 | |
471 | 471 | // ! in the first position of the comment means preserve |
472 | 472 | // so push to the preserved tokens keeping the ! |
@@ -488,7 +488,7 @@ discard block |
||
488 | 488 | |
489 | 489 | // Keep empty comments after child selectors (IE7 hack) |
490 | 490 | // e.g. html >/**/ body |
491 | - if (strlen($comment) === 0 && strpos($css, '>/*'.$commentId) !== false) { |
|
491 | + if (strlen($comment) === 0 && strpos($css, '>/*' . $commentId) !== false) { |
|
492 | 492 | $css = str_replace($commentId, $this->registerPreservedToken(''), $css); |
493 | 493 | continue; |
494 | 494 | } |
@@ -524,7 +524,7 @@ discard block |
||
524 | 524 | } else { |
525 | 525 | $ruleBody = substr($css, $blockStartPos + 1, $blockEndPos - $blockStartPos - 1); |
526 | 526 | $ruleBodyToken = $this->registerRuleBodyToken($this->processRuleBody($ruleBody)); |
527 | - $ret .= '{'. $ruleBodyToken .'}'; |
|
527 | + $ret .= '{' . $ruleBodyToken . '}'; |
|
528 | 528 | $searchOffset = $blockEndPos + 1; |
529 | 529 | } |
530 | 530 | |
@@ -613,8 +613,8 @@ discard block |
||
613 | 613 | $body = preg_replace('/([ :,(])\+(\.?\d+)/S', '$1$2', $body); |
614 | 614 | |
615 | 615 | // shorten ms to s |
616 | - $body = preg_replace_callback('/([ :,(])(-?)(\d{3,})ms/Si', function ($matches) { |
|
617 | - return $matches[1] . $matches[2] . ((int) $matches[3] / 1000) .'s'; |
|
616 | + $body = preg_replace_callback('/([ :,(])(-?)(\d{3,})ms/Si', function($matches) { |
|
617 | + return $matches[1] . $matches[2] . ((int) $matches[3] / 1000) . 's'; |
|
618 | 618 | }, $body); |
619 | 619 | |
620 | 620 | // Remove leading zeros from integer and float numbers. |
@@ -654,7 +654,7 @@ discard block |
||
654 | 654 | // Shorten suitable shorthand properties with repeated values |
655 | 655 | $body = preg_replace( |
656 | 656 | array( |
657 | - '/(margin|padding|border-(?:width|radius)):('.$this->numRegex.')(?: \2)+( !|;|$)/Si', |
|
657 | + '/(margin|padding|border-(?:width|radius)):(' . $this->numRegex . ')(?: \2)+( !|;|$)/Si', |
|
658 | 658 | '/(border-(?:style|color)):([#a-z0-9]+)(?: \2)+( !|;|$)/Si' |
659 | 659 | ), |
660 | 660 | '$1:$2$3', |
@@ -662,8 +662,8 @@ discard block |
||
662 | 662 | ); |
663 | 663 | $body = preg_replace( |
664 | 664 | array( |
665 | - '/(margin|padding|border-(?:width|radius)):'. |
|
666 | - '('.$this->numRegex.') ('.$this->numRegex.') \2 \3( !|;|$)/Si', |
|
665 | + '/(margin|padding|border-(?:width|radius)):' . |
|
666 | + '(' . $this->numRegex . ') (' . $this->numRegex . ') \2 \3( !|;|$)/Si', |
|
667 | 667 | '/(border-(?:style|color)):([#a-z0-9]+) ([#a-z0-9]+) \2 \3( !|;|$)/Si' |
668 | 668 | ), |
669 | 669 | '$1:$2 $3$4', |
@@ -671,8 +671,8 @@ discard block |
||
671 | 671 | ); |
672 | 672 | $body = preg_replace( |
673 | 673 | array( |
674 | - '/(margin|padding|border-(?:width|radius)):'. |
|
675 | - '('.$this->numRegex.') ('.$this->numRegex.') ('.$this->numRegex.') \3( !|;|$)/Si', |
|
674 | + '/(margin|padding|border-(?:width|radius)):' . |
|
675 | + '(' . $this->numRegex . ') (' . $this->numRegex . ') (' . $this->numRegex . ') \3( !|;|$)/Si', |
|
676 | 676 | '/(border-(?:style|color)):([#a-z0-9]+) ([#a-z0-9]+) ([#a-z0-9]+) \3( !|;|$)/Si' |
677 | 677 | ), |
678 | 678 | '$1:$2 $3 $4$5', |
@@ -681,9 +681,9 @@ discard block |
||
681 | 681 | |
682 | 682 | // Lowercase some common functions that can be values |
683 | 683 | $body = preg_replace_callback( |
684 | - '/(?:attr|blur|brightness|circle|contrast|cubic-bezier|drop-shadow|ellipse|from|grayscale|'. |
|
685 | - 'hsla?|hue-rotate|inset|invert|local|minmax|opacity|perspective|polygon|rgba?|rect|repeat|saturate|sepia|'. |
|
686 | - 'steps|to|url|var|-webkit-gradient|'. |
|
684 | + '/(?:attr|blur|brightness|circle|contrast|cubic-bezier|drop-shadow|ellipse|from|grayscale|' . |
|
685 | + 'hsla?|hue-rotate|inset|invert|local|minmax|opacity|perspective|polygon|rgba?|rect|repeat|saturate|sepia|' . |
|
686 | + 'steps|to|url|var|-webkit-gradient|' . |
|
687 | 687 | '(?:-(?:atsc|khtml|moz|ms|o|wap|webkit)-)?(?:calc|(?:repeating-)?(?:linear|radial)-gradient))\(/Si', |
688 | 688 | array($this, 'strtolowerCallback'), |
689 | 689 | $body |
@@ -716,13 +716,13 @@ discard block |
||
716 | 716 | $css = preg_replace('/::(before|after|first-(?:line|letter))(\{|,)/Si', ':$1$2', $css); |
717 | 717 | |
718 | 718 | // Retain space for special IE6 cases |
719 | - $css = preg_replace_callback('/:first-(line|letter)(\{|,)/Si', function ($matches) { |
|
720 | - return ':first-'. strtolower($matches[1]) .' '. $matches[2]; |
|
719 | + $css = preg_replace_callback('/:first-(line|letter)(\{|,)/Si', function($matches) { |
|
720 | + return ':first-' . strtolower($matches[1]) . ' ' . $matches[2]; |
|
721 | 721 | }, $css); |
722 | 722 | |
723 | 723 | // Find a fraction that may used in some @media queries such as: (min-aspect-ratio: 1/1) |
724 | 724 | // Add token to add the "/" back in later |
725 | - $css = preg_replace('/\(([a-z-]+):([0-9]+)\/([0-9]+)\)/Si', '($1:$2'. self::QUERY_FRACTION .'$3)', $css); |
|
725 | + $css = preg_replace('/\(([a-z-]+):([0-9]+)\/([0-9]+)\)/Si', '($1:$2' . self::QUERY_FRACTION . '$3)', $css); |
|
726 | 726 | |
727 | 727 | // Remove empty rule blocks up to 2 levels deep. |
728 | 728 | $css = preg_replace(array_fill(0, 2, '/(\{)[^{};\/\n]+\{\}/S'), '$1', $css); |
@@ -738,7 +738,7 @@ discard block |
||
738 | 738 | |
739 | 739 | // Lowercase some popular @directives |
740 | 740 | $css = preg_replace_callback( |
741 | - '/(?<!\\\\)@(?:charset|document|font-face|import|(?:-(?:atsc|khtml|moz|ms|o|wap|webkit)-)?keyframes|media|'. |
|
741 | + '/(?<!\\\\)@(?:charset|document|font-face|import|(?:-(?:atsc|khtml|moz|ms|o|wap|webkit)-)?keyframes|media|' . |
|
742 | 742 | 'namespace|page|supports|viewport)/Si', |
743 | 743 | array($this, 'strtolowerCallback'), |
744 | 744 | $css |
@@ -753,9 +753,9 @@ discard block |
||
753 | 753 | |
754 | 754 | // Lowercase some common pseudo-classes & pseudo-elements |
755 | 755 | $css = preg_replace_callback( |
756 | - '/(?<!\\\\):(?:active|after|before|checked|default|disabled|empty|enabled|first-(?:child|of-type)|'. |
|
757 | - 'focus(?:-within)?|hover|indeterminate|in-range|invalid|lang\(|last-(?:child|of-type)|left|link|not\(|'. |
|
758 | - 'nth-(?:child|of-type)\(|nth-last-(?:child|of-type)\(|only-(?:child|of-type)|optional|out-of-range|'. |
|
756 | + '/(?<!\\\\):(?:active|after|before|checked|default|disabled|empty|enabled|first-(?:child|of-type)|' . |
|
757 | + 'focus(?:-within)?|hover|indeterminate|in-range|invalid|lang\(|last-(?:child|of-type)|left|link|not\(|' . |
|
758 | + 'nth-(?:child|of-type)\(|nth-last-(?:child|of-type)\(|only-(?:child|of-type)|optional|out-of-range|' . |
|
759 | 759 | 'read-(?:only|write)|required|right|root|:selection|target|valid|visited)/Si', |
760 | 760 | array($this, 'strtolowerCallback'), |
761 | 761 | $css |
@@ -770,7 +770,7 @@ discard block |
||
770 | 770 | } |
771 | 771 | |
772 | 772 | // @import handling |
773 | - $css = preg_replace_callback($this->importRegex, function ($matches) use (&$imports) { |
|
773 | + $css = preg_replace_callback($this->importRegex, function($matches) use (&$imports) { |
|
774 | 774 | // Keep all @import at-rules found for later |
775 | 775 | $imports .= $matches[0]; |
776 | 776 | // Delete all @import at-rules |
@@ -778,7 +778,7 @@ discard block |
||
778 | 778 | }, $css); |
779 | 779 | |
780 | 780 | // @namespace handling |
781 | - $css = preg_replace_callback($this->namespaceRegex, function ($matches) use (&$namespaces) { |
|
781 | + $css = preg_replace_callback($this->namespaceRegex, function($matches) use (&$namespaces) { |
|
782 | 782 | // Keep all @namespace at-rules found for later |
783 | 783 | $namespaces .= $matches[0]; |
784 | 784 | // Delete all @namespace at-rules |
@@ -843,10 +843,10 @@ discard block |
||
843 | 843 | // Restore space after rgb() or hsl() function in some cases such as: |
844 | 844 | // background-image: linear-gradient(to bottom, rgb(210,180,140) 10%, rgb(255,0,0) 90%); |
845 | 845 | if (!empty($terminator) && !preg_match('/[ ,);]/S', $terminator)) { |
846 | - $terminator = ' '. $terminator; |
|
846 | + $terminator = ' ' . $terminator; |
|
847 | 847 | } |
848 | 848 | |
849 | - return '#'. implode('', $hexColors) . $terminator; |
|
849 | + return '#' . implode('', $hexColors) . $terminator; |
|
850 | 850 | } |
851 | 851 | |
852 | 852 | /** |
@@ -864,7 +864,7 @@ discard block |
||
864 | 864 | } |
865 | 865 | |
866 | 866 | // Lowercase |
867 | - $hex = '#'. strtolower($hex); |
|
867 | + $hex = '#' . strtolower($hex); |
|
868 | 868 | |
869 | 869 | // Replace Hex colors with shorter color names |
870 | 870 | $color = array_key_exists($hex, $this->hexToNamedColorsMap) ? $this->hexToNamedColorsMap[$hex] : $hex; |