74 private $_script =
'';
75 private $_encoding = 62;
76 private $_fastDecode =
true;
77 private $_specialChars =
false;
79 private $LITERAL_ENCODING = array(
86 public function __construct($_script, $_encoding = 62, $_fastDecode =
true, $_specialChars =
false)
88 $this->_script = $_script .
"\n";
89 if (array_key_exists($_encoding, $this->LITERAL_ENCODING))
90 $_encoding = $this->LITERAL_ENCODING[$_encoding];
91 $this->_encoding = min((
int)$_encoding, 95);
92 $this->_fastDecode = $_fastDecode;
93 $this->_specialChars = $_specialChars;
97 $this->_addParser(
'_basicCompression');
98 if ($this->_specialChars)
99 $this->_addParser(
'_encodeSpecialChars');
100 if ($this->_encoding)
101 $this->_addParser(
'_encodeKeywords');
104 return $this->_pack($this->_script);
108 private function _pack($script) {
109 for ($i = 0; isset($this->_parsers[$i]); $i++) {
110 $script = call_user_func(array(&$this,$this->_parsers[$i]), $script);
116 private $_parsers = array();
117 private function _addParser($parser) {
118 $this->_parsers[] = $parser;
122 private function _basicCompression($script) {
125 $parser->escapeChar =
'\\';
127 $parser->add(
'/\'[^\'\\n\\r]*\'/', self::IGNORE);
128 $parser->add(
'/"[^"\\n\\r]*"/', self::IGNORE);
130 $parser->add(
'/\\/\\/[^\\n\\r]*[\\n\\r]/',
' ');
131 $parser->add(
'/\\/\\*[^*]*\\*+([^\\/][^*]*\\*+)*\\//',
' ');
133 $parser->add(
'/\\s+(\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/g?i?)/',
'$2');
134 $parser->add(
'/[^\\w\\x24\\/\'"*)\\?:]\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/g?i?/', self::IGNORE);
136 if ($this->_specialChars) $parser->add(
'/;;;[^\\n\\r]+[\\n\\r]/');
138 $parser->add(
'/\\(;;\\)/', self::IGNORE);
139 $parser->add(
'/;+\\s*([};])/',
'$2');
141 $script = $parser->exec($script);
144 $parser->add(
'/(\\b|\\x24)\\s+(\\b|\\x24)/',
'$2 $3');
145 $parser->add(
'/([+\\-])\\s+([+\\-])/',
'$2 $3');
146 $parser->add(
'/\\s+/',
'');
148 return $parser->exec($script);
151 private function _encodeSpecialChars($script) {
154 $parser->add(
'/((\\x24+)([a-zA-Z$_]+))(\\d*)/',
155 array(
'fn' =>
'_replace_name')
158 $regexp =
'/\\b_[A-Za-z\\d]\\w*/';
160 $keywords = $this->_analyze($script, $regexp,
'_encodePrivate');
162 $encoded = $keywords[
'encoded'];
164 $parser->add($regexp,
166 'fn' =>
'_replace_encoded',
170 return $parser->exec($script);
173 private function _encodeKeywords($script) {
175 if ($this->_encoding > 62)
176 $script = $this->_escape95($script);
179 $encode = $this->_getEncoder($this->_encoding);
181 $regexp = ($this->_encoding > 62) ?
'/\\w\\w+/' :
'/\\w+/';
183 $keywords = $this->_analyze($script, $regexp, $encode);
184 $encoded = $keywords[
'encoded'];
187 $parser->add($regexp,
189 'fn' =>
'_replace_encoded',
193 if (empty($script))
return $script;
198 return $this->_bootStrap($parser->exec($script), $keywords);
202 private function _analyze($script, $regexp, $encode) {
206 preg_match_all($regexp, $script, $all);
209 $_protected = array();
213 $protected = array();
215 $this->_count = array();
216 $i = count($all); $j = 0;
220 $word =
'$' . $all[$i];
221 if (!isset($this->_count[$word])) {
222 $this->_count[$word] = 0;
223 $unsorted[$j] = $word;
227 $values[$j] = call_user_func(array(&$this, $encode), $j);
228 $protected[
'$' . $values[$j]] = $j++;
231 $this->_count[$word]++;
239 $i = count($unsorted);
241 $word = $unsorted[--$i];
242 if (isset($protected[$word]) ) {
243 $_sorted[$protected[$word]] = substr($word, 1);
244 $_protected[$protected[$word]] =
true;
245 $this->_count[$word] = 0;
259 usort($unsorted, array(&$this,
'_sortWords'));
264 if (!isset($_sorted[$i]))
265 $_sorted[$i] = substr($unsorted[$j++], 1);
266 $_encoded[$_sorted[$i]] = $values[$i];
267 }
while (++$i < count($unsorted));
270 'sorted' => $_sorted,
271 'encoded' => $_encoded,
272 'protected' => $_protected);
275 private $_count = array();
276 private function _sortWords($match1, $match2) {
277 return $this->_count[$match2] - $this->_count[$match1];
281 private function _bootStrap($packed, $keywords) {
282 $ENCODE = $this->_safeRegExp(
'$encode\\($count\\)');
285 $packed =
"'" . $this->_escape($packed) .
"'";
288 $ascii = min(count($keywords[
'sorted']), $this->_encoding);
289 if ($ascii == 0) $ascii = 1;
292 $count = count($keywords[
'sorted']);
295 foreach ($keywords[
'protected'] as $i=>$value) {
296 $keywords[
'sorted'][$i] =
'';
299 ksort($keywords[
'sorted']);
300 $keywords =
"'" . implode(
'|',$keywords[
'sorted']) .
"'.split('|')";
302 $encode = ($this->_encoding > 62) ?
'_encode95' : $this->_getEncoder($ascii);
303 $encode = $this->_getJSFunction($encode);
304 $encode = preg_replace(
'/_encoding/',
'$ascii', $encode);
305 $encode = preg_replace(
'/arguments\\.callee/',
'$encode', $encode);
306 $inline =
'\\$count' . ($ascii > 10 ?
'.toString(\\$ascii)' :
'');
309 if ($this->_fastDecode) {
311 $decode = $this->_getJSFunction(
'_decodeBody');
312 if ($this->_encoding > 62)
313 $decode = preg_replace(
'/\\\\w/',
'[\\xa1-\\xff]', $decode);
316 $decode = preg_replace($ENCODE, $inline, $decode);
320 $decode = preg_replace($this->_safeRegExp(
'($count)\\s*=\\s*1'),
'$1=0', $decode, 1);
324 $unpack = $this->_getJSFunction(
'_unpack');
325 if ($this->_fastDecode) {
327 $this->buffer = $decode;
328 $unpack = preg_replace_callback(
'/\\{/', array(&$this,
'_insertFastDecode'), $unpack, 1);
330 $unpack = preg_replace(
'/"/',
"'", $unpack);
331 if ($this->_encoding > 62) {
333 $unpack = preg_replace(
'/\'\\\\\\\\b\'\s*\\+|\\+\s*\'\\\\\\\\b\'/',
'', $unpack);
335 if ($ascii > 36 || $this->_encoding > 62 || $this->_fastDecode) {
337 $this->buffer = $encode;
338 $unpack = preg_replace_callback(
'/\\{/', array(&$this,
'_insertFastEncode'), $unpack, 1);
341 $unpack = preg_replace($ENCODE, $inline, $unpack);
345 $unpack = $unpackPacker->pack();
348 $params = array($packed, $ascii, $count, $keywords);
349 if ($this->_fastDecode) {
353 $params = implode(
',', $params);
356 return 'eval(' . $unpack .
'(' . $params .
"))\n";
360 private function _insertFastDecode($match) {
361 return '{' . $this->buffer .
';';
363 private function _insertFastEncode($match) {
364 return '{$encode=' . $this->buffer .
';';
368 private function _getEncoder($ascii) {
369 return $ascii > 10 ? $ascii > 36 ? $ascii > 62 ?
370 '_encode95' :
'_encode62' :
'_encode36' :
'_encode10';
375 private function _encode10($charCode) {
381 private function _encode36($charCode) {
382 return base_convert($charCode, 10, 36);
387 private function _encode62($charCode) {
389 if ($charCode >= $this->_encoding) {
390 $res = $this->_encode62((
int)($charCode / $this->_encoding));
392 $charCode = $charCode % $this->_encoding;
395 return $res . chr($charCode + 29);
397 return $res . base_convert($charCode, 10, 36);
402 private function _encode95($charCode) {
404 if ($charCode >= $this->_encoding)
405 $res = $this->_encode95($charCode / $this->_encoding);
407 return $res . chr(($charCode % $this->_encoding) + 161);
410 private function _safeRegExp($string) {
411 return '/'.preg_replace(
'/\$/',
'\\\$', $string).
'/';
414 private function _encodePrivate($charCode) {
415 return "_" . $charCode;
419 private function _escape($script) {
420 return preg_replace(
'/([\\\\\'])/',
'\\\$1', $script);
424 private function _escape95($script) {
425 return preg_replace_callback(
427 array(&$this,
'_escape95Bis'),
431 private function _escape95Bis($match) {
432 return '\x'.((string)dechex(ord($match)));
436 private function _getJSFunction($aName) {
437 if (defined(
'self::JSFUNCTION'.$aName))
438 return constant(
'self::JSFUNCTION'.$aName);
457 'function($packed, $ascii, $count, $keywords, $encode, $decode) { 459 if ($keywords[$count]) { 460 $packed = $packed.replace(new RegExp(\'\\\\b\' + $encode($count) + \'\\\\b\', \'g\'), $keywords[$count]); 480 ' if (!\'\'.replace(/^/, String)) { 481 // decode all the values we need 483 $decode[$encode($count)] = $keywords[$count] || $encode($count); 485 // global replacement function 486 $keywords = [function ($encoded) {return $decode[$encoded]}]; 488 $encode = function () {return \'\\\\w+\'}; 489 // reset the loop counter - we are now doing a global replace 510 'function($charCode) { 517 'function($charCode) { 518 return $charCode.toString(36); 524 'function($charCode) { 525 return ($charCode < _encoding ? \'\' : arguments.callee(parseInt($charCode / _encoding))) + 526 (($charCode = $charCode % _encoding) > 35 ? String.fromCharCode($charCode + 29) : $charCode.toString(36)); 532 'function($charCode) { 533 return ($charCode < _encoding ? \'\' : arguments.callee($charCode / _encoding)) + 534 String.fromCharCode($charCode % _encoding + 161); 541 public $ignoreCase =
false;
542 public $escapeChar =
'';
545 const EXPRESSION = 0;
546 const REPLACEMENT = 1;
550 private $GROUPS =
'/\\(/';
551 private $SUB_REPLACE =
'/\\$\\d/';
552 private $INDEXED =
'/^\\$\\d+$/';
553 private $TRIM =
'/([\'"])\\1\\.(.*)\\.\\1\\1$/';
554 private $ESCAPE =
'/\\\./';
555 private $QUOTE =
'/\'/';
556 private $DELETED =
'/\\x01[^\\x01]*\\x01/';
558 public function add($expression, $replacement =
'') {
561 $length = 1 + preg_match_all($this->GROUPS, $this->_internalEscape((
string)$expression), $out);
564 if (is_string($replacement)) {
566 if (preg_match($this->SUB_REPLACE, $replacement)) {
568 if (preg_match($this->INDEXED, $replacement)) {
570 $replacement = (int)(substr($replacement, 1)) - 1;
573 $quote = preg_match($this->QUOTE, $this->_internalEscape($replacement))
575 $replacement = array(
576 'fn' =>
'_backReferences',
578 'replacement' => $replacement,
587 if (!empty($expression)) $this->_add($expression, $replacement, $length);
588 else $this->_add(
'/^$/', $replacement, $length);
591 public function exec($string) {
593 $this->_escaped = array();
597 foreach ($this->_patterns as $reg) {
598 $regexp .=
'(' . substr($reg[self::EXPRESSION], 1, -1) .
')|';
600 $regexp = substr($regexp, 0, -1) .
'/';
601 $regexp .= ($this->ignoreCase) ?
'i' :
'';
603 $string = $this->_escape($string, $this->escapeChar);
604 $string = preg_replace_callback(
612 $string = $this->_unescape($string, $this->escapeChar);
614 return preg_replace($this->DELETED,
'', $string);
619 $this->_patterns = array();
623 private $_escaped = array();
624 private $_patterns = array();
627 private function _add() {
628 $arguments = func_get_args();
629 $this->_patterns[] = $arguments;
633 private function _replacement($arguments) {
634 if (empty($arguments))
return '';
638 while (isset($this->_patterns[$j])) {
639 $pattern = $this->_patterns[$j++];
641 if (isset($arguments[$i]) && ($arguments[$i] !=
'')) {
642 $replacement = $pattern[self::REPLACEMENT];
644 if (is_array($replacement) && isset($replacement[
'fn'])) {
646 if (isset($replacement[
'data'])) $this->buffer = $replacement[
'data'];
647 return call_user_func(array(&$this, $replacement[
'fn']), $arguments, $i);
649 } elseif (is_int($replacement)) {
650 return $arguments[$replacement + $i];
653 $delete = ($this->escapeChar ==
'' ||
654 strpos($arguments[$i], $this->escapeChar) ===
false)
655 ?
'' :
"\x01" . $arguments[$i] .
"\x01";
656 return $delete . $replacement;
660 $i += $pattern[self::LENGTH];
665 private function _backReferences($match, $offset) {
666 $replacement = $this->buffer[
'replacement'];
667 $quote = $this->buffer[
'quote'];
668 $i = $this->buffer[
'length'];
670 $replacement = str_replace(
'$'.$i--, $match[$offset + $i], $replacement);
675 private function _replace_name($match, $offset){
676 $length = strlen($match[$offset + 2]);
677 $start = $length - max($length - strlen($match[$offset + 3]), 0);
678 return substr($match[$offset + 1], $start, $length) . $match[$offset + 4];
681 private function _replace_encoded($match, $offset) {
682 return $this->buffer[$match[$offset]];
691 private function _escape($string, $escapeChar) {
693 $this->buffer = $escapeChar;
694 return preg_replace_callback(
695 '/\\' . $escapeChar .
'(.)' .
'/',
696 array(&$this,
'_escapeBis'),
704 private function _escapeBis($match) {
705 $this->_escaped[] = $match[1];
706 return $this->buffer;
710 private function _unescape($string, $escapeChar) {
712 $regexp =
'/'.
'\\'.$escapeChar.
'/';
713 $this->buffer = array(
'escapeChar'=> $escapeChar,
'i' => 0);
714 return preg_replace_callback
717 array(&$this,
'_unescapeBis'),
725 private function _unescapeBis() {
726 if (isset($this->_escaped[$this->buffer[
'i']])
727 && $this->_escaped[$this->buffer[
'i']] !=
'')
729 $temp = $this->_escaped[$this->buffer[
'i']];
733 $this->buffer[
'i']++;
734 return $this->buffer[
'escapeChar'] . $temp;
737 private function _internalEscape($string) {
738 return preg_replace($this->ESCAPE,
'', $string);
add($expression, $replacement= '')
__construct($_script, $_encoding=62, $_fastDecode=true, $_specialChars=false)
const JSFUNCTION_encode95
const JSFUNCTION_encode10
const JSFUNCTION_encode62
const JSFUNCTION_encode36
const JSFUNCTION_decodeBody