-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CAPTCHA and CSRF token verification (#4)
* Add CAPTCHA and CSRF token verification * Split email sending into its own function * Add type declarations and use strict types
- Loading branch information
1 parent
f51a34e
commit 457556e
Showing
4 changed files
with
188 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spencer14420\PhpEmailHandler; | ||
|
||
class CaptchaVerifier | ||
{ | ||
private $secret; | ||
private $verifyUrl; | ||
|
||
public function __construct(string $secret, string $verifyUrl) | ||
{ | ||
$this->secret = $secret; | ||
$this->verifyUrl = $verifyUrl; | ||
} | ||
|
||
public function verify(?string $token, string $remoteIp): bool | ||
{ | ||
if (empty($this->secret) || empty($this->verifyUrl)) { | ||
return true; // Skip verification if CAPTCHA is not configured | ||
} | ||
|
||
if (empty($token)) { | ||
throw new \Exception('CAPTCHA verification failed: $captchaToken does not exist or is not set.'); | ||
} | ||
|
||
$data = [ | ||
"secret" => $this->secret, | ||
"response" => $token, | ||
"remoteip" => $remoteIp, | ||
]; | ||
|
||
$curl = curl_init(); | ||
curl_setopt($curl, CURLOPT_URL, $this->verifyUrl); | ||
curl_setopt($curl, CURLOPT_POST, true); | ||
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); | ||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | ||
|
||
$response = curl_exec($curl); | ||
|
||
if (curl_errno($curl)) { | ||
curl_close($curl); | ||
throw new \Exception("CAPTCHA verification failed due to a network error: " . curl_error($curl)); | ||
} | ||
|
||
$responseData = json_decode($response, true); | ||
curl_close($curl); | ||
|
||
if (!empty($responseData['error-codes'])) { | ||
throw new \Exception("CAPTCHA verification failed: " . implode(", ", $responseData['error-codes'])); | ||
} | ||
|
||
return isset($responseData['success']) && $responseData['success'] === true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters