Skip to content

Commit

Permalink
HW7
Browse files Browse the repository at this point in the history
  • Loading branch information
RofFlexor committed Sep 18, 2023
1 parent 86cad63 commit f680cd5
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 79 deletions.
11 changes: 10 additions & 1 deletion app/Actions/CheckEmailAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@

namespace Rofflexor\Hw\Actions;

use Exception;
use Rofflexor\Hw\Tasks\CheckEmailDomainWithDoHTask;
use Rofflexor\Hw\Tasks\CheckEmailTask;

class CheckEmailAction
{
/**
* @throws Exception
*/
public function run(string $email): false|int
{
return (new CheckEmailTask())->run($email);
$isValidFormat = (new CheckEmailTask())->run($email);
if($isValidFormat) {

Check failure on line 17 in app/Actions/CheckEmailAction.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 1 space(s) after IF keyword; 0 found
return (new CheckEmailDomainWithDoHTask())->run($email);
}
return false;
}

}

Check failure on line 23 in app/Actions/CheckEmailAction.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 1 newline at end of file; 0 found

Check failure on line 23 in app/Actions/CheckEmailAction.php

View workflow job for this annotation

GitHub Actions / phpcs

The closing brace for the class must go on the next line after the body
65 changes: 65 additions & 0 deletions app/Tasks/CheckEmailDomainWithDoHTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Rofflexor\Hw\Tasks;

use Exception;

class CheckEmailDomainWithDoHTask
{

protected array $defaultServers = [
'Quad9 Foundation' => 'https://dns.quad9.net:5053/dns-query',
'Cloudflare for Teams' => 'https://security.cloudflare-dns.com/dns-query',
'Cloudflare' => 'https://cloudflare-dns.com/dns-query',
'Google' => 'https://dns.google/resolve',
];

/**
* @throws Exception
*/
public function run(string $email): bool
{
$domain = explode('@', $email)[1];

foreach ($this->defaultServers as $serverName => $urlPrefix) {
$url = "$urlPrefix?name={$domain}&type=MX";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['accept: application/dns-json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);

$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

if ($result === false || $httpCode !== 200) {
continue;
}

$json = json_decode($result, false, 512, JSON_THROW_ON_ERROR);

if (json_last_error() !== JSON_ERROR_NONE) {
continue;
}

if ($json->Status === 5) {
return false;
}

if ($json->Status !== 0) {
return false;
}

if (!isset($json->Answer) || (isset($json->Answer[0]) && $json->Answer[0]->data === '0.0.0.0')) {
return false;
}
return true;
}

throw new \RuntimeException("The DNS queries to all servers failed while validating domain of email: {$email}. Not sure what's happening but it's likely a problem on our side.");

}

}
2 changes: 1 addition & 1 deletion app/Tasks/CheckEmailTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ class CheckEmailTask
{
public function run(string $email): false|int
{
return preg_match('/^[^()\n]*+(\((?>[^()\n]|(?1))*+\)[^()\n]*+)++$/m', $email);
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
}
3 changes: 0 additions & 3 deletions code/.env.example

This file was deleted.

10 changes: 0 additions & 10 deletions code/CheckRequest.php

This file was deleted.

33 changes: 0 additions & 33 deletions code/JsonResponse.php

This file was deleted.

31 changes: 0 additions & 31 deletions code/index.php

This file was deleted.

0 comments on commit f680cd5

Please sign in to comment.