-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
189 lines (166 loc) · 6.46 KB
/
index.php
File metadata and controls
189 lines (166 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
function getReqest($url) {
$curl = curl_init(BASE_URL . $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
function postRequest($url) {
$curl = curl_init(BASE_URL . $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
function createNewCaptcha(
$length,
$width,
$height,
$filters,
$authToken = AUTH_TOKEN,
) {
$url = "/api/v1/new?auth=$authToken&len=$length&width=$width&height=$height&filters=$filters";
$response = getReqest($url);
return $response;
}
function verifyCaptchaCode($captchaId, $code, $authToken = AUTH_TOKEN) {
$url = "/api/v1/verify?id=$captchaId&code=$code&auth=$authToken";
$response = postRequest($url);
return $response;
}
function head() {
echo '<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Captcha Example</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>';
}
// Authentication token
const AUTH_TOKEN = "secret";
// Base URL of the API
const BASE_URL = "http://localhost:8000";
// Captcha settings
const CAPTCHA_LENGTH = 6;
const CAPTCHA_WIDTH = 200;
const CAPTCHA_HEIGHT = 100;
const CAPTCHA_FILTERS = null;
session_start();
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
// Update session variables
if (isset($_GET['length'])) {
$_SESSION['length'] = $_GET['length'];
}
if (isset($_GET['width'])) {
$_SESSION['width'] = $_GET['width'];
}
if (isset($_GET['height'])) {
$_SESSION['height'] = $_GET['height'];
}
if (isset($_GET['filters'])) {
$_SESSION['filters'] = $_GET['filters'];
}
// Generate new captcha
$captcha = createNewCaptcha(
length: $_SESSION['length'] ?? CAPTCHA_LENGTH,
width: $_SESSION['width'] ?? CAPTCHA_WIDTH,
height: $_SESSION['height'] ?? CAPTCHA_HEIGHT,
filters: $_SESSION['filters'] ?? CAPTCHA_FILTERS,
);
// Decode captcha response
$captcha = json_decode($captcha, true);
// Check if captcha is generated successfully
if (!isset($captcha['id']) && !isset($captcha['url']) && !isset($captcha['expire_time'])) {
die("An error occured while generating captcha");
}
// Get captcha details
$captchaId = $captcha['id'];
$captchaUrl = $captcha['url'];
$captchaExpiresAt = $captcha['expire_time'];
}
// Verify captcha
else if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$captchaId = $_POST['captchaId'];
$code = $_POST['code'];
$verifyCaptcha = verifyCaptchaCode($captchaId, $code);
// Decode captcha response
$response = json_decode($verifyCaptcha, true);
echo "<html lang='en'>";
head();
echo "<div class='container'>";
if (isset($response['ok'])) {
echo "<h1 style='color: green;'>Response is OK</h1>";
echo "<div class='alert alert-success'>" . $response['ok'] . "</div>";
} else if (isset($response['warn'])) {
echo "<h1 style='color: orange;'>Response is Warning</h1>";
echo "<div class='alert alert-warning'>" . $response['warn'] . "</div>";
} else if (isset($response['error'])) {
echo "<h1 style='color: red;'>Response is Error</h1>";
echo "<div class='alert alert-danger'>" . $response['error'] . "</div>";
} else {
echo "<h1 style='color: red;'>Unknown Response</h1>";
echo "<div class='alert alert-danger'>" . $response . "</div>";
}
echo "<a href='index.php'>Go Back</a>";
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Example Captcha</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h1>Capthca Example</h1>
<div class="col-md-6">
<h2>Captcha Image</h2>
<img src="<?= $captchaUrl ?>" alt="Captcha Image">
<hr>
<h2>Verify Captcha</h2>
<form method="POST" action="index.php">
<div class="form-group">
<input type="hidden" name="captchaId" value="<?= $captchaId ?>">
<label for="code">Enter the CAPTCHA Code:</label>
<input type="text" name="code" id="code" class="form-control" required>
</div>
<button type="submit" class="btn btn-primary">Verify CAPTCHA</button>
</form>
<hr>
<h3>Settings</h3>
<form method="GET" action="index.php">
<div class="form-group">
<label for="length">Length:</label>
<input type="number" name="length" id="length" class="form-control" value="<?= $_SESSION['length'] ?? CAPTCHA_LENGTH ?>">
</div>
<div class="form-group">
<label for="width">Width:</label>
<input type="number" name="width" id="width" class="form-control" value="<?= $_SESSION['width'] ?? CAPTCHA_WIDTH ?>">
</div>
<div class="form-group">
<label for="height">Height:</label>
<input type="number" name="height" id="height" class="form-control" value="<?= $_SESSION['height'] ?? CAPTCHA_HEIGHT ?>">
</div>
<div class="form-group">
<label for="filters">Filters:</label>
<input type="text" name="filters" id="filters" class="form-control" value="<?= $_SESSION['filters'] ?? CAPTCHA_FILTERS ?>">
<small class="form-text text-muted">For more info about the filters syntax goto <a href="<?= BASE_URL ?>/api/v1/help/filters" target="_blank"><?= BASE_URL ?>/api/v1/help/filters</a></small>
</div>
<button type="submit" class="btn btn-primary">Generate New Captcha</button>
</form>
<hr>
<h3>Details</h3>
<ul>
<li>Captcha ID: <code><?= $captchaId ?></code></li>
<li>Captcha Expires At: <code><?= $captchaExpiresAt ?></code></li>
<li>Captcha URL: <code><?= $captchaUrl ?></code></li>
</ul>
</div>
</div>
</body>
</html>