summaryrefslogtreecommitdiff
path: root/public/captcha.php
diff options
context:
space:
mode:
Diffstat (limited to 'public/captcha.php')
-rw-r--r--public/captcha.php92
1 files changed, 43 insertions, 49 deletions
diff --git a/public/captcha.php b/public/captcha.php
index 58283bf..b454b7d 100644
--- a/public/captcha.php
+++ b/public/captcha.php
@@ -1,65 +1,59 @@
<?php
include_once "../src/config.php";
include_once "../src/alert.php";
+include_once "../src/captcha.php";
+include_once "../src/utils.php";
session_start();
-if (!HCAPTCHA_ENABLE) {
- $_SESSION["captcha_solved"] = true;
- header("Location: /");
+if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["answer"])) {
+ if ($_POST["answer"] == ($_SESSION["captcha_word"] ?? "")) {
+ $_SESSION["captcha_solved"] = true;
+ echo json_response([
+ "status_code" => 200,
+ "message" => "Solved!",
+ "data" => null
+ ]);
+ } else {
+ echo json_response([
+ "status_code" => 400,
+ "message" => "Wrong answer!",
+ "data" => null
+ ], 400);
+ }
exit;
}
-if (isset($_SESSION["captcha_solved"]) && $_SESSION["captcha_solved"]) {
- header("Location: /");
+$file_folder = $_SERVER["DOCUMENT_ROOT"] . '/static/img/captcha';
+
+if (!CAPTCHA_ENABLE || ($_SESSION["captcha_solved"] ?? false) || !is_dir($file_folder)) {
+ $_SESSION["captcha_solved"] = true;
+ echo json_response([
+ "status_code" => 200,
+ "message" => "No need to solve captcha",
+ "data" => null
+ ]);
exit;
}
-if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["h-captcha-response"])) {
- // sending a request to captcha api
- $request = curl_init("https://hcaptcha.com/siteverify");
- curl_setopt($request, CURLOPT_POST, 1);
- curl_setopt($request, CURLOPT_HTTPHEADER, [sprintf("User-Agent: %s/1.0", INSTANCE_NAME)]);
- curl_setopt(
- $request,
- CURLOPT_POSTFIELDS,
- http_build_query(array("secret" => HCAPTCHA_SECRETKEY, "response" => $_POST["h-captcha-response"]))
- );
- curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
-
- $response = curl_exec($request);
- curl_close($request);
-
- $json = json_decode($response);
-
- if ($json->success) {
- $_SESSION["captcha_solved"] = true;
- header("Location: /");
- exit;
- }
-}
-?>
+$files = scandir($file_folder);
+array_splice($files, 0, 2);
-<html>
+$filename = $files[random_int(0, count($files) - 1)];
+$filename = basename($filename, ".png");
-<head>
- <title>Resolving a hCaptcha - <?php echo INSTANCE_NAME ?></title>
- <link rel="stylesheet" href="/static/style.css">
- <link rel="shortcut icon" href="/static/favicon.ico" type="image/x-icon">
- <script src='https://www.hCaptcha.com/1/api.js' async defer></script>
-</head>
+$_SESSION["captcha_word"] = $filename;
-<body>
- <noscript>JavaScript is required to solve hCaptcha</noscript>
- <div class="container">
- <div class="wrapper">
- <section class="row" style="padding: 4px; justify-content: center;">
- <section class="box">
- <div class="h-captcha" data-sitekey="<?php echo HCAPTCHA_SITEKEY ?>"></div>
- </section>
- </section>
- </div>
- </div>
-</body>
+$image = generate_image_captcha(
+ CAPTCHA_SIZE[0],
+ CAPTCHA_SIZE[1],
+ random_int(1, 3),
+ $filename,
+ $file_folder
+);
-</html> \ No newline at end of file
+echo json_response([
+ "status_code" => 200,
+ "message" => null,
+ "data" => $image
+]); \ No newline at end of file