2021-03-24 06:27:51 +13:00
|
|
|
<?php
|
|
|
|
|
2021-03-25 06:47:17 +13:00
|
|
|
namespace Appwrite\Network\Validator;
|
2021-03-24 06:27:51 +13:00
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
$urlValidator = new URL();
|
|
|
|
|
|
|
|
if (!$urlValidator->isValid($value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (\in_array(\parse_url($value, PHP_URL_HOST), $this->whitelist)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2021-04-13 20:46:30 +12:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2021-03-24 06:27:51 +13:00
|
|
|
}
|