/** * Validate a domain name value * * @return void * @throws \Magento\Framework\Model\Exception */ protected function _beforeSave() { $value = $this->getValue(); $validator = new \Zend\Validator\Hostname(\Zend\Validator\Hostname::ALLOW_ALL); // Empty value is treated valid and will be handled when read the value out if (!empty($value) && !$validator->isValid($value)) { throw new \Magento\Framework\Model\Exception('Invalid domain name: ' . join('; ', $validator->getMessages())); } }
/** * {@inheritdoc} */ public function isValid($value) { $this->_clearMessages(); if (!is_string($value)) { $this->_addMessages(['must be a string']); return false; } $validator = new \Zend\Validator\Hostname(\Zend\Validator\Hostname::ALLOW_ALL); if (!empty($value) && !$validator->isValid($value)) { $this->_addMessages($validator->getMessages()); return false; } return true; }
/** * @param $url * @return bool */ public function isValidUrl($url) { $uri = new \Zend\Validator\Uri(); if (!$uri->isValid($url)) { return false; } $parseUrl = parse_url($url); if (!isset($parseUrl['host']) || empty($parseUrl['host'])) { return false; } $validator = new \Zend\Validator\Hostname(\Zend\Validator\Hostname::ALLOW_DNS); if (!$validator->isValid($parseUrl['host'])) { return false; } if (!filter_var($parseUrl['host'], FILTER_VALIDATE_URL) === false) { return false; } list($status) = get_headers($url); if (strpos($status, '200') === FALSE) { return false; } return true; }
/** * Set session.cookie_domain * * @param string $cookieDomain * @return $this * @throws \InvalidArgumentException */ public function setCookieDomain($cookieDomain) { if (!is_string($cookieDomain)) { throw new \InvalidArgumentException('Invalid cookie domain: must be a string'); } $validator = new \Zend\Validator\Hostname(\Zend\Validator\Hostname::ALLOW_ALL); if (!empty($cookieDomain) && !$validator->isValid($cookieDomain)) { throw new \InvalidArgumentException('Invalid cookie domain: ' . join('; ', $validator->getMessages())); } $this->setOption('session.cookie_domain', $cookieDomain); return $this; }