mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-19 03:04:05 +00:00
Support internationalized domain names (IDN) in email validation
Add IdnEmail validation rule that converts IDN domains to Punycode via idn_to_ascii() before validating with FILTER_VALIDATE_EMAIL. Applied to all email fields: customers, members, profiles, admin users, customer portal profiles, and mail configuration. Includes unit tests for standard emails, IDN emails, and invalid inputs. Fixes #388
This commit is contained in:
46
app/Rules/IdnEmail.php
Normal file
46
app/Rules/IdnEmail.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rules;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
|
||||
/**
|
||||
* Validates email addresses including those with internationalized
|
||||
* domain names (IDN) like user@münchen.de or michel@exempleé.fr.
|
||||
*
|
||||
* Converts the domain part to Punycode (ASCII) before validation.
|
||||
*/
|
||||
class IdnEmail implements ValidationRule
|
||||
{
|
||||
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||
{
|
||||
if (! is_string($value) || $value === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
$parts = explode('@', $value);
|
||||
|
||||
if (count($parts) !== 2) {
|
||||
$fail('The :attribute must be a valid email address.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
[$local, $domain] = $parts;
|
||||
|
||||
if (function_exists('idn_to_ascii') && $domain !== '') {
|
||||
$ascii = idn_to_ascii($domain, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46);
|
||||
|
||||
if ($ascii !== false) {
|
||||
$domain = $ascii;
|
||||
}
|
||||
}
|
||||
|
||||
$normalized = $local.'@'.$domain;
|
||||
|
||||
if (! filter_var($normalized, FILTER_VALIDATE_EMAIL)) {
|
||||
$fail('The :attribute must be a valid email address.');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user