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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* URL
|
|
|
|
*
|
|
|
|
* Validate that an variable is a valid URL
|
|
|
|
*
|
2022-02-17 04:16:37 +13:00
|
|
|
* @package Appwrite\Network\Validator
|
2021-03-24 06:27:51 +13:00
|
|
|
*/
|
|
|
|
class URL extends Validator
|
|
|
|
{
|
2022-02-17 04:16:37 +13:00
|
|
|
protected array $allowedSchemes;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $allowedSchemes
|
|
|
|
*/
|
|
|
|
public function __construct(array $allowedSchemes = [])
|
|
|
|
{
|
|
|
|
$this->allowedSchemes = $allowedSchemes;
|
|
|
|
}
|
2022-02-17 04:19:27 +13:00
|
|
|
|
2021-03-24 06:27:51 +13:00
|
|
|
/**
|
|
|
|
* Get Description
|
|
|
|
*
|
|
|
|
* Returns validator description
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-12-14 04:42:40 +13:00
|
|
|
public function getDescription(): string
|
2021-03-24 06:27:51 +13:00
|
|
|
{
|
2022-02-17 04:16:37 +13:00
|
|
|
if (!empty($this->allowedSchemes)) {
|
2022-02-17 04:19:27 +13:00
|
|
|
return 'Value must be a valid URL with following schemes (' . \implode(', ', $this->allowedSchemes) . ')';
|
2022-02-17 04:16:37 +13:00
|
|
|
}
|
|
|
|
|
2021-03-24 06:27:51 +13:00
|
|
|
return 'Value must be a valid URL';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is valid
|
|
|
|
*
|
|
|
|
* Validation will pass when $value is valid URL.
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-12-14 04:42:40 +13:00
|
|
|
public function isValid($value): bool
|
2021-03-24 06:27:51 +13:00
|
|
|
{
|
2022-09-30 23:28:56 +13:00
|
|
|
$sanitized_url = '';
|
|
|
|
|
|
|
|
foreach (str_split($value) as $character) {
|
|
|
|
$sanitized_url .= (ord($character) > 127) ? rawurlencode($character) : $character;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (\filter_var($sanitized_url, FILTER_VALIDATE_URL) === false) {
|
2021-03-24 06:27:51 +13:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-07 13:41:38 +13:00
|
|
|
if (!empty($this->allowedSchemes) && !\in_array(\parse_url($sanitized_url, PHP_URL_SCHEME), $this->allowedSchemes)) {
|
2022-02-17 04:16:37 +13:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-24 06:27:51 +13:00
|
|
|
return true;
|
|
|
|
}
|
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
|
|
|
}
|