-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse.php
More file actions
executable file
·213 lines (171 loc) · 7.98 KB
/
parse.php
File metadata and controls
executable file
·213 lines (171 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
header('Content-Type:text/html; charset=utf-8');
require_once __DIR__ . '/lib/CSSTidy.php';
$processed = false;
$inputError = false;
$file_ok = false; // if output to file has been selected and file has been generated, this will be set to true
$url = (isset($_POST['url']) && !empty($_POST['url']) && $_POST['url']!='http://') ? $_POST['url'] : false;
/* ------------- Preparation & Sanitation ------------ */
if (get_magic_quotes_gpc()) {
if (isset($_POST['css_text'])) {
$_POST['css_text'] = stripslashes($_POST['css_text']);
}
if (isset($_POST['custom'])) {
$_POST['custom'] = stripslashes($_POST['custom']);
}
if (isset($_COOKIE['custom_template'])) {
$_COOKIE['custom_template'] = stripslashes($_COOKIE['custom_template']);
}
}
function rmdirr($dirname,$oc=0)
{
// Sanity check
if (!file_exists($dirname)) {
return false;
}
// Simple delete for a file
if (is_file($dirname) && (time()-fileatime($dirname))>3600) {
return unlink($dirname);
}
// Loop through the folder
if(is_dir($dirname))
{
$dir = dir($dirname);
while (false !== $entry = $dir->read()) {
// Skip pointers
if ($entry === '.' || $entry === '..') {
continue;
}
// Recurse
rmdirr($dirname.'/'.$entry,$oc);
}
$dir->close();
}
// Clean up
if ($oc==1)
{
return rmdir($dirname);
}
}
/* ------------- Settings & Processing ------------ */
if (isset($_POST['css_text']) || $url) {
$processed = true;
$inputCss = $_POST['css_text'];
$cssTidy = new \CSSTidy\CSSTidy;
$is_custom = isset($_POST['custom']) && !empty($_POST['custom']) && isset($_POST['template']) && ($_POST['template'] === 'custom');
if($is_custom) {
setcookie ('custom_template', $_POST['custom'], time()+360000);
$cssTidy->configuration->setTemplate($_POST['custom']);
}
if($_POST['template'] == 'low') $cssTidy->configuration->loadPredefinedTemplate(\CSSTidy\Configuration::LOW_COMPRESSION);
else if($_POST['template'] == 'standard') $cssTidy->configuration->loadPredefinedTemplate(\CSSTidy\Configuration::STANDARD_COMPRESSION);
else if($_POST['template'] == 'high') $cssTidy->configuration->loadPredefinedTemplate(\CSSTidy\Configuration::HIGH_COMPRESSION);
else $cssTidy->configuration->loadPredefinedTemplate(\CSSTidy\Configuration::HIGHEST_COMPRESSION);
// The default options (see Configuration.php) do not need to be checked - in case no parameters submitted, CSSTidy will revert to them
if($_POST['css_level'] == '10') $cssTidy->configuration->setCssLevel(\CSSTidy\Configuration::CSS1_0);
else if($_POST['css_level'] == '20') $cssTidy->configuration->setCssLevel(\CSSTidy\Configuration::CSS2_0);
else if($_POST['css_level'] == '21') $cssTidy->configuration->setCssLevel(\CSSTidy\Configuration::CSS2_1);
else if($_POST['css_level'] == '30') $cssTidy->configuration->setCssLevel(\CSSTidy\Configuration::CSS3_0);
if($_POST['merge_selectors'] == '1') $cssTidy->configuration->setMergeSelectors(\CSSTidy\Configuration::SEPARATE_SELECTORS);
else if($_POST['merge_selectors'] == '2') $cssTidy->configuration->setMergeSelectors(\CSSTidy\Configuration::MERGE_SELECTORS);
if($_POST['optimise_shorthands'] == '0') $cssTidy->configuration->setOptimiseShorthands(\CSSTidy\Configuration::NOTHING);
else if($_POST['optimise_shorthands'] == '2') $cssTidy->configuration->setOptimiseShorthands(\CSSTidy\Configuration::FONT);
else if($_POST['optimise_shorthands'] == '3') $cssTidy->configuration->setOptimiseShorthands(\CSSTidy\Configuration::BACKGROUND);
if($_POST['case_properties'] == '2') $cssTidy->configuration->setCaseProperties(\CSSTidy\Configuration::UPPERCASE);
if(!isset($_POST['remove_last_sem']) && isset($_POST['post'])) $cssTidy->configuration->setRemoveLastSemicolon(false);
if(isset($_POST['lowercase'])) $cssTidy->configuration->setLowerCaseSelectors(true);
if(!isset($_POST['compress_c']) && isset($_POST['post'])) $cssTidy->configuration->setCompressColors(false);
if(!isset($_POST['compress_fw']) && isset($_POST['post'])) $cssTidy->configuration->setCompressFontWeight(false);
if(!isset($_POST['rbs']) && isset($_POST['post'])) $cssTidy->configuration->setRemoveBackSlash(false);
if(isset($_POST['preserve_css'])) $cssTidy->configuration->setPreserveComments(true);
if(isset($_POST['sort_sel'])) $cssTidy->configuration->setSortSelectors(true);
if(isset($_POST['sort_de'])) $cssTidy->configuration->setSortProperties(true);
if(isset($_POST['discard'])) $cssTidy->configuration->setDiscardInvalidProperties(true);
if(isset($_POST['timestamp'])) $cssTidy->configuration->setAddTimestamp(true);
//$cssTidy->configuration->setDiscardInvalidSelectors();
//$cssTidy->configuration->setConvertUnit();
if($url) // validate URL, then fetch content
{
if(substr($_POST['url'],0,7) != 'http://') {
$_POST['url'] = 'http://'.$_POST['url'];
}
ini_set('user_agent', 'Devilo.us CSS parser v2');
$inputCss = @file_get_contents($url);
}
try {
$output = $cssTidy->process($inputCss);
$inputError = false;
}
catch (\Exception $e) {
$inputError = true;
// echo $e; // Debugger
}
}
/* ------------- Output Results ------------ */
if ($processed) {
if ($inputError) {
echo '<div class="message error">No or invalid CSS input!</div>';
}
else {
$ratio = $output->getRatio();
$diff = $output->getDiff();
if($ratio>0) $ratio = '<span style="color: #0c0;">'.$ratio.'%</span></strong> ('.$diff.' Bytes)';
else $ratio = '<span style="color: #c00;">'.$ratio.'%</span></strong> ('.$diff.' Bytes)';
if(isset($_POST['file_output']))
{
/// rmdirr('temp'); // empty previous files (only when requested to create new one, to decrease server load -- Unfortunately deletes download script as well -.-
$filename = md5(mt_rand().time().mt_rand());
$handle = fopen('temp/'.$filename.'.css','w');
if($handle) {
if(fwrite($handle,$output->plain()))
{
$file_ok = true;
}
}
fclose($handle);
}
if($cssTidy->logger->getMessages() > 0): ?>
<br /><br /><p>Parser Log</p>
<fieldset id="messages">
<div id="logWrapper"><dl>
<?php foreach ($cssTidy->logger->getMessages() as $line => $messages): ?>
<dt><?php echo $line ?></dt>
<?php foreach ($messages as $message): ?>
<?php switch ($message[\CSSTidy\Logger::TYPE]) {
case \CSSTidy\Logger::INFORMATION:
echo '<dd class="information">';
break;
case \CSSTidy\Logger::WARNING:
echo '<dd class="warning">';
break;
case \CSSTidy\Logger::ERROR:
echo '<dd class="error">';
}
echo $message[\CSSTidy\Logger::MESSAGE] ?>
</dd>
<?php endforeach; ?>
<?php endforeach; ?>
</dl></div>
</fieldset>
<?php endif;
if($file_ok) // ----> Output as CSS file
{
echo '<br /><br />';
echo '<div id="downloadFile"><a href="temp/?f='.$filename.'">Download CSS file</a></div>';
echo '<p class="fileResults">Input: '.$output->size(\CSSTidy\Output::INPUT).' kB, Output: '.$output->size(\CSSTidy\Output::OUTPUT).' kB<br /><strong>Compression ratio: '.$ratio.'</p><br />';
}
else // ------------> Output Code
{
echo '<p class="compressionResults">Input: '.$output->size(\CSSTidy\Output::INPUT).' kB, Output: '.$output->size(\CSSTidy\Output::OUTPUT).' kB — <strong>Compression ratio: '.$ratio.'</p>';
echo '<br /><p class="right"><a href="javascript:ClipBoard()">copy to clipboard</a></p>';
echo '<fieldset id="convertedCode">';
echo '<div><pre><code id="copytext">';
echo $output->formatted();
echo '</code></pre></div>';
echo '</fieldset>';
echo '<p class="right"><a href="javascript:ClipBoard()">copy to clipboard</a></p>';
echo '<br /><br /><p><a href="#" onClick="$.smoothScroll({offset: -100});return false;">↑ Go back up</a></p>';
}
}
}
?>