PageRenderTime 86ms CodeModel.GetById 46ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/custom-css/csstidy/css_optimiser.php

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