2 /* 9 April 2008. version 1.1
4 * This is the php version of the Dean Edwards JavaScript's Packer,
7 * ParseMaster, version 1.0.2 (2005-08-19) Copyright 2005, Dean Edwards
8 * a multi-pattern parser.
9 * KNOWN BUG: erroneous behavior when using escapeChar with a replacement
10 * value that is a function
12 * packer, version 2.0.2 (2005-08-19) Copyright 2004-2005, Dean Edwards
14 * License: http://creativecommons.org/licenses/LGPL/2.1/
16 * Ported to PHP by Nicolas Martin.
17 * modified by Mark Fabrizio Jr. to work with php 4
19 * ----------------------------------------------------------------------
21 * 1.1 : correct a bug, '\0' packed then unpacked becomes '\'.
22 * ----------------------------------------------------------------------
25 * $myPacker = new JavaScriptPacker($script, 62, true, false);
26 * $packed = $myPacker->pack();
30 * $myPacker = new JavaScriptPacker($script, 'Normal', true, false);
31 * $packed = $myPacker->pack();
35 * $myPacker = new JavaScriptPacker($script);
36 * $packed = $myPacker->pack();
39 * params of the constructor :
40 * $script: the JavaScript to pack, string.
41 * $encoding: level of encoding, int or string :
42 * 0,10,62,95 or 'None', 'Numeric', 'Normal', 'High ASCII'.
44 * $fastDecode: include the fast decoder in the packed result, boolean.
46 * $specialChars: if you are flagged your private and local variables
47 * in the script, boolean.
50 * The pack() method return the compressed JavasScript, as a string.
52 * see http://dean.edwards.name/packer/usage/ for more information.
55 * # [del]need PHP 5 . Tested with PHP 5.1.2[/del]
56 * this is a modified version for PHP 4
58 * # The packed result may be different than with the Dean Edwards
59 * version, but with the same length. The reason is that the PHP
60 * function usort to sort array don't necessarily preserve the
61 * original order of two equal member. The Javascript sort function
62 * in fact preserve this order (but that's not require by the
63 * ECMAScript standard). So the encoded keywords order can be
64 * different in the two results.
66 * # Be careful with the 'High ASCII' Level encoding if you use
67 * UTF-8 in your files...
71 * modified by Mark Fabrizio Jr. to work with php 4
75 class JavaScriptPacker
{
78 // validate parameters
81 var $_fastDecode = true;
82 var $_specialChars = false;
84 var $LITERAL_ENCODING = array(
91 // http://code.spip.net/@JavaScriptPacker
92 function JavaScriptPacker($_script, $_encoding = 62, $_fastDecode = true, $_specialChars = false)
94 $this->_script
= $_script . "\n";
95 if (array_key_exists($_encoding, $this->LITERAL_ENCODING
))
96 $_encoding = $this->LITERAL_ENCODING
[$_encoding];
97 $this->_encoding
= min((int)$_encoding, 95);
98 $this->_fastDecode
= $_fastDecode;
99 $this->_specialChars
= $_specialChars;
102 // http://code.spip.net/@pack
104 $this->_addParser('_basicCompression');
105 if ($this->_specialChars
)
106 $this->_addParser('_encodeSpecialChars');
107 if ($this->_encoding
)
108 $this->_addParser('_encodeKeywords');
111 return $this->_pack($this->_script
);
114 // apply all parsing routines
115 // http://code.spip.net/@_pack
116 function _pack($script) {
117 for ($i = 0; isset($this->_parsers
[$i]); $i++
) {
118 $script = call_user_func(array(&$this,$this->_parsers
[$i]), $script);
123 // keep a list of parsing functions, they'll be executed all at once
124 var $_parsers = array();
125 // http://code.spip.net/@_addParser
126 function _addParser($parser) {
127 $this->_parsers
[] = $parser;
130 // zero encoding - just removal of white space and comments
131 // http://code.spip.net/@_basicCompression
132 function _basicCompression($script) {
133 $parser = new ParseMaster();
135 $parser->escapeChar
= '\\';
137 $parser->add('/\'[^\'\\n\\r]*\'/',$this->IGNORE
);
138 $parser->add('/"[^"\\n\\r]*"/', $this->IGNORE
);
140 $parser->add('/\\/\\/[^\\n\\r]*[\\n\\r]/', "\n");
141 $parser->add('/\\/\\*[^*]*\\*+([^\\/][^*]*\\*+)*\\//', ' ');
142 // protect regular expressions
143 $parser->add('/\\s+(\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/g?i?)/', '$2'); // IGNORE
144 $parser->add('/[^\\w\\x24\\/\'"*)\\?:]\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/g?i?/', $this->IGNORE
);
145 // remove: ;;; doSomething();
146 if ($this->_specialChars
) $parser->add('/;;;[^\\n\\r]+[\\n\\r]/');
147 // remove redundant semi-colons
148 // Disabled : breaks for(i=0; ;i++) {...}
149 // $parser->add('/\\(;;\\)/', $this->IGNORE); // protect for (;;) loops
150 // $parser->add('/;+\\s*([};])/', '$2');
152 $script = $parser->exec($script);
154 // remove white-space
155 # $parser->add('/(\\b|\\x24)\\s+(\\b|\\x24)/', '$2 $3');
156 # $parser->add('/([+\\-])\\s+([+\\-])/', '$2 $3');
157 # $parser->add('/\\s+/', '');
158 # Modif fil@rezo.net pour conserver les \n
159 $parser->add('/(\\b|\\x24)[\\t ]+(\\b|\\x24)/', '$2 $3');
160 $parser->add('/([+\\-])[\\t ]+([+\\-])/', '$2 $3');
161 $parser->add('/[\\t ]+/', '');
162 $parser->add('/\\s+/', "\n");
164 return $parser->exec($script);
167 // http://code.spip.net/@_encodeSpecialChars
168 function _encodeSpecialChars($script) {
169 $parser = new ParseMaster();
170 // replace: $name -> n, $$name -> na
171 $parser->add('/((\\x24+)([a-zA-Z$_]+))(\\d*)/',
172 array('fn' => '_replace_name')
174 // replace: _name -> _0, double-underscore (__name) is ignored
175 $regexp = '/\\b_[A-Za-z\\d]\\w*/';
176 // build the word list
177 $keywords = $this->_analyze($script, $regexp, '_encodePrivate');
179 $encoded = $keywords['encoded'];
181 $parser->add($regexp,
183 'fn' => '_replace_encoded',
187 return $parser->exec($script);
190 // http://code.spip.net/@_encodeKeywords
191 function _encodeKeywords($script) {
192 // escape high-ascii values already in the script (i.e. in strings)
193 if ($this->_encoding
> 62)
194 $script = $this->_escape95($script);
196 $parser = new ParseMaster();
197 $encode = $this->_getEncoder($this->_encoding
);
198 // for high-ascii, don't encode single character low-ascii
199 $regexp = ($this->_encoding
> 62) ?
'/\\w\\w+/' : '/\\w+/';
200 // build the word list
201 $keywords = $this->_analyze($script, $regexp, $encode);
202 $encoded = $keywords['encoded'];
205 $parser->add($regexp,
207 'fn' => '_replace_encoded',
211 if (empty($script)) return $script;
213 //$res = $parser->exec($script);
214 //$res = $this->_bootStrap($res, $keywords);
216 return $this->_bootStrap($parser->exec($script), $keywords);
220 // http://code.spip.net/@_analyze
221 function _analyze($script, $regexp, $encode) {
223 // retreive all words in the script
225 preg_match_all($regexp, $script, $all);
226 $_sorted = array(); // list of words sorted by frequency
227 $_encoded = array(); // dictionary of word->encoding
228 $_protected = array(); // instances of "protected" words
229 $all = $all[0]; // simulate the javascript comportement of global match
231 $unsorted = array(); // same list, not sorted
232 $protected = array(); // "protected" words (dictionary of word->"word")
233 $value = array(); // dictionary of charCode->encoding (eg. 256->ff)
234 $this->_count
= array(); // word->count
235 $i = count($all); $j = 0; //$word = null;
236 // count the occurrences - used for sorting later
239 $word = '$' . $all[$i];
240 if (!isset($this->_count
[$word])) {
241 $this->_count
[$word] = 0;
242 $unsorted[$j] = $word;
243 // make a dictionary of all of the protected words in this script
244 // these are words that might be mistaken for encoding
245 //if (is_string($encode) && method_exists($this, $encode))
246 $values[$j] = call_user_func(array(&$this, $encode), $j);
247 $protected['$' . $values[$j]] = $j++
;
249 // increment the word counter
250 $this->_count
[$word]++
;
252 // prepare to sort the word list, first we must protect
253 // words that are also used as codes. we assign them a code
254 // equivalent to the word itself.
255 // e.g. if "do" falls within our encoding range
256 // then we store keywords["do"] = "do";
257 // this avoids problems when decoding
258 $i = count($unsorted);
260 $word = $unsorted[--$i];
261 if (isset($protected[$word]) /*!= null*/) {
262 $_sorted[$protected[$word]] = substr($word, 1);
263 $_protected[$protected[$word]] = true;
264 $this->_count
[$word] = 0;
268 // sort the words by frequency
269 // Note: the javascript and php version of sort can be different :
270 // in php manual, usort :
271 // " If two members compare as equal,
272 // their order in the sorted array is undefined."
273 // so the final packed script is different of the Dean's javascript version
275 // the ECMAscript standard does not guarantee this behaviour,
276 // and thus not all browsers (e.g. Mozilla versions dating back to at
277 // least 2003) respect this.
278 usort($unsorted, array(&$this, '_sortWords'));
280 // because there are "protected" words in the list
281 // we must add the sorted words around them
283 if (!isset($_sorted[$i]))
284 $_sorted[$i] = substr($unsorted[$j++
], 1);
285 $_encoded[$_sorted[$i]] = $values[$i];
286 } while (++
$i < count($unsorted));
289 'sorted' => $_sorted,
290 'encoded' => $_encoded,
291 'protected' => $_protected);
294 var $_count = array();
295 // http://code.spip.net/@_sortWords
296 function _sortWords($match1, $match2) {
297 return $this->_count
[$match2] - $this->_count
[$match1];
300 // build the boot function used for loading and decoding
301 // http://code.spip.net/@_bootStrap
302 function _bootStrap($packed, $keywords) {
303 $ENCODE = $this->_safeRegExp('$encode\\($count\\)');
305 // $packed: the packed script
306 $packed = "'" . $this->_escape($packed) . "'";
308 // $ascii: base for encoding
309 $ascii = min(count($keywords['sorted']), $this->_encoding
);
310 if ($ascii == 0) $ascii = 1;
312 // $count: number of words contained in the script
313 $count = count($keywords['sorted']);
315 // $keywords: list of words contained in the script
316 foreach ($keywords['protected'] as $i=>$value) {
317 $keywords['sorted'][$i] = '';
319 // convert from a string to an array
320 ksort($keywords['sorted']);
321 $keywords = "'" . implode('|',$keywords['sorted']) . "'.split('|')";
323 $encode = ($this->_encoding
> 62) ?
'_encode95' : $this->_getEncoder($ascii);
324 $encode = $this->_getJSFunction($encode);
325 $encode = preg_replace('/_encoding/','$ascii', $encode);
326 $encode = preg_replace('/arguments\\.callee/','$encode', $encode);
327 $inline = '\\$count' . ($ascii > 10 ?
'.toString(\\$ascii)' : '');
329 // $decode: code snippet to speed up decoding
330 if ($this->_fastDecode
) {
331 // create the decoder
332 $decode = $this->_getJSFunction('_decodeBody');
333 if ($this->_encoding
> 62)
334 $decode = preg_replace('/\\\\w/', '[\\xa1-\\xff]', $decode);
335 // perform the encoding inline for lower ascii values
337 $decode = preg_replace($ENCODE, $inline, $decode);
338 // special case: when $count==0 there are no keywords. I want to keep
339 // the basic shape of the unpacking funcion so i'll frig the code...
341 $decode = preg_replace($this->_safeRegExp('($count)\\s*=\\s*1'), '$1=0', $decode, 1);
345 $unpack = $this->_getJSFunction('_unpack');
346 if ($this->_fastDecode
) {
347 // insert the decoder
348 $this->buffer
= $decode;
349 $unpack = preg_replace_callback('/\\{/', array(&$this, '_insertFastDecode'), $unpack, 1);
351 $unpack = preg_replace('/"/', "'", $unpack);
352 if ($this->_encoding
> 62) { // high-ascii
353 // get rid of the word-boundaries for regexp matches
354 $unpack = preg_replace('/\'\\\\\\\\b\'\s*\\+|\\+\s*\'\\\\\\\\b\'/', '', $unpack);
356 if ($ascii > 36 ||
$this->_encoding
> 62 ||
$this->_fastDecode
) {
357 // insert the encode function
358 $this->buffer
= $encode;
359 $unpack = preg_replace_callback('/\\{/', array(&$this, '_insertFastEncode'), $unpack, 1);
361 // perform the encoding inline
362 $unpack = preg_replace($ENCODE, $inline, $unpack);
364 // pack the boot function too
365 $unpackPacker = new JavaScriptPacker($unpack, 0, false, true);
366 $unpack = $unpackPacker->pack();
369 $params = array($packed, $ascii, $count, $keywords);
370 if ($this->_fastDecode
) {
374 $params = implode(',', $params);
377 return 'eval(' . $unpack . '(' . $params . "))\n";
381 // http://code.spip.net/@_insertFastDecode
382 function _insertFastDecode($match) {
383 return '{' . $this->buffer
. ';';
385 // http://code.spip.net/@_insertFastEncode
386 function _insertFastEncode($match) {
387 return '{$encode=' . $this->buffer
. ';';
390 // mmm.. ..which one do i need ??
391 // http://code.spip.net/@_getEncoder
392 function _getEncoder($ascii) {
393 return $ascii > 10 ?
$ascii > 36 ?
$ascii > 62 ?
394 '_encode95' : '_encode62' : '_encode36' : '_encode10';
398 // characters: 0123456789
399 // http://code.spip.net/@_encode10
400 function _encode10($charCode) {
404 // inherent base36 support
405 // characters: 0123456789abcdefghijklmnopqrstuvwxyz
406 // http://code.spip.net/@_encode36
407 function _encode36($charCode) {
408 return base_convert($charCode, 10, 36);
411 // hitch a ride on base36 and add the upper case alpha characters
412 // characters: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
413 // http://code.spip.net/@_encode62
414 function _encode62($charCode) {
416 if ($charCode >= $this->_encoding
) {
417 $res = $this->_encode62((int)($charCode / $this->_encoding
));
419 $charCode = $charCode %
$this->_encoding
;
422 return $res . chr($charCode +
29);
424 return $res . base_convert($charCode, 10, 36);
427 // use high-ascii values
428 // characters: ¬°¬¢¬£¬§¬•¬¶¬ß¬®¬©¬™¬´¬¨¬≠¬Æ¬Ø¬∞¬±¬≤¬≥¬¥¬µ¬∂¬∑¬∏¬π¬∫¬ª¬º¬Ω¬æ¬ø√Ä√?√Ç√É√Ñ√Ö√Ü√á√à√â√ä√ã√å√?√é√?√?√ë√í√ì√î√ï√ñ√ó√ò√ô√ö√õ√ú√?√û√ü√†√°√¢√£√§√•√¶√ß√®√©√™√´√¨√≠√Æ√Ø√∞√±√≤√≥√¥√µ√∂√∑√∏√π√∫√ª√º√Ω√æ
429 // http://code.spip.net/@_encode95
430 function _encode95($charCode) {
432 if ($charCode >= $this->_encoding
)
433 $res = $this->_encode95($charCode / $this->_encoding
);
435 return $res . chr(($charCode %
$this->_encoding
) +
161);
438 // http://code.spip.net/@_safeRegExp
439 function _safeRegExp($string) {
440 return '/'.preg_replace('/\$/', '\\\$', $string).'/';
443 // http://code.spip.net/@_encodePrivate
444 function _encodePrivate($charCode) {
445 return "_" . $charCode;
448 // protect characters used by the parser
449 // http://code.spip.net/@_escape
450 function _escape($script) {
451 return preg_replace('/([\\\\\'])/', '\\\$1', $script);
454 // protect high-ascii characters already in the script
455 // http://code.spip.net/@_escape95
456 function _escape95($script) {
457 return preg_replace_callback(
459 array(&$this, '_escape95Bis'),
463 // http://code.spip.net/@_escape95Bis
464 function _escape95Bis($match) {
465 return '\x'.((string)dechex(ord($match)));
469 // http://code.spip.net/@_getJSFunction
470 function _getJSFunction($aName) {
471 $func = 'JSFUNCTION'.$aName;
472 if (isset($this->$func)){
479 // JavaScript Functions used.
480 // Note : In Dean's version, these functions are converted
481 // with 'String(aFunctionName);'.
482 // This internal conversion complete the original code, ex :
483 // 'while (aBool) anAction();' is converted to
484 // 'while (aBool) { anAction(); }'.
485 // The JavaScript functions below are corrected.
487 // unpacking function - this is the boot strap function
488 // data extracted from this packing routine is passed to
489 // this function when decoded in the target
490 // NOTE ! : without the ';' final.
491 var $JSFUNCTION_unpack = 'function($packed, $ascii, $count, $keywords, $encode, $decode) {
493 if ($keywords[$count]) {
494 $packed = $packed.replace(new RegExp(\'\\\\b\' + $encode($count) + \'\\\\b\', \'g\'), $keywords[$count]);
500 'function($packed, $ascii, $count, $keywords, $encode, $decode) {
502 if ($keywords[$count])
503 $packed = $packed.replace(new RegExp(\'\\\\b\' + $encode($count) + \'\\\\b\', \'g\'), $keywords[$count]);
508 // code-snippet inserted into the unpacker to speed up decoding
509 var $JSFUNCTION_decodeBody = ' if (!\'\'.replace(/^/, String)) {
510 // decode all the values we need
512 $decode[$encode($count)] = $keywords[$count] || $encode($count);
514 // global replacement function
515 $keywords = [function ($encoded) {return $decode[$encoded]}];
517 $encode = function () {return \'\\\\w+\'};
518 // reset the loop counter - we are now doing a global replace
524 ' if (!\'\'.replace(/^/, String)) {
525 // decode all the values we need
526 while ($count--) $decode[$encode($count)] = $keywords[$count] || $encode($count);
527 // global replacement function
528 $keywords = [function ($encoded) {return $decode[$encoded]}];
530 $encode = function () {return\'\\\\w+\'};
531 // reset the loop counter - we are now doing a global replace
537 // characters: 0123456789
538 var $JSFUNCTION_encode10 = 'function($charCode) {
542 // inherent base36 support
543 // characters: 0123456789abcdefghijklmnopqrstuvwxyz
544 var $JSFUNCTION_encode36 = 'function($charCode) {
545 return $charCode.toString(36);
548 // hitch a ride on base36 and add the upper case alpha characters
549 // characters: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
550 var $JSFUNCTION_encode62 = 'function($charCode) {
551 return ($charCode < _encoding ? \'\' : arguments.callee(parseInt($charCode / _encoding))) +
552 (($charCode = $charCode % _encoding) > 35 ? String.fromCharCode($charCode + 29) : $charCode.toString(36));
555 // use high-ascii values
556 // characters: ¬°¬¢¬£¬§¬•¬¶¬ß¬®¬©¬™¬´¬¨¬≠¬Æ¬Ø¬∞¬±¬≤¬≥¬¥¬µ¬∂¬∑¬∏¬π¬∫¬ª¬º¬Ω¬æ¬ø√Ä√?√Ç√É√Ñ√Ö√Ü√á√à√â√ä√ã√å√?√é√?√?√ë√í√ì√î√ï√ñ√ó√ò√ô√ö√õ√ú√?√û√ü√†√°√¢√£√§√•√¶√ß√®√©√™√´√¨√≠√Æ√Ø√∞√±√≤√≥√¥√µ√∂√∑√∏√π√∫√ª√º√Ω√æ
557 var $JSFUNCTION_encode95 = 'function($charCode) {
558 return ($charCode < _encoding ? \'\' : arguments.callee($charCode / _encoding)) +
559 String.fromCharCode($charCode % _encoding + 161);
566 var $ignoreCase = false;
567 var $escapeChar = '';
571 var $REPLACEMENT = 1;
574 // used to determine nesting levels
575 var $GROUPS = '/\\(/';//g
576 var $SUB_REPLACE = '/\\$\\d/';
577 var $INDEXED = '/^\\$\\d+$/';
578 var $TRIM = '/([\'"])\\1\\.(.*)\\.\\1\\1$/';
579 var $ESCAPE = '/\\\./';//g
581 var $DELETED = '/\\x01[^\\x01]*\\x01/';//g
583 // http://code.spip.net/@add
584 function add($expression, $replacement = '') {
585 // count the number of sub-expressions
586 // - add one because each pattern is itself a sub-expression
587 $length = 1 +
preg_match_all($this->GROUPS
, $this->_internalEscape((string)$expression), $out);
589 // treat only strings $replacement
590 if (is_string($replacement)) {
591 // does the pattern deal with sub-expressions?
592 if (preg_match($this->SUB_REPLACE
, $replacement)) {
593 // a simple lookup? (e.g. "$2")
594 if (preg_match($this->INDEXED
, $replacement)) {
595 // store the index (used for fast retrieval of matched strings)
596 $replacement = (int)(substr($replacement, 1)) - 1;
597 } else { // a complicated lookup (e.g. "Hello $2 $1")
598 // build a function to do the lookup
599 $quote = preg_match($this->QUOTE
, $this->_internalEscape($replacement))
601 $replacement = array(
602 'fn' => '_backReferences',
604 'replacement' => $replacement,
612 // pass the modified arguments
613 if (!empty($expression)) $this->_add($expression, $replacement, $length);
614 else $this->_add('/^$/', $replacement, $length);
617 // http://code.spip.net/@exec
618 function exec($string) {
619 // execute the global replacement
620 $this->_escaped
= array();
622 // simulate the _patterns.toSTring of Dean
624 foreach ($this->_patterns
as $reg) {
625 $regexp .= '(' . substr($reg[$this->EXPRESSION
], 1, -1) . ')|';
627 $regexp = substr($regexp, 0, -1) . '/';
628 $regexp .= ($this->ignoreCase
) ?
'i' : '';
630 $string = $this->_escape($string, $this->escapeChar
);
631 $string = preg_replace_callback(
639 $string = $this->_unescape($string, $this->escapeChar
);
641 return preg_replace($this->DELETED
, '', $string);
644 // http://code.spip.net/@reset
646 // clear the patterns collection so that this object may be re-used
647 $this->_patterns
= array();
651 var $_escaped = array(); // escaped characters
652 var $_patterns = array(); // patterns stored by index
654 // create and add a new pattern to the patterns collection
655 // http://code.spip.net/@_add
657 $arguments = func_get_args();
658 $this->_patterns
[] = $arguments;
661 // this is the global replace function (it's quite complicated)
662 // http://code.spip.net/@_replacement
663 function _replacement($arguments) {
664 if (empty($arguments)) return '';
667 // loop through the patterns
668 while (isset($this->_patterns
[$j])) {
669 $pattern = $this->_patterns
[$j++
];
670 // do we have a result?
671 if (isset($arguments[$i]) && ($arguments[$i] != '')) {
672 $replacement = $pattern[$this->REPLACEMENT
];
674 if (is_array($replacement) && isset($replacement['fn'])) {
676 if (isset($replacement['data'])) $this->buffer
= $replacement['data'];
677 return call_user_func(array(&$this, $replacement['fn']), $arguments, $i);
679 } elseif (is_int($replacement)) {
680 return $arguments[$replacement +
$i];
683 $delete = ($this->escapeChar
== '' ||
684 strpos($arguments[$i], $this->escapeChar
) === false)
685 ?
'' : "\x01" . $arguments[$i] . "\x01";
686 return $delete . $replacement;
688 // skip over references to sub-expressions
690 $i +
= $pattern[$this->LENGTH
];
695 // http://code.spip.net/@_backReferences
696 function _backReferences($match, $offset) {
697 $replacement = $this->buffer
['replacement'];
698 $quote = $this->buffer
['quote'];
699 $i = $this->buffer
['length'];
701 $replacement = str_replace('$'.$i--, $match[$offset +
$i], $replacement);
706 // http://code.spip.net/@_replace_name
707 function _replace_name($match, $offset){
708 $length = strlen($match[$offset +
2]);
709 $start = $length - max($length - strlen($match[$offset +
3]), 0);
710 return substr($match[$offset +
1], $start, $length) . $match[$offset +
4];
713 // http://code.spip.net/@_replace_encoded
714 function _replace_encoded($match, $offset) {
715 return $this->buffer
[$match[$offset]];
719 // php : we cannot pass additional data to preg_replace_callback,
720 // and we cannot use &$this in create_function, so let's go to lower level
723 // encode escaped characters
724 // http://code.spip.net/@_escape
725 function _escape($string, $escapeChar) {
727 $this->buffer
= $escapeChar;
728 return preg_replace_callback(
729 '/\\' . $escapeChar . '(.)' .'/',
730 array(&$this, '_escapeBis'),
738 // http://code.spip.net/@_escapeBis
739 function _escapeBis($match) {
740 $this->_escaped
[] = $match[1];
741 return $this->buffer
;
744 // decode escaped characters
745 // http://code.spip.net/@_unescape
746 function _unescape($string, $escapeChar) {
748 $regexp = '/'.'\\'.$escapeChar.'/';
749 $this->buffer
= array('escapeChar'=> $escapeChar, 'i' => 0);
750 return preg_replace_callback
753 array(&$this, '_unescapeBis'),
761 // http://code.spip.net/@_unescapeBis
762 function _unescapeBis() {
763 if (isset($this->_escaped
[$this->buffer
['i']])
764 && $this->_escaped
[$this->buffer
['i']] != '')
766 $temp = $this->_escaped
[$this->buffer
['i']];
770 $this->buffer
['i']++
;
771 return $this->buffer
['escapeChar'] . $temp;
774 // http://code.spip.net/@_internalEscape
775 function _internalEscape($string) {
776 return preg_replace($this->ESCAPE
, '', $string);