This repository was archived by the owner on Jan 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathcss.php
More file actions
119 lines (102 loc) · 3.25 KB
/
css.php
File metadata and controls
119 lines (102 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/*
* SmartOptimizer CSS Minifier
*/
function convertUrl($url, $count)
{
global $settings, $mimeTypes, $fileDir;
static $baseUrl = '';
$url = trim($url);
if (preg_match('@^[^/]+:@', $url)) return $url;
$fileType = substr(strrchr($url, '.'), 1);
if (isset($mimeTypes[$fileType]))
$mimeType = $mimeTypes[$fileType];
elseif (function_exists('mime_content_type') && file_exists($fileDir.$url))
$mimeType = mime_content_type($fileDir.$url);
else
$mimeType = null;
if (!$settings['embed'] ||
!file_exists($fileDir.$url) ||
($settings['embedMaxSize'] > 0 && filesize($fileDir.$url) > $settings['embedMaxSize']) ||
!$fileType ||
in_array($fileType, $settings['embedExceptions']) ||
!$mimeType ||
$count > 1) {
if (strpos($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME'].'?') === 0 ||
strpos($_SERVER['REQUEST_URI'], rtrim(dirname($_SERVER['SCRIPT_NAME']), '\/').'/?') === 0) {
if (!$baseUrl) return $fileDir . $url;
}
return $baseUrl . $url;
}
$contents = file_get_contents($fileDir.$url);
if ($fileType == 'css') {
$oldFileDir = $fileDir;
$fileDir = rtrim(dirname($fileDir.$url), '\/').'/';
$oldBaseUrl = $baseUrl;
$baseUrl = 'http'.(@$_SERVER['HTTPS']?'s':'').'://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['SCRIPT_NAME']), '\/').'/'.$fileDir;
$contents = minify_css($contents);
$fileDir = $oldFileDir;
$baseUrl = $oldBaseUrl;
}
$base64 = base64_encode($contents);
return 'data:' . $mimeType . ';base64,' . $base64;
}
function minify_css($str) {
$res = '';
$i=0;
$inside_block = false;
$current_char = '';
while ($i+1<strlen($str)) {
if ($str[$i]=='"' || $str[$i]=="'") {//quoted string detected
$res .= $quote = $str[$i++];
$url = '';
while ($i<strlen($str) && $str[$i]!=$quote) {
if ($str[$i] == '\\') {
$url .= $str[$i++];
}
$url .= $str[$i++];
}
if (strtolower(substr($res, -5, 4))=='url(' || strtolower(substr($res, -9, 8)) == '@import ') {
$url = convertUrl($url, substr_count($str, $url));
}
$res .= $url;
$res .= $str[$i++];
continue;
} elseif (strtolower(substr($res, -4))=='url(') {//url detected
$url = '';
do {
if ($str[$i] == '\\') {
$url .= $str[$i++];
}
$url .= $str[$i++];
} while ($i<strlen($str) && $str[$i]!=')');
$url = convertUrl($url, substr_count($str, $url));
$res .= $url;
$res .= $str[$i++];
continue;
} elseif ($str[$i].$str[$i+1]=='/*') {//css comment detected
$i+=3;
while ($i<strlen($str) && $str[$i-1].$str[$i]!='*/') $i++;
if ($current_char == "\n") $str[$i] = "\n";
else $str[$i] = ' ';
}
if (strlen($str) <= $i+1) break;
$current_char = $str[$i];
if ($inside_block && $current_char == '}') {
$inside_block = false;
}
if ($current_char == '{') {
$inside_block = true;
}
if (preg_match('/[\n\r\t ]/', $current_char)) $current_char = " ";
if ($current_char == " ") {
$pattern = $inside_block?'/^[^{};,:\n\r\t ]{2}$/':'/^[^{};,>+\n\r\t ]{2}$/';
if (strlen($res) && preg_match($pattern, $res[strlen($res)-1].$str[$i+1]))
$res .= $current_char;
} else $res .= $current_char;
$i++;
}
if ($i<strlen($str) && preg_match('/[^\n\r\t ]/', $str[$i])) $res .= $str[$i];
return $res;
}
?>