Subversion Repositories ALCASAR

Rev

Rev 2976 | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
2775 rexy 1
<?php
2
/* 25 October 2011. version 1.1-FF4
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
 *
18
 * ----------------------------------------------------------------------
19
 * changelog:
20
 * 1.1 : correct a bug, '\0' packed then unpacked becomes '\'.
21
 * 1.1-FF4 : Firefox 4 fix, Copyright 2011, Mieczyslaw Nalewaj
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
 * # need PHP 5 . Tested with PHP 5.1.2, 5.1.3, 5.1.4, 5.2.3
56
 *
57
 * # The packed result may be different than with the Dean Edwards
58
 *   version, but with the same length. The reason is that the PHP
59
 *   function usort to sort array don't necessarily preserve the
60
 *   original order of two equal member. The Javascript sort function
61
 *   in fact preserve this order (but that's not require by the
62
 *   ECMAScript standard). So the encoded keywords order can be
63
 *   different in the two results.
64
 *
65
 * # Be careful with the 'High ASCII' Level encoding if you use
66
 *   UTF-8 in your files...
67
 */
68
 
69
 
70
class JavaScriptPacker
71
{
72
    // constants
73
    const IGNORE = '$1';
74
 
75
    // validate parameters
76
    private $_script = '';
77
    private $_encoding = 62;
78
    private $_fastDecode = true;
79
    private $_specialChars = false;
80
 
81
    private $LITERAL_ENCODING = array(
82
        'None' => 0,
83
        'Numeric' => 10,
84
        'Normal' => 62,
85
        'High ASCII' => 95
86
    );
87
 
88
    public function __construct($_script, $_encoding = 62, $_fastDecode = true, $_specialChars = false)
89
    {
90
        $this->_script = $_script . "\n";
91
        if (array_key_exists($_encoding, $this->LITERAL_ENCODING))
92
            $_encoding = $this->LITERAL_ENCODING[$_encoding];
93
        $this->_encoding = min((int) $_encoding, 95);
94
        $this->_fastDecode = $_fastDecode;
95
        $this->_specialChars = $_specialChars;
96
    }
97
 
98
    public function pack()
99
    {
100
        $this->_addParser('_basicCompression');
101
        if ($this->_specialChars)
102
            $this->_addParser('_encodeSpecialChars');
103
        if ($this->_encoding)
104
            $this->_addParser('_encodeKeywords');
105
 
106
        // go!
107
        return $this->_pack($this->_script);
108
    }
109
 
110
    // apply all parsing routines
111
    private function _pack($script)
112
    {
113
        for ($i = 0; isset($this->_parsers[$i]); $i++) {
114
            $script = call_user_func(array(&$this, $this->_parsers[$i]), $script);
115
        }
116
 
117
        return $script;
118
    }
119
 
120
    // keep a list of parsing functions, they'll be executed all at once
121
    private $_parsers = array();
122
    private function _addParser($parser)
123
    {
124
        $this->_parsers[] = $parser;
125
    }
126
 
127
    // zero encoding - just removal of white space and comments
128
    private function _basicCompression($script)
129
    {
130
        $parser = new ParseMaster();
131
        // make safe
132
        $parser->escapeChar = '\\';
133
        // protect strings
134
        $parser->add('/\'[^\'\\n\\r]*\'/', self::IGNORE);
135
        $parser->add('/"[^"\\n\\r]*"/', self::IGNORE);
136
        // remove comments
137
        $parser->add('/\\/\\/[^\\n\\r]*[\\n\\r]/', ' ');
138
        $parser->add('/\\/\\*[^*]*\\*+([^\\/][^*]*\\*+)*\\//', ' ');
139
        // protect regular expressions
140
        $parser->add('/\\s+(\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/g?i?)/', '$2'); // IGNORE
141
        $parser->add('/[^\\w\\x24\\/\'"*)\\?:]\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/g?i?/', self::IGNORE);
142
        // remove: ;;; doSomething();
143
        if ($this->_specialChars) $parser->add('/;;;[^\\n\\r]+[\\n\\r]/');
144
        // remove redundant semi-colons
145
        $parser->add('/\\(;;\\)/', self::IGNORE); // protect for (;;) loops
146
        $parser->add('/;+\\s*([};])/', '$2');
147
        // apply the above
148
        $script = $parser->exec($script);
149
 
150
        // remove white-space
151
        $parser->add('/(\\b|\\x24)\\s+(\\b|\\x24)/', '$2 $3');
152
        $parser->add('/([+\\-])\\s+([+\\-])/', '$2 $3');
153
        $parser->add('/\\s+/', '');
154
        // done
155
        return $parser->exec($script);
156
    }
157
 
158
    private function _encodeSpecialChars($script)
159
    {
160
        $parser = new ParseMaster();
161
        // replace: $name -> n, $$name -> na
3037 rexy 162
        $parser->add(
163
            '/((\\x24+)([a-zA-Z$_]+))(\\d*)/',
164
            array('fn' => '_replace_name')
2775 rexy 165
        );
166
        // replace: _name -> _0, double-underscore (__name) is ignored
167
        $regexp = '/\\b_[A-Za-z\\d]\\w*/';
168
        // build the word list
169
        $keywords = $this->_analyze($script, $regexp, '_encodePrivate');
170
        // quick ref
171
        $encoded = $keywords['encoded'];
172
 
3037 rexy 173
        $parser->add(
174
            $regexp,
2775 rexy 175
            array(
176
                'fn' => '_replace_encoded',
177
                'data' => $encoded
178
            )
179
        );
180
 
181
        return $parser->exec($script);
182
    }
183
 
184
    private function _encodeKeywords($script)
185
    {
186
        // escape high-ascii values already in the script (i.e. in strings)
187
        if ($this->_encoding > 62)
188
            $script = $this->_escape95($script);
189
        // create the parser
190
        $parser = new ParseMaster();
191
        $encode = $this->_getEncoder($this->_encoding);
192
        // for high-ascii, don't encode single character low-ascii
193
        $regexp = ($this->_encoding > 62) ? '/\\w\\w+/' : '/\\w+/';
194
        // build the word list
195
        $keywords = $this->_analyze($script, $regexp, $encode);
196
        $encoded = $keywords['encoded'];
197
 
198
        // encode
3037 rexy 199
        $parser->add(
200
            $regexp,
2775 rexy 201
            array(
202
                'fn' => '_replace_encoded',
203
                'data' => $encoded
204
            )
205
        );
206
        if (empty($script)) return $script;
207
        else {
208
            //$res = $parser->exec($script);
209
            //$res = $this->_bootStrap($res, $keywords);
210
            //return $res;
211
            return $this->_bootStrap($parser->exec($script), $keywords);
212
        }
213
    }
214
 
215
    private function _analyze($script, $regexp, $encode)
216
    {
217
        // analyse
218
        // retreive all words in the script
219
        $all = array();
220
        preg_match_all($regexp, $script, $all);
221
        $_sorted = array(); // list of words sorted by frequency
222
        $_encoded = array(); // dictionary of word->encoding
223
        $_protected = array(); // instances of "protected" words
224
        $all = $all[0]; // simulate the javascript comportement of global match
225
        if (!empty($all)) {
226
            $unsorted = array(); // same list, not sorted
227
            $protected = array(); // "protected" words (dictionary of word->"word")
228
            $value = array(); // dictionary of charCode->encoding (eg. 256->ff)
229
            $this->_count = array(); // word->count
230
            $i = count($all); $j = 0; //$word = null;
231
            // count the occurrences - used for sorting later
232
            do {
233
                --$i;
234
                $word = '$' . $all[$i];
235
                if (!isset($this->_count[$word])) {
236
                    $this->_count[$word] = 0;
237
                    $unsorted[$j] = $word;
238
                    // make a dictionary of all of the protected words in this script
239
                    //  these are words that might be mistaken for encoding
240
                    //if (is_string($encode) && method_exists($this, $encode))
241
                    $values[$j] = call_user_func(array(&$this, $encode), $j);
242
                    $protected['$' . $values[$j]] = $j++;
243
                }
244
                // increment the word counter
245
                $this->_count[$word]++;
246
            } while ($i > 0);
247
            // prepare to sort the word list, first we must protect
248
            //  words that are also used as codes. we assign them a code
249
            //  equivalent to the word itself.
250
            // e.g. if "do" falls within our encoding range
251
            //      then we store keywords["do"] = "do";
252
            // this avoids problems when decoding
253
            $i = count($unsorted);
254
            do {
255
                $word = $unsorted[--$i];
256
                if (isset($protected[$word]) /*!= null*/) {
257
                    $_sorted[$protected[$word]] = substr($word, 1);
258
                    $_protected[$protected[$word]] = true;
259
                    $this->_count[$word] = 0;
260
                }
261
            } while ($i);
262
 
263
            // sort the words by frequency
264
            // Note: the javascript and php version of sort can be different :
265
            // in php manual, usort :
266
            // " If two members compare as equal,
267
            // their order in the sorted array is undefined."
268
            // so the final packed script is different of the Dean's javascript version
269
            // but equivalent.
270
            // the ECMAscript standard does not guarantee this behaviour,
271
            // and thus not all browsers (e.g. Mozilla versions dating back to at
272
            // least 2003) respect this.
273
            usort($unsorted, array(&$this, '_sortWords'));
274
            $j = 0;
275
            // because there are "protected" words in the list
276
            //  we must add the sorted words around them
277
            do {
278
                if (!isset($_sorted[$i]))
279
                    $_sorted[$i] = substr($unsorted[$j++], 1);
280
                $_encoded[$_sorted[$i]] = $values[$i];
281
            } while (++$i < count($unsorted));
282
        }
283
 
284
        return array(
285
            'sorted'  => $_sorted,
286
            'encoded' => $_encoded,
287
            'protected' => $_protected);
288
    }
289
 
290
    private $_count = array();
291
    private function _sortWords($match1, $match2)
292
    {
293
        return $this->_count[$match2] - $this->_count[$match1];
294
    }
295
 
296
    // build the boot function used for loading and decoding
297
    private function _bootStrap($packed, $keywords)
298
    {
299
        $ENCODE = $this->_safeRegExp('$encode\\($count\\)');
300
 
301
        // $packed: the packed script
302
        $packed = "'" . $this->_escape($packed) . "'";
303
 
304
        // $ascii: base for encoding
305
        $ascii = min(count($keywords['sorted']), $this->_encoding);
306
        if ($ascii == 0) $ascii = 1;
307
 
308
        // $count: number of words contained in the script
309
        $count = count($keywords['sorted']);
310
 
311
        // $keywords: list of words contained in the script
312
        foreach ($keywords['protected'] as $i=>$value) {
313
            $keywords['sorted'][$i] = '';
314
        }
315
        // convert from a string to an array
316
        ksort($keywords['sorted']);
317
        $keywords = "'" . implode('|', $keywords['sorted']) . "'.split('|')";
318
 
319
        $encode = ($this->_encoding > 62) ? '_encode95' : $this->_getEncoder($ascii);
320
        $encode = $this->_getJSFunction($encode);
321
        $encode = preg_replace('/_encoding/', '$ascii', $encode);
322
        $encode = preg_replace('/arguments\\.callee/', '$encode', $encode);
323
        $inline = '\\$count' . ($ascii > 10 ? '.toString(\\$ascii)' : '');
324
 
325
        // $decode: code snippet to speed up decoding
326
        if ($this->_fastDecode) {
327
            // create the decoder
328
            $decode = $this->_getJSFunction('_decodeBody');
329
            if ($this->_encoding > 62)
330
                $decode = preg_replace('/\\\\w/', '[\\xa1-\\xff]', $decode);
331
            // perform the encoding inline for lower ascii values
332
            elseif ($ascii < 36)
333
                $decode = preg_replace($ENCODE, $inline, $decode);
334
            // special case: when $count==0 there are no keywords. I want to keep
335
            //  the basic shape of the unpacking funcion so i'll frig the code...
336
            if ($count == 0)
337
                $decode = preg_replace($this->_safeRegExp('($count)\\s*=\\s*1'), '$1=0', $decode, 1);
338
        }
339
 
340
        // boot function
341
        $unpack = $this->_getJSFunction('_unpack');
342
        if ($this->_fastDecode) {
343
            // insert the decoder
344
            $this->buffer = $decode;
345
            $unpack = preg_replace_callback('/\\{/', array(&$this, '_insertFastDecode'), $unpack, 1);
346
        }
347
        $unpack = preg_replace('/"/', "'", $unpack);
348
        if ($this->_encoding > 62) { // high-ascii
349
            // get rid of the word-boundaries for regexp matches
350
            $unpack = preg_replace('/\'\\\\\\\\b\'\s*\\+|\\+\s*\'\\\\\\\\b\'/', '', $unpack);
351
        }
352
        if ($ascii > 36 || $this->_encoding > 62 || $this->_fastDecode) {
353
            // insert the encode function
354
            $this->buffer = $encode;
355
            $unpack = preg_replace_callback('/\\{/', array(&$this, '_insertFastEncode'), $unpack, 1);
356
        } else {
357
            // perform the encoding inline
358
            $unpack = preg_replace($ENCODE, $inline, $unpack);
359
        }
360
        // pack the boot function too
361
        $unpackPacker = new JavaScriptPacker($unpack, 0, false, true);
362
        $unpack = $unpackPacker->pack();
363
 
364
        // arguments
365
        $params = array($packed, $ascii, $count, $keywords);
366
        if ($this->_fastDecode) {
367
            $params[] = 0;
368
            $params[] = '{}';
369
        }
370
        $params = implode(',', $params);
371
 
372
        // the whole thing
373
        //Firefox 4 fix, old: return 'eval(' . $unpack . '(' . $params . "))\n";
374
        return "(typeof setTimeout=='function'?setTimeout:eval)(" . $unpack . "(" . $params . "));\n";
375
    }
376
 
377
    private $buffer;
378
    private function _insertFastDecode($match)
379
    {
380
        return '{' . $this->buffer . ';';
381
    }
382
    private function _insertFastEncode($match)
383
    {
384
        return '{$encode=' . $this->buffer . ';';
385
    }
386
 
387
    // mmm.. ..which one do i need ??
388
    private function _getEncoder($ascii)
389
    {
390
        return $ascii > 10 ? $ascii > 36 ? $ascii > 62 ?
391
               '_encode95' : '_encode62' : '_encode36' : '_encode10';
392
    }
393
 
394
    // zero encoding
395
    // characters: 0123456789
396
    private function _encode10($charCode)
397
    {
398
        return $charCode;
399
    }
400
 
401
    // inherent base36 support
402
    // characters: 0123456789abcdefghijklmnopqrstuvwxyz
403
    private function _encode36($charCode)
404
    {
405
        return base_convert($charCode, 10, 36);
406
    }
407
 
408
    // hitch a ride on base36 and add the upper case alpha characters
409
    // characters: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
410
    private function _encode62($charCode)
411
    {
412
        $res = '';
413
        if ($charCode >= $this->_encoding) {
414
            $res = $this->_encode62((int) ($charCode / $this->_encoding));
415
        }
416
        $charCode = $charCode % $this->_encoding;
417
 
418
        if ($charCode > 35)
419
            return $res . chr($charCode + 29);
420
        else
421
            return $res . base_convert($charCode, 10, 36);
422
    }
423
 
424
    // use high-ascii values
425
    // characters: ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ
426
    private function _encode95($charCode)
427
    {
428
        $res = '';
429
        if ($charCode >= $this->_encoding)
430
            $res = $this->_encode95($charCode / $this->_encoding);
431
 
432
        return $res . chr(($charCode % $this->_encoding) + 161);
433
    }
434
 
435
    private function _safeRegExp($string)
436
    {
437
        return '/'.preg_replace('/\$/', '\\\$', $string).'/';
438
    }
439
 
440
    private function _encodePrivate($charCode)
441
    {
442
        return "_" . $charCode;
443
    }
444
 
445
    // protect characters used by the parser
446
    private function _escape($script)
447
    {
448
        return preg_replace('/([\\\\\'])/', '\\\$1', $script);
449
    }
450
 
451
    // protect high-ascii characters already in the script
452
    private function _escape95($script)
453
    {
454
        return preg_replace_callback(
455
            '/[\\xa1-\\xff]/',
456
            array(&$this, '_escape95Bis'),
457
            $script
458
        );
459
    }
460
    private function _escape95Bis($match)
461
    {
462
        return '\x'.((string) dechex(ord($match)));
463
    }
464
 
465
    private function _getJSFunction($aName)
466
    {
467
        if (defined('self::JSFUNCTION'.$aName))
468
            return constant('self::JSFUNCTION'.$aName);
469
        else
470
            return '';
471
    }
472
 
473
    // JavaScript Functions used.
474
    // Note : In Dean's version, these functions are converted
475
    // with 'String(aFunctionName);'.
476
    // This internal conversion complete the original code, ex :
477
    // 'while (aBool) anAction();' is converted to
478
    // 'while (aBool) { anAction(); }'.
479
    // The JavaScript functions below are corrected.
480
 
481
    // unpacking function - this is the boot strap function
482
    //  data extracted from this packing routine is passed to
483
    //  this function when decoded in the target
484
    // NOTE ! : without the ';' final.
485
    const JSFUNCTION_unpack =
486
 
487
'function ($packed, $ascii, $count, $keywords, $encode, $decode) {
488
    while ($count--) {
489
        if ($keywords[$count]) {
490
            $packed = $packed.replace(new RegExp(\'\\\\b\' + $encode($count) + \'\\\\b\', \'g\'), $keywords[$count]);
491
        }
492
    }
493
 
494
    return $packed;
495
}';
496
/*
497
'function ($packed, $ascii, $count, $keywords, $encode, $decode) {
498
    while ($count--)
499
        if ($keywords[$count])
500
            $packed = $packed.replace(new RegExp(\'\\\\b\' + $encode($count) + \'\\\\b\', \'g\'), $keywords[$count]);
501
 
502
    return $packed;
503
}';
504
*/
505
 
506
    // code-snippet inserted into the unpacker to speed up decoding
507
    const JSFUNCTION_decodeBody =
508
//_decode = function () {
509
// does the browser support String.replace where the
510
//  replacement value is a function?
511
 
512
'    if (!\'\'.replace(/^/, String)) {
513
        // decode all the values we need
514
        while ($count--) {
515
            $decode[$encode($count)] = $keywords[$count] || $encode($count);
516
        }
517
        // global replacement function
518
        $keywords = [function ($encoded) {return $decode[$encoded]}];
519
        // generic match
520
        $encode = function () {return \'\\\\w+\'};
521
        // reset the loop counter -  we are now doing a global replace
522
        $count = 1;
523
    }
524
';
525
//};
526
/*
2976 rexy 527
'    if (!\'\'.replace(/^/, String)) {
2775 rexy 528
        // decode all the values we need
529
        while ($count--) $decode[$encode($count)] = $keywords[$count] || $encode($count);
530
        // global replacement function
531
        $keywords = [function ($encoded) {return $decode[$encoded]}];
532
        // generic match
533
        $encode = function () {return\'\\\\w+\'};
534
        // reset the loop counter -  we are now doing a global replace
535
        $count = 1;
536
    }';
537
*/
538
 
539
     // zero encoding
540
     // characters: 0123456789
541
     const JSFUNCTION_encode10 =
542
'function ($charCode) {
543
    return $charCode;
544
}';//;';
545
 
546
     // inherent base36 support
547
     // characters: 0123456789abcdefghijklmnopqrstuvwxyz
548
     const JSFUNCTION_encode36 =
549
'function ($charCode) {
550
    return $charCode.toString(36);
551
}';//;';
552
 
