This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * JSMin.php - modified PHP implementation of Douglas Crockford's JSMin. |
||
4 | * |
||
5 | * <code> |
||
6 | * $minifiedJs = JSMin::minify($js); |
||
7 | * </code> |
||
8 | * |
||
9 | * This is a modified port of jsmin.c. Improvements: |
||
10 | * |
||
11 | * Does not choke on some regexp literals containing quote characters. E.g. /'/ |
||
12 | * |
||
13 | * Spaces are preserved after some add/sub operators, so they are not mistakenly |
||
14 | * converted to post-inc/dec. E.g. a + ++b -> a+ ++b |
||
15 | * |
||
16 | * Preserves multi-line comments that begin with /*! |
||
17 | * |
||
18 | * PHP 5 or higher is required. |
||
19 | * |
||
20 | * Permission is hereby granted to use this version of the library under the |
||
21 | * same terms as jsmin.c, which has the following license: |
||
22 | * |
||
23 | * -- |
||
24 | * Copyright (c) 2002 Douglas Crockford (www.crockford.com) |
||
25 | * |
||
26 | * Permission is hereby granted, free of charge, to any person obtaining a copy of |
||
27 | * this software and associated documentation files (the "Software"), to deal in |
||
28 | * the Software without restriction, including without limitation the rights to |
||
29 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
||
30 | * of the Software, and to permit persons to whom the Software is furnished to do |
||
31 | * so, subject to the following conditions: |
||
32 | * |
||
33 | * The above copyright notice and this permission notice shall be included in all |
||
34 | * copies or substantial portions of the Software. |
||
35 | * |
||
36 | * The Software shall be used for Good, not Evil. |
||
37 | * |
||
38 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||
39 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||
40 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||
41 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||
42 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||
43 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||
44 | * SOFTWARE. |
||
45 | * -- |
||
46 | * |
||
47 | * @package JSMin |
||
48 | * @author Ryan Grove <[email protected]> (PHP port) |
||
49 | * @author Steve Clay <[email protected]> (modifications + cleanup) |
||
50 | * @author Andrea Giammarchi <http://www.3site.eu> (spaceBeforeRegExp) |
||
51 | * @copyright 2002 Douglas Crockford <[email protected]> (jsmin.c) |
||
52 | * @copyright 2008 Ryan Grove <[email protected]> (PHP port) |
||
53 | * @license http://opensource.org/licenses/mit-license.php MIT License |
||
54 | * @link http://code.google.com/p/jsmin-php/ |
||
55 | */ |
||
56 | |||
57 | // This is from https://github.com/mrclay/jsmin-php 2.3.2 |
||
58 | |||
59 | class JSMin { |
||
60 | const ORD_LF = 10; |
||
61 | const ORD_SPACE = 32; |
||
62 | const ACTION_KEEP_A = 1; |
||
63 | const ACTION_DELETE_A = 2; |
||
64 | const ACTION_DELETE_A_B = 3; |
||
65 | |||
66 | protected $a = "\n"; |
||
67 | protected $b = ''; |
||
68 | protected $input = ''; |
||
69 | protected $inputIndex = 0; |
||
70 | protected $inputLength = 0; |
||
71 | protected $lookAhead = null; |
||
72 | protected $output = ''; |
||
73 | protected $lastByteOut = ''; |
||
74 | protected $keptComment = ''; |
||
75 | |||
76 | /** |
||
77 | * Minify Javascript. |
||
78 | * |
||
79 | * @param string $js Javascript to be minified |
||
80 | * |
||
81 | * @return string |
||
82 | */ |
||
83 | public static function minify($js) |
||
84 | { |
||
85 | $jsmin = new JSMin($js); |
||
86 | return $jsmin->min(); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param string $input |
||
91 | */ |
||
92 | public function __construct($input) |
||
93 | { |
||
94 | $this->input = $input; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Perform minification, return result |
||
99 | * |
||
100 | * @return string |
||
101 | */ |
||
102 | public function min() |
||
103 | { |
||
104 | if ($this->output !== '') { // min already run |
||
105 | return $this->output; |
||
106 | } |
||
107 | |||
108 | $mbIntEnc = null; |
||
109 | if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) { |
||
110 | $mbIntEnc = mb_internal_encoding(); |
||
111 | mb_internal_encoding('8bit'); |
||
112 | } |
||
113 | |||
114 | if (isset($this->input[0]) && $this->input[0] === "\xef") { |
||
115 | $this->input = substr($this->input, 3); |
||
116 | } |
||
117 | |||
118 | $this->input = str_replace("\r\n", "\n", $this->input); |
||
119 | $this->inputLength = strlen($this->input); |
||
120 | |||
121 | $this->action(self::ACTION_DELETE_A_B); |
||
122 | |||
123 | while ($this->a !== null) { |
||
124 | // determine next command |
||
125 | $command = self::ACTION_KEEP_A; // default |
||
126 | if ($this->a === ' ') { |
||
127 | if (($this->lastByteOut === '+' || $this->lastByteOut === '-') |
||
128 | && ($this->b === $this->lastByteOut)) { |
||
129 | // Don't delete this space. If we do, the addition/subtraction |
||
130 | // could be parsed as a post-increment |
||
131 | } elseif (! $this->isAlphaNum($this->b)) { |
||
132 | $command = self::ACTION_DELETE_A; |
||
133 | } |
||
134 | } elseif ($this->a === "\n") { |
||
135 | if ($this->b === ' ') { |
||
136 | $command = self::ACTION_DELETE_A_B; |
||
137 | |||
138 | // in case of mbstring.func_overload & 2, must check for null b, |
||
139 | // otherwise mb_strpos will give WARNING |
||
140 | } elseif ($this->b === null |
||
141 | || (false === strpos('{[(+-!~', $this->b) |
||
142 | && ! $this->isAlphaNum($this->b))) { |
||
143 | $command = self::ACTION_DELETE_A; |
||
144 | } |
||
145 | } elseif (! $this->isAlphaNum($this->a)) { |
||
146 | if ($this->b === ' ' |
||
147 | || ($this->b === "\n" |
||
148 | && (false === strpos('}])+-"\'', $this->a)))) { |
||
149 | $command = self::ACTION_DELETE_A_B; |
||
150 | } |
||
151 | } |
||
152 | $this->action($command); |
||
153 | } |
||
154 | $this->output = trim($this->output); |
||
155 | |||
156 | if ($mbIntEnc !== null) { |
||
157 | mb_internal_encoding($mbIntEnc); |
||
158 | } |
||
159 | return $this->output; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * ACTION_KEEP_A = Output A. Copy B to A. Get the next B. |
||
164 | * ACTION_DELETE_A = Copy B to A. Get the next B. |
||
165 | * ACTION_DELETE_A_B = Get the next B. |
||
166 | * |
||
167 | * @param int $command |
||
168 | * @throws JSMin_UnterminatedRegExpException|JSMin_UnterminatedStringException |
||
169 | */ |
||
170 | protected function action($command) |
||
171 | { |
||
172 | // make sure we don't compress "a + ++b" to "a+++b", etc. |
||
173 | if ($command === self::ACTION_DELETE_A_B |
||
174 | && $this->b === ' ' |
||
175 | && ($this->a === '+' || $this->a === '-')) { |
||
176 | // Note: we're at an addition/substraction operator; the inputIndex |
||
177 | // will certainly be a valid index |
||
178 | if ($this->input[$this->inputIndex] === $this->a) { |
||
179 | // This is "+ +" or "- -". Don't delete the space. |
||
180 | $command = self::ACTION_KEEP_A; |
||
181 | } |
||
182 | } |
||
183 | |||
184 | switch ($command) { |
||
185 | case self::ACTION_KEEP_A: // 1 |
||
186 | $this->output .= $this->a; |
||
187 | |||
188 | if ($this->keptComment) { |
||
189 | $this->output = rtrim($this->output, "\n"); |
||
190 | $this->output .= $this->keptComment; |
||
191 | $this->keptComment = ''; |
||
192 | } |
||
193 | |||
194 | $this->lastByteOut = $this->a; |
||
195 | |||
196 | // fallthrough intentional |
||
197 | case self::ACTION_DELETE_A: // 2 |
||
198 | $this->a = $this->b; |
||
199 | if ($this->a === "'" || $this->a === '"' || $this->a === '`') { // string/template literal |
||
200 | $delimiter = $this->a; |
||
201 | $str = $this->a; // in case needed for exception |
||
202 | for(;;) { |
||
203 | $this->output .= $this->a; |
||
204 | $this->lastByteOut = $this->a; |
||
205 | |||
206 | $this->a = $this->get(); |
||
207 | if ($this->a === $this->b) { // end quote |
||
208 | break; |
||
209 | } |
||
210 | if ($delimiter === '`' && $this->a === "\n") { |
||
211 | // leave the newline |
||
212 | } elseif ($this->isEOF($this->a)) { |
||
213 | $byte = $this->inputIndex - 1; |
||
214 | throw new JSMin_UnterminatedStringException( |
||
215 | "JSMin: Unterminated String at byte {$byte}: {$str}"); |
||
216 | } |
||
217 | $str .= $this->a; |
||
218 | if ($this->a === '\\') { |
||
219 | $this->output .= $this->a; |
||
220 | $this->lastByteOut = $this->a; |
||
221 | |||
222 | $this->a = $this->get(); |
||
223 | $str .= $this->a; |
||
224 | } |
||
225 | } |
||
226 | } |
||
227 | |||
228 | // fallthrough intentional |
||
229 | case self::ACTION_DELETE_A_B: // 3 |
||
230 | $this->b = $this->next(); |
||
231 | if ($this->b === '/' && $this->isRegexpLiteral()) { |
||
232 | $this->output .= $this->a . $this->b; |
||
233 | $pattern = '/'; // keep entire pattern in case we need to report it in the exception |
||
234 | for(;;) { |
||
235 | $this->a = $this->get(); |
||
236 | $pattern .= $this->a; |
||
237 | if ($this->a === '[') { |
||
238 | for(;;) { |
||
239 | $this->output .= $this->a; |
||
240 | $this->a = $this->get(); |
||
241 | $pattern .= $this->a; |
||
242 | if ($this->a === ']') { |
||
243 | break; |
||
244 | } |
||
245 | View Code Duplication | if ($this->a === '\\') { |
|
0 ignored issues
–
show
|
|||
246 | $this->output .= $this->a; |
||
247 | $this->a = $this->get(); |
||
248 | $pattern .= $this->a; |
||
249 | } |
||
250 | if ($this->isEOF($this->a)) { |
||
251 | throw new JSMin_UnterminatedRegExpException( |
||
252 | "JSMin: Unterminated set in RegExp at byte " |
||
253 | . $this->inputIndex .": {$pattern}"); |
||
254 | } |
||
255 | } |
||
256 | } |
||
257 | |||
258 | if ($this->a === '/') { // end pattern |
||
259 | break; // while (true) |
||
260 | View Code Duplication | } elseif ($this->a === '\\') { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
261 | $this->output .= $this->a; |
||
262 | $this->a = $this->get(); |
||
263 | $pattern .= $this->a; |
||
264 | } elseif ($this->isEOF($this->a)) { |
||
265 | $byte = $this->inputIndex - 1; |
||
266 | throw new JSMin_UnterminatedRegExpException( |
||
267 | "JSMin: Unterminated RegExp at byte {$byte}: {$pattern}"); |
||
268 | } |
||
269 | $this->output .= $this->a; |
||
270 | $this->lastByteOut = $this->a; |
||
271 | } |
||
272 | $this->b = $this->next(); |
||
273 | } |
||
274 | // end case ACTION_DELETE_A_B |
||
275 | } |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * @return bool |
||
280 | */ |
||
281 | protected function isRegexpLiteral() |
||
282 | { |
||
283 | if (false !== strpos("(,=:[!&|?+-~*{;", $this->a)) { |
||
284 | // we can't divide after these tokens |
||
285 | return true; |
||
286 | } |
||
287 | |||
288 | // check if first non-ws token is "/" (see starts-regex.js) |
||
289 | $length = strlen($this->output); |
||
290 | if ($this->a === ' ' || $this->a === "\n") { |
||
291 | if ($length < 2) { // weird edge case |
||
292 | return true; |
||
293 | } |
||
294 | } |
||
295 | |||
296 | // if the "/" follows a keyword, it must be a regexp, otherwise it's best to assume division |
||
297 | |||
298 | $subject = $this->output . trim($this->a); |
||
299 | if (!preg_match('/(?:case|else|in|return|typeof)$/', $subject, $m)) { |
||
300 | // not a keyword |
||
301 | return false; |
||
302 | } |
||
303 | |||
304 | // can't be sure it's a keyword yet (see not-regexp.js) |
||
305 | $charBeforeKeyword = substr($subject, 0 - strlen($m[0]) - 1, 1); |
||
306 | if ($this->isAlphaNum($charBeforeKeyword)) { |
||
307 | // this is really an identifier ending in a keyword, e.g. "xreturn" |
||
308 | return false; |
||
309 | } |
||
310 | |||
311 | // it's a regexp. Remove unneeded whitespace after keyword |
||
312 | if ($this->a === ' ' || $this->a === "\n") { |
||
313 | $this->a = ''; |
||
314 | } |
||
315 | |||
316 | return true; |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * Return the next character from stdin. Watch out for lookahead. If the character is a control character, |
||
321 | * translate it to a space or linefeed. |
||
322 | * |
||
323 | * @return string |
||
324 | */ |
||
325 | protected function get() |
||
326 | { |
||
327 | $c = $this->lookAhead; |
||
328 | $this->lookAhead = null; |
||
329 | if ($c === null) { |
||
330 | // getc(stdin) |
||
331 | if ($this->inputIndex < $this->inputLength) { |
||
332 | $c = $this->input[$this->inputIndex]; |
||
333 | $this->inputIndex += 1; |
||
334 | } else { |
||
335 | $c = null; |
||
336 | } |
||
337 | } |
||
338 | if (ord($c) >= self::ORD_SPACE || $c === "\n" || $c === null) { |
||
339 | return $c; |
||
340 | } |
||
341 | if ($c === "\r") { |
||
342 | return "\n"; |
||
343 | } |
||
344 | return ' '; |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * Does $a indicate end of input? |
||
349 | * |
||
350 | * @param string $a |
||
351 | * @return bool |
||
352 | */ |
||
353 | protected function isEOF($a) |
||
354 | { |
||
355 | return ord($a) <= self::ORD_LF; |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * Get next char (without getting it). If is ctrl character, translate to a space or newline. |
||
360 | * |
||
361 | * @return string |
||
362 | */ |
||
363 | protected function peek() |
||
364 | { |
||
365 | $this->lookAhead = $this->get(); |
||
366 | return $this->lookAhead; |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * Return true if the character is a letter, digit, underscore, dollar sign, or non-ASCII character. |
||
371 | * |
||
372 | * @param string $c |
||
373 | * |
||
374 | * @return bool |
||
375 | */ |
||
376 | protected function isAlphaNum($c) |
||
377 | { |
||
378 | return (preg_match('/^[a-z0-9A-Z_\\$\\\\]$/', $c) || ord($c) > 126); |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * Consume a single line comment from input (possibly retaining it) |
||
383 | */ |
||
384 | protected function consumeSingleLineComment() |
||
385 | { |
||
386 | $comment = ''; |
||
387 | while (true) { |
||
388 | $get = $this->get(); |
||
389 | $comment .= $get; |
||
390 | if (ord($get) <= self::ORD_LF) { // end of line reached |
||
391 | // if IE conditional comment |
||
392 | if (preg_match('/^\\/@(?:cc_on|if|elif|else|end)\\b/', $comment)) { |
||
393 | $this->keptComment .= "/{$comment}"; |
||
394 | } |
||
395 | return; |
||
396 | } |
||
397 | } |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * Consume a multiple line comment from input (possibly retaining it) |
||
402 | * |
||
403 | * @throws JSMin_UnterminatedCommentException |
||
404 | */ |
||
405 | protected function consumeMultipleLineComment() |
||
406 | { |
||
407 | $this->get(); |
||
408 | $comment = ''; |
||
409 | for(;;) { |
||
410 | $get = $this->get(); |
||
411 | if ($get === '*') { |
||
412 | if ($this->peek() === '/') { // end of comment reached |
||
413 | $this->get(); |
||
414 | if (0 === strpos($comment, '!')) { |
||
415 | // preserved by YUI Compressor |
||
416 | if (!$this->keptComment) { |
||
417 | // don't prepend a newline if two comments right after one another |
||
418 | $this->keptComment = "\n"; |
||
419 | } |
||
420 | $this->keptComment .= "/*!" . substr($comment, 1) . "*/\n"; |
||
421 | } else if (preg_match('/^@(?:cc_on|if|elif|else|end)\\b/', $comment)) { |
||
422 | // IE conditional |
||
423 | $this->keptComment .= "/*{$comment}*/"; |
||
424 | } |
||
425 | return; |
||
426 | } |
||
427 | } elseif ($get === null) { |
||
428 | throw new JSMin_UnterminatedCommentException( |
||
429 | "JSMin: Unterminated comment at byte {$this->inputIndex}: /*{$comment}"); |
||
430 | } |
||
431 | $comment .= $get; |
||
432 | } |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * Get the next character, skipping over comments. Some comments may be preserved. |
||
437 | * |
||
438 | * @return string |
||
439 | */ |
||
440 | protected function next() |
||
441 | { |
||
442 | $get = $this->get(); |
||
443 | if ($get === '/') { |
||
444 | switch ($this->peek()) { |
||
445 | case '/': |
||
446 | $this->consumeSingleLineComment(); |
||
447 | $get = "\n"; |
||
448 | break; |
||
449 | case '*': |
||
450 | $this->consumeMultipleLineComment(); |
||
451 | $get = ' '; |
||
452 | break; |
||
453 | } |
||
454 | } |
||
455 | return $get; |
||
456 | } |
||
457 | } |
||
458 | |||
459 | class JSMin_UnterminatedStringException extends Exception {} |
||
460 | class JSMin_UnterminatedCommentException extends Exception {} |
||
461 | class JSMin_UnterminatedRegExpException extends Exception {} |
||
462 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.