PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/application/library/Thirdpart/Minify/min_extras/tools/minifyTextarea.php

https://gitlab.com/flyhope/Hiblog
PHP | 186 lines | 158 code | 18 blank | 10 comment | 27 complexity | 0bc6f5e72478a386232efab4b82ccf3d MD5 | raw file
  1. <?php
  2. die('Disabled: use this only for testing');
  3. require __DIR__ . '/../../bootstrap.php';
  4. function getPost($key) {
  5. return get_magic_quotes_gpc()
  6. ? stripslashes($_POST[$key])
  7. : $_POST[$key];
  8. }
  9. function h($txt) {
  10. return htmlspecialchars($txt, ENT_QUOTES, 'UTF-8');
  11. }
  12. if (isset($_POST['textIn'])) {
  13. require '../config.php';
  14. $textIn = str_replace("\r\n", "\n", getPost('textIn'));
  15. }
  16. if (isset($_POST['method']) && $_POST['method'] === 'Minify and serve') {
  17. $base = trim(getPost('base'));
  18. if ($base) {
  19. $textIn = preg_replace(
  20. '@(<head\\b[^>]*>)@i'
  21. ,'$1<base href="' . h($base) . '" />'
  22. ,$textIn
  23. );
  24. }
  25. $sourceSpec['content'] = $textIn;
  26. $sourceSpec['id'] = 'foo';
  27. if (isset($_POST['minJs'])) {
  28. $sourceSpec['minifyOptions']['jsMinifier'] = array('JSMin\\JSMin', 'minify');
  29. }
  30. if (isset($_POST['minCss'])) {
  31. $sourceSpec['minifyOptions']['cssMinifier'] = array('Minify_CSSmin', 'minify');
  32. }
  33. $source = new Minify_Source($sourceSpec);
  34. Minify_Logger::setLogger(FirePHP::getInstance(true));
  35. $env = new Minify_Env();
  36. $controller = new Minify_Controller_Files($env, new Minify_Source_Factory($env));
  37. $minify = new Minify(new Minify_Cache_Null());
  38. try {
  39. $minify->serve($controller, array(
  40. 'files' => $source
  41. ,'contentType' => Minify::TYPE_HTML
  42. ));
  43. } catch (Exception $e) {
  44. echo h($e->getMessage());
  45. }
  46. exit;
  47. }
  48. $tpl = array();
  49. $tpl['classes'] = array('Minify_HTML', 'JSMin\\JSMin', 'Minify_CSS');
  50. if (isset($_POST['method']) && in_array($_POST['method'], $tpl['classes'])) {
  51. $args = array($textIn);
  52. if ($_POST['method'] === 'Minify_HTML') {
  53. $args[] = array(
  54. 'cssMinifier' => array('Minify_CSSmin', 'minify')
  55. ,'jsMinifier' => array('JSMin\\JSMin', 'minify')
  56. );
  57. }
  58. $func = array($_POST['method'], 'minify');
  59. $tpl['inBytes'] = strlen($textIn);
  60. $startTime = microtime(true);
  61. try {
  62. $tpl['output'] = call_user_func_array($func, $args);
  63. } catch (Exception $e) {
  64. $tpl['exceptionMsg'] = getExceptionMsg($e, $textIn);
  65. $tpl['output'] = $textIn;
  66. sendPage($tpl);
  67. }
  68. $tpl['time'] = microtime(true) - $startTime;
  69. $tpl['outBytes'] = strlen($tpl['output']);
  70. }
  71. sendPage($tpl);
  72. /**
  73. * @param Exception $e
  74. * @param string $input
  75. * @return string HTML
  76. */
  77. function getExceptionMsg(Exception $e, $input) {
  78. $msg = "<p>" . h($e->getMessage()) . "</p>";
  79. if (0 === strpos(get_class($e), 'JSMin_Unterminated')
  80. && preg_match('~byte (\d+)~', $e->getMessage(), $m)) {
  81. $msg .= "<pre>";
  82. if ($m[1] > 200) {
  83. $msg .= h(substr($input, ($m[1] - 200), 200));
  84. } else {
  85. $msg .= h(substr($input, 0, $m[1]));
  86. }
  87. $highlighted = isset($input[$m[1]]) ? h($input[$m[1]]) : '&#9220;';
  88. if ($highlighted === "\n") {
  89. $highlighted = "&#9166;\n";
  90. }
  91. $msg .= "<span style='background:#c00;color:#fff'>$highlighted</span>";
  92. $msg .= h(substr($input, $m[1] + 1, 200)) . "</span></pre>";
  93. }
  94. return $msg;
  95. }
  96. /**
  97. * Draw page
  98. *
  99. * @param array $vars
  100. */
  101. function sendPage($vars) {
  102. header('Content-Type: text/html; charset=utf-8');
  103. ?>
  104. <!DOCTYPE html><head><title>minifyTextarea</title></head>
  105. <p><strong>Warning! Please do not place this application on a public site.</strong> This should be used only for testing.</p>
  106. <?php
  107. if (isset($vars['exceptionMsg'])) {
  108. echo $vars['exceptionMsg'];
  109. }
  110. if (isset($vars['time'])) {
  111. echo "
  112. <table>
  113. <tr><th>Bytes in</th><td>{$vars['inBytes']} (after line endings normalized to <code>\\n</code>)</td></tr>
  114. <tr><th>Bytes out</th><td>{$vars['outBytes']} (reduced " . round(100 - (100 * $vars['outBytes'] / $vars['inBytes'])) . "%)</td></tr>
  115. <tr><th>Time (s)</th><td>" . round($vars['time'], 5) . "</td></tr>
  116. </table>
  117. ";
  118. }
  119. ?>
  120. <form action="?2" method="post">
  121. <p><label>Content<br><textarea name="textIn" cols="80" rows="35" style="width:99%"><?php
  122. if (isset($vars['output'])) {
  123. echo h($vars['output']);
  124. }
  125. ?></textarea></label></p>
  126. <p>Minify with:
  127. <?php foreach ($vars['classes'] as $minClass): ?>
  128. <input type="submit" name="method" value="<?php echo $minClass; ?>">
  129. <?php endForEach; ?>
  130. </p>
  131. <p>...or <input type="submit" name="method" value="Minify and serve"> this HTML to the browser. Also minify:
  132. <label>CSS <input type="checkbox" name="minCss" checked></label> :
  133. <label>JS <input type="checkbox" name="minJs" checked></label>.
  134. <label>Insert BASE element w/ href: <input type="text" name="base" size="20"></label>
  135. </p>
  136. </form>
  137. <?php if (isset($vars['selectByte'])) { ?>
  138. <script>
  139. function selectText(el, begin, end) {
  140. var len = el.value.length;
  141. end = end || len;
  142. if (begin == null) {
  143. el.select();
  144. } else {
  145. if (el.setSelectionRange) {
  146. el.setSelectionRange(begin, end);
  147. } else {
  148. if (el.createTextRange) {
  149. var tr = el.createTextRange()
  150. ,c = "character";
  151. tr.moveStart(c, begin);
  152. tr.moveEnd(c, end - len);
  153. tr.select();
  154. } else {
  155. el.select();
  156. }
  157. }
  158. }
  159. el.focus();
  160. }
  161. window.onload = function () {
  162. var ta = document.querySelector('textarea[name="textIn"]');
  163. selectText(ta, <?= $vars['selectByte'] ?>, <?= ($vars['selectByte'] + 1) ?>);
  164. };
  165. </script>
  166. <?php }
  167. exit;
  168. }