Ejemplo n.º 1
0
 public function isValid($value)
 {
     if (null === $value) {
         return true;
     }
     return parent::isValid($value);
 }
Ejemplo n.º 2
0
 /**
  * Returns a Hostname
  *
  * @param string $value
  */
 public function __construct($value)
 {
     $validator = new Validator(array('allow' => Validator::ALLOW_DNS | Validator::ALLOW_LOCAL));
     if (false === $validator->isValid($value)) {
         throw new InvalidNativeArgumentException($value, array('string (valid hostname)'));
     }
     $this->value = $value;
 }
Ejemplo n.º 3
0
 /**
  * Returns a Hostname
  *
  * @param string $value
  */
 public function __construct($value)
 {
     $validator = new Hostname(array('allow' => Hostname::ALLOW_DNS | Hostname::ALLOW_LOCAL));
     if (false === $validator->isValid($value)) {
         throw new InvalidValueObjectsArgumentException($value, 'Hostname');
     }
     parent::__construct($value);
 }
Ejemplo n.º 4
0
 /**
  * Add Server
  *
  * @param string $host
  * @param int $port
  * @return MemcachedOptions
  * @throws Exception\InvalidArgumentException
  */
 public function addServer($host, $port = 11211)
 {
     $hostNameValidator = new Hostname(array('allow' => Hostname::ALLOW_ALL));
     if (!$hostNameValidator->isValid($host)) {
         throw new Exception\InvalidArgumentException(sprintf('%s expects a valid hostname: %s', __METHOD__, implode("\n", $hostNameValidator->getMessages())));
     }
     if (!is_numeric($port) || $port <= 0) {
         throw new Exception\InvalidArgumentException(sprintf('%s expects a positive integer', __METHOD__));
     }
     $this->servers[] = array($host, $port);
     return $this;
 }
Ejemplo n.º 5
0
 /**
  * @group ZF-11796
  */
 public function testIDNSI()
 {
     $validator = new Hostname(Hostname::ALLOW_ALL);
     $this->assertTrue($validator->isValid('Test123.si'));
     $this->assertTrue($validator->isValid('țest123.si'));
     $this->assertTrue($validator->isValid('tĕst123.si'));
     $this->assertTrue($validator->isValid('tàrø.si'));
     $this->assertFalse($validator->isValid('رات.si'));
 }
Ejemplo n.º 6
0
 /**
  * Check if an address is a valid DNS hostname
  *
  * @param  string $host
  * @return boolean
  */
 protected static function isValidDnsHostname($host)
 {
     $validator = new Validator\Hostname(array('allow' => Validator\Hostname::ALLOW_DNS | Validator\Hostname::ALLOW_LOCAL));
     return $validator->isValid($host);
 }
Ejemplo n.º 7
0
 /**
  * Set session.cookie_domain
  * 
  * @param  string $cookieDomain 
  * @return StandardConfiguration
  * @throws SessionException
  */
 public function setCookieDomain($cookieDomain)
 {
     if (!is_string($cookieDomain)) {
         throw new Exception\InvalidArgumentException('Invalid cookie domain: must be a string');
     }
     $validator = new HostnameValidator(HostnameValidator::ALLOW_ALL);
     if (!empty($cookieDomain) && !$validator->isValid($cookieDomain)) {
         throw new Exception\InvalidArgumentException('Invalid cookie domain: ' . implode('; ', $validator->getMessages()));
     }
     $this->_cookieDomain = $cookieDomain;
     $this->setStorageOption('cookie_domain', $cookieDomain);
     return $this;
 }
Ejemplo n.º 8
0
    /**
     * validateHost()
     * 
     * @param $host
     * @return bool
     */
    public static function validateHost($host)
    {
        // If the host is empty, then it is considered invalid
        if (strlen($host) === 0) {
            return false;
        }

        // Check the host against the allowed values; delegated to Zend_Filter.
        $validate = new Hostname(Hostname::ALLOW_ALL);

        return $validate->isValid($host);
    }
