CoreFiles by mySites.guru
ToIdn.php66 lines, 1,989 bytes, from Joomla_4.4.0-beta2-Beta-Full_Package.zip
Raw History Compare with 4.4.0-beta1
md5
57c9cd6eec4651c8e2fd3fbe6f6a7465
sha1
0d0d3e0e73b34a1cfeaddc21df5cd627552a5de6
sha256
e1cbae27216872e800ecbff501841b1625659f1e87b390b446387566110969af
<?php
namespace Algo26\IdnaConvert;

use Algo26\IdnaConvert\Exception\InvalidCharacterException;
use Algo26\IdnaConvert\Exception\InvalidIdnVersionException;
use Algo26\IdnaConvert\Punycode\ToPunycode;
use Algo26\IdnaConvert\TranscodeUnicode\TranscodeUnicode;

class ToIdn extends AbstractIdnaConvert implements IdnaConvertInterface
{
    /** @var TranscodeUnicode */
    private $unicodeTransCoder;

    /** @var ToPunycode */
    private $punycodeEncoder;

    /**
     * @throws InvalidIdnVersionException
     */
    public function __construct($idnVersion = null)
    {
        $this->unicodeTransCoder = new TranscodeUnicode();
        $this->punycodeEncoder = new ToPunycode($idnVersion);
    }

    /**
     * @param string $host
     *
     * @return string
     * @throws InvalidCharacterException
     * @throws Exception\AlreadyPunycodeException
     */
    public function convert(string $host): string
    {
        if (strlen($host) === 0) {
            return $host;
        }

        if (strpos('/', $host) !== false
            || strpos(':', $host) !== false
            || strpos('?', $host) !== false
            || strpos('@', $host) !== false
        ) {
            throw new InvalidCharacterException('Neither email addresses nor URLs are allowed', 205);
        }

        // These three punctuation characters are treated like the dot
        $host = str_replace(['。', '.', '。'], '.', $host);

        // Operate per label
        $hostLabels = explode('.', $host);
        foreach ($hostLabels as $index => $label) {
            $asUcs4Array = $this->unicodeTransCoder->convert(
                $label,
                $this->unicodeTransCoder::FORMAT_UTF8,
                $this->unicodeTransCoder::FORMAT_UCS4_ARRAY
            );
            $encoded = $this->punycodeEncoder->convert($asUcs4Array);
            if ($encoded) {
                $hostLabels[$index] = $encoded;
            }
        }

        return implode('.', $hostLabels);
    }
}

The same content is in 239 releases, Joomla 4.0.0-alpha11 to 5.4.12. See them all, or every version of this file.