Permalink
Browse files

[hotfix/ZF-6363] Zend_Validator_Hostname

- allow hostnames with ending '.'
  • Loading branch information...
1 parent 4fdd5ef commit 7882b336830e2f67e48bede18957c47ba1a19be9 @thomasweidner committed Jul 3, 2011
@@ -94,14 +94,14 @@ class Hostname extends AbstractValidator
const ALLOW_LOCAL = 4;
/**
- * Allows all types of hostnames
+ * Allows URI hostnames
*/
- const ALLOW_ALL = 7;
+ const ALLOW_URI = 8;
/**
* Allows all types of hostnames
*/
- const ALLOW_URI = 8;
+ const ALLOW_ALL = 15;
/**
* Array of valid top-level-domains
@@ -526,8 +526,27 @@ public function isValid($value)
}
}
- // Check input against DNS hostname schema
+ // Local hostnames are allowed to be partitial (ending '.')
+ if ($this->_options['allow'] & self::ALLOW_LOCAL) {
+ if (substr($value, -1) === '.') {
+ $value = substr($value, 0, -1);
+ if (substr($value, -1) === '.') {
+ // Empty hostnames (ending '..') are not allowed
+ $this->_error(self::INVALID_LOCAL_NAME);
+ return false;
+ }
+ }
+ }
+
$domainParts = explode('.', $value);
+
+ // Prevent partitial IP V4 adresses (ending '.')
+ if ((count($domainParts) == 4) && preg_match('/^[0-9.a-e:.]*$/i', $value) &&
+ $this->_options['ip']->setTranslator($this->getTranslator())->isValid($value)) {
+ $this->_error(self::INVALID_LOCAL_NAME);
+ }
+
+ // Check input against DNS hostname schema
if ((count($domainParts) > 1) && (strlen($value) >= 4) && (strlen($value) <= 254)) {
$status = false;
@@ -646,7 +665,7 @@ public function isValid($value)
}
// Check input against local network name schema; last chance to pass validation
- $regexLocal = '/^(([a-zA-Z0-9\x2d]{1,63}\x2e)*[a-zA-Z0-9\x2d]{1,63}){1,254}$/';
+ $regexLocal = '/^(([a-zA-Z0-9\x2d]{1,63}\x2e)*[a-zA-Z0-9\x2d]{1,63}[\x2e]{0,1}){1,254}$/';
$status = @preg_match($regexLocal, $value);
// If the input passes as a local network name, and local network names are allowed, then the
@@ -67,8 +67,8 @@
"'%value%' does not fit the date format '%format%'" => "'%value%' non corrisponde al formato data '%format%'",
// Zend_Validate_Db_Abstract
- "No record matching %value% was found" => "Non è stato trovato un record con valore %value%",
- "A record matching %value% was found" => "E' stato trovato un record con valore %value%",
+ "No record matching '%value%' was found" => "Non è stato trovata nessuna riga con valore '%value%'",
+ "A record matching '%value%' was found" => "E' già stata trovata una riga con valore '%value%'",
// Zend_Validate_Digits
"Invalid type given. String, integer or float expected" => "Tipo di dato non valido. Era atteso un dato di tipo string, float o integer",
@@ -398,4 +398,27 @@ public function testURI()
}
}
}
+
+ /**
+ * Ensure that a trailing "." in a local hostname is permitted
+ *
+ * @group ZF-6363
+ */
+ public function testTrailingDot()
+ {
+ $valuesExpected = array(
+ array(Hostname::ALLOW_ALL, true, array('example.', 'example.com.', '~ex%20ample.')),
+ array(Hostname::ALLOW_ALL, false, array('example..')),
+ array(Hostname::ALLOW_ALL, true, array('1.2.3.4.')),
+ array(Hostname::ALLOW_DNS, false, array('example..', '~ex%20ample..')),
+ array(Hostname::ALLOW_LOCAL, true, array('example.', 'example.com.')),
+ );
+
+ foreach ($valuesExpected as $element) {
+ $validator = new Hostname($element[0]);
+ foreach ($element[2] as $input) {
+ $this->assertEquals($element[1], $validator->isValid($input), implode("\n", $validator->getMessages()) . $input);
+ }
+ }
+ }
}
@@ -19,6 +19,8 @@
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
+use Zend\Locale\Locale;
+
/**
* @category Zend
* @package Zend_resources
@@ -76,7 +78,7 @@ public function setUp()
public function testIsLocale()
{
foreach ($this->_languages as $lang) {
- if (!Zend\Locale\Locale::isLocale($lang, true)) {
+ if (!\Zend\Locale\Locale::isLocale($lang, true)) {
$this->fail("Language directory '{$lang}' not a valid locale");
}
}

0 comments on commit 7882b33

Please sign in to comment.