diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index f9425f690b..e7df74b686 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -10,8 +10,8 @@ use Appwrite\Event\Mail; use Appwrite\Event\Phone as EventPhone; use Appwrite\Extend\Exception; use Appwrite\Network\Validator\Email; -use Appwrite\Network\Validator\Host; -use Appwrite\Network\Validator\URL; +use Utopia\Validator\Host; +use Utopia\Validator\URL; use Appwrite\OpenSSL\OpenSSL; use Appwrite\Template\Template; use Appwrite\URL\URL as URLParser; diff --git a/app/controllers/api/avatars.php b/app/controllers/api/avatars.php index 75d9ad434b..19daaea20d 100644 --- a/app/controllers/api/avatars.php +++ b/app/controllers/api/avatars.php @@ -1,7 +1,7 @@ whitelist = $whitelist; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription(): string - { - return 'URL host must be one of: ' . \implode(', ', $this->whitelist); - } - - /** - * Is valid - * - * Validation will pass when $value starts with one of the given hosts - * - * @param mixed $value - * @return bool - */ - public function isValid($value): bool - { - // Check if value is valid URL - $urlValidator = new URL(); - - if (!$urlValidator->isValid($value)) { - return false; - } - - $hostname = \parse_url($value, PHP_URL_HOST); - $hostnameValidator = new Hostname($this->whitelist); - return $hostnameValidator->isValid($hostname); - } - - /** - * Is array - * - * Function will return true if object is array. - * - * @return bool - */ - public function isArray(): bool - { - return false; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType(): string - { - return self::TYPE_STRING; - } -} diff --git a/src/Appwrite/Network/Validator/IP.php b/src/Appwrite/Network/Validator/IP.php deleted file mode 100644 index 0245d59d3e..0000000000 --- a/src/Appwrite/Network/Validator/IP.php +++ /dev/null @@ -1,114 +0,0 @@ -type = $type; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription(): string - { - return 'Value must be a valid IP address'; - } - - /** - * Is valid - * - * Validation will pass when $value is valid IP address. - * - * @param mixed $value - * @return bool - */ - public function isValid($value): bool - { - switch ($this->type) { - case self::ALL: - if (\filter_var($value, FILTER_VALIDATE_IP)) { - return true; - } - break; - - case self::V4: - if (\filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { - return true; - } - break; - - case self::V6: - if (\filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { - return true; - } - break; - - default: - return false; - break; - } - - return false; - } - - /** - * Is array - * - * Function will return true if object is array. - * - * @return bool - */ - public function isArray(): bool - { - return false; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType(): string - { - return self::TYPE_STRING; - } -} diff --git a/src/Appwrite/Network/Validator/URL.php b/src/Appwrite/Network/Validator/URL.php deleted file mode 100644 index 40a12420f5..0000000000 --- a/src/Appwrite/Network/Validator/URL.php +++ /dev/null @@ -1,92 +0,0 @@ -allowedSchemes = $allowedSchemes; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription(): string - { - if (!empty($this->allowedSchemes)) { - return 'Value must be a valid URL with following schemes (' . \implode(', ', $this->allowedSchemes) . ')'; - } - - return 'Value must be a valid URL'; - } - - /** - * Is valid - * - * Validation will pass when $value is valid URL. - * - * @param mixed $value - * @return bool - */ - public function isValid($value): bool - { - $sanitizedURL = ''; - - foreach (str_split($value) as $character) { - $sanitizedURL .= (ord($character) > 127) ? rawurlencode($character) : $character; - } - - if (\filter_var($sanitizedURL, FILTER_VALIDATE_URL) === false) { - return false; - } - - if (!empty($this->allowedSchemes) && !\in_array(\parse_url($sanitizedURL, PHP_URL_SCHEME), $this->allowedSchemes)) { - return false; - } - - return true; - } - - /** - * Is array - * - * Function will return true if object is array. - * - * @return bool - */ - public function isArray(): bool - { - return false; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType(): string - { - return self::TYPE_STRING; - } -} diff --git a/src/Appwrite/Specification/Format/OpenAPI3.php b/src/Appwrite/Specification/Format/OpenAPI3.php index fb7208c569..0e54977d60 100644 --- a/src/Appwrite/Specification/Format/OpenAPI3.php +++ b/src/Appwrite/Specification/Format/OpenAPI3.php @@ -311,7 +311,7 @@ class OpenAPI3 extends Format $node['schema']['format'] = 'email'; $node['schema']['x-example'] = 'email@example.com'; break; - case 'Appwrite\Network\Validator\URL': + case 'Utopia\Validator\URL': $node['schema']['type'] = $validator->getType(); $node['schema']['format'] = 'url'; $node['schema']['x-example'] = 'https://example.com'; @@ -391,7 +391,7 @@ class OpenAPI3 extends Format case 'Utopia\Validator\Length': $node['schema']['type'] = $validator->getType(); break; - case 'Appwrite\Network\Validator\Host': + case 'Utopia\Validator\Host': $node['schema']['type'] = $validator->getType(); $node['schema']['format'] = 'url'; $node['schema']['x-example'] = 'https://example.com'; diff --git a/src/Appwrite/Specification/Format/Swagger2.php b/src/Appwrite/Specification/Format/Swagger2.php index e4d673ed14..ea48d671f2 100644 --- a/src/Appwrite/Specification/Format/Swagger2.php +++ b/src/Appwrite/Specification/Format/Swagger2.php @@ -312,7 +312,7 @@ class Swagger2 extends Format $node['format'] = 'email'; $node['x-example'] = 'email@example.com'; break; - case 'Appwrite\Network\Validator\URL': + case 'Utopia\Validator\URL': $node['type'] = $validator->getType(); $node['format'] = 'url'; $node['x-example'] = 'https://example.com'; @@ -393,7 +393,7 @@ class Swagger2 extends Format case 'Utopia\Validator\Length': $node['type'] = $validator->getType(); break; - case 'Appwrite\Network\Validator\Host': + case 'Utopia\Validator\Host': $node['type'] = $validator->getType(); $node['format'] = 'url'; $node['x-example'] = 'https://example.com'; diff --git a/tests/unit/Network/Validators/DomainTest.php b/tests/unit/Network/Validators/DomainTest.php deleted file mode 100644 index 631ea10753..0000000000 --- a/tests/unit/Network/Validators/DomainTest.php +++ /dev/null @@ -1,43 +0,0 @@ -domain = new Domain(); - } - - public function tearDown(): void - { - $this->domain = null; - } - - public function testIsValid(): void - { - // Assertions - $this->assertEquals(true, $this->domain->isValid('example.com')); - $this->assertEquals(true, $this->domain->isValid('subdomain.example.com')); - $this->assertEquals(true, $this->domain->isValid('subdomain.example-app.com')); - $this->assertEquals(true, $this->domain->isValid('subdomain.example_app.com')); - $this->assertEquals(true, $this->domain->isValid('subdomain-new.example.com')); - $this->assertEquals(true, $this->domain->isValid('subdomain_new.example.com')); - $this->assertEquals(true, $this->domain->isValid('localhost')); - $this->assertEquals(true, $this->domain->isValid('appwrite.io')); - $this->assertEquals(true, $this->domain->isValid('appwrite.org')); - $this->assertEquals(true, $this->domain->isValid('appwrite.org')); - $this->assertEquals(false, $this->domain->isValid(false)); - $this->assertEquals(false, $this->domain->isValid('.')); - $this->assertEquals(false, $this->domain->isValid('..')); - $this->assertEquals(false, $this->domain->isValid('')); - $this->assertEquals(false, $this->domain->isValid(['string', 'string'])); - $this->assertEquals(false, $this->domain->isValid(1)); - $this->assertEquals(false, $this->domain->isValid(1.2)); - } -} diff --git a/tests/unit/Network/Validators/HostTest.php b/tests/unit/Network/Validators/HostTest.php deleted file mode 100755 index 7974bf86a1..0000000000 --- a/tests/unit/Network/Validators/HostTest.php +++ /dev/null @@ -1,44 +0,0 @@ - - * @version 1.0 RC4 - * @license The MIT License (MIT) - */ - -namespace Tests\Unit\Network\Validators; - -use Appwrite\Network\Validator\Host; -use PHPUnit\Framework\TestCase; - -class HostTest extends TestCase -{ - protected ?Host $host = null; - - public function setUp(): void - { - $this->host = new Host(['appwrite.io', 'subdomain.appwrite.test', 'localhost']); - } - - public function tearDown(): void - { - $this->host = null; - } - - public function testIsValid(): void - { - // Assertions - $this->assertEquals($this->host->isValid('https://appwrite.io/link'), true); - $this->assertEquals($this->host->isValid('https://localhost'), true); - $this->assertEquals($this->host->isValid('localhost'), false); - $this->assertEquals($this->host->isValid('http://subdomain.appwrite.test/path'), true); - $this->assertEquals($this->host->isValid('http://test.subdomain.appwrite.test/path'), false); - $this->assertEquals($this->host->getType(), 'string'); - } -} diff --git a/tests/unit/Network/Validators/IPTest.php b/tests/unit/Network/Validators/IPTest.php deleted file mode 100755 index 57e395111c..0000000000 --- a/tests/unit/Network/Validators/IPTest.php +++ /dev/null @@ -1,87 +0,0 @@ - - * @version 1.0 RC4 - * @license The MIT License (MIT) - */ - -namespace Tests\Unit\Network\Validators; - -use Appwrite\Network\Validator\IP; -use PHPUnit\Framework\TestCase; - -class IPTest extends TestCase -{ - protected ?IP $validator; - - public function setUp(): void - { - $this->validator = new IP(); - } - - public function tearDown(): void - { - $this->validator = null; - } - - public function testIsValidIP(): void - { - $this->assertEquals($this->validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), true); - $this->assertEquals($this->validator->isValid('109.67.204.101'), true); - $this->assertEquals($this->validator->isValid(23.5), false); - $this->assertEquals($this->validator->isValid('23.5'), false); - $this->assertEquals($this->validator->isValid(null), false); - $this->assertEquals($this->validator->isValid(true), false); - $this->assertEquals($this->validator->isValid(false), false); - $this->assertEquals($this->validator->getType(), 'string'); - } - - public function testIsValidIPALL(): void - { - $this->validator = new IP(IP::ALL); - - // Assertions - $this->assertEquals($this->validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), true); - $this->assertEquals($this->validator->isValid('109.67.204.101'), true); - $this->assertEquals($this->validator->isValid(23.5), false); - $this->assertEquals($this->validator->isValid('23.5'), false); - $this->assertEquals($this->validator->isValid(null), false); - $this->assertEquals($this->validator->isValid(true), false); - $this->assertEquals($this->validator->isValid(false), false); - } - - public function testIsValidIPV4(): void - { - $this->validator = new IP(IP::V4); - - // Assertions - $this->assertEquals($this->validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), false); - $this->assertEquals($this->validator->isValid('109.67.204.101'), true); - $this->assertEquals($this->validator->isValid(23.5), false); - $this->assertEquals($this->validator->isValid('23.5'), false); - $this->assertEquals($this->validator->isValid(null), false); - $this->assertEquals($this->validator->isValid(true), false); - $this->assertEquals($this->validator->isValid(false), false); - } - - public function testIsValidIPV6(): void - { - $this->validator = new IP(IP::V6); - - // Assertions - $this->assertEquals($this->validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), true); - $this->assertEquals($this->validator->isValid('109.67.204.101'), false); - $this->assertEquals($this->validator->isValid(23.5), false); - $this->assertEquals($this->validator->isValid('23.5'), false); - $this->assertEquals($this->validator->isValid(null), false); - $this->assertEquals($this->validator->isValid(true), false); - $this->assertEquals($this->validator->isValid(false), false); - } -} diff --git a/tests/unit/Network/Validators/URLTest.php b/tests/unit/Network/Validators/URLTest.php deleted file mode 100755 index bc43f25623..0000000000 --- a/tests/unit/Network/Validators/URLTest.php +++ /dev/null @@ -1,57 +0,0 @@ - - * @version 1.0 RC4 - * @license The MIT License (MIT) - */ - -namespace Tests\Unit\Network\Validators; - -use Appwrite\Network\Validator\URL; -use PHPUnit\Framework\TestCase; - -class URLTest extends TestCase -{ - protected ?URL $url; - - public function setUp(): void - { - $this->url = new URL(); - } - - public function tearDown(): void - { - $this->url = null; - } - - public function testIsValid(): void - { - $this->assertEquals('Value must be a valid URL', $this->url->getDescription()); - $this->assertEquals(true, $this->url->isValid('http://example.com')); - $this->assertEquals(true, $this->url->isValid('https://example.com')); - $this->assertEquals(true, $this->url->isValid('htts://example.com')); // does not validate protocol - $this->assertEquals(false, $this->url->isValid('example.com')); // though, requires some kind of protocol - $this->assertEquals(false, $this->url->isValid('http:/example.com')); - $this->assertEquals(true, $this->url->isValid('http://exa-mple.com')); - $this->assertEquals(false, $this->url->isValid('htt@s://example.com')); - $this->assertEquals(true, $this->url->isValid('http://www.example.com/foo%2\u00c2\u00a9zbar')); - $this->assertEquals(true, $this->url->isValid('http://www.example.com/?q=%3Casdf%3E')); - $this->assertEquals(true, $this->url->isValid('https://example.com/foo%2\u00c2\u00ä9zbär')); - } - - public function testIsValidAllowedSchemes(): void - { - $this->url = new URL(['http', 'https']); - $this->assertEquals('Value must be a valid URL with following schemes (http, https)', $this->url->getDescription()); - $this->assertEquals(true, $this->url->isValid('http://example.com')); - $this->assertEquals(true, $this->url->isValid('https://example.com')); - $this->assertEquals(false, $this->url->isValid('gopher://www.example.com')); - } -}