web-base/Core/Objects/Captcha/CaptchaProvider.class.php

64 lines
1.5 KiB
PHP
Raw Normal View History

2024-04-23 14:05:29 +02:00
<?php
namespace Core\Objects\Captcha;
use Core\Objects\ApiObject;
abstract class CaptchaProvider extends ApiObject {
2024-04-24 16:02:16 +02:00
const DISABLED = "disabled";
2024-04-23 14:05:29 +02:00
const RECAPTCHA = "recaptcha";
const HCAPTCHA = "hcaptcha";
2024-04-24 16:02:16 +02:00
const PROVIDERS = [self::DISABLED, self::RECAPTCHA, self::HCAPTCHA];
2024-04-23 14:05:29 +02:00
private string $siteKey;
private string $secretKey;
protected string $error;
public function __construct(string $siteKey, string $secretKey) {
$this->siteKey = $siteKey;
$this->secretKey = $secretKey;
$this->error = "";
}
public function getSiteKey(): string {
return $this->siteKey;
}
public function getError(): string {
return $this->error;
}
public static function isValid(string $type): bool {
return in_array($type, [self::RECAPTCHA, self::HCAPTCHA]);
}
public abstract function verify(string $captcha, string $action): bool;
public abstract function getName(): string;
public function jsonSerialize(): array {
return [
"name" => $this->getName(),
"siteKey" => $this->getSiteKey(),
];
}
protected function performVerifyRequest(string $url, string $captcha) {
$params = [
"secret" => $this->secretKey,
"response" => $captcha
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = @json_decode(curl_exec($ch), true);
curl_close($ch);
return $response;
}
}