<?php

class DomainValidator extends CValidator
{
    public $type;
    public $pattern = '/^(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)/i';
    protected function validateAttribute($object, $attribute)
    {
        $value = $object->$attribute;
        $val = $this->validateValue($value);
        if ($val == false) {
            $message = (null !== $this->message) ? $this->message : Yii::t('yii', '{attribute} is not a valid Domain.');
            $this->addError($object, $attribute, $message);

            return;
        }
        $val = checkdnsrr("$value", $this->type);

        if ($val != 1) {
            $message = (null !== $this->message) ? $this->message : Yii::t('yii', '{attribute} is not a valid Domain.');
            $this->addError($object, $attribute, $message);
        }
    }
    public function validateValue($value)
    {
        if (is_string($value) && strlen($value) < 2000) {
            // make sure the length is limited to avoid DOS attacks

            if (preg_match($this->pattern, $value)) {
                return $value;
            }
        }

        return false;
    }
}
