UrlValidator.php 3.65 KB
Newer Older
w  
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
w  
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
8 9
namespace yii\validators;

Qiang Xue committed
10 11
use Yii;

w  
Qiang Xue committed
12
/**
w  
Qiang Xue committed
13
 * UrlValidator validates that the attribute value is a valid http or https URL.
w  
Qiang Xue committed
14 15
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
16
 * @since 2.0
w  
Qiang Xue committed
17
 */
w  
Qiang Xue committed
18
class UrlValidator extends Validator
w  
Qiang Xue committed
19 20 21
{
	/**
	 * @var string the regular expression used to validate the attribute value.
w  
Qiang Xue committed
22 23
	 * The pattern may contain a `{schemes}` token that will be replaced
	 * by a regular expression which represents the [[validSchemes]].
w  
Qiang Xue committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37
	 */
	public $pattern = '/^{schemes}:\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)/i';
	/**
	 * @var array list of URI schemes which should be considered valid. By default, http and https
	 * are considered to be valid schemes.
	 **/
	public $validSchemes = array('http', 'https');
	/**
	 * @var string the default URI scheme. If the input doesn't contain the scheme part, the default
	 * scheme will be prepended to it (thus changing the input). Defaults to null, meaning a URL must
	 * contain the scheme part.
	 **/
	public $defaultScheme;

Qiang Xue committed
38 39 40 41 42 43 44 45 46 47 48 49

	/**
	 * Initializes the validator.
	 */
	public function init()
	{
		parent::init();
		if ($this->message === null) {
			$this->message = Yii::t('yii|{attribute} is not a valid URL.');
		}
	}

w  
Qiang Xue committed
50 51 52
	/**
	 * Validates the attribute of the object.
	 * If there is any error, the error message is added to the object.
w  
Qiang Xue committed
53
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
54 55
	 * @param string $attribute the attribute being validated
	 */
w  
Qiang Xue committed
56
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
57 58
	{
		$value = $object->$attribute;
Qiang Xue committed
59 60 61 62
		if ($this->validateValue($value)) {
			if ($this->defaultScheme !== null && strpos($value, '://') === false) {
				$object->$attribute = $this->defaultScheme . '://' . $value;
			}
Qiang Xue committed
63
		} else {
Qiang Xue committed
64
			$this->addError($object, $attribute, $this->message);
w  
Qiang Xue committed
65 66 67 68
		}
	}

	/**
Qiang Xue committed
69 70 71
	 * Validates the given value.
	 * @param mixed $value the value to be validated.
	 * @return boolean whether the value is valid.
w  
Qiang Xue committed
72 73 74
	 */
	public function validateValue($value)
	{
w  
Qiang Xue committed
75 76 77
		// make sure the length is limited to avoid DOS attacks
		if (is_string($value) && strlen($value) < 2000) {
			if ($this->defaultScheme !== null && strpos($value, '://') === false) {
w  
Qiang Xue committed
78
				$value = $this->defaultScheme . '://' . $value;
w  
Qiang Xue committed
79
			}
w  
Qiang Xue committed
80

w  
Qiang Xue committed
81
			if (strpos($this->pattern, '{schemes}') !== false) {
w  
Qiang Xue committed
82
				$pattern = str_replace('{schemes}', '(' . implode('|', $this->validSchemes) . ')', $this->pattern);
Qiang Xue committed
83
			} else {
w  
Qiang Xue committed
84
				$pattern = $this->pattern;
w  
Qiang Xue committed
85
			}
w  
Qiang Xue committed
86

w  
Qiang Xue committed
87
			if (preg_match($pattern, $value)) {
Qiang Xue committed
88
				return true;
w  
Qiang Xue committed
89
			}
w  
Qiang Xue committed
90 91 92 93 94 95
		}
		return false;
	}

	/**
	 * Returns the JavaScript needed for performing client-side validation.
w  
Qiang Xue committed
96
	 * @param \yii\base\Model $object the data object being validated
w  
Qiang Xue committed
97 98
	 * @param string $attribute the name of the attribute to be validated.
	 * @return string the client-side validation script.
Qiang Xue committed
99
	 * @see \yii\Web\ActiveForm::enableClientValidation
w  
Qiang Xue committed
100 101 102
	 */
	public function clientValidateAttribute($object, $attribute)
	{
Qiang Xue committed
103
		$message = strtr($this->message, array(
w  
Qiang Xue committed
104
			'{attribute}' => $object->getAttributeLabel($attribute),
w  
Qiang Xue committed
105
			'{value}' => $object->$attribute,
w  
Qiang Xue committed
106 107
		));

Alexander Makarov committed
108
		if (strpos($this->pattern, '{schemes}') !== false) {
w  
Qiang Xue committed
109
			$pattern = str_replace('{schemes}', '(' . implode('|', $this->validSchemes) . ')', $this->pattern);
Alexander Makarov committed
110
		} else {
w  
Qiang Xue committed
111
			$pattern = $this->pattern;
Alexander Makarov committed
112
		}
w  
Qiang Xue committed
113 114 115

		$js = "
if(!value.match($pattern)) {
w  
Qiang Xue committed
116
	messages.push(" . json_encode($message) . ");
w  
Qiang Xue committed
117 118
}
";
Alexander Makarov committed
119
		if ($this->defaultScheme !== null) {
w  
Qiang Xue committed
120 121
			$js = "
if(!value.match(/:\\/\\//)) {
w  
Qiang Xue committed
122
	value=" . json_encode($this->defaultScheme) . "+'://'+value;
w  
Qiang Xue committed
123 124 125 126 127
}
$js
";
		}

Qiang Xue committed
128
		if ($this->skipOnEmpty) {
w  
Qiang Xue committed
129 130 131 132 133 134 135 136 137 138 139
			$js = "
if($.trim(value)!='') {
	$js
}
";
		}

		return $js;
	}
}