DomainAliasValidator.php in Domain Access 8
File
domain_alias/src/DomainAliasValidator.php
View source
<?php
namespace Drupal\domain_alias;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class DomainAliasValidator implements DomainAliasValidatorInterface {
use StringTranslationTrait;
protected $configFactory;
protected $entityTypeManager;
protected $aliasStorage;
protected $domainStorage;
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
$this->configFactory = $config_factory;
$this->entityTypeManager = $entity_type_manager;
$this->aliasStorage = $this->entityTypeManager
->getStorage('domain_alias');
$this->domainStorage = $this->entityTypeManager
->getStorage('domain');
}
public function validate(DomainAliasInterface $alias) {
$pattern = $alias
->getPattern();
$localhost_check = explode(':', $pattern);
if (substr_count($pattern, '.') == 0 && $localhost_check[0] != 'localhost') {
return $this
->t('At least one dot (.) is required, except when using <em>localhost</em>.');
}
$count = substr_count($pattern, '*') + substr_count($pattern, '?');
if ($count > 1) {
return $this
->t('You may only have one wildcard character in each alias.');
}
$count = substr_count($pattern, ':');
if ($count > 1) {
return $this
->t('You may only have one colon ":" character in each alias.');
}
elseif ($count == 1) {
$int = substr($pattern, strpos($pattern, ':') + 1);
if (!is_numeric($int) && $int !== '*') {
return $this
->t('A colon may only be followed by an integer indicating the proper port or the wildcard character (*).');
}
}
$non_ascii = $this->configFactory
->get('domain.settings')
->get('allow_non_ascii');
if (!$non_ascii) {
$check = preg_match('/^[a-z0-9\\.\\+\\-\\*\\?:]*$/', $pattern);
if ($check == 0) {
return $this
->t('The pattern contains invalid characters.');
}
}
if (substr($pattern, 0, 1) == '.') {
return $this
->t('The pattern cannot begin with a dot.');
}
if (substr($pattern, -1) == '.') {
return $this
->t('The pattern cannot end with a dot.');
}
$check = preg_match('/[a-z0-9\\.\\+\\-:]*$/', $pattern);
if ($check == 1 && $this->domainStorage
->loadByHostname($pattern)) {
return $this
->t('The pattern matches an existing domain record.');
}
if ($alias_check = $this->aliasStorage
->loadByPattern($pattern)) {
if ($alias_check
->id() != $alias
->id()) {
return $this
->t('The pattern already exists.');
}
}
}
}