1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2025-01-07 12:01:43 +01:00
vichan/8chan-captcha/entrypoint.php

67 lines
1.8 KiB
PHP
Raw Normal View History

2015-03-13 00:48:05 +01:00
<?php
header('Access-Control-Allow-Origin: *');
$mode = @$_GET['mode'];
chdir('..'); // for "cool PHP CAPTCHA"'s resourcesPath
include "inc/functions.php"; // general 8chan functions
2015-03-13 00:48:05 +01:00
switch ($mode) {
// Request: GET entrypoint.php?mode=get&extra=1234567890
// Response: JSON: cookie => "generatedcookie", captchahtml => "captchahtml", expires_in => 120
case "get":
if (!isset ($_GET['extra'])) {
die();
}
$extra = $_GET['extra'];
$nojs = isset($_GET['nojs']);
2015-03-13 05:45:57 +01:00
$captcha = generate_captcha($extra);
$cookie = $captcha['cookie'];
$html = $captcha['html'];
2015-03-13 00:48:05 +01:00
if ($nojs) {
header("Content-type: text/html");
echo "<html><body>You do not have JavaScript enabled. To fill out the CAPTCHA, please copy the ID to the post form in the ID field, and write the answer in the answer field.<br><br>CAPTCHA ID: $cookie<br>CAPTCHA image: $html</body></html>";
} else {
header("Content-type: application/json");
echo json_encode(["cookie" => $cookie, "captchahtml" => $html, "expires_in" => $config['captcha']['expires_in']]);
2015-03-13 00:48:05 +01:00
}
break;
// Request: GET entrypoint.php?mode=check&cookie=generatedcookie&extra=1234567890&text=captcha
// Response: 0 OR 1
case "check":
if (!isset ($_GET['mode'])
|| !isset ($_GET['cookie'])
|| !isset ($_GET['extra'])
|| !isset ($_GET['text'])) {
die();
}
cleanup();
2015-03-13 00:48:05 +01:00
$query = prepare("SELECT * FROM `captchas` WHERE `cookie` = ? AND `extra` = ?");
2015-03-13 00:48:05 +01:00
$query->execute([$_GET['cookie'], $_GET['extra']]);
$ary = $query->fetchAll();
if (!$ary) {
echo "0";
}
else {
$query = prepare("DELETE FROM `captchas` WHERE `cookie` = ? AND `extra` = ?");
2015-03-13 00:48:05 +01:00
$query->execute([$_GET['cookie'], $_GET['extra']]);
if ($ary[0]['text'] !== $_GET['text']) {
echo "0";
}
else {
echo "1";
}
}
break;
}