UrlValidator.php 4.1 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;

w  
Qiang Xue committed
10
/**
w  
Qiang Xue committed
11
 * UrlValidator validates that the attribute value is a valid http or https URL.
w  
Qiang Xue committed
12 13
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
14
 * @since 2.0
w  
Qiang Xue committed
15
 */
w  
Qiang Xue committed
16
class UrlValidator extends Validator
w  
Qiang Xue committed
17 18 19
{
	/**
	 * @var string the regular expression used to validate the attribute value.
w  
Qiang Xue committed
20 21
	 * The pattern may contain a `{schemes}` token that will be replaced
	 * by a regular expression which represents the [[validSchemes]].
w  
Qiang Xue committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
	 */
	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;
	/**
	 * @var boolean whether the attribute value can be null or empty. Defaults to true,
	 * meaning that if the attribute is empty, it is considered valid.
	 */
	public $allowEmpty = true;

	/**
	 * Validates the attribute of the object.
	 * If there is any error, the error message is added to the object.
w  
Qiang Xue committed
44
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
45 46
	 * @param string $attribute the attribute being validated
	 */
w  
Qiang Xue committed
47
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
48 49
	{
		$value = $object->$attribute;
w  
Qiang Xue committed
50
		if ($this->allowEmpty && $this->isEmpty($value)) {
w  
Qiang Xue committed
51
			return;
w  
Qiang Xue committed
52 53
		}
		if (($value = $this->validateValue($value)) !== false) {
w  
Qiang Xue committed
54
			$object->$attribute = $value;
Qiang Xue committed
55
		} else {
56
			$message = ($this->message !== null) ? $this->message : \Yii::t('yii|{attribute} is not a valid URL.');
w  
Qiang Xue committed
57 58 59 60 61 62
			$this->addError($object, $attribute, $message);
		}
	}

	/**
	 * Validates a static value to see if it is a valid URL.
w  
Qiang Xue committed
63
	 * Note that this method does not respect [[allowEmpty]] property.
w  
Qiang Xue committed
64 65 66 67 68 69
	 * This method is provided so that you can call it directly without going through the model validation rule mechanism.
	 * @param mixed $value the value to be validated
	 * @return mixed false if the the value is not a valid URL, otherwise the possibly modified value ({@see defaultScheme})
	 */
	public function validateValue($value)
	{
w  
Qiang Xue committed
70 71 72
		// 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
73
				$value = $this->defaultScheme . '://' . $value;
w  
Qiang Xue committed
74
			}
w  
Qiang Xue committed
75

w  
Qiang Xue committed
76
			if (strpos($this->pattern, '{schemes}') !== false) {
w  
Qiang Xue committed
77
				$pattern = str_replace('{schemes}', '(' . implode('|', $this->validSchemes) . ')', $this->pattern);
Qiang Xue committed
78
			} else {
w  
Qiang Xue committed
79
				$pattern = $this->pattern;
w  
Qiang Xue committed
80
			}
w  
Qiang Xue committed
81

w  
Qiang Xue committed
82
			if (preg_match($pattern, $value)) {
w  
Qiang Xue committed
83
				return $value;
w  
Qiang Xue committed
84
			}
w  
Qiang Xue committed
85 86 87 88 89 90
		}
		return false;
	}

	/**
	 * Returns the JavaScript needed for performing client-side validation.
w  
Qiang Xue committed
91
	 * @param \yii\base\Model $object the data object being validated
w  
Qiang Xue committed
92 93
	 * @param string $attribute the name of the attribute to be validated.
	 * @return string the client-side validation script.
Qiang Xue committed
94
	 * @see \yii\Web\ActiveForm::enableClientValidation
w  
Qiang Xue committed
95 96 97
	 */
	public function clientValidateAttribute($object, $attribute)
	{
98
		$message = ($this->message !== null) ? $this->message : \Yii::t('yii|{attribute} is not a valid URL.');
w  
Qiang Xue committed
99 100
		$message = strtr($message, array(
			'{attribute}' => $object->getAttributeLabel($attribute),
w  
Qiang Xue committed
101
			'{value}' => $object->$attribute,
w  
Qiang Xue committed
102 103
		));

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

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

Alexander Makarov committed
124
		if ($this->allowEmpty) {
w  
Qiang Xue committed
125 126 127 128 129 130 131 132 133 134 135
			$js = "
if($.trim(value)!='') {
	$js
}
";
		}

		return $js;
	}
}