Subversion Repositories ALCASAR

Rev

Rev 2976 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

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