appwrite/src/Appwrite/Network/Validator/Host.php

85 lines
1.6 KiB
PHP
Raw Normal View History

2021-03-23 17:27:51 +00:00
<?php
2021-03-24 17:47:17 +00:00
namespace Appwrite\Network\Validator;
2021-03-23 17:27:51 +00:00
2022-04-25 09:03:03 +00:00
use Utopia\Validator\Hostname;
2021-03-23 17:27:51 +00: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(): string
2021-03-23 17:27:51 +00:00
{
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
2021-03-23 17:27:51 +00:00
{
2022-04-20 09:31:17 +00:00
// Check if value is valid URL
2021-03-23 17:27:51 +00:00
$urlValidator = new URL();
if (!$urlValidator->isValid($value)) {
return false;
}
2022-04-20 09:31:17 +00:00
$hostname = \parse_url($value, PHP_URL_HOST);
2022-04-20 12:39:53 +00:00
$hostnameValidator = new Hostname($this->whitelist);
return $hostnameValidator->isValid($hostname);
2021-03-23 17:27:51 +00: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-23 17:27:51 +00:00
}