Skip to content

Commit

Permalink
Adds Prosopo Procaptcha support
Browse files Browse the repository at this point in the history
* Adds Prosopo Procaptcha as captcha provider
  • Loading branch information
forgetso committed Feb 27, 2024
1 parent 0fa2e5d commit c6abf4b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Captcha/Captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,25 @@

use App\Captcha\Providers\HCaptchaProvider;
use App\Captcha\Providers\ReCaptchaProvider;
use App\Captcha\Providers\ProcaptchaProvider;

final class Captcha
{
public const CAPTCHA_PROVIDER_RECAPTCHA = 0;
public const CAPTCHA_PROVIDER_HCAPTCHA = 1;
public const CAPTCHA_PROVIDER_PROCAPTCHA = 2;

public function getProviders(): array
{
return [
self::CAPTCHA_PROVIDER_RECAPTCHA => new ReCaptchaProvider(),
self::CAPTCHA_PROVIDER_HCAPTCHA => new HCaptchaProvider(),
self::CAPTCHA_PROVIDER_PROCAPTCHA => new ProcaptchaProvider()
];
}

public function getProvider(int $provider): ?CaptchaProviderInterface
{
return $this->getProviders()[$provider] ?? null;
}
}
}
53 changes: 53 additions & 0 deletions src/Captcha/Providers/ProcaptchaProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
namespace App\Captcha\Providers;

use App\Captcha\CaptchaProviderInterface;

class ProcaptchaProvider implements CaptchaProviderInterface
{
private string $verifyUrl = 'https://api.procaptcha.io/siteverify';

public function validate(string $response, string $secretKey, ?string $userIp = null): bool
{
if (empty($response)) {
return false;
}

$data = [
'captcha' => $response,
'maxVerifiedTime' => 60,
];

$options = [
'http' => [
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
]
];

$context = stream_context_create($options);
$response = file_get_contents($this->verifyUrl, false, $context);
if ($response === false) {
return false;
}

$result = json_decode($response);
return $result->success;
}

public function getName(): string
{
return 'Procaptcha';
}

public function getHomePageUrl(): string
{
return 'https://www.prosopo.io/?utm_source=phpform&utm_medium=plugin&utm_campaign=phpform';
}

public function getDocumentationUrl(): string
{
return 'https://github.com/prosopo/captcha/blob/main/README.md';
}
}

0 comments on commit c6abf4b

Please sign in to comment.