xref: /plugin/pagecss/vendor/csstidy-2.2.1/css_optimiser.php (revision 7d6669007238fef7e8728f167d637ed824899eb0)
1<?php
2
3/**
4 * CSSTidy - CSS Optimiser Interface
5 * This file produces an XHTML interface for optimising CSS code
6 *
7 * Copyright 2005, 2006, 2007 Florian Schmitz
8 *
9 * This file is part of CSSTidy.
10 *
11 *  CSSTidy is free software; you can redistribute it and/or modify
12 *  it under the terms of the GNU Lesser General Public License as published by
13 *  the Free Software Foundation; either version 2.1 of the License, or
14 *   (at your option) any later version.
15 *
16 *   CSSTidy is distributed in the hope that it will be useful,
17 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
18 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 *   GNU Lesser General Public License for more details.
20 *
21 *   You should have received a copy of the GNU Lesser General Public License
22 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 *
24 * @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
25 * @package csstidy
26 * @author Florian Schmitz (floele at gmail dot com) 2005-2007
27 * @author Brett Zamir (brettz9 at yahoo dot com) 2007
28 */
29
30require('class.csstidy.php');
31require('lang.inc.php');
32
33if (!file_exists(__DIR__ . DIRECTORY_SEPARATOR . ".unlock_css_optimiser")) {
34		print 'Access Denied. Add a file `.unlock_css_optimiser` to the directory to unlock css_optimiser';
35		exit;
36}
37
38
39if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
40	if (isset($_REQUEST['css_text'])) {
41		$_REQUEST['css_text'] = stripslashes($_REQUEST['css_text']);
42	}
43	if (isset($_REQUEST['custom'])) {
44		$_REQUEST['custom'] = stripslashes($_REQUEST['custom']);
45	}
46	if (isset($_COOKIE['custom_template'])) {
47		$_COOKIE['custom_template'] = stripslashes($_COOKIE['custom_template']);
48	}
49}
50
51function rmdirr($dirname,$oc=0)
52{
53	// Sanity check
54	if (!file_exists($dirname)) {
55		return false;
56	}
57	// Simple delete for a file
58	if (is_file($dirname) && (time()-fileatime($dirname))>3600) {
59		 return unlink($dirname);
60	}
61	// Loop through the folder
62	if (is_dir($dirname)) {
63		$dir = dir($dirname);
64		while (false !== $entry = $dir->read()) {
65			// Skip pointers
66			if ($entry === '.' || $entry === '..') {
67				 continue;
68			}
69			// Recurse
70			rmdirr($dirname.'/'.$entry,$oc);
71		}
72		$dir->close();
73	}
74	// Clean up
75	if ($oc==1) {
76		return rmdir($dirname);
77	}
78}
79
80function options($options, $selected = null, $labelIsValue = false)
81{
82	$html = '';
83
84	settype($selected, 'array');
85	settype($options, 'array');
86
87	foreach ($options as $value=>$label) {
88		if (is_array($label)) {
89			$value = $label[0];
90			$label = $label[1];
91		}
92		$label = htmlspecialchars($label, ENT_QUOTES, 'utf-8');
93		$value = $labelIsValue ? $label
94								 : htmlspecialchars($value, ENT_QUOTES, 'utf-8');
95
96		$html .= '<option value="'.$value.'"';
97		if (in_array($value, $selected)) {
98			$html .= ' selected="selected"';
99		}
100		$html .= '>'.$label.'</option>';
101	}
102	if (!$html) {
103		$html .= '<option value="0">---</option>';
104	}
105
106	return $html;
107}
108
109$css = new csstidy();
110$is_custom = isset($_REQUEST['custom']) && !empty($_REQUEST['custom']) && isset($_REQUEST['template']) && ($_REQUEST['template'] === '4');
111if ($is_custom)
112{
113	setcookie ('custom_template', $_REQUEST['custom'], time()+360000);
114}
115
116rmdirr('temp');
117
118if (isset($_REQUEST['case_properties'])) $css->set_cfg('case_properties',$_REQUEST['case_properties']);
119if (isset($_REQUEST['lowercase'])) $css->set_cfg('lowercase_s',true);
120if (!isset($_REQUEST['compress_c']) && isset($_REQUEST['post'])) $css->set_cfg('compress_colors',false);
121if (!isset($_REQUEST['compress_fw']) && isset($_REQUEST['post'])) $css->set_cfg('compress_font-weight',false);
122if (isset($_REQUEST['merge_selectors'])) $css->set_cfg('merge_selectors', $_REQUEST['merge_selectors']);
123if (isset($_REQUEST['optimise_shorthands'])) $css->set_cfg('optimise_shorthands',$_REQUEST['optimise_shorthands']);
124if (!isset($_REQUEST['rbs']) && isset($_REQUEST['post'])) $css->set_cfg('remove_bslash',false);
125if (isset($_REQUEST['preserve_css'])) $css->set_cfg('preserve_css',true);
126if (isset($_REQUEST['sort_sel'])) $css->set_cfg('sort_selectors',true);
127if (isset($_REQUEST['sort_de'])) $css->set_cfg('sort_properties',true);
128if (isset($_REQUEST['remove_last_sem'])) $css->set_cfg('remove_last_;',true);
129if (isset($_REQUEST['discard'])) $css->set_cfg('discard_invalid_properties',true);
130if (isset($_REQUEST['css_level'])) $css->set_cfg('css_level',$_REQUEST['css_level']);
131if (isset($_REQUEST['timestamp'])) $css->set_cfg('timestamp',true);
132if (isset($_REQUEST['rtl'])) $css->set_cfg('reverse_left_and_right',true);
133
134
135// This by itself is enough since our scripts don't use DOM to create elements (in which case the namespace aware ones
136// should be used when serving as application/xhtml+xml but not when served as text/html ;
137// also, case will be different when retrieving element names, as HTML DOM returns in upper case,
138// genuine XHTML DOM (when XHTML served as such) as lower
139if (stristr($_SERVER['HTTP_ACCEPT'], 'application/xhtml+xml')) {
140	$http_accept = 'application/xhtml+xml';
141} elseif (stristr($_SERVER['HTTP_ACCEPT'], 'application/xml')) {
142	$http_accept = 'application/xml';
143} elseif (stristr($_SERVER['HTTP_ACCEPT'], 'text/xml')) {
144	$http_accept = 'text/xml';
145} elseif (stristr($_SERVER['HTTP_USER_AGENT'], 'Opera ') || stristr($_SERVER['HTTP_USER_AGENT'], 'Opera/')) {
146	preg_match('@Opera/(\d)@', $_SERVER['HTTP_USER_AGENT'], $matches);
147	if (isset($matches[1]) && $matches[1] >= 7) {
148		$http_accept = 'application/xhtml+xml';
149	} else {
150		$http_accept = 'text/html';
151	}
152} else {
153	$http_accept = 'text/html';
154}
155
156header('Content-Type: '.$http_accept.'; charset=utf-8');
157
158if ($http_accept === 'text/html') {
159
160?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
161	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
162<?php
163} else {
164
165?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
166<?php
167}
168
169?><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $l; ?>">
170	<head>
171	<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
172	<title>
173		<?php echo $lang[$l][0]; echo $css->version; ?>)
174	</title>
175	<link rel="stylesheet" href="cssparse.css" type="text/css" />
176	<script type="text/javascript"><!--/*--><![CDATA[/*><!--*/
177	function enable_disable_preserve() {
178		var inputs =   new Array('sort_sel', 'sort_de', 'optimise_shorthands', 'merge_selectors', 'none');
179		var inputs_v = new Array( true,       true,      true,                  true,              false);
180		for (var i = 0; i < inputs.length; i++) {
181			if (document.getElementById('preserve_css').checked)  {
182				document.getElementById(inputs[i]).disabled = inputs_v[i];
183			} else {
184				document.getElementById(inputs[i]).disabled = !inputs_v[i];
185			}
186		}
187	}
188	function ClipBoard() {
189		if (window.clipboardData) { // Feature testing
190			window.clipboardData.setData('Text',document.getElementById("copytext").innerText);
191		} else if (navigator.userAgent.indexOf('Gecko') != -1
192					&& navigator.userAgent.indexOf('Apple') == -1
193					) {
194			try {
195				netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
196				const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].
197									getService(Components.interfaces.nsIClipboardHelper);
198									gClipboardHelper.copyString(document.getElementById("copytext").innerHTML);
199			}
200			catch (e) {
201				alert(e+"\n\n"+"<?php echo $lang[$l][66] ?>");
202			}
203		} else {
204			alert("<?php echo $lang[$l][60]; ?>");
205		}
206	}
207	/*]]>*/-->
208	</script>
209	</head>
210	<body onload="enable_disable_preserve()">
211	<div><h1 style="display:inline">
212		<?php echo $lang[$l][1]; ?>
213	</h1>
214	<?php echo $lang[$l][2]; ?> <a
215		href="http://csstidy.sourceforge.net/">csstidy</a> <?php echo $css->version; ?>)
216	</div><p>
217	<?php echo $lang[$l][39]; ?>: <a hreflang="en" href="?lang=en">English</a> <a hreflang="de" href="?lang=de">Deutsch</a> <a hreflang="fr" href="?lang=fr">French</a> <a hreflang="zh" href="?lang=zh">Chinese</a></p>
218	<p><?php echo $lang[$l][4]; ?>
219		<?php echo $lang[$l][6]; ?>
220	</p>
221
222	<form method="post" action="">
223		<div>
224		<fieldset id="field_input">
225			<legend><?php echo $lang[$l][8]; ?></legend> <label for="css_text"
226			class="block"><?php echo $lang[$l][9]; ?></label><textarea id="css_text" name="css_text" rows="20" cols="35"><?php if (isset($_REQUEST['css_text'])) echo htmlspecialchars($_REQUEST['css_text'], ENT_QUOTES, "utf-8"); ?></textarea>
227			<label for="url"><?php echo $lang[$l][10]; ?></label> <input type="text"
228			name="url" id="url" <?php if (isset($_REQUEST['url']) &&
229			!empty($_REQUEST['url'])) echo 'value="',htmlspecialchars($_REQUEST['url'], ENT_QUOTES, 'utf-8'),'"'; ?>
230			size="35" /><br />
231			<input type="submit" value="<?php echo $lang[$l][35]; ?>" id="submit" />
232		</fieldset>
233		<div id="rightcol">
234			<fieldset id="code_layout">
235			<legend><?php echo $lang[$l][11]; ?></legend> <label for="template"
236			class="block"><?php echo $lang[$l][12]; ?></label> <select
237			id="template" name="template" style="margin-bottom:1em">
238				<?php
239				$num = (isset($_REQUEST['template'])) ? intval($_REQUEST['template']) : 1;
240				echo options(array(3 => $lang[$l][13], 2 => $lang[$l][14], 1 => $lang[$l][15], 0 => $lang[$l][16], 4 => $lang[$l][17]), $num);
241				?>
242			</select><br />
243			<label for="custom" class="block">
244			<?php echo $lang[$l][18]; ?> </label> <textarea id="custom"
245			name="custom" cols="33" rows="4"><?php
246				 if ($is_custom) echo
247				htmlspecialchars($_REQUEST['custom'], ENT_QUOTES, 'utf-8');
248				 elseif(isset($_COOKIE['custom_template']) &&
249				!empty($_COOKIE['custom_template'])) echo
250				htmlspecialchars($_COOKIE['custom_template'], ENT_QUOTES, 'utf-8');
251				 ?></textarea>
252			</fieldset>
253			<fieldset id="options">
254		 <legend><?php echo $lang[$l][19]; ?></legend>
255
256			<input onchange="enable_disable_preserve()" type="checkbox" name="preserve_css" id="preserve_css"
257					 <?php if ($css->get_cfg('preserve_css')) echo 'checked="checked"'; ?> />
258			<label for="preserve_css" title="<?php echo $lang[$l][52]; ?>" class="help"><?php echo $lang[$l][51]; ?></label><br />
259
260
261			<input type="checkbox" name="sort_sel" id="sort_sel"
262					 <?php if ($css->get_cfg('sort_selectors')) echo 'checked="checked"'; ?> />
263			<label for="sort_sel" title="<?php echo $lang[$l][41]; ?>" class="help"><?php echo $lang[$l][20]; ?></label><br />
264
265
266			<input type="checkbox" name="sort_de" id="sort_de"
267					 <?php if ($css->get_cfg('sort_properties')) echo 'checked="checked"'; ?> />
268			<label for="sort_de"><?php echo $lang[$l][21]; ?></label><br />
269
270
271			<label for="merge_selectors"><?php echo $lang[$l][22]; ?></label>
272			<select style="width:15em;" name="merge_selectors" id="merge_selectors">
273				<?php echo options(array('0' => $lang[$l][47], '1' => $lang[$l][48], '2' => $lang[$l][49]), $css->get_cfg('merge_selectors')); ?>
274			</select><br />
275
276			<label for="optimise_shorthands"><?php echo $lang[$l][23]; ?></label>
277			<select name="optimise_shorthands" id="optimise_shorthands">
278			<?php echo options(array($lang[$l][54], $lang[$l][55], $lang[$l][56]), $css->get_cfg('optimise_shorthands')); ?>
279			</select><br />
280
281
282			<input type="checkbox" name="compress_c" id="compress_c"
283					 <?php if ($css->get_cfg('compress_colors')) echo 'checked="checked"';?> />
284			<label for="compress_c"><?php echo $lang[$l][24]; ?></label><br />
285
286
287			<input type="checkbox" name="compress_fw" id="compress_fw"
288					 <?php if ($css->get_cfg('compress_font-weight')) echo 'checked="checked"';?> />
289			<label for="compress_fw"><?php echo $lang[$l][45]; ?></label><br />
290
291
292			<input type="checkbox" name="lowercase" id="lowercase" value="lowercase"
293					 <?php if ($css->get_cfg('lowercase_s')) echo 'checked="checked"'; ?> />
294			<label title="<?php echo $lang[$l][30]; ?>" class="help" for="lowercase"><?php echo $lang[$l][25]; ?></label><br />
295
296
297			<?php echo $lang[$l][26]; ?><br />
298			<input type="radio" name="case_properties" id="none" value="0"
299					 <?php if ($css->get_cfg('case_properties') === 0) echo 'checked="checked"'; ?> />
300			<label for="none"><?php echo $lang[$l][53]; ?></label>
301			<input type="radio" name="case_properties" id="lower_yes" value="1"
302					 <?php if ($css->get_cfg('case_properties') === 1) echo 'checked="checked"'; ?> />
303			<label for="lower_yes"><?php echo $lang[$l][27]; ?></label>
304			<input type="radio" name="case_properties" id="upper_yes" value="2"
305					 <?php if ($css->get_cfg('case_properties') === 2) echo 'checked="checked"'; ?> />
306			<label for="upper_yes"><?php echo $lang[$l][29]; ?></label><br />
307
308			<input type="checkbox" name="rbs" id="rbs"
309					 <?php if ($css->get_cfg('remove_bslash')) echo 'checked="checked"'; ?> />
310			<label for="rbs"><?php echo $lang[$l][31]; ?></label><br />
311
312
313			<input type="checkbox" id="remove_last_sem" name="remove_last_sem"
314					 <?php if ($css->get_cfg('remove_last_;')) echo 'checked="checked"'; ?> />
315	 			<label for="remove_last_sem"><?php echo $lang[$l][42]; ?></label><br />
316
317
318			<input type="checkbox" id="discard" name="discard"
319					 <?php if ($css->get_cfg('discard_invalid_properties')) echo 'checked="checked"'; ?> />
320			<label for="discard"><?php echo $lang[$l][43]; ?></label>
321			<select name="css_level"><?php echo options(array('CSS3.0','CSS2.1','CSS2.0','CSS1.0'),$css->get_cfg('css_level'), true); ?></select><br />
322
323
324			<input type="checkbox" id="timestamp" name="timestamp"
325					 <?php if ($css->get_cfg('timestamp')) echo 'checked="checked"'; ?> />
326	 			<label for="timestamp"><?php echo $lang[$l][57]; ?></label><br />
327
328			<input type="checkbox" id="rtl" name="rtl"
329					 <?php if ($css->get_cfg('reverse_left_and_right')) echo 'checked="checked"'; ?> />
330	 			<label for="rtl"><?php echo $lang[$l][67]; ?></label><br />
331
332			<input type="checkbox" id="whole_file" name="whole_file"
333					 <?php if (isset($_REQUEST['whole_file'])) echo 'checked="checked"'; ?> />
334	 			<label for="whole_file"><?php echo $lang[$l][63]; ?></label><br />
335
336
337			<input type="checkbox" name="file_output" id="file_output" value="file_output"
338					 <?php if (isset($_REQUEST['file_output'])) echo 'checked="checked"'; ?> />
339			<label class="help" title="<?php echo $lang[$l][34]; ?>" for="file_output">
340				<strong><?php echo $lang[$l][33]; ?></strong>
341			</label><br />
342
343			</fieldset>
344		<input type="hidden" name="post" />
345		</div>
346		</div>
347	</form>
348	<?php
349
350	$file_ok = false;
351	$result = false;
352
353	$url = (isset($_REQUEST['url']) && !empty($_REQUEST['url'])) ? $_REQUEST['url'] : false;
354
355	if (isset($_REQUEST['template'])) {
356		switch ($_REQUEST['template']) {
357			case 4:
358			if ($is_custom) {
359				$css->load_template($_REQUEST['custom'],false);
360			}
361			break;
362
363			case 3:
364			$css->load_template('highest_compression');
365			break;
366
367			case 2:
368			$css->load_template('high_compression');
369			break;
370
371			case 0:
372			$css->load_template('low_compression');
373			break;
374		}
375	}
376
377	if ($url) {
378		if (substr($_REQUEST['url'],0,7) !== 'http://') {
379			$_REQUEST['url'] = 'http://'.$_REQUEST['url'];
380		}
381		$result = $css->parse_from_url($_REQUEST['url'],0);
382	} elseif (isset($_REQUEST['css_text']) && strlen($_REQUEST['css_text'])>5) {
383		$result = $css->parse($_REQUEST['css_text']);
384	}
385
386	if ($result) {
387		$ratio = $css->print->get_ratio();
388		$diff = $css->print->get_diff();
389		if (isset($_REQUEST['file_output'])) {
390			$filename = md5(mt_rand().time().mt_rand());
391			if (!is_dir('temp')) {
392				$madedir = mkdir('temp');
393				if (!$madedir) {
394					print 'Could not make directory "temp" in '.dirname(__FILE__);
395					exit;
396				}
397			}
398			$handle = fopen('temp/'.$filename.'.css','w');
399			if ($handle) {
400				if (fwrite($handle,$css->print->plain())) {
401					$file_ok = true;
402				}
403			}
404			fclose($handle);
405		}
406		if ($ratio>0) $ratio = '<span style="color:green">'.$ratio.'%</span>
407	('.$diff.' Bytes)'; else $ratio = '<span
408	style="color:red;">'.$ratio.'%</span> ('.$diff.' Bytes)';
409		if (count($css->log) > 0): ?>
410		<fieldset id="messages"><legend>Messages</legend>
411			<div><dl><?php
412			foreach ($css->log as $line => $array) {
413				echo '<dt>',$line,'</dt>';
414				$array_size = count($array);
415				for ($i = 0; $i < $array_size; ++$i) {
416					echo '<dd class="',$array[$i]['t'],'">',$array[$i]['m'],'</dd>';
417				}
418			}
419			?></dl></div>
420		</fieldset>
421		<?php endif;
422		echo '<fieldset><legend>',$lang[$l][37],': ',$css->print->size('input'),'KB, ',$lang[$l][38],':',$css->print->size('output'),'KB, ',$lang[$l][36],': ',$ratio;
423		if ($file_ok) {
424			echo ' - <a href="temp/',$filename,'.css">Download</a>';
425		}
426		echo ' - <a href="javascript:ClipBoard()">',$lang[$l][58],'</a>'
427		, '</legend>'
428		, '<code id="copytext">'
429		, $css->print->formatted()
430		, '</code></fieldset><div><br /></div>'
431
432		, '<fieldset class="code_output"><legend>',$lang[$l][64],'</legend>'
433		, '<textarea rows="10" cols="80">';
434
435		if (isset($_REQUEST['whole_file'])) {
436			echo htmlspecialchars($css->print->formatted_page('xhtml1.1', false, '', 'en'), ENT_QUOTES, 'utf-8');
437		} else {
438			echo htmlspecialchars('<code id="copytext">', ENT_QUOTES, 'utf-8'),"\n";
439			echo htmlspecialchars($css->print->formatted()."\n".'</code>', ENT_QUOTES, 'utf-8');
440		}
441		echo '</textarea></fieldset>'
442		, '<fieldset class="code_output"><legend>',$lang[$l][65],'</legend>'
443		, '<textarea rows="10" cols="30">';
444
445		echo file_get_contents('cssparsed.css');
446		echo '</textarea>'
447
448		, '</fieldset><p><a href="javascript:scrollTo(0,0)">&#8593; ',$lang[$l][59],'</a></p>';
449
450	 } elseif (isset($_REQUEST['css_text']) || isset($_REQUEST['url'])) {
451		echo '<p class="important">',$lang[$l][28],'</p>';
452	 }
453	 ?>
454	<p style="text-align:center;font-size:.8em;clear:both">
455		<?php echo $lang[$l][61] ?> <a
456		href="http://csstidy.sourceforge.net/contact.php"><?php echo $lang[$l][62] ?></a>.
457	</p>
458	</body>
459</html>