Ejemplo n.º 9
0
 /**
  * Provide an alternate Parameter Container implementation for server parameters in this object,
  * (this is NOT the primary API for value setting, for that see getServer())
  *
  * @param  ParametersInterface $server
  * @return Request
  */
 public function setServer(ParametersInterface $server)
 {
     $this->serverParams = $server;
     // This seems to be the only way to get the Authorization header on Apache
     if (function_exists('apache_request_headers')) {
         $apacheRequestHeaders = apache_request_headers();
         if (!isset($this->serverParams['HTTP_AUTHORIZATION'])) {
             if (isset($apacheRequestHeaders['Authorization'])) {
                 $this->serverParams->set('HTTP_AUTHORIZATION', $apacheRequestHeaders['Authorization']);
             } elseif (isset($apacheRequestHeaders['authorization'])) {
                 $this->serverParams->set('HTTP_AUTHORIZATION', $apacheRequestHeaders['authorization']);
             }
         }
     }
     // set headers
     $headers = array();
     foreach ($server as $key => $value) {
         if ($value && strpos($key, 'HTTP_') === 0) {
             if (strpos($key, 'HTTP_COOKIE') === 0) {
                 // Cookies are handled using the $_COOKIE superglobal
                 continue;
             }
             $name = strtr(substr($key, 5), '_', ' ');
             $name = strtr(ucwords(strtolower($name)), ' ', '-');
         } elseif ($value && strpos($key, 'CONTENT_') === 0) {
             $name = substr($key, 8);
             // Content-
             $name = 'Content-' . ($name == 'MD5' ? $name : ucfirst(strtolower($name)));
         } else {
             continue;
         }
         $headers[$name] = $value;
     }
     $this->getHeaders()->addHeaders($headers);
     // set method
     if (isset($this->serverParams['REQUEST_METHOD'])) {
         $this->setMethod($this->serverParams['REQUEST_METHOD']);
     }
     // set HTTP version
     if (isset($this->serverParams['SERVER_PROTOCOL']) && strpos($this->serverParams['SERVER_PROTOCOL'], self::VERSION_10) !== false) {
         $this->setVersion(self::VERSION_10);
     }
     // set URI
     $uri = new HttpUri();
     // URI scheme
     if (!empty($this->serverParams['HTTPS']) && $this->serverParams['HTTPS'] !== 'off' || !empty($this->serverParams['HTTP_X_FORWARDED_PROTO']) && $this->serverParams['HTTP_X_FORWARDED_PROTO'] == 'https') {
         $scheme = 'https';
     } else {
         $scheme = 'http';
     }
     $uri->setScheme($scheme);
     // URI host & port
     $host = null;
     $port = null;
     // Set the host
     if ($this->getHeaders()->get('host')) {
         $host = $this->getHeaders()->get('host')->getFieldValue();
         // works for regname, IPv4 & IPv6
         if (preg_match('|\\:(\\d+)$|', $host, $matches)) {
             $host = substr($host, 0, -1 * (strlen($matches[1]) + 1));
             $port = (int) $matches[1];
         }
         // set up a validator that check if the hostname is legal (not spoofed)
         $hostnameValidator = new HostnameValidator(array('allow' => HostnameValidator::ALLOW_ALL, 'useIdnCheck' => false, 'useTldCheck' => false));
         // If invalid. Reset the host & port
         if (!$hostnameValidator->isValid($host)) {
             $host = null;
             $port = null;
         }
     }
     if (!$host && isset($this->serverParams['SERVER_NAME'])) {
         $host = $this->serverParams['SERVER_NAME'];
         if (isset($this->serverParams['SERVER_PORT'])) {
             $port = (int) $this->serverParams['SERVER_PORT'];
         }
         // Check for missinterpreted IPv6-Address
         // Reported at least for Safari on Windows
         if (isset($this->serverParams['SERVER_ADDR']) && preg_match('/^\\[[0-9a-fA-F\\:]+\\]$/', $host)) {
             $host = '[' . $this->serverParams['SERVER_ADDR'] . ']';
             if ($port . ']' == substr($host, strrpos($host, ':') + 1)) {
                 // The last digit of the IPv6-Address has been taken as port
                 // Unset the port so the default port can be used
                 $port = null;
             }
         }
     }
     $uri->setHost($host);
     $uri->setPort($port);
     // URI path
     $requestUri = $this->getRequestUri();
     if (($qpos = strpos($requestUri, '?')) !== false) {
         $requestUri = substr($requestUri, 0, $qpos);
     }
     $uri->setPath($requestUri);
     // URI query
     if (isset($this->serverParams['QUERY_STRING'])) {
         $uri->setQuery($this->serverParams['QUERY_STRING']);
     }
     $this->setUri($uri);
     return $this;
 }
