简体   繁体   中英

Zend 2 Hostname Validator for IDN Domains

I can register töst.tv as a domain in other words it is a valid domain name.

The Zend 2 Hostname Validator will return false on following example:

// create hostname validator
$oHostnameValidator = new \Zend\Validator\Hostname(array(
    'allow' => \Zend\Validator\Hostname::ALLOW_DNS,
    'useIdnCheck' => true,
    'useTldCheck' => false,
));

if(!$oHostnameValidator->isValid('töst.tv')) // isValid returns false
{
    print_r($oHostnameValidator->getMessages());
}

getMessages will return:

Array
(
    [hostnameInvalidHostnameSchema] => Die Eingabe scheint ein DNS Hostname zu sein, passt aber ...
    [hostnameInvalidLocalName] => Die Eingabe scheint kein gültiger lokaler Netzerkname zu...
)

I see that protected $validIdns does not include the tld tv (in class Zend\\Validator\\Hostname )

Is there a way to (update-safe) inject current valid idn checks into some tlds in the zend hostname validator?

Or is this a bug which should be reported?

Edit

i just extended the hostname validator (thanks to Wilt )

<?php

namespace yourNamespace;

class Hostname extends \Zend\Validator\Hostname
{
    /**
     * Sets validator options.
     *
     * @param int  $allow       OPTIONAL Set what types of hostname to allow (default ALLOW_DNS)
     * @param bool $useIdnCheck OPTIONAL Set whether IDN domains are validated (default true)
     * @param bool $useTldCheck Set whether the TLD element of a hostname is validated (default true)
     * @param Ip   $ipValidator OPTIONAL
     * @see http://www.iana.org/cctld/specifications-policies-cctlds-01apr02.htm  Technical Specifications for ccTLDs
     */
    public function __construct($options = array())
    {
        // call parent construct
        parent::__construct($options);

        // inject valid idns
        $this->_injectValidIDNs();
    }

    /**
     * inject new valid idns - use first DE validation as default (until we get the specified correct ones ...)
     */
    protected function _injectValidIDNs()
    {
        // inject TV validation
        if(!isset($this->validIdns['TV']))
        {
            $this->validIdns['TV'] = array(
                1 => array_values($this->validIdns['DE'])[0],
            );
        }
    }
}

You can make an issue on GitHub and make a pull request for the Zend\\Validator\\Hostname class in which you add the value that according to you should be also inside the $validIdns array.

Otherwise you can also extend the existing class in your project and overwrite the existing $validIdns value with your custom one:

<?php

namespace My\Validator;

class HostName extends \Zend\Validator\Hostname
{
    protected $validIdns = [
        //...your custom value for TV + existing ones...
    ]
}

Now you can use it like this:

$oHostnameValidator = new \My\Validator\Hostname(array(
    'allow' => \My\Validator\Hostname::ALLOW_DNS,
    'useIdnCheck' => true,
    'useTldCheck' => false,
));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM