2020-02-20 22:15:53 +00:00
|
|
|
<?php
|
|
|
|
|
|
2020-06-11 19:36:10 +00:00
|
|
|
namespace Appwrite\Network\Validator;
|
2020-02-20 22:15:53 +00:00
|
|
|
|
|
|
|
|
use Utopia\Validator;
|
|
|
|
|
|
|
|
|
|
class CNAME extends Validator
|
|
|
|
|
{
|
|
|
|
|
/**
|
2020-10-27 00:08:29 +00:00
|
|
|
* @var string
|
2020-02-20 22:15:53 +00:00
|
|
|
*/
|
|
|
|
|
protected $target;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $target
|
|
|
|
|
*/
|
|
|
|
|
public function __construct($target)
|
|
|
|
|
{
|
|
|
|
|
$this->target = $target;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-27 00:08:29 +00:00
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2021-12-13 15:42:40 +00:00
|
|
|
public function getDescription(): string
|
2020-02-20 22:15:53 +00:00
|
|
|
{
|
2020-06-15 20:05:49 +00:00
|
|
|
return 'Invalid CNAME record';
|
2020-02-20 22:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if CNAME record target value matches selected target
|
2020-06-24 21:05:16 +00:00
|
|
|
*
|
2020-10-27 00:08:29 +00:00
|
|
|
* @param mixed $domain
|
2020-02-20 22:15:53 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2021-12-13 15:42:40 +00:00
|
|
|
public function isValid($domain): bool
|
2020-02-20 22:15:53 +00:00
|
|
|
{
|
2020-10-27 19:44:15 +00:00
|
|
|
if (!is_string($domain)) {
|
2020-02-20 22:15:53 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-27 00:08:29 +00:00
|
|
|
try {
|
|
|
|
|
$records = \dns_get_record($domain, DNS_CNAME);
|
|
|
|
|
} catch (\Throwable $th) {
|
2020-02-20 22:15:53 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-09 11:45:15 +00:00
|
|
|
if (!$records) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-24 21:02:27 +00:00
|
|
|
foreach ($records as $record) {
|
|
|
|
|
if (isset($record['target']) && $record['target'] === $this->target) {
|
2020-02-20 22:15:53 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-04-13 08:46:30 +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;
|
|
|
|
|
}
|
2020-02-20 22:15:53 +00:00
|
|
|
}
|