navigatecms /lib/external/idna_convert/src/IdnaConvert.php

Language PHP Lines 406
MD5 Hash a9473cc50392d9419e40121529a3e21e Estimated Cost $6,381 (why?)
Repository https://bitbucket.org/navigatecms/navigatecms View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php

// {{{ license

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
//
// +----------------------------------------------------------------------+
// | This library is free software; you can redistribute it and/or modify |
// | it under the terms of the GNU Lesser General Public License as       |
// | published by the Free Software Foundation; either version 2.1 of the |
// | License, or (at your option) any later version.                      |
// |                                                                      |
// | This library is distributed in the hope that it will be useful, but  |
// | WITHOUT ANY WARRANTY; without even the implied warranty of           |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// |                                                                      |
// | You should have received a copy of the GNU Lesser General Public     |
// | License along with this library; if not, write to the Free Software  |
// | Foundation, Inc., 51 Franklin St, Boston, MA 02110, United States    |
// +----------------------------------------------------------------------+
//
// }}}

/**
 * Encode/decode Internationalized Domain Names.
 *
 * The class allows to convert internationalized domain names
 * (see RFC 3490 for details) as they can be used with various registries worldwide
 * to be translated between their original (localized) form and their encoded form
 * as it will be used in the DNS (Domain Name System).
 *
 * The class provides two public methods, encode() and decode(), which do exactly
 * what you would expect them to do. You are allowed to use complete domain names,
 * simple strings and complete email addresses as well. That means, that you might
 * use any of the following notations:
 *
 * - www.nรถrgler.com
 * - xn--nrgler-wxa
 * - xn--brse-5qa.xn--knrz-1ra.info
 *
 * Unicode input might be given as either UTF-8 string, UCS-4 string or UCS-4 array.
 * Unicode output is available in the same formats.
 * You can select your preferred format via {@link set_paramter()}.
 *
 * ACE input and output is always expected to be ASCII.
 *
 * @author  Matthias Sommerfeld <mso@phlylabs.de>
 * @copyright 2004-2016 phlyLabs Berlin, http://phlylabs.de
 * @version 1.0.1-dev 2016-01-12
 */

namespace Mso\IdnaConvert;

class IdnaConvert {

    const Version = '1.1.0';
    const SubVersion = 'main';

    // Internal settings, do not touch!
    protected $encoding = 'utf8';          // Default input charset is UTF-8
    protected $strictMode = false;         // Behave strict or not
    protected $idnVersion = '2008';          // Can be either 2003 (old) or 2008 (default)

    protected $NamePrepData = null;
    protected $UnicodeTranscoder = null;

    /**
     * the constructor
     *
     * @param array|null $params Parameters to control the class' behaviour
     * @since 0.5.2
     */
    public function __construct($params = null)
    {
        $this->UnicodeTranscoder = new UnicodeTranscoder();

        // Kept for backwarsds compatibility. Consider using the setter methods instead.
        if (!empty($params) && is_array($params)) {
            if (isset($params['encoding'])) {
                $this->setEncoding($params['encoding']);
            }

            if (isset($params['idn_version'])) {
                $this->setIdnVersion($params['idn_version']);
            }

            if (isset($params['strict_mode'])) {
                $this->setStrictMode($params['strict_mode']);
            }
        }

        $this->setIdnVersion($this->idnVersion);
    }

    public function getClassVersion()
    {
        return self::Version.'-'.self::SubVersion;
    }

    /**
     * @return string
     */
    public function getEncoding()
    {
        return $this->encoding;
    }

    /**
     * @param string $encoding
     */
    public function setEncoding($encoding)
    {
        switch ($encoding) {
            case 'utf8':
            case 'ucs4_string':
            case 'ucs4_array':
                $this->encoding = $encoding;
                break;
            default:
                throw new \InvalidArgumentException(sprintf('Invalid encoding %s', $encoding));
        }
    }

    /**
     * @return boolean
     */
    public function isStrictMode()
    {
        return $this->strictMode;
    }

    /**
     * @param boolean $strictMode
     */
    public function setStrictMode($strictMode)
    {
        $this->strictMode = ($strictMode) ? true : false;
    }

    /**
     * @return int
     */
    public function getIdnVersion()
    {
        return $this->idnVersion;
    }

    /**
     * @param int $idnVersion
     */
    public function setIdnVersion($idnVersion)
    {
        if (in_array($idnVersion, array('2003', '2008'))) {
            if (is_null($this->NamePrepData) || $idnVersion != $this->idnVersion) {
                $this->NamePrepData = null; // Ought to destroy the object's reference
                // Re-instantiate with different data set
                $this->NamePrepData = ($idnVersion == 2003)
                        ? new NamePrepData2003()
                        : new NamePrepData();
            }

            $this->idnVersion = $idnVersion;

        } else {
            throw new \InvalidArgumentException(sprintf('Invalid IDN version %d', $idnVersion));
        }
    }