553
    // hitch a ride on base36 and add the upper case alpha characters
554
    // characters: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
555
    const JSFUNCTION_encode62 =
556
'function ($charCode) {
557
    return ($charCode < _encoding ? \'\' : arguments.callee(parseInt($charCode / _encoding))) +
558
    (($charCode = $charCode % _encoding) > 35 ? String.fromCharCode($charCode + 29) : $charCode.toString(36));
559
}';
560
 
561
    // use high-ascii values
562
    // characters: ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ
563
    const JSFUNCTION_encode95 =
564
'function ($charCode) {
565
    return ($charCode < _encoding ? \'\' : arguments.callee($charCode / _encoding)) +
566
        String.fromCharCode($charCode % _encoding + 161);
567
}';
568
 
569
}
570
 
571
class ParseMaster
572
{
573
    public $ignoreCase = false;
574
    public $escapeChar = '';
575
 
576
    // constants
577
    const EXPRESSION = 0;
578
    const REPLACEMENT = 1;
579
    const LENGTH = 2;
580
 
581
    // used to determine nesting levels
582
    private $GROUPS = '/\\(/';//g
583
    private $SUB_REPLACE = '/\\$\\d/';
584
    private $INDEXED = '/^\\$\\d+$/';
585
    private $TRIM = '/([\'"])\\1\\.(.*)\\.\\1\\1$/';
586
    private $ESCAPE = '/\\\./';//g
587
    private $QUOTE = '/\'/';
588
    private $DELETED = '/\\x01[^\\x01]*\\x01/';//g
589
 
590
    public function add($expression, $replacement = '')
591
    {
592
        // count the number of sub-expressions
593
        //  - add one because each pattern is itself a sub-expression
594
        $length = 1 + preg_match_all($this->GROUPS, $this->_internalEscape((string) $expression), $out);
595
 
596
        // treat only strings $replacement
597
        if (is_string($replacement)) {
598
            // does the pattern deal with sub-expressions?
599
            if (preg_match($this->SUB_REPLACE, $replacement)) {
600
                // a simple lookup? (e.g. "$2")
601
                if (preg_match($this->INDEXED, $replacement)) {
602
                    // store the index (used for fast retrieval of matched strings)
603
                    $replacement = (int) (substr($replacement, 1)) - 1;
604
                } else { // a complicated lookup (e.g. "Hello $2 $1")
605
                    // build a function to do the lookup
606
                    $quote = preg_match($this->QUOTE, $this->_internalEscape($replacement))
607
                             ? '"' : "'";
608
                    $replacement = array(
609
                        'fn' => '_backReferences',
610
                        'data' => array(
611
                            'replacement' => $replacement,
612
                            'length' => $length,
613
                            'quote' => $quote
614
                        )
615
                    );
616
                }
617
            }
618
        }
619
        // pass the modified arguments
620
        if (!empty($expression)) $this->_add($expression, $replacement, $length);
621
        else $this->_add('/^$/', $replacement, $length);
622
    }
623
 
624
    public function exec($string)
625
    {
626
        // execute the global replacement
627
        $this->_escaped = array();
628
 
629
        // simulate the _patterns.toSTring of Dean
630
        $regexp = '/';
631
        foreach ($this->_patterns as $reg) {
632
            $regexp .= '(' . substr($reg[self::EXPRESSION], 1, -1) . ')|';
633
        }
634
        $regexp = substr($regexp, 0, -1) . '/';
635
        $regexp .= ($this->ignoreCase) ? 'i' : '';
636
 
637
        $string = $this->_escape($string, $this->escapeChar);
638
        $string = preg_replace_callback(
639
            $regexp,
640
            array(
641
                &$this,
642
                '_replacement'
643
            ),
644
            $string
645
        );
646
        $string = $this->_unescape($string, $this->escapeChar);
647
 
648
        return preg_replace($this->DELETED, '', $string);
649
    }
650
 
651
    public function reset()
652
    {
653
        // clear the patterns collection so that this object may be re-used
654
        $this->_patterns = array();
655
    }
656
 
657
    // private
658
    private $_escaped = array();  // escaped characters
659
    private $_patterns = array(); // patterns stored by index
660
 
661
    // create and add a new pattern to the patterns collection
662
    private function _add()
663
    {
664
        $arguments = func_get_args();
665
        $this->_patterns[] = $arguments;
666
    }
667
 
668
    // this is the global replace function (it's quite complicated)
669
    private function _replacement($arguments)
670
    {
671
        if (empty($arguments)) return '';
672
 
673
        $i = 1; $j = 0;
674
        // loop through the patterns
675
        while (isset($this->_patterns[$j])) {
676
            $pattern = $this->_patterns[$j++];
677
            // do we have a result?
678
            if (isset($arguments[$i]) && ($arguments[$i] != '')) {
679
                $replacement = $pattern[self::REPLACEMENT];
680
 
681
                if (is_array($replacement) && isset($replacement['fn'])) {
682
 
683
                    if (isset($replacement['data'])) $this->buffer = $replacement['data'];
684
                    return call_user_func(array(&$this, $replacement['fn']), $arguments, $i);
685
 
686
                } elseif (is_int($replacement)) {
687
                    return $arguments[$replacement + $i];
688
 
689
                }
690
                $delete = ($this->escapeChar == '' ||
691
                           strpos($arguments[$i], $this->escapeChar) === false)
692
                        ? '' : "\x01" . $arguments[$i] . "\x01";
693
 
694
                return $delete . $replacement;
695
 
696
            // skip over references to sub-expressions
697
            } else {
698
                $i += $pattern[self::LENGTH];
699
            }
700
        }
701
    }
702
 
703
    private function _backReferences($match, $offset)
704
    {
705
        $replacement = $this->buffer['replacement'];
706
        $quote = $this->buffer['quote'];
707
        $i = $this->buffer['length'];
708
        while ($i) {
709
            $replacement = str_replace('$'.$i--, $match[$offset + $i], $replacement);
710
        }
711
 
712
        return $replacement;
713
    }
714
 
715
    private function _replace_name($match, $offset)
716
    {
717
        $length = strlen($match[$offset + 2]);
718
        $start = $length - max($length - strlen($match[$offset + 3]), 0);
719
 
720
        return substr($match[$offset + 1], $start, $length) . $match[$offset + 4];
721
    }
722
 
723
    private function _replace_encoded($match, $offset)
724
    {
725
        return $this->buffer[$match[$offset]];
726
    }
727
 
728
 
729
    // php : we cannot pass additional data to preg_replace_callback,
730
    // and we cannot use &$this in create_function, so let's go to lower level
731
    private $buffer;
732
 
733
    // encode escaped characters
734
    private function _escape($string, $escapeChar)
735
    {
736
        if ($escapeChar) {
737
            $this->buffer = $escapeChar;
738
 
739
            return preg_replace_callback(
740
                '/\\' . $escapeChar . '(.)' .'/',
741
                array(&$this, '_escapeBis'),
742
                $string
743
            );
744
 
745
        } else {
746
            return $string;
747
        }
748
    }
749
    private function _escapeBis($match)
750
    {
751
        $this->_escaped[] = $match[1];
752
 
753
        return $this->buffer;
754
    }
755
 
756
    // decode escaped characters
757
    private function _unescape($string, $escapeChar)
758
    {
759
        if ($escapeChar) {
760
            $regexp = '/'.'\\'.$escapeChar.'/';
761
            $this->buffer = array('escapeChar'=> $escapeChar, 'i' => 0);
762
 
763
            return preg_replace_callback(
764
                $regexp,
765
                array(&$this, '_unescapeBis'),
766
                $string
767
            );
768
 
769
        } else {
770
            return $string;
771
        }
772
    }
773
    private function _unescapeBis()
774
    {
775
        if (isset($this->_escaped[$this->buffer['i']])
776
            && $this->_escaped[$this->buffer['i']] != '')
777
        {
778
             $temp = $this->_escaped[$this->buffer['i']];
779
        } else {
780
            $temp = '';
781
        }
782
        $this->buffer['i']++;
783
 
784
        return $this->buffer['escapeChar'] . $temp;
785
    }
786
 
787
    private function _internalEscape($string)
788
    {
789
        return preg_replace($this->ESCAPE, '', $string);
790
    }
791
}