mirror of
https://github.com/getnamingo/registry.git
synced 2025-05-15 17:16:59 +02:00
Improvements on domain length check
This commit is contained in:
parent
aae8cad364
commit
e3f5d9f94e
2 changed files with 48 additions and 18 deletions
|
@ -211,26 +211,41 @@ function validate_identifier($identifier) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isDomainValid(string $domain): bool {
|
||||||
|
// Split the domain into its labels (subdomains, SLD, etc.)
|
||||||
|
$labels = explode('.', $domain);
|
||||||
|
foreach ($labels as $label) {
|
||||||
|
if (strlen($label) > 63) { // or mb_strlen() if you need multibyte support
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
function validate_label($label, $db) {
|
function validate_label($label, $db) {
|
||||||
if (!$label) {
|
if (!$label) {
|
||||||
return 'You must enter a domain name';
|
return 'You must enter a domain name';
|
||||||
}
|
}
|
||||||
if (strlen($label) > 63) {
|
if (!isDomainValid($label)) {
|
||||||
return 'Total lenght of your domain must be less then 63 characters';
|
return 'Domain label is too long (exceeds 63 characters)';
|
||||||
}
|
}
|
||||||
if (strlen($label) < 2) {
|
$parts = extractDomainAndTLD($label);
|
||||||
return 'Total lenght of your domain must be greater then 2 characters';
|
$tld = "." . $parts['tld'];
|
||||||
|
if (strlen($parts['domain']) > 63) {
|
||||||
|
return 'Total length of your domain must be less then 63 characters';
|
||||||
|
}
|
||||||
|
if (strlen($parts['domain']) < 2) {
|
||||||
|
return 'Total length of your domain must be greater then 2 characters';
|
||||||
}
|
}
|
||||||
if (strpos($label, '.') === false) {
|
if (strpos($label, '.') === false) {
|
||||||
return 'Invalid domain name format, must contain at least one dot (.)';
|
return 'Invalid domain name format, must contain at least one dot (.)';
|
||||||
}
|
}
|
||||||
if (strpos($label, 'xn--') === false && preg_match("/(^-|^\.|-\.|\.-|--|\.\.|-$|\.$)/", $label)) {
|
if (!preg_match('/^[a-zA-Z0-9].*[a-zA-Z0-9]$/', $parts['domain'])) {
|
||||||
return 'Invalid domain name format, cannot begin or end with a hyphen (-)';
|
return 'Domain name must start and end with an alphanumeric character';
|
||||||
|
}
|
||||||
|
if (strpos($parts['domain'], 'xn--') === false && preg_match("/(^-|^\.|-\.|\.-|--|\.\.|-$|\.$)/", $parts['domain'])) {
|
||||||
|
return 'Domain name cannot contain consecutive dashes (--) unless it is a punycode domain';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract TLD from the domain and prepend a dot
|
|
||||||
$parts = extractDomainAndTLD($label);
|
|
||||||
$tld = "." . $parts['tld'];
|
|
||||||
|
|
||||||
// Check if the TLD exists in the domain_tld table
|
// Check if the TLD exists in the domain_tld table
|
||||||
$tldExists = $db->select('SELECT COUNT(*) FROM domain_tld WHERE tld = ?', [$tld]);
|
$tldExists = $db->select('SELECT COUNT(*) FROM domain_tld WHERE tld = ?', [$tld]);
|
||||||
|
|
|
@ -182,26 +182,41 @@ function validate_identifier($identifier) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isDomainValid(string $domain): bool {
|
||||||
|
// Split the domain into its labels (subdomains, SLD, etc.)
|
||||||
|
$labels = explode('.', $domain);
|
||||||
|
foreach ($labels as $label) {
|
||||||
|
if (strlen($label) > 63) { // or mb_strlen() if you need multibyte support
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
function validate_label($label, $pdo) {
|
function validate_label($label, $pdo) {
|
||||||
if (!$label) {
|
if (!$label) {
|
||||||
return 'You must enter a domain name';
|
return 'You must enter a domain name';
|
||||||
}
|
}
|
||||||
if (strlen($label) > 63) {
|
if (!isDomainValid($label)) {
|
||||||
|
return 'Domain label is too long (exceeds 63 characters)';
|
||||||
|
}
|
||||||
|
$parts = extractDomainAndTLD($label);
|
||||||
|
$tld = "." . $parts['tld'];
|
||||||
|
if (strlen($parts['domain']) > 63) {
|
||||||
return 'Total length of your domain must be less then 63 characters';
|
return 'Total length of your domain must be less then 63 characters';
|
||||||
}
|
}
|
||||||
if (strlen($label) < 2) {
|
if (strlen($parts['domain']) < 2) {
|
||||||
return 'Total length of your domain must be greater then 2 characters';
|
return 'Total length of your domain must be greater then 2 characters';
|
||||||
}
|
}
|
||||||
if (strpos($label, '.') === false) {
|
if (strpos($label, '.') === false) {
|
||||||
return 'Invalid domain name format, must contain at least one dot (.)';
|
return 'Invalid domain name format, must contain at least one dot (.)';
|
||||||
}
|
}
|
||||||
if (strpos($label, 'xn--') === false && preg_match("/(^-|^\.|-\.|\.-|--|\.\.|-$|\.$)/", $label)) {
|
if (!preg_match('/^[a-zA-Z0-9].*[a-zA-Z0-9]$/', $parts['domain'])) {
|
||||||
return 'Invalid domain name format, cannot begin or end with a hyphen (-)';
|
return 'Domain name must start and end with an alphanumeric character';
|
||||||
|
}
|
||||||
|
if (strpos($parts['domain'], 'xn--') === false && preg_match("/(^-|^\.|-\.|\.-|--|\.\.|-$|\.$)/", $parts['domain'])) {
|
||||||
|
return 'Domain name cannot contain consecutive dashes (--) unless it is a punycode domain';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract TLD from the domain and prepend a dot
|
|
||||||
$parts = extractDomainAndTLD($label);
|
|
||||||
$tld = "." . $parts['tld'];
|
|
||||||
|
|
||||||
// Check if the TLD exists in the domain_tld table
|
// Check if the TLD exists in the domain_tld table
|
||||||
$stmtTLD = $pdo->prepare("SELECT COUNT(*) FROM domain_tld WHERE tld = :tld");
|
$stmtTLD = $pdo->prepare("SELECT COUNT(*) FROM domain_tld WHERE tld = :tld");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue