PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/phpfreechat/lib/csstidy-1.2/css_optimiser.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 506 lines | 455 code | 45 blank | 6 comment | 79 complexity | b7f4bf02d5985e9e9c49c51bc3e83433 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. header('Content-Type:text/html; charset=utf-8');
  3. require ('class.csstidy.php');
  4. require ('lang.inc.php');
  5. if (isset($_REQUEST['css_text']) && get_magic_quotes_gpc())
  6. {
  7. $_REQUEST['css_text'] = stripslashes($_REQUEST['css_text']);
  8. }
  9. function rmdirr($dirname, $oc = 0)
  10. {
  11. // Sanity check
  12. if (! file_exists($dirname))
  13. {
  14. return false;
  15. }
  16. // Simple delete for a file
  17. if (is_file($dirname) && (time() - fileatime($dirname)) > 3600)
  18. {
  19. return unlink($dirname);
  20. }
  21. // Loop through the folder
  22. if (is_dir($dirname))
  23. {
  24. $dir = dir($dirname);
  25. while (false !== $entry = $dir->read())
  26. {
  27. // Skip pointers
  28. if ($entry == '.' || $entry == '..')
  29. {
  30. continue;
  31. }
  32. // Recurse
  33. rmdirr("$dirname/$entry", $oc);
  34. }
  35. $dir->close();
  36. }
  37. // Clean up
  38. if ($oc == 1)
  39. {
  40. return rmdir($dirname);
  41. }
  42. }
  43. function options($options, $selected = null, $labelIsValue = false)
  44. {
  45. $html = '';
  46. settype($selected, 'array');
  47. settype($options, 'array');
  48. foreach ($options as $value => $label)
  49. {
  50. if (is_array($label))
  51. {
  52. $value = $label[0];
  53. $label = $label[1];
  54. }
  55. $label = htmlspecialchars($label, ENT_QUOTES, "utf-8");
  56. $value = $labelIsValue ? $label : htmlspecialchars($value, ENT_QUOTES, "utf-8");
  57. $html .= '<option value="' . $value . '"';
  58. if (in_array($value, $selected))
  59. {
  60. $html .= ' selected="selected"';
  61. }
  62. $html .= '>' . $label . '</option>';
  63. }
  64. if (! $html)
  65. {
  66. $html .= '<option value="0">---</option>';
  67. }
  68. return $html;
  69. }
  70. $css = new csstidy();
  71. if (isset($_REQUEST['custom']) && ! empty($_REQUEST['custom']))
  72. {
  73. setcookie('custom_template', $_REQUEST['custom'], time() + 360000);
  74. }
  75. rmdirr('temp');
  76. if (isset($_REQUEST['case_properties']))
  77. $css->set_cfg('case_properties', $_REQUEST['case_properties']);
  78. if (isset($_REQUEST['lowercase']))
  79. $css->set_cfg('lowercase_s', true);
  80. if (! isset($_REQUEST['compress_c']) && isset($_REQUEST['post']))
  81. $css->set_cfg('compress_colors', false);
  82. if (! isset($_REQUEST['compress_fw']) && isset($_REQUEST['post']))
  83. $css->set_cfg('compress_font-weight', false);
  84. if (isset($_REQUEST['merge_selectors']))
  85. $css->set_cfg('merge_selectors', $_REQUEST['merge_selectors']);
  86. if (isset($_REQUEST['optimise_shorthands']))
  87. $css->set_cfg('optimise_shorthands', $_REQUEST['optimise_shorthands']);
  88. if (! isset($_REQUEST['rbs']) && isset($_REQUEST['post']))
  89. $css->set_cfg('remove_bslash', false);
  90. if (isset($_REQUEST['preserve_css']))
  91. $css->set_cfg('preserve_css', true);
  92. if (isset($_REQUEST['sort_sel']))
  93. $css->set_cfg('sort_selectors', true);
  94. if (isset($_REQUEST['sort_de']))
  95. $css->set_cfg('sort_properties', true);
  96. if (isset($_REQUEST['remove_last_sem']))
  97. $css->set_cfg('remove_last_;', true);
  98. if (isset($_REQUEST['discard']))
  99. $css->set_cfg('discard_invalid_properties', true);
  100. if (isset($_REQUEST['css_level']))
  101. $css->set_cfg('css_level', $_REQUEST['css_level']);
  102. if (isset($_REQUEST['timestamp']))
  103. $css->set_cfg('timestamp', true);
  104. ?>
  105. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  106. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  107. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  108. <head>
  109. <title>
  110. <?php
  111. echo $lang[$l][0];
  112. echo $css->version;
  113. ?>)
  114. </title>
  115. <meta http-equiv="Content-Type"
  116. content="application/xhtml+xml; charset=utf-8" />
  117. <link rel="stylesheet" href="cssparse.css" type="text/css" />
  118. <script type="text/javascript">
  119. function enable_disable_preserve()
  120. {
  121. var inputs = new Array('sort_sel', 'sort_de', 'optimise_shorthands', 'merge_selectors', 'none');
  122. var inputs_v = new Array( true, true, true, true, false);
  123. for(var i = 0; i < inputs.length; i++)
  124. {
  125. if(document.getElementById('preserve_css').checked) {
  126. document.getElementById(inputs[i]).disabled = inputs_v[i];
  127. } else {
  128. document.getElementById(inputs[i]).disabled = !inputs_v[i];
  129. }
  130. }
  131. }
  132. </script>
  133. </head>
  134. <body onload="enable_disable_preserve()">
  135. <div>
  136. <h1 style="display: inline">
  137. <?php
  138. echo $lang[$l][1];
  139. ?>
  140. </h1>
  141. <?php
  142. echo $lang[$l][2];
  143. ?> <a
  144. href="http://csstidy.sourceforge.net/">csstidy</a> <?php
  145. echo $css->version;
  146. ?>)
  147. </div>
  148. <p>
  149. <?php
  150. echo $lang[$l][39];
  151. ?>: <a hreflang="en" href="?lang=en">English</a>
  152. <a hreflang="de" href="?lang=de">Deutsch</a> <a hreflang="fr"
  153. href="?lang=fr">French</a> <a hreflang="zh" href="?lang=zh">Chinese</a></p>
  154. <p><?php
  155. echo $lang[$l][4];
  156. ?>
  157. <?php
  158. echo $lang[$l][6];
  159. ?>
  160. </p>
  161. <form method="post" action="">
  162. <div>
  163. <fieldset id="field_input"><legend><?php
  164. echo $lang[$l][8];
  165. ?></legend>
  166. <label for="css_text" class="block"><?php
  167. echo $lang[$l][9];
  168. ?></label><textarea
  169. id="css_text" name="css_text" rows="20" cols="35"><?php
  170. if (isset($_REQUEST['css_text']))
  171. echo htmlspecialchars($_REQUEST['css_text']);
  172. ?></textarea>
  173. <label for="url"><?php
  174. echo $lang[$l][10];
  175. ?></label> <input type="text"
  176. name="url" id="url"
  177. <?php
  178. if (isset($_REQUEST['url']) && ! empty($_REQUEST['url']))
  179. echo 'value="' . $_REQUEST['url'] . '"';
  180. ?>
  181. size="35" /><br />
  182. <input type="submit" value="<?php
  183. echo $lang[$l][35];
  184. ?>" id="submit" />
  185. </fieldset>
  186. <div id="rightcol">
  187. <fieldset id="code_layout"><legend><?php
  188. echo $lang[$l][11];
  189. ?></legend>
  190. <label for="template" class="block"><?php
  191. echo $lang[$l][12];
  192. ?></label>
  193. <select id="template" name="template" style="margin-bottom: 1em;">
  194. <?php
  195. $num = ($_REQUEST['template']) ? intval($_REQUEST['template']) : 1;
  196. echo options(array(3 => $lang[$l][13], 2 => $lang[$l][14], 1 => $lang[$l][15], 0 => $lang[$l][16],
  197. 4 => $lang[$l][17]), $num);
  198. ?>
  199. </select><br />
  200. <label for="custom" class="block">
  201. <?php
  202. echo $lang[$l][18];
  203. ?> </label> <textarea id="custom"
  204. name="custom" cols="33" rows="4"><?php
  205. if (isset($_REQUEST['custom']) && ! empty($_REQUEST['custom']))
  206. echo htmlspecialchars($_REQUEST['custom']);
  207. elseif (isset($_COOKIE['custom_template']) && ! empty($_COOKIE['custom_template']))
  208. echo htmlspecialchars($_COOKIE['custom_template']);
  209. ?></textarea></fieldset>
  210. <fieldset id="options"><legend><?php
  211. echo $lang[$l][19];
  212. ?></legend> <input
  213. onchange="enable_disable_preserve()" type="checkbox"
  214. name="preserve_css" id="preserve_css"
  215. <?php
  216. if ($css->get_cfg('preserve_css'))
  217. echo 'checked="checked"';
  218. ?> />
  219. <label for="preserve_css" title="<?php
  220. echo $lang[$l][52];
  221. ?>"
  222. class="help"><?php
  223. echo $lang[$l][51];
  224. ?></label><br />
  225. <input type="checkbox" name="sort_sel" id="sort_sel"
  226. <?php
  227. if ($css->get_cfg('sort_selectors'))
  228. echo 'checked="checked"';
  229. ?> />
  230. <label for="sort_sel" title="<?php
  231. echo $lang[$l][41];
  232. ?>" class="help"><?php
  233. echo $lang[$l][20];
  234. ?></label><br />
  235. <input type="checkbox" name="sort_de" id="sort_de"
  236. <?php
  237. if ($css->get_cfg('sort_properties'))
  238. echo 'checked="checked"';
  239. ?> />
  240. <label for="sort_de"><?php
  241. echo $lang[$l][21];
  242. ?></label><br />
  243. <label for="merge_selectors"><?php
  244. echo $lang[$l][22];
  245. ?></label> <select
  246. style="width: 15em;" name="merge_selectors" id="merge_selectors">
  247. <?php
  248. echo options(array('0' => $lang[$l][47], '1' => $lang[$l][48], '2' => $lang[$l][49]), $css->get_cfg('merge_selectors'));
  249. ?>
  250. </select><br />
  251. <label for="optimise_shorthands"><?php
  252. echo $lang[$l][23];
  253. ?></label> <select
  254. name="optimise_shorthands" id="optimise_shorthands">
  255. <?php
  256. echo options(array($lang[$l][54], $lang[$l][55], $lang[$l][56]), $css->get_cfg('optimise_shorthands'));
  257. ?>
  258. </select><br />
  259. <input type="checkbox" name="compress_c" id="compress_c"
  260. <?php
  261. if ($css->get_cfg('compress_colors'))
  262. echo 'checked="checked"';
  263. ?> />
  264. <label for="compress_c"><?php
  265. echo $lang[$l][24];
  266. ?></label><br />
  267. <input type="checkbox" name="compress_fw" id="compress_fw"
  268. <?php
  269. if ($css->get_cfg('compress_font-weight'))
  270. echo 'checked="checked"';
  271. ?> />
  272. <label for="compress_fw"><?php
  273. echo $lang[$l][45];
  274. ?></label><br />
  275. <input type="checkbox" name="lowercase" id="lowercase" value="lowercase"
  276. <?php
  277. if ($css->get_cfg('lowercase_s'))
  278. echo 'checked="checked"';
  279. ?> />
  280. <label title="<?php
  281. echo $lang[$l][30];
  282. ?>" class="help" for="lowercase"><?php
  283. echo $lang[$l][25];
  284. ?></label><br />
  285. <?php
  286. echo $lang[$l][26];
  287. ?><br />
  288. <input type="radio" name="case_properties" id="none" value="0"
  289. <?php
  290. if ($css->get_cfg('case_properties') == 0)
  291. echo 'checked="checked"';
  292. ?> />
  293. <label for="none"><?php
  294. echo $lang[$l][53];
  295. ?></label> <input
  296. type="radio" name="case_properties" id="lower_yes" value="1"
  297. <?php
  298. if ($css->get_cfg('case_properties') == 1)
  299. echo 'checked="checked"';
  300. ?> />
  301. <label for="lower_yes"><?php
  302. echo $lang[$l][27];
  303. ?></label> <input
  304. type="radio" name="case_properties" id="upper_yes" value="2"
  305. <?php
  306. if ($css->get_cfg('case_properties') == 2)
  307. echo 'checked="checked"';
  308. ?> />
  309. <label for="upper_yes"><?php
  310. echo $lang[$l][29];
  311. ?></label><br />
  312. <input type="checkbox" name="rbs" id="rbs"
  313. <?php
  314. if ($css->get_cfg('remove_bslash'))
  315. echo 'checked="checked"';
  316. ?> />
  317. <label for="rbs"><?php
  318. echo $lang[$l][31];
  319. ?></label><br />
  320. <input type="checkbox" id="remove_last_sem" name="remove_last_sem"
  321. <?php
  322. if ($css->get_cfg('remove_last_;'))
  323. echo 'checked="checked"';
  324. ?> />
  325. <label for="remove_last_sem"><?php
  326. echo $lang[$l][42];
  327. ?></label><br />
  328. <input type="checkbox" id="discard" name="discard"
  329. <?php
  330. if ($css->get_cfg('discard_invalid_properties'))
  331. echo 'checked="checked"';
  332. ?> />
  333. <label for="discard"><?php
  334. echo $lang[$l][43];
  335. ?></label> <select
  336. name="css_level"><?php
  337. echo options(array('CSS2.1', 'CSS2.0', 'CSS1.0'), $css->get_cfg('css_level'), true);
  338. ?></select><br />
  339. <input type="checkbox" id="timestamp" name="timestamp"
  340. <?php
  341. if ($css->get_cfg('timestamp'))
  342. echo 'checked="checked"';
  343. ?> /> <label
  344. for="timestamp"><?php
  345. echo $lang[$l][57];
  346. ?></label><br />
  347. <input type="checkbox" name="file_output" id="file_output"
  348. value="file_output"
  349. <?php
  350. if (isset($_REQUEST['file_output']))
  351. echo 'checked="checked"';
  352. ?> />
  353. <label class="help" title="<?php
  354. echo $lang[$l][34];
  355. ?>"
  356. for="file_output"> <strong><?php
  357. echo $lang[$l][33];
  358. ?></strong> </label><br />
  359. </fieldset>
  360. <input type="hidden" name="post" /></div>
  361. </div>
  362. </form>
  363. <?php
  364. $file_ok = false;
  365. $result = false;
  366. $url = (isset($_REQUEST['url']) && ! empty($_REQUEST['url'])) ? $_REQUEST['url'] : false;
  367. if (isset($_REQUEST['template']))
  368. {
  369. switch ($_REQUEST['template'])
  370. {
  371. case 4 :
  372. if (isset($_REQUEST['custom']) && ! empty($_REQUEST['custom']))
  373. {
  374. $css->load_template($_REQUEST['custom'], false);
  375. }
  376. break;
  377. case 3 :
  378. $css->load_template('highest_compression');
  379. break;
  380. case 2 :
  381. $css->load_template('high_compression');
  382. break;
  383. case 0 :
  384. $css->load_template('low_compression');
  385. break;
  386. }
  387. }
  388. if ($url)
  389. {
  390. if (substr($_REQUEST['url'], 0, 7) != 'http://')
  391. {
  392. $_REQUEST['url'] = 'http://' . $_REQUEST['url'];
  393. }
  394. $result = $css->parse_from_url($_REQUEST['url'], 0);
  395. }
  396. elseif (isset($_REQUEST['css_text']) && strlen($_REQUEST['css_text']) > 5)
  397. {
  398. $result = $css->parse($_REQUEST['css_text']);
  399. }
  400. if ($result)
  401. {
  402. $ratio = $css->print->get_ratio();
  403. $diff = $css->print->get_diff();
  404. if (isset($_REQUEST['file_output']))
  405. {
  406. $filename = md5(mt_rand() . time() . mt_rand());
  407. $handle = fopen('temp/' . $filename . '.css', 'w');
  408. if ($handle)
  409. {
  410. if (fwrite($handle, $css->print->plain()))
  411. {
  412. $file_ok = true;
  413. }
  414. }
  415. fclose($handle);
  416. }
  417. if ($ratio > 0)
  418. $ratio = '<span style="color:green;">' . $ratio . '%</span>
  419. (' . $diff . ' Bytes)';
  420. else
  421. $ratio = '<span
  422. style="color:red;">' . $ratio . '%</span> (' . $diff . ' Bytes)';
  423. if (count($css->log) > 0)
  424. :
  425. ?>
  426. <fieldset id="messages"><legend>Messages</legend>
  427. <div>
  428. <dl><?php
  429. foreach ($css->log as $line => $array)
  430. {
  431. echo '<dt>' . $line . '</dt>';
  432. for($i = 0; $i < count($array); $i ++)
  433. {
  434. echo '<dd class="' . $array[$i]['t'] . '">' . $array[$i]['m'] . '</dd>';
  435. }
  436. }
  437. ?></dl>
  438. </div>
  439. </fieldset>
  440. <?php endif;
  441. echo '<fieldset><legend>' . $lang[$l][37] . ': ' . $css->print->size('input') . 'KB, ' . $lang[$l][38] . ':' . $css->print->size('output') . 'KB, ' . $lang[$l][36] . ': ' . $ratio;
  442. if ($file_ok)
  443. {
  444. echo ' - <a href="temp/' . $filename . '.css">Download</a>';
  445. }
  446. echo '</legend>';
  447. echo '<pre><code>';
  448. echo $css->print->formatted();
  449. echo '</code></pre>';
  450. echo '</fieldset>';
  451. }
  452. elseif (isset($_REQUEST['css_text']) || isset($_REQUEST['url']))
  453. {
  454. echo '<p class="important">' . $lang[$l][28] . '</p>';
  455. }
  456. ?>
  457. <p style="text-align: center; font-size: 0.8em; clear: both;">For
  458. bugs and suggestions feel free to <a
  459. href="http://csstidy.sourceforge.net/contact.php">contact me</a>.</p>
  460. </body>
  461. </html>