Skip to content

Multisite new with uri #3889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Command/Multisite/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Drupal\Console\Command\Multisite;

use Drupal\Console\Core\Command\Command;
use Drupal\Console\Utils\Validator;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -154,6 +155,15 @@ protected function addToSitesFile($uri)
throw new FileNotFoundException($this->trans('commands.multisite.new.errors.sites-missing'));
}

// Use $uri only when it matches '<port>.<domain>.<path>' -pattern requirement (no schema allowed).
try {
if (Validator::validateHostname($uri)) {
$sites_file_contents .= "\n\$sites['$uri'] = '$this->directory';";
}
} catch (\Exception $error) {
// Swallow exceptions.
}

$sites_file_contents .= "\n\$sites['$this->directory'] = '$this->directory';";

try {
Expand Down
10 changes: 10 additions & 0 deletions src/Utils/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Validator
const REGEX_MACHINE_NAME = '/^[a-z0-9_]+$/';
// This REGEX remove spaces between words
const REGEX_REMOVE_SPACES = '/[\\s+]/';
const REGEX_HOSTNAME = '/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/';

protected $appRoot;

Expand Down Expand Up @@ -114,6 +115,15 @@ public function validateControllerName($class_name)
}
}

public function validateHostname($hostname)
{
if (count(preg_grep(self::REGEX_HOSTNAME, [$hostname]))) {
return $hostname;
} else {
throw new \InvalidArgumentException(sprintf('Hostname name "%s" is invalid.', $hostname));
}
}

public function validateMachineName($machine_name)
{
if (preg_match(self::REGEX_MACHINE_NAME, $machine_name)) {
Expand Down