UrlValidator.php 4.13 KB
Newer Older
w  
Qiang Xue committed
1 2
<?php
/**
w  
Qiang Xue committed
3
 * UrlValidator class file.
w  
Qiang Xue committed
4 5
 *
 * @link http://www.yiiframework.com/
w  
Qiang Xue committed
6
 * @copyright Copyright &copy; 2008-2012 Yii Software LLC
w  
Qiang Xue committed
7 8 9
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
10 11
namespace yii\validators;

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 38 39 40 41 42 43 44 45
	 */
	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
46
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
47 48
	 * @param string $attribute the attribute being validated
	 */
w  
Qiang Xue committed
49
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
50 51
	{
		$value = $object->$attribute;
w  
Qiang Xue committed
52
		if ($this->allowEmpty && $this->isEmpty($value)) {
w  
Qiang Xue committed
53
			return;
w  
Qiang Xue committed
54 55
		}
		if (($value = $this->validateValue($value)) !== false) {
w  
Qiang Xue committed
56
			$object->$attribute = $value;
w  
Qiang Xue committed
57 58
		}
		else {
w  
Qiang Xue committed
59 60 61 62 63 64 65
			$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} is not a valid URL.');
			$this->addError($object, $attribute, $message);
		}
	}

	/**
	 * Validates a static value to see if it is a valid URL.
w  
Qiang Xue committed
66
	 * Note that this method does not respect [[allowEmpty]] property.
w  
Qiang Xue committed
67 68 69 70 71 72
	 * 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
73 74 75
		// 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
76
				$value = $this->defaultScheme . '://' . $value;
w  
Qiang Xue committed
77
			}
w  
Qiang Xue committed
78

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

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

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

		if (strpos($this->pattern, '{schemes}') !== false)
			$pattern = str_replace('{schemes}', '(' . implode('|', $this->validSchemes) . ')', $this->pattern);
		else
			$pattern = $this->pattern;

		$js = "
if(!value.match($pattern)) {
w  
Qiang Xue committed
115
	messages.push(" . json_encode($message) . ");
w  
Qiang Xue committed
116 117 118 119 120 121
}
";
		if ($this->defaultScheme !== null)
		{
			$js = "
if(!value.match(/:\\/\\//)) {
w  
Qiang Xue committed
122
	value=" . json_encode($this->defaultScheme) . "+'://'+value;
w  
Qiang Xue committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
}
$js
";
		}

		if ($this->allowEmpty)
		{
			$js = "
if($.trim(value)!='') {
	$js
}
";
		}

		return $js;
	}
}