a4db495c6e32ab5522acdb877308451a654b4ac9
[lhc/web/clavette_www.git] / www / plugins-dist / compresseur / lib / JavascriptPacker / class.JavaScriptPacker.php
1 <?php
2 /* 9 April 2008. version 1.1
3 *
4 * This is the php version of the Dean Edwards JavaScript's Packer,
5 * Based on :
6 *
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
11 *
12 * packer, version 2.0.2 (2005-08-19) Copyright 2004-2005, Dean Edwards
13 *
14 * License: http://creativecommons.org/licenses/LGPL/2.1/
15 *
16 * Ported to PHP by Nicolas Martin.
17 * modified by Mark Fabrizio Jr. to work with php 4
18 *
19 * ----------------------------------------------------------------------
20 * changelog:
21 * 1.1 : correct a bug, '\0' packed then unpacked becomes '\'.
22 * ----------------------------------------------------------------------
23 *
24 * examples of usage :
25 * $myPacker = new JavaScriptPacker($script, 62, true, false);
26 * $packed = $myPacker->pack();
27 *
28 * or
29 *
30 * $myPacker = new JavaScriptPacker($script, 'Normal', true, false);
31 * $packed = $myPacker->pack();
32 *
33 * or (default values)
34 *
35 * $myPacker = new JavaScriptPacker($script);
36 * $packed = $myPacker->pack();
37 *
38 *
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'.
43 * default: 62.
44 * $fastDecode: include the fast decoder in the packed result, boolean.
45 * default : true.
46 * $specialChars: if you are flagged your private and local variables
47 * in the script, boolean.
48 * default: false.
49 *
50 * The pack() method return the compressed JavasScript, as a string.
51 *
52 * see http://dean.edwards.name/packer/usage/ for more information.
53 *
54 * Notes :
55 * # [del]need PHP 5 . Tested with PHP 5.1.2[/del]
56 * this is a modified version for PHP 4
57 *
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.
65 *
66 * # Be careful with the 'High ASCII' Level encoding if you use
67 * UTF-8 in your files...
68 */
69
70 /*
71 * modified by Mark Fabrizio Jr. to work with php 4
72 */
73
74
75 class JavaScriptPacker {
76 var $IGNORE = '$1';
77
78 // validate parameters
79 var $_script = '';
80 var $_encoding = 62;
81 var $_fastDecode = true;
82 var $_specialChars = false;
83
84 var $LITERAL_ENCODING = array(
85 'None' => 0,
86 'Numeric' => 10,
87 'Normal' => 62,
88 'High ASCII' => 95
89 );
90
91 // http://doc.spip.org/@JavaScriptPacker
92 function JavaScriptPacker($_script, $_encoding = 62, $_fastDecode = true, $_specialChars = false)
93 {
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;
100 }
101
102 // http://doc.spip.org/@pack
103 function pack() {
104 $this->_addParser('_basicCompression');
105 if ($this->_specialChars)
106 $this->_addParser('_encodeSpecialChars');
107 if ($this->_encoding)
108 $this->_addParser('_encodeKeywords');
109
110 // go!
111 return $this->_pack($this->_script);
112 }
113
114 // apply all parsing routines
115 // http://doc.spip.org/@_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);
119 }
120 return $script;
121 }
122
123 // keep a list of parsing functions, they'll be executed all at once
124 var $_parsers = array();
125 // http://doc.spip.org/@_addParser
126 function _addParser($parser) {
127 $this->_parsers[] = $parser;
128 }
129
130 // zero encoding - just removal of white space and comments
131 // http://doc.spip.org/@_basicCompression
132 function _basicCompression($script) {
133 $parser = new ParseMaster();
134 // make safe
135 $parser->escapeChar = '\\';
136 // protect strings
137 $parser->add('/\'[^\'\\n\\r]*\'/',$this->IGNORE);
138 $parser->add('/"[^"\\n\\r]*"/', $this->IGNORE);
139 // remove comments
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');
151 // apply the above
152 $script = $parser->exec($script);
153
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");
163 // done
164 return $parser->exec($script);
165 }
166
167 // http://doc.spip.org/@_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')
173 );
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');
178 // quick ref
179 $encoded = $keywords['encoded'];
180
181 $parser->add($regexp,
182 array(
183 'fn' => '_replace_encoded',
184 'data' => $encoded
185 )
186 );
187 return $parser->exec($script);
188 }
189
190 // http://doc.spip.org/@_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);
195 // create the parser
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'];
203
204 // encode
205 $parser->add($regexp,
206 array(
207 'fn' => '_replace_encoded',
208 'data' => $encoded
209 )
210 );
211 if (empty($script)) return $script;
212 else {
213 //$res = $parser->exec($script);
214 //$res = $this->_bootStrap($res, $keywords);
215 //return $res;
216 return $this->_bootStrap($parser->exec($script), $keywords);
217 }
218 }
219
220 // http://doc.spip.org/@_analyze
221 function _analyze($script, $regexp, $encode) {
222 // analyse
223 // retreive all words in the script
224 $all = array();
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
230 if (!empty($all)) {
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
237 do {
238 --$i;
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++;
248 }
249 // increment the word counter
250 $this->_count[$word]++;
251 } while ($i > 0);
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);
259 do {
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;
265 }
266 } while ($i);
267
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
274 // but equivalent.
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'));
279 $j = 0;
280 // because there are "protected" words in the list
281 // we must add the sorted words around them
282 do {
283 if (!isset($_sorted[$i]))
284 $_sorted[$i] = substr($unsorted[$j++], 1);
285 $_encoded[$_sorted[$i]] = $values[$i];
286 } while (++$i < count($unsorted));
287 }
288 return array(
289 'sorted' => $_sorted,
290 'encoded' => $_encoded,
291 'protected' => $_protected);
292 }
293
294 var $_count = array();
295 // http://doc.spip.org/@_sortWords
296 function _sortWords($match1, $match2) {
297 return $this->_count[$match2] - $this->_count[$match1];
298 }
299
300 // build the boot function used for loading and decoding
301 // http://doc.spip.org/@_bootStrap
302 function _bootStrap($packed, $keywords) {
303 $ENCODE = $this->_safeRegExp('$encode\\($count\\)');
304
305 // $packed: the packed script
306 $packed = "'" . $this->_escape($packed) . "'";
307
308 // $ascii: base for encoding
309 $ascii = min(count($keywords['sorted']), $this->_encoding);
310 if ($ascii == 0) $ascii = 1;
311
312 // $count: number of words contained in the script
313 $count = count($keywords['sorted']);
314
315 // $keywords: list of words contained in the script
316 foreach ($keywords['protected'] as $i=>$value) {
317 $keywords['sorted'][$i] = '';
318 }
319 // convert from a string to an array
320 ksort($keywords['sorted']);
321 $keywords = "'" . implode('|',$keywords['sorted']) . "'.split('|')";
322
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)' : '');
328
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
336 elseif ($ascii < 36)
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...
340 if ($count == 0)
341 $decode = preg_replace($this->_safeRegExp('($count)\\s*=\\s*1'), '$1=0', $decode, 1);
342 }
343
344 // boot function
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);
350 }
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);
355 }
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);
360 } else {
361 // perform the encoding inline
362 $unpack = preg_replace($ENCODE, $inline, $unpack);
363 }
364 // pack the boot function too
365 $unpackPacker = new JavaScriptPacker($unpack, 0, false, true);
366 $unpack = $unpackPacker->pack();
367
368 // arguments
369 $params = array($packed, $ascii, $count, $keywords);
370 if ($this->_fastDecode) {
371 $params[] = 0;
372 $params[] = '{}';
373 }
374 $params = implode(',', $params);
375
376 // the whole thing
377 return 'eval(' . $unpack . '(' . $params . "))\n";
378 }
379
380 var $buffer;
381 // http://doc.spip.org/@_insertFastDecode
382 function _insertFastDecode($match) {
383 return '{' . $this->buffer . ';';
384 }
385 // http://doc.spip.org/@_insertFastEncode
386 function _insertFastEncode($match) {
387 return '{$encode=' . $this->buffer . ';';
388 }
389
390 // mmm.. ..which one do i need ??
391 // http://doc.spip.org/@_getEncoder
392 function _getEncoder($ascii) {
393 return $ascii > 10 ? $ascii > 36 ? $ascii > 62 ?
394 '_encode95' : '_encode62' : '_encode36' : '_encode10';
395 }
396
397 // zero encoding
398 // characters: 0123456789
399 // http://doc.spip.org/@_encode10
400 function _encode10($charCode) {
401 return $charCode;
402 }
403
404 // inherent base36 support
405 // characters: 0123456789abcdefghijklmnopqrstuvwxyz
406 // http://doc.spip.org/@_encode36
407 function _encode36($charCode) {
408 return base_convert($charCode, 10, 36);
409 }
410
411 // hitch a ride on base36 and add the upper case alpha characters
412 // characters: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
413 // http://doc.spip.org/@_encode62
414 function _encode62($charCode) {
415 $res = '';
416 if ($charCode >= $this->_encoding) {
417 $res = $this->_encode62((int)($charCode / $this->_encoding));
418 }
419 $charCode = $charCode % $this->_encoding;
420
421 if ($charCode > 35)
422 return $res . chr($charCode + 29);
423 else
424 return $res . base_convert($charCode, 10, 36);
425 }
426
427 // use high-ascii values
428 // characters: ¬°¬¢¬£¬§¬•¬¶¬ß¬®¬©¬™¬´¬¨¬≠¬Æ¬Ø¬∞¬±¬≤¬≥¬¥¬µ¬∂¬∑¬∏¬π¬∫¬ª¬º¬Ω¬æ¬ø√Ä√?√Ç√É√Ñ√Ö√Ü√á√à√â√ä√ã√å√?√é√?√?√ë√í√ì√î√ï√ñ√ó√ò√ô√ö√õ√ú√?√û√ü√†√°√¢√£√§√•√¶√ß√®√©√™√´√¨√≠√Æ√Ø√∞√±√≤√≥√¥√µ√∂√∑√∏√π√∫√ª√º√Ω√æ
429 // http://doc.spip.org/@_encode95
430 function _encode95($charCode) {
431 $res = '';
432 if ($charCode >= $this->_encoding)
433 $res = $this->_encode95($charCode / $this->_encoding);
434
435 return $res . chr(($charCode % $this->_encoding) + 161);
436 }
437
438 // http://doc.spip.org/@_safeRegExp
439 function _safeRegExp($string) {
440 return '/'.preg_replace('/\$/', '\\\$', $string).'/';
441 }
442
443 // http://doc.spip.org/@_encodePrivate
444 function _encodePrivate($charCode) {
445 return "_" . $charCode;
446 }
447
448 // protect characters used by the parser
449 // http://doc.spip.org/@_escape
450 function _escape($script) {
451 return preg_replace('/([\\\\\'])/', '\\\$1', $script);
452 }
453
454 // protect high-ascii characters already in the script
455 // http://doc.spip.org/@_escape95
456 function _escape95($script) {
457 return preg_replace_callback(
458 '/[\\xa1-\\xff]/',
459 array(&$this, '_escape95Bis'),
460 $script
461 );
462 }
463 // http://doc.spip.org/@_escape95Bis
464 function _escape95Bis($match) {
465 return '\x'.((string)dechex(ord($match)));
466 }
467
468
469 // http://doc.spip.org/@_getJSFunction
470 function _getJSFunction($aName) {
471 $func = 'JSFUNCTION'.$aName;
472 if (isset($this->$func)){
473 return $this->$func;
474 }
475 else
476 return '';
477 }
478
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.
486
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) {
492 while ($count--) {
493 if ($keywords[$count]) {
494 $packed = $packed.replace(new RegExp(\'\\\\b\' + $encode($count) + \'\\\\b\', \'g\'), $keywords[$count]);
495 }
496 }
497 return $packed;
498 }';
499 /*
500 'function($packed, $ascii, $count, $keywords, $encode, $decode) {
501 while ($count--)
502 if ($keywords[$count])
503 $packed = $packed.replace(new RegExp(\'\\\\b\' + $encode($count) + \'\\\\b\', \'g\'), $keywords[$count]);
504 return $packed;
505 }';
506 */
507
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
511 while ($count--) {
512 $decode[$encode($count)] = $keywords[$count] || $encode($count);
513 }
514 // global replacement function
515 $keywords = [function ($encoded) {return $decode[$encoded]}];
516 // generic match
517 $encode = function () {return \'\\\\w+\'};
518 // reset the loop counter - we are now doing a global replace
519 $count = 1;
520 }
521 ';
522 //};
523 /*
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]}];
529 // generic match
530 $encode = function () {return\'\\\\w+\'};
531 // reset the loop counter - we are now doing a global replace
532 $count = 1;
533 }';
534 */
535
536 // zero encoding
537 // characters: 0123456789
538 var $JSFUNCTION_encode10 = 'function($charCode) {
539 return $charCode;
540 }';//;';
541
542 // inherent base36 support
543 // characters: 0123456789abcdefghijklmnopqrstuvwxyz
544 var $JSFUNCTION_encode36 = 'function($charCode) {
545 return $charCode.toString(36);
546 }';//;';
547
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));
553 }';
554
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);
560 }';
561
562 }
563
564
565 class ParseMaster {
566 var $ignoreCase = false;
567 var $escapeChar = '';
568
569 // constants
570 var $EXPRESSION = 0;
571 var $REPLACEMENT = 1;
572 var $LENGTH = 2;
573
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
580 var $QUOTE = '/\'/';
581 var $DELETED = '/\\x01[^\\x01]*\\x01/';//g
582
583 // http://doc.spip.org/@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);
588
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))
600 ? '"' : "'";
601 $replacement = array(
602 'fn' => '_backReferences',
603 'data' => array(
604 'replacement' => $replacement,
605 'length' => $length,
606 'quote' => $quote
607 )
608 );
609 }
610 }
611 }
612 // pass the modified arguments
613 if (!empty($expression)) $this->_add($expression, $replacement, $length);
614 else $this->_add('/^$/', $replacement, $length);
615 }
616
617 // http://doc.spip.org/@exec
618 function exec($string) {
619 // execute the global replacement
620 $this->_escaped = array();
621
622 // simulate the _patterns.toSTring of Dean
623 $regexp = '/';
624 foreach ($this->_patterns as $reg) {
625 $regexp .= '(' . substr($reg[$this->EXPRESSION], 1, -1) . ')|';
626 }
627 $regexp = substr($regexp, 0, -1) . '/';
628 $regexp .= ($this->ignoreCase) ? 'i' : '';
629
630 $string = $this->_escape($string, $this->escapeChar);
631 $string = preg_replace_callback(
632 $regexp,
633 array(
634 &$this,
635 '_replacement'
636 ),
637 $string
638 );
639 $string = $this->_unescape($string, $this->escapeChar);
640
641 return preg_replace($this->DELETED, '', $string);
642 }
643
644 // http://doc.spip.org/@reset
645 function reset() {
646 // clear the patterns collection so that this object may be re-used
647 $this->_patterns = array();
648 }
649
650 // private
651 var $_escaped = array(); // escaped characters
652 var $_patterns = array(); // patterns stored by index
653
654 // create and add a new pattern to the patterns collection
655 // http://doc.spip.org/@_add
656 function _add() {
657 $arguments = func_get_args();
658 $this->_patterns[] = $arguments;
659 }
660
661 // this is the global replace function (it's quite complicated)
662 // http://doc.spip.org/@_replacement
663 function _replacement($arguments) {
664 if (empty($arguments)) return '';
665
666 $i = 1; $j = 0;
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];
673
674 if (is_array($replacement) && isset($replacement['fn'])) {
675
676 if (isset($replacement['data'])) $this->buffer = $replacement['data'];
677 return call_user_func(array(&$this, $replacement['fn']), $arguments, $i);
678
679 } elseif (is_int($replacement)) {
680 return $arguments[$replacement + $i];
681
682 }
683 $delete = ($this->escapeChar == '' ||
684 strpos($arguments[$i], $this->escapeChar) === false)
685 ? '' : "\x01" . $arguments[$i] . "\x01";
686 return $delete . $replacement;
687
688 // skip over references to sub-expressions
689 } else {
690 $i += $pattern[$this->LENGTH];
691 }
692 }
693 }
694
695 // http://doc.spip.org/@_backReferences
696 function _backReferences($match, $offset) {
697 $replacement = $this->buffer['replacement'];
698 $quote = $this->buffer['quote'];
699 $i = $this->buffer['length'];
700 while ($i) {
701 $replacement = str_replace('$'.$i--, $match[$offset + $i], $replacement);
702 }
703 return $replacement;
704 }
705
706 // http://doc.spip.org/@_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];
711 }
712
713 // http://doc.spip.org/@_replace_encoded
714 function _replace_encoded($match, $offset) {
715 return $this->buffer[$match[$offset]];
716 }
717
718
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
721 var $buffer;
722
723 // encode escaped characters
724 // http://doc.spip.org/@_escape
725 function _escape($string, $escapeChar) {
726 if ($escapeChar) {
727 $this->buffer = $escapeChar;
728 return preg_replace_callback(
729 '/\\' . $escapeChar . '(.)' .'/',
730 array(&$this, '_escapeBis'),
731 $string
732 );
733
734 } else {
735 return $string;
736 }
737 }
738 // http://doc.spip.org/@_escapeBis
739 function _escapeBis($match) {
740 $this->_escaped[] = $match[1];
741 return $this->buffer;
742 }
743
744 // decode escaped characters
745 // http://doc.spip.org/@_unescape
746 function _unescape($string, $escapeChar) {
747 if ($escapeChar) {
748 $regexp = '/'.'\\'.$escapeChar.'/';
749 $this->buffer = array('escapeChar'=> $escapeChar, 'i' => 0);
750 return preg_replace_callback
751 (
752 $regexp,
753 array(&$this, '_unescapeBis'),
754 $string
755 );
756
757 } else {
758 return $string;
759 }
760 }
761 // http://doc.spip.org/@_unescapeBis
762 function _unescapeBis() {
763 if (isset($this->_escaped[$this->buffer['i']])
764 && $this->_escaped[$this->buffer['i']] != '')
765 {
766 $temp = $this->_escaped[$this->buffer['i']];
767 } else {
768 $temp = '';
769 }
770 $this->buffer['i']++;
771 return $this->buffer['escapeChar'] . $temp;
772 }
773
774 // http://doc.spip.org/@_internalEscape
775 function _internalEscape($string) {
776 return preg_replace($this->ESCAPE, '', $string);
777 }
778 }
779 ?>