-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from utopia-php/feat-backport-appwrite-validators
Backport Appwrite Validators to Utopia
- Loading branch information
Showing
8 changed files
with
598 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace Utopia\Validator; | ||
|
||
use Utopia\Validator; | ||
|
||
/** | ||
* Domain | ||
* | ||
* Validate that an variable is a valid domain address | ||
* | ||
* @package Utopia\Validator | ||
*/ | ||
class Domain extends Validator | ||
{ | ||
/** | ||
* Get Description | ||
* | ||
* Returns validator description | ||
* | ||
* @return string | ||
*/ | ||
public function getDescription(): string | ||
{ | ||
return 'Value must be a valid domain'; | ||
} | ||
|
||
/** | ||
* Is valid | ||
* | ||
* Validation will pass when $value is valid domain. | ||
* | ||
* Validates domain names against RFC 1034, RFC 1035, RFC 952, RFC 1123, RFC 2732, RFC 2181, and RFC 1123. | ||
* | ||
* @param mixed $value | ||
* @return bool | ||
*/ | ||
public function isValid($value): bool | ||
{ | ||
if (empty($value)) { | ||
return false; | ||
} | ||
|
||
if (!is_string($value)) { | ||
return false; | ||
} | ||
|
||
if (\filter_var($value, FILTER_VALIDATE_DOMAIN) === false) { | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace Utopia\Validator; | ||
|
||
use Utopia\Validator; | ||
|
||
/** | ||
* Host | ||
* | ||
* Validate that a host is allowed from given whitelisted hosts list | ||
* | ||
* @package Utopia\Validator | ||
*/ | ||
class Host extends Validator | ||
{ | ||
protected $whitelist = []; | ||
|
||
/** | ||
* @param array $whitelist | ||
*/ | ||
public function __construct(array $whitelist) | ||
{ | ||
$this->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 | ||
{ | ||
$urlValidator = new URL(); | ||
|
||
if (!$urlValidator->isValid($value)) { | ||
return false; | ||
} | ||
|
||
if (\in_array(\parse_url($value, PHP_URL_HOST), $this->whitelist)) { | ||
return true; | ||
} | ||
|
||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
|
||
namespace Utopia\Validator; | ||
|
||
use Exception; | ||
use Utopia\Validator; | ||
|
||
/** | ||
* IP | ||
* | ||
* Validate that an variable is a valid IP address | ||
* | ||
* @package Utopia\Validator | ||
*/ | ||
class IP extends Validator | ||
{ | ||
public const ALL = 'all'; | ||
public const V4 = 'ipv4'; | ||
public const V6 = 'ipv6'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $type = self::ALL; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* Set a the type of IP check. | ||
* | ||
* @param string $type | ||
*/ | ||
public function __construct(string $type = self::ALL) | ||
{ | ||
if (!in_array($type, [self::ALL, self::V4, self::V6])) { | ||
throw new Exception('Unsupported IP type'); | ||
} | ||
|
||
$this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace Utopia\Validator; | ||
|
||
use Utopia\Validator; | ||
|
||
/** | ||
* URL | ||
* | ||
* Validate that an variable is a valid URL | ||
* | ||
* @package Appwrite\Network\Validator | ||
*/ | ||
class URL extends Validator | ||
{ | ||
protected array $allowedSchemes; | ||
|
||
/** | ||
* @param array $allowedSchemes | ||
*/ | ||
public function __construct(array $allowedSchemes = []) | ||
{ | ||
$this->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 | ||
{ | ||
if (\filter_var($value, FILTER_VALIDATE_URL) === false) { | ||
return false; | ||
} | ||
|
||
if (!empty($this->allowedSchemes) && !\in_array(\parse_url($value, 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; | ||
} | ||
} |
Oops, something went wrong.