Ejemplo n.º 10
0
 /**
  * @see ZF-7277
  */
 public function testDifferentIconvEncoding()
 {
     iconv_set_encoding('internal_encoding', 'ISO8859-1');
     $validator = new Hostname();
     $valuesExpected = array(array(true, array('bürger.com', 'hãllo.com', 'hållo.com')), array(true, array('bÜrger.com', 'hÃllo.com', 'hÅllo.com')), array(false, array('hãllo.lt', 'bürger.lt', 'hãllo.lt')));
     foreach ($valuesExpected as $element) {
         foreach ($element[1] as $input) {
             $this->assertEquals($element[0], $validator->isValid($input), implode("\n", $validator->getMessages()) . $input);
         }
     }
 }
Ejemplo n.º 11
0
 /**
  * @group ZF-11334
  */
 public function testSupportsIpv6AddressesWhichContainHexDigitF()
 {
     $validator = new Hostname(Hostname::ALLOW_ALL);
     $this->assertTrue($validator->isValid('FEDC:BA98:7654:3210:FEDC:BA98:7654:3210'));
     $this->assertTrue($validator->isValid('1080:0:0:0:8:800:200C:417A'));
     $this->assertTrue($validator->isValid('3ffe:2a00:100:7031::1'));
     $this->assertTrue($validator->isValid('1080::8:800:200C:417A'));
     $this->assertTrue($validator->isValid('::192.9.5.5'));
     $this->assertTrue($validator->isValid('::FFFF:129.144.52.38'));
     $this->assertTrue($validator->isValid('2010:836B:4179::836B:4179'));
 }
Ejemplo n.º 12
0
 /**
  * Test extended greek charset
  *
  * @group ZF-11751
  */
 public function testExtendedGreek()
 {
     $validator = new Hostname(Hostname::ALLOW_ALL);
     $this->assertEquals(true, $validator->isValid('ῆὧὰῧῲ.com'));
 }
Ejemplo n.º 13
0
 /**
  * Checks if the given hostname is valid.
  *
  * @param      $hostname
  * @param bool $allowIp       Allow IP Addresses
  * @param bool $allowWildcard Allow Wildcard names
  * @param bool $allowLocal    Allow local Addresses
  * @param bool $looseCheck    Switches checking off checks only if
  *                            given variable is not empty
  *
  * @return bool
  */
 public function _checkHostname($hostname, $allowIp = true, $allowWildcard = false, $allowLocal = true, $looseCheck = false)
 {
     if ('.' == substr($hostname, -1)) {
         return false;
     }
     if ($looseCheck === true) {
         return 0 == strlen(trim($hostname)) ? false : true;
     }
     $n = new Hostname();
     $n->setAllow(Hostname::ALLOW_DNS);
     if ($allowIp) {
         $n->setAllow(Hostname::ALLOW_IP | Hostname::ALLOW_DNS);
     }
     if ($allowWildcard) {
         $hostname = str_replace('*.', 'bogus', $hostname);
     }
     if (!filter_var($hostname, FILTER_VALIDATE_IP)) {
         $exp = explode('.', $hostname);
         if (1 == count($exp)) {
             if (!$allowLocal) {
                 return false;
             }
             $hostname = $hostname . '.syseleven.de';
         }
     }
     if (!$n->isValid($hostname)) {
         return false;
     }
     return true;
 }
Ejemplo n.º 14
0
 /**
  * @group ZF-10267
  */
 public function testURI()
 {
     $valuesExpected = array(array(Hostname::ALLOW_URI, true, array('localhost', 'example.com', '~ex%20ample')), array(Hostname::ALLOW_URI, false, array('§bad', 'don?t.know', 'thisisaverylonghostnamewhichextendstwohundredfiftysixcharactersandthereforshouldnotbeallowedbythisvalidatorbecauserfc3986limitstheallowedcharacterstoalimitoftwohunderedfiftysixcharactersinsumbutifthistestwouldfailthenitshouldreturntruewhichthrowsanexceptionbytheunittest')));
     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);
         }
     }
 }
Ejemplo n.º 15
0
 public function testIDNIT()
 {
     $validator = new Hostname(Hostname::ALLOW_ALL);
     $this->assertTrue($validator->isValid('plainascii.it'));
     $this->assertTrue($validator->isValid('città-caffè.it'));
     $this->assertTrue($validator->isValid('edgetest-àâäèéêëìîïòôöùûüæœçÿß.it'));
     $this->assertFalse($validator->isValid('رات.it'));
 }