jsminifier.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php namespace HashOver;
  2. // Copyright (C) 2015 Jacob Barkdull
  3. // This file is part of HashOver.
  4. //
  5. // HashOver is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as
  7. // published by the Free Software Foundation, either version 3 of the
  8. // License, or (at your option) any later version.
  9. //
  10. // HashOver is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with HashOver. If not, see <http://www.gnu.org/licenses/>.
  17. // Display source code
  18. if (basename ($_SERVER['PHP_SELF']) === basename (__FILE__)) {
  19. if (isset ($_GET['source'])) {
  20. header ('Content-type: text/plain; charset=UTF-8');
  21. exit (file_get_contents (basename (__FILE__)));
  22. } else {
  23. exit ('<b>HashOver</b>: This is a class file.');
  24. }
  25. }
  26. class JSMinifier
  27. {
  28. // Array for locking minification
  29. protected $lock = array (
  30. 'status' => false,
  31. 'char' => ''
  32. );
  33. // JavaScript minification function
  34. public function minify ($js, $level = 4)
  35. {
  36. if ($level <= 0) {
  37. return $js;
  38. }
  39. if ($level >= 1) {
  40. // Remove single-line code comments
  41. $js = preg_replace ('/^[\t ]*?\/\/.*\s?/m', '', $js);
  42. // Remove end-of-line code comments
  43. $js = preg_replace ('/([\s;})]+)\/\/.*/m', '\\1', $js);
  44. // Remove multi-line code comments
  45. $js = preg_replace ('/\/\*[\s\S]*?\*\//', '', $js);
  46. }
  47. if ($level >= 2) {
  48. // Remove whitespace
  49. $js = preg_replace ('/^\s*/m', '', $js);
  50. // Replace multiple tabs with a single space
  51. $js = preg_replace ('/\t+/m', ' ', $js);
  52. }
  53. if ($level >= 3) {
  54. // Remove newlines
  55. $js = preg_replace ('/[\r\n]+/', '', $js);
  56. }
  57. if ($level >= 4) {
  58. // Split input JavaScript by single and double quotes
  59. $js_substrings = preg_split ('/([\'"])/', $js, -1, PREG_SPLIT_DELIM_CAPTURE);
  60. // Empty variable for minified JavaScript
  61. $js = '';
  62. foreach ($js_substrings as $substring) {
  63. // Check if substring is split delimiter
  64. if ($substring === '\'' or $substring === '"') {
  65. // If so, check whether minification is unlocked
  66. if ($this->lock['status'] === false) {
  67. // If so, lock it and set lock character
  68. $this->lock['status'] = true;
  69. $this->lock['char'] = $substring;
  70. } else {
  71. // If not, check if substring is lock character
  72. if ($substring === $this->lock['char']) {
  73. // If so, unlock minification
  74. $this->lock['status'] = false;
  75. $this->lock['char'] = '';
  76. }
  77. }
  78. // Add substring to minified output
  79. $js .= $substring;
  80. continue;
  81. }
  82. // Minify current substring if minification is unlocked
  83. if ($this->lock['status'] === false) {
  84. // Remove unnecessary semicolons
  85. $substring = str_replace (';}', '}', $substring);
  86. // Remove spaces round operators
  87. $substring = preg_replace ('/ *([<>=+\-!\|{(},;&:?]+) */', '\\1', $substring);
  88. }
  89. // Add substring to minified output
  90. $js .= $substring;
  91. }
  92. }
  93. // Get URL add "unminified" URL query
  94. $unminified_url = 'http' . (isset ($_SERVER['HTTPS']) ? 's' : '') . '://';
  95. $unminified_url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  96. $unminified_url .= '&hashover-unminified';
  97. // Copyright notice and URL to unminified code
  98. $copyright = array (
  99. '// Copyright (C) 2015 Jacob Barkdull',
  100. '// Under the terms of the GNU Affero General Public License.',
  101. '//',
  102. '// Non-minified JavaScript:',
  103. '//',
  104. '// ' . $unminified_url . PHP_EOL . PHP_EOL
  105. );
  106. // Return final minified JavaScript
  107. return implode (PHP_EOL, $copyright) . trim ($js);
  108. }
  109. }