diff --git a/inc/Service/SecureimageCaptchaQuery.php b/inc/Service/SecureimageCaptchaQuery.php
index 90dcdbb6..0b33c059 100644
--- a/inc/Service/SecureimageCaptchaQuery.php
+++ b/inc/Service/SecureimageCaptchaQuery.php
@@ -38,6 +38,8 @@ class SecureImageCaptchaQuery {
];
$ret = $this->http->requestGet($this->domain . '/' . $this->provider_check, $data);
- return $ret === '1';
+ $resp = \json_decode($ret, true, 16, \JSON_THROW_ON_ERROR);
+
+ return isset($resp['success']) && $resp['success'];
}
}
diff --git a/inc/config.php b/inc/config.php
index feecaca9..5f9b7130 100644
--- a/inc/config.php
+++ b/inc/config.php
@@ -393,7 +393,9 @@
'new_thread_capt' => false,
// Securimage customization options
// https://github.com/dapphp/securimage/blob/nextgen/examples/securimage_show_example.php#L49
- 'securimage_options' => ['send_headers' => false, 'no_exit' => true]
+ 'securimage_options' => ['send_headers' => false, 'no_exit' => true],
+ // Captcha expires (in seconds)
+ 'expires_in' => 320
]
];
diff --git a/securimage.php b/securimage.php
index f5e5a81c..8ebfa154 100644
--- a/securimage.php
+++ b/securimage.php
@@ -1,69 +1,88 @@
execute([time() - $expires_in]);
}
+function handleGetRequestCaptcha(array $config): void {
+ $extra = $config['captcha']['native']['extra'];
+ $cookie = rand_string(20, $extra);
-$mode = @$_GET['mode'];
-switch ($mode) {
+ $securimage = new Securimage($config['captcha']['native']['securimage_options']);
+ $securimage->createCode();
+
+ ob_start();
+ $securimage->show();
+ $rawImage = ob_get_clean();
+
+ $base64Image = 'data:image/png;base64,' . base64_encode($rawImage);
+ $html = '
';
+ $captchaCode = $securimage->getCode();
+
+ prepare("INSERT INTO `captchas` (`cookie`, `extra`, `text`, `created_at`) VALUES (?, ?, ?, ?)")
+ ->execute([$cookie, $extra, $captchaCode->code_display, $captchaCode->creationTime]);
+
+ if (isset($_GET['raw'])) {
+ $_SESSION['captcha_cookie'] = $cookie;
+ header('Content-Type: image/png');
+ echo $rawImage;
+ } else {
+ header("Content-Type: application/json");
+ echo json_encode([
+ "cookie" => $cookie,
+ "captchahtml" => $html,
+ "expires_in" => $config['captcha']['native']['expires_in'],
+ ]);
+ }
+}
+
+function handleCheckRequestCaptcha(int $expires_in): void {
+ cleanup($expires_in);
+
+ $cookie = $_GET['cookie'] ?? null;
+ $text = $_GET['text'] ?? null;
+
+ if (!$cookie || !$text) {
+ echo json_encode(["success" => false]);
+ return;
+ }
+
+ $query = prepare("SELECT * FROM `captchas` WHERE `cookie` = ?");
+ $query->execute([$cookie]);
+ $captchaData = $query->fetchAll();
+
+ if (!$captchaData) {
+ echo json_encode(["success" => false]);
+ return;
+ }
+
+ prepare("DELETE FROM `captchas` WHERE `cookie` = ?")->execute([$cookie]);
+
+ $isSuccessful = $captchaData[0]['text'] === $text;
+ echo json_encode(["success" => $isSuccessful]);
+}
+
+$mode = $_GET['mode'] ?? null;
+
+switch($mode) {
case 'get':
- header("Content-type: application/json");
- $extra = $config['captcha']['native']['extra'];
- $cookie = rand_string(20, $extra);
- $i = new Securimage($config['captcha']['native']['securimage_options']);
- $i->createCode();
- ob_start();
- $i->show();
- $rawimg = ob_get_contents();
- $b64img = 'data:image/png;base64,'.base64_encode($rawimg);
- $html = '
';
- ob_end_clean();
- $cdata = $i->getCode();
- $query = prepare("INSERT INTO `captchas` (`cookie`, `extra`, `text`, `created_at`) VALUES (?, ?, ?, ?)");
- $query->execute([$cookie, $extra, $cdata->code_display, $cdata->creationTime]);
- if (isset($_GET['raw'])) {
- $_SESSION['captcha_cookie'] = $cookie;
- header('Content-Type: image/png');
- echo $rawimg;
- } else {
- echo json_encode(["cookie" => $cookie, "captchahtml" => $html, "expires_in" => $expires_in]);
- }
+ handleGetRequestCaptcha($config);
break;
case 'check':
- cleanup();
- if (!isset ($_GET['mode']) || !isset ($_GET['cookie']) || !isset ($_GET['text'])) {
- die();
- }
-
- $query = prepare("SELECT * FROM `captchas` WHERE `cookie` = ?");
- $query->execute([$_GET['cookie']]);
-
- $ary = $query->fetchAll();
-
- if (!$ary) { // captcha expired
- echo "0";
- break;
- } else {
- $query = prepare("DELETE FROM `captchas` WHERE `cookie` = ?");
- $query->execute([$_GET['cookie']]);
- }
-
- if ($ary[0]['text'] !== $_GET['text']) {
- echo "0";
- } else {
- echo "1";
- }
+ handleCheckRequestCaptcha($config['captcha']['native']['expires_in']);
break;
-}
+ case '':
+ default:
+ http_response_code(400);
+ echo json_encode(["success" => false, "error" => "Invalid mode"]);
+ break;
+}
\ No newline at end of file