用于IDN域的Zend 2主机名验证器

时间:2015-11-18 13:41:13

标签: php zend-framework2 hostname

我可以将töst.tv注册为域名,换句话说,它是一个有效的域名。

Zend 2 Hostname Validator将在以下示例中返回false:

// 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将返回:

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

我发现protected $validIdns不包含tld tv(在课程Zend\Validator\Hostname中)

有没有办法(update-safe)将当前有效的idn检查注入zend hostname validator中的某些tld?

或者这是一个应该报告的错误?

修改

我刚刚扩展了主机名验证器(感谢 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],
            );
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以在GitHub上发出问题,并为Zend\Validator\Hostname类提取拉取请求,在该类中添加根据您应该也在$validIdns数组中的值。

否则,您还可以扩展项目中的现有类,并使用自定义的值覆盖现有的$validIdns值:

<?php

namespace My\Validator;

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

现在您可以像这样使用它:

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