Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty domain handling in Email validation and add PHP 8.3/8.4 support #159

Merged
merged 3 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
- '8.0'
- '8.1'
- '8.2'
- '8.3'
- '8.4'
steps:
- name: Checkout
uses: actions/checkout@v1
Expand Down
8 changes: 4 additions & 4 deletions src/Rule/AbstractStrlen.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected function strlen(string $str): int
return strlen(utf8_to_iso8859_1($str));
}

/**
/**
*
* Wrapper for `iconv_substr()` to throw an exception on malformed UTF-8.
*
Expand All @@ -87,10 +87,10 @@ protected function strlen(string $str): int
* @throws Exception\MalformedUtf8
*
*/
protected function substrIconv(string $str,int $start,int $length)
protected function substrIconv(string $str, int $start, ?int $length = null): string
{
$level = error_reporting(0);
$substr = iconv_substr($str,$start,$length, 'UTF-8');
$substr = iconv_substr($str, $start, $length ?? 0, 'UTF-8');
error_reporting($level);

if ($substr !== false) {
Expand All @@ -111,7 +111,7 @@ protected function substrIconv(string $str,int $start,int $length)
* @throws Exception\MalformedUtf8
*
*/
protected function strlenIconv(string $str)
protected function strlenIconv(string $str): int
{
$level = error_reporting(0);
$strlen = iconv_strlen($str, 'UTF-8');
Expand Down
19 changes: 14 additions & 5 deletions src/Rule/Validate/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,15 +404,24 @@ protected function intl(): bool
*/
protected function idnToAscii(string $email): string
{
if (strpos($email, '@') === false) {
return $email;
}

$parts = explode('@', $email);
$domain = array_pop($parts);
if (! $parts) {
// no parts remaining, so no @ symbol, so not valid to begin with

if (!isset($parts[1]) || empty($parts[1])) {
return $email;
}

// put the parts back together, with the domain part converted to ascii
return implode('@', $parts) . '@' . idn_to_ascii($domain, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46);
$domain = array_pop($parts);
$localPart = implode('@', $parts);

$asciiDomain = idn_to_ascii($domain, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46);
if ($asciiDomain === false) {
return $email;
}
return $localPart . '@' . $asciiDomain;
}

/**
Expand Down
Loading