Skip to content
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
82 changes: 0 additions & 82 deletions src/Validator/Host.php

This file was deleted.

47 changes: 37 additions & 10 deletions src/Validator/URL.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Utopia\Validator;

use Utopia\Validator;
use Utopia\Validator\URL\Pattern;

/**
* URL
Expand All @@ -13,14 +14,17 @@
*/
class URL extends Validator
{
protected array $allowedSchemes;
/**
* @var array<Pattern>
*/
protected array $patterns;

/**
* @param array $allowedSchemes
* @param array<Pattern> $patterns
*/
public function __construct(array $allowedSchemes = [])
public function __construct(array $patterns = [])
{
$this->allowedSchemes = $allowedSchemes;
$this->patterns = $patterns;
}

/**
Expand All @@ -32,8 +36,8 @@ public function __construct(array $allowedSchemes = [])
*/
public function getDescription(): string
{
if (!empty($this->allowedSchemes)) {
return 'Value must be a valid URL with following schemes (' . \implode(', ', $this->allowedSchemes) . ')';
if (!empty($this->patterns)) {
return 'Value must be a valid URL matching one of the following patterns (' . \implode(', ', $this->patterns) . ')';
}

return 'Value must be a valid URL';
Expand All @@ -49,15 +53,38 @@ public function getDescription(): string
*/
public function isValid($value): bool
{
if (\filter_var($value, FILTER_VALIDATE_URL) === false) {
$parsed = $this->parseUrl($value);
if (!$parsed) {
return false;
}

if (!empty($this->allowedSchemes) && !\in_array(\parse_url($value, PHP_URL_SCHEME), $this->allowedSchemes)) {
return false;
if (empty($this->patterns)) {
return true;
}

foreach ($this->patterns as $pattern) {
$schemeMatch = empty($pattern->schemes) || in_array($parsed['scheme'], $pattern->schemes);
$hostMatch = empty($pattern->hosts) || in_array($parsed['host'], $pattern->hosts);

if ($schemeMatch && $hostMatch) {
return true;
}
}

return false;
}

protected function parseUrl($value): mixed
{
$parsed = \parse_url($value);

// `parse_url` returns false if the URL is invalid, and when hostname is missing.
// In this case, try to extract the scheme using regex.
if (!$parsed && $matches = \preg_match('/^([a-z]+):\/\//', $value, $matches)) {
$parsed = ['scheme' => $matches[1] ?? ''];
}

return true;
return $parsed;
}

/**
Expand Down
22 changes: 22 additions & 0 deletions src/Validator/URL/Pattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Utopia\Validator\URL;

class Pattern
{
public array $schemes;
public array $hosts;

public function __construct(string|array $schemes = [], string|array $hosts = [])
{
if (!is_array($schemes)) {
$schemes = [$schemes];
}
$this->schemes = $schemes;

if (!is_array($hosts)) {
$hosts = [$hosts];
}
$this->hosts = $hosts;
}
}
Loading