    /**
     * Decode a given ACE domain name
     * @param string $input  Domain name (ACE string)
     * [@param string $one_time_encoding  Desired output encoding]
     * @return string  Decoded Domain name (UTF-8 or UCS-4)
     */
    public function decode($input, $one_time_encoding = null)
    {
        $punyCode = $this->punycodeFactory();

        // Optionally set
        if ($one_time_encoding) {
            switch ($one_time_encoding) {
                case 'utf8':
                case 'ucs4_string':
                case 'ucs4_array':
                    break;
                default:
                    throw new \InvalidArgumentException(sprintf('Invalid encoding %s', $one_time_encoding));
            }
        }
        // Make sure to drop any newline characters around
        $input = trim($input);

        // Negotiate input and try to determine, whether it is a plain string,
        // an email address or something like a complete URL
        if (strpos($input, '@')) { // Maybe it is an email address
            // No no in strict mode
            if ($this->strictMode) {
                throw new \InvalidArgumentException('Only individual domain name parts can be handled in strict mode');
            }
            list ($email_pref, $input) = explode('@', $input, 2);
            $arr = explode('.', $input);
            foreach ($arr as $k => $v) {
                $conv = $punyCode->decode($v);
                if ($conv) {
                    $arr[$k] = $conv;
                }
            }
            $input = join('.', $arr);
            $arr = explode('.', $email_pref);
            foreach ($arr as $k => $v) {
                $conv = $punyCode->decode($v);
                if ($conv) {
                    $arr[$k] = $conv;
                }
            }
            $email_pref = join('.', $arr);
            $return = $email_pref . '@' . $input;
        } elseif (preg_match('![:\./]!', $input)) { // Or a complete domain name (with or without paths / parameters)
            // No no in strict mode
            if ($this->strictMode) {
                throw new \InvalidArgumentException('Only individual domain name parts can be handled in strict mode');
            }
            $parsed = parse_url($input);
            if (isset($parsed['host'])) {
                $arr = explode('.', $parsed['host']);
                foreach ($arr as $k => $v) {
                    $conv = $punyCode->decode($v);
                    if ($conv) {
                        $arr[$k] = $conv;
                    }
                }
                $parsed['host'] = join('.', $arr);
                $return = (empty($parsed['scheme']) ? '' : $parsed['scheme'] . (strtolower($parsed['scheme']) == 'mailto' ? ':' : '://')).
                        (empty($parsed['user']) ? '' : $parsed['user'] . (empty($parsed['pass']) ? '' : ':' . $parsed['pass']) . '@').
                        $parsed['host'].
                        (empty($parsed['port']) ? '' : ':' . $parsed['port']).
                        (empty($parsed['path']) ? '' : $parsed['path']).
                        (empty($parsed['query']) ? '' : '?' . $parsed['query']).
                        (empty($parsed['fragment']) ? '' : '#' . $parsed['fragment']);
            } else { // parse_url seems to have failed, try without it
                $arr = explode('.', $input);
                foreach ($arr as $k => $v) {
                    $conv = $punyCode->decode($v);
                    if ($conv) {
                        $arr[$k] = $conv;
                    }
                }
                $return = join('.', $arr);
            }
        } else { // Otherwise we consider it being a pure domain name string
            $return = $punyCode->decode($input);
            if (!$return) {
                $return = $input;
            }
        }
        // The output is UTF-8 by default, other output formats need conversion here
        // If one time encoding is given, use this, else the objects property
        $outputEncoding = ($one_time_encoding) ? $one_time_encoding : $this->encoding;
        switch ($outputEncoding) {
            case 'utf8':
                return $return; // break;
            case 'ucs4_string':
                return $this->UnicodeTranscoder->convert($return, 'utf8', 'ucs4');  // break;
            case 'ucs4_array':
                return $this->UnicodeTranscoder->convert($return, 'utf8', 'ucs4array');  // break;
            default:
                throw new \InvalidArgumentException(sprintf('Unsupported output encoding %s', $outputEncoding));
        }
    }

    /**
     * Encode a given UTF-8 domain name
     * @param string $decoded  Domain name (UTF-8 or UCS-4)
     * [@param boolean  $one_time_encoding  Desired input encoding, see {@link set_parameter}]
     * @return string   Encoded Domain name (ACE string)
     */
    public function encode($decoded, $one_time_encoding = false)
    {
        // Forcing conversion of input to UCS4 array
        // If one time encoding is given, use this, else the objects property
        $inputEncoding = $one_time_encoding ? $one_time_encoding : $this->encoding;
        switch ($inputEncoding) {
            case 'utf8':
                $decoded = $this->UnicodeTranscoder->convert($decoded, 'utf8', 'ucs4array');
                break;
            case 'ucs4_string':
                $decoded = $this->UnicodeTranscoder->convert($decoded, 'ucs4', 'ucs4array');
                break;
            case 'ucs4_array':
                break;
            default:
                throw new \InvalidArgumentException(sprintf('Unsupported input encoding %s', $inputEncoding));
        }

        // No input, no output, what else did you expect?
        if (empty($decoded)) {
            return '';
        }

        $punyCode = $this->punycodeFactory();

        // Anchors for iteration
        $last_begin = 0;
        // Output string
        $output = '';
        foreach ($decoded as $k => $v) {
            // Make sure to use just the plain dot
            switch ($v) {
                case 0x3002:
                case 0xFF0E:
                case 0xFF61:
                    $decoded[$k] = 0x2E;
                    // Right, no break here, the above are converted to dots anyway
                // Stumbling across an anchoring character
                case 0x2E:
                case 0x2F:
                case 0x3A:
                case 0x3F:
                case 0x40:
                    // Neither email addresses nor URLs allowed in strict mode
                    if ($this->strictMode) {
                        throw new \InvalidArgumentException('Neither email addresses nor URLs are allowed in strict mode.');
                    } else {
                        // Skip first char
                        if ($k) {
                            $encoded = $punyCode->encode(array_slice($decoded, $last_begin, (($k) - $last_begin)));
                            if ($encoded) {
                                $output .= $encoded;
                            } else {
                                $output .= $this->UnicodeTranscoder->convert(array_slice($decoded, $last_begin, (($k) - $last_begin)), 'ucs4array', 'utf8');
                            }
                            $output .= chr($decoded[$k]);
                        }
                        $last_begin = $k + 1;
                    }
            }
        }
        // Catch the rest of the string
        if ($last_begin) {
            $inp_len = sizeof($decoded);
            $encoded = $punyCode->encode(array_slice($decoded, $last_begin, (($inp_len) - $last_begin)));
            if ($encoded) {
                $output .= $encoded;
            } else {
                $output .= $this->UnicodeTranscoder->convert(array_slice($decoded, $last_begin, (($inp_len) - $last_begin)), 'ucs4array', 'utf8');
            }
            return $output;
        } else {
            if (false !== ($output = $punyCode->encode($decoded))) {
                return $output;
            } else {
                return $this->UnicodeTranscoder->convert($decoded, 'ucs4array', 'utf8');
            }
        }
    }

    /**
     * Mitigates a weakness of encode(), which cannot properly handle URIs but instead encodes their
     * path or query components, too.
     * @param string  $uri  Expects the URI as a UTF-8 (or ASCII) string
     * @return  string  The URI encoded to Punycode, everything but the host component is left alone
     * @since 0.6.4
     */
    public function encodeUri($uri)
    {
        $parsed = parse_url($uri);
        if (!isset($parsed['host'])) {
            throw new \InvalidArgumentException('The given string does not look like a URI');
        }
        $arr = explode('.', $parsed['host']);
        foreach ($arr as $k => $v) {
            $conv = $this->encode($v, 'utf8');
            if ($conv) {
                $arr[$k] = $conv;
            }
        }
        $parsed['host'] = join('.', $arr);
        $return = (empty($parsed['scheme']) ? '' : $parsed['scheme'] . (strtolower($parsed['scheme']) == 'mailto' ? ':' : '://')).
                (empty($parsed['user']) ? '' : $parsed['user'] . (empty($parsed['pass']) ? '' : ':' . $parsed['pass']) . '@').
                $parsed['host'].
                (empty($parsed['port']) ? '' : ':' . $parsed['port']).
                (empty($parsed['path']) ? '' : $parsed['path']).
                (empty($parsed['query']) ? '' : '?' . $parsed['query']).
                (empty($parsed['fragment']) ? '' : '#' . $parsed['fragment']);
        return $return;
    }

    /**
     * The actual punycode class is rather costly, as well as passing the huge nameprep database around.
     * This factory method allows to ease the burden when dealing with multiple IDN versions.
     *
     * @return \Mso\IdnaConvert\Punycode
     */
    protected function punycodeFactory()
    {
        static $instances = array();

        if (!isset($instances[$this->idnVersion])) {
            $instances[$this->idnVersion] = new Punycode($this->NamePrepData, $this->UnicodeTranscoder);
        }
        return $instances[$this->idnVersion];
    }

}
Back to Top