79 private $_script =
'';
80 private $_encoding = 62;
81 private $_fastDecode =
true;
82 private $_specialChars =
false;
84 private $LITERAL_ENCODING = array(
91 public function __construct($_script, $_encoding = 62, $_fastDecode =
true, $_specialChars =
false)
93 $this->_script = $_script .
"\n";
94 if (array_key_exists($_encoding, $this->LITERAL_ENCODING))
95 $_encoding = $this->LITERAL_ENCODING[$_encoding];
96 $this->_encoding = min((
int)$_encoding, 95);
97 $this->_fastDecode = $_fastDecode;
98 $this->_specialChars = $_specialChars;
102 $this->_addParser(
'_basicCompression');
103 if ($this->_specialChars)
104 $this->_addParser(
'_encodeSpecialChars');
105 if ($this->_encoding)
106 $this->_addParser(
'_encodeKeywords');
109 return $this->_pack($this->_script);
113 private function _pack(
$script) {
114 for ($i = 0; isset($this->_parsers[$i]); $i++) {
115 $script = call_user_func(array(&$this,$this->_parsers[$i]),
$script);
121 private $_parsers = array();
122 private function _addParser($parser) {
123 $this->_parsers[] = $parser;
127 private function _basicCompression(
$script) {
130 $parser->escapeChar =
'\\';
132 $parser->add(
'/\'[^\'\\n\\r]*\'/', self::IGNORE);
133 $parser->add(
'/"[^"\\n\\r]*"/', self::IGNORE);
135 $parser->add(
'/\\/\\/[^\\n\\r]*[\\n\\r]/',
' ');
136 $parser->add(
'/\\/\\*[^*]*\\*+([^\\/][^*]*\\*+)*\\//',
' ');
138 $parser->add(
'/\\s+(\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/g?i?)/',
'$2');
139 $parser->add(
'/[^\\w\\x24\\/\'"*)\\?:]\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/g?i?/', self::IGNORE);
141 if ($this->_specialChars) $parser->add(
'/;;;[^\\n\\r]+[\\n\\r]/');
143 $parser->add(
'/\\(;;\\)/', self::IGNORE);
144 $parser->add(
'/;+\\s*([};])/',
'$2');
149 $parser->add(
'/(\\b|\\x24)\\s+(\\b|\\x24)/',
'$2 $3');
150 $parser->add(
'/([+\\-])\\s+([+\\-])/',
'$2 $3');
151 $parser->add(
'/\\s+/',
'');
156 private function _encodeSpecialChars(
$script) {
159 $parser->add(
'/((\\x24+)([a-zA-Z$_]+))(\\d*)/',
160 array(
'fn' =>
'_replace_name')
163 $regexp =
'/\\b_[A-Za-z\\d]\\w*/';
169 $parser->add($regexp,
171 'fn' =>
'_replace_encoded',
178 private function _encodeKeywords(
$script) {
180 if ($this->_encoding > 62)
184 $encode = $this->_getEncoder($this->_encoding);
186 $regexp = ($this->_encoding > 62) ?
'/\\w\\w+/' :
'/\\w+/';
192 $parser->add($regexp,
194 'fn' =>
'_replace_encoded',
207 private function _analyze(
$script, $regexp, $encode) {
214 $_protected = array();
218 $protected = array();
220 $this->_count = array();
221 $i = count(
$all); $j = 0;
225 $word =
'$' .
$all[$i];
226 if (!isset($this->_count[$word])) {
227 $this->_count[$word] = 0;
228 $unsorted[$j] = $word;
232 $values[$j] = call_user_func(array(&$this, $encode), $j);
233 $protected[
'$' . $values[$j]] = $j++;
236 $this->_count[$word]++;
244 $i = count($unsorted);
246 $word = $unsorted[--$i];
247 if (isset($protected[$word]) ) {
248 $_sorted[$protected[$word]] = substr($word, 1);
249 $_protected[$protected[$word]] =
true;
250 $this->_count[$word] = 0;
264 usort($unsorted, array(&$this,
'_sortWords'));
269 if (!isset($_sorted[$i]))
270 $_sorted[$i] = substr($unsorted[$j++], 1);
271 $_encoded[$_sorted[$i]] = $values[$i];
272 }
while (++$i < count($unsorted));
275 'sorted' => $_sorted,
276 'encoded' => $_encoded,
277 'protected' => $_protected);
280 private $_count = array();
281 private function _sortWords($match1, $match2) {
282 return $this->_count[$match2] - $this->_count[$match1];
286 private function _bootStrap($packed,
$keywords) {
287 $ENCODE = $this->_safeRegExp(
'$encode\\($count\\)');
290 $packed =
"'" . $this->_escape($packed) .
"'";
293 $ascii = min(count(
$keywords[
'sorted']), $this->_encoding);
294 if ($ascii == 0) $ascii = 1;
300 foreach (
$keywords[
'protected'] as $i=>$value) {
307 $encode = ($this->_encoding > 62) ?
'_encode95' : $this->_getEncoder($ascii);
308 $encode = $this->_getJSFunction($encode);
309 $encode = preg_replace(
'/_encoding/',
'$ascii', $encode);
310 $encode = preg_replace(
'/arguments\\.callee/',
'$encode', $encode);
311 $inline =
'\\$count' . ($ascii > 10 ?
'.toString(\\$ascii)' :
'');
314 if ($this->_fastDecode) {
316 $decode = $this->_getJSFunction(
'_decodeBody');
317 if ($this->_encoding > 62)
318 $decode = preg_replace(
'/\\\\w/',
'[\\xa1-\\xff]', $decode);
321 $decode = preg_replace($ENCODE, $inline, $decode);
325 $decode = preg_replace($this->_safeRegExp('(
$count)\\s*=\\s*1'), '$1=0', $decode, 1);
329 $unpack = $this->_getJSFunction('_unpack');
330 if ($this->_fastDecode) {
332 $this->buffer = $decode;
333 $unpack = preg_replace_callback(
'/\\{/', array(&$this,
'_insertFastDecode'), $unpack, 1);
335 $unpack = preg_replace(
'/"/',
"'", $unpack);
336 if ($this->_encoding > 62) {
338 $unpack = preg_replace(
'/\'\\\\\\\\b\'\s*\\+|\\+\s*\'\\\\\\\\b\'/',
'', $unpack);
340 if ($ascii > 36 || $this->_encoding > 62 || $this->_fastDecode) {
342 $this->buffer = $encode;
343 $unpack = preg_replace_callback(
'/\\{/', array(&$this,
'_insertFastEncode'), $unpack, 1);
346 $unpack = preg_replace($ENCODE, $inline, $unpack);
350 $unpack = $unpackPacker->pack();
354 if ($this->_fastDecode) {
358 $params = implode(
',', $params);
361 return 'eval(' . $unpack .
'(' . $params .
"))\n";
365 private function _insertFastDecode($match) {
366 return '{' . $this->buffer .
';';
368 private function _insertFastEncode($match) {
369 return '{$encode=' . $this->buffer .
';';
373 private function _getEncoder($ascii) {
374 return $ascii > 10 ? $ascii > 36 ? $ascii > 62 ?
375 '_encode95' :
'_encode62' :
'_encode36' :
'_encode10';
380 private function _encode10($charCode) {
386 private function _encode36($charCode) {
387 return base_convert($charCode, 10, 36);
392 private function _encode62($charCode) {
394 if ($charCode >= $this->_encoding) {
395 $res = $this->_encode62((
int)($charCode / $this->_encoding));
397 $charCode = $charCode % $this->_encoding;
400 return $res . chr($charCode + 29);
402 return $res . base_convert($charCode, 10, 36);
407 private function _encode95($charCode) {
409 if ($charCode >= $this->_encoding)
410 $res = $this->_encode95($charCode / $this->_encoding);
412 return $res . chr(($charCode % $this->_encoding) + 161);
415 private function _safeRegExp($string) {
416 return '/'.preg_replace(
'/\$/',
'\\\$', $string).
'/';
419 private function _encodePrivate($charCode) {
420 return "_" . $charCode;
424 private function _escape(
$script) {
425 return preg_replace(
'/([\\\\\'])/',
'\\\$1',
$script);
429 private function _escape95(
$script) {
430 return preg_replace_callback(
432 array(&$this,
'_escape95Bis'),
436 private function _escape95Bis($match) {
437 return '\x'.((string)dechex(ord($match)));
441 private function _getJSFunction($aName) {
442 if (defined(
'self::JSFUNCTION'.$aName))
443 return constant(
'self::JSFUNCTION'.$aName);
462 'function($packed, $ascii, $count, $keywords, $encode, $decode) {
464 if ($keywords[$count]) {
465 $packed = $packed.replace(new RegExp(\'\\\\b\' + $encode($count) + \'\\\\b\', \'g\'), $keywords[$count]);
485 ' if (!\'\'.replace(/^/, String)) {
486 // decode all the values we need
488 $decode[$encode($count)] = $keywords[$count] || $encode($count);
490 // global replacement function
491 $keywords = [function ($encoded) {return $decode[$encoded]}];
493 $encode = function () {return \'\\\\w+\'};
494 // reset the loop counter - we are now doing a global replace
515 'function($charCode) {
522 'function($charCode) {
523 return $charCode.toString(36);
529 'function($charCode) {
530 return ($charCode < _encoding ? \'\' : arguments.callee(parseInt($charCode / _encoding))) +
531 (($charCode = $charCode % _encoding) > 35 ? String.fromCharCode($charCode + 29) : $charCode.toString(36));
537 'function($charCode) {
538 return ($charCode < _encoding ? \'\' : arguments.callee($charCode / _encoding)) +
539 String.fromCharCode($charCode % _encoding + 161);
546 public $ignoreCase =
false;
547 public $escapeChar =
'';
550 const EXPRESSION = 0;
551 const REPLACEMENT = 1;
555 private $GROUPS =
'/\\(/';
556 private $SUB_REPLACE =
'/\\$\\d/';
557 private $INDEXED =
'/^\\$\\d+$/';
558 private $TRIM =
'/([\'"])\\1\\.(.*)\\.\\1\\1$/';
559 private $ESCAPE =
'/\\\./';
560 private $QUOTE =
'/\'/';
561 private $DELETED =
'/\\x01[^\\x01]*\\x01/';
563 public function add($expression, $replacement =
'') {
566 $length = 1 + preg_match_all($this->GROUPS, $this->_internalEscape((
string)$expression),
$out);
569 if (is_string($replacement)) {
571 if (preg_match($this->SUB_REPLACE, $replacement)) {
573 if (preg_match($this->INDEXED, $replacement)) {
575 $replacement = (int)(substr($replacement, 1)) - 1;
578 $quote = preg_match($this->QUOTE, $this->_internalEscape($replacement))
580 $replacement = array(
581 'fn' =>
'_backReferences',
583 'replacement' => $replacement,
592 if (!empty($expression)) $this->_add($expression, $replacement, $length);
593 else $this->_add(
'/^$/', $replacement, $length);
596 public function exec($string) {
598 $this->_escaped = array();
602 foreach ($this->_patterns as $reg) {
603 $regexp .=
'(' . substr($reg[self::EXPRESSION], 1, -1) .
')|';
605 $regexp = substr($regexp, 0, -1) .
'/';
606 $regexp .= ($this->ignoreCase) ?
'i' :
'';
608 $string = $this->_escape($string, $this->escapeChar);
609 $string = preg_replace_callback(
617 $string = $this->_unescape($string, $this->escapeChar);
619 return preg_replace($this->DELETED,
'', $string);
624 $this->_patterns = array();
628 private $_escaped = array();
629 private $_patterns = array();
632 private function _add() {
633 $arguments = func_get_args();
634 $this->_patterns[] = $arguments;
638 private function _replacement($arguments) {
639 if (empty($arguments))
return '';
643 while (isset($this->_patterns[$j])) {
644 $pattern = $this->_patterns[$j++];
646 if (isset($arguments[$i]) && ($arguments[$i] !=
'')) {
647 $replacement = $pattern[self::REPLACEMENT];
649 if (is_array($replacement) && isset($replacement[
'fn'])) {
651 if (isset($replacement[
'data'])) $this->buffer = $replacement[
'data'];
652 return call_user_func(array(&$this, $replacement[
'fn']), $arguments, $i);
654 } elseif (is_int($replacement)) {
655 return $arguments[$replacement + $i];
658 $delete = ($this->escapeChar ==
'' ||
659 strpos($arguments[$i], $this->escapeChar) ===
false)
660 ?
'' :
"\x01" . $arguments[$i] .
"\x01";
661 return $delete . $replacement;
665 $i += $pattern[self::LENGTH];
670 private function _backReferences($match,
$offset) {
671 $replacement = $this->buffer[
'replacement'];
672 $quote = $this->buffer[
'quote'];
673 $i = $this->buffer[
'length'];
675 $replacement = str_replace(
'$'.$i--, $match[
$offset + $i], $replacement);
680 private function _replace_name($match,
$offset){
681 $length = strlen($match[
$offset + 2]);
686 private function _replace_encoded($match,
$offset) {
687 return $this->buffer[$match[
$offset]];
696 private function _escape($string, $escapeChar) {
698 $this->buffer = $escapeChar;
699 return preg_replace_callback(
700 '/\\' . $escapeChar .
'(.)' .
'/',
701 array(&$this,
'_escapeBis'),
709 private function _escapeBis($match) {
710 $this->_escaped[] = $match[1];
711 return $this->buffer;
715 private function _unescape($string, $escapeChar) {
717 $regexp =
'/'.
'\\'.$escapeChar.
'/';
718 $this->buffer = array(
'escapeChar'=> $escapeChar,
'i' => 0);
719 return preg_replace_callback
722 array(&$this,
'_unescapeBis'),
730 private function _unescapeBis() {
731 if (isset($this->_escaped[$this->buffer[
'i']])
732 && $this->_escaped[$this->buffer[
'i']] !=
'')
734 $temp = $this->_escaped[$this->buffer[
'i']];
738 $this->buffer[
'i']++;
739 return $this->buffer[
'escapeChar'] . $temp;
742 private function _internalEscape($string) {
743 return preg_replace($this->ESCAPE,
'', $string);
const JSFUNCTION_decodeBody
const JSFUNCTION_encode10
const JSFUNCTION_encode62
const JSFUNCTION_encode95
const JSFUNCTION_encode36
__construct($_script, $_encoding=62, $_fastDecode=true, $_specialChars=false)
add($expression, $replacement='')