summaryrefslogtreecommitdiff
path: root/lib/captcha_gen.php
blob: 2ae824be695c9e639ba036366c4631e59bc9ea3e (plain)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php

class captcha{
	
	public function __construct($frontend, $get, $filters, $page, $output){
		
		/*
			Validate cookie, if it exists
		*/
		if(isset($_COOKIE["pass"])){
			
			if(
				// check if key is not malformed
				preg_match(
					'/^c[0-9]+\.[A-Za-z0-9]{20}$/',
					$_COOKIE["pass"]
				) &&
				// does key exist
				apcu_exists($_COOKIE["pass"])
			){
					
				// exists, increment counter
				$inc = apcu_inc($_COOKIE["pass"]);
				
				// we start counting from 1
				// when it has been incremented to 102, it has reached
				// 100 reqs
				if($inc >= 102){
					
					// reached limit, delete and give captcha
					apcu_delete($_COOKIE["pass"]);
				}else{
					
					// the cookie is OK! dont die() and give results
					if($output === true){
						$frontend->loadheader(
							$get,
							$filters,
							$page
						);
					}
					return;
				}
			}
		}
		
		if($output === false){
			
			echo json_encode([
				"status" => "The \"pass\" token in your cookies is missing or has expired!!"
			]);
			die();
		}
		
		/*
			Validate form data
		*/
		$lines =
			explode(
				"\r\n",
				file_get_contents("php://input")
			);

		$invalid = false;
		$answers = [];
		$key = false;
		$error = "";

		foreach($lines as $line){
			
			$line = explode("=", $line, 2);
			
			if(count($line) !== 2){
				
				$invalid = true;
				break;
			}
			
			preg_match(
				'/^c\[([0-9]+)\]$/',
				$line[0],
				$regex
			);
			
			if(
				$line[1] != "on" ||
				!isset($regex[0][1])
			){
				
				// check if its k
				if(
					$line[0] == "k" &&
					strpos($line[1], "c.") === 0
				){
					
					$key = apcu_fetch($line[1]);
					apcu_delete($line[1]);
				}
				break;
			}
			
			$regex = (int)$regex[1];
			
			if(
				$regex >= 16 ||
				$regex <= -1
			){
				
				$invalid = true;
				break;
			}
			
			$answers[] = $regex;
		}

		if(
			!$invalid &&
			$key !== false
		){
			$check = $key[1];
			
			// validate answer
			for($i=0; $i<count($key[0]); $i++){
				
				if(!in_array($i, $answers)){
					
					continue;
				}
				
				if($key[0][$i][0] == $key[2]){
					
					$check--;
				}else{
					
					// got a wrong answer
					$check = -1;
					break;
				}
			}
			
			if($check === 0){
				
				// we passed the captcha
				// set cookie
				$inc = apcu_inc("cookie");
				$chars =
					array_merge(
						range("A", "Z"),
						range("a", "z"),
						range(0, 9)
					);
				
				$c = count($chars) - 1;
				
				$key = "c" . $inc . ".";
				
				for($i=0; $i<20; $i++){
					
					$key .= $chars[random_int(0, $c)];
				}
				
				apcu_inc($key, 1, $stupid, 86400);
				
				setcookie(
					"pass",
					$key,
					[
						"expires" => time() + 86400, // expires in 24 hours
						"samesite" => "Strict",
						"path" => "/"
					]
				);
				
				$frontend->loadheader(
					$get,
					$filters,
					$page
				);
				return;
				
			}else{
				
				$error = "<div class=\"quote\">You were <a href=\"https://www.youtube.com/watch?v=e1d7fkQx2rk\" target=\"_BLANK\" rel=\"noreferrer nofollow\">kicked out of Mensa.</a> Please try again.</div>";
			}
		}

		/*
			Generate random grid data to pass to captcha.php
		*/
		$dataset = [
			["birds", 2263],
			["fumo_plushies", 1006],
			["minecraft", 848]
		];

		// get the positions for the answers
		// will return between 3 and 6 answer positions
		$range = range(0, 15);
		$answer_pos = [];

		array_splice($range, 0, 1);

		for($i=0; $i<random_int(3, 6); $i++){
			
			$answer_pos_tmp =
				array_splice(
					$range,
					random_int(
						0,
						14 - $i
					),
					1
				);
			
			$answer_pos[] = $answer_pos_tmp[0];
		}

		// choose a dataset
		$choosen = &$dataset[random_int(0, count($dataset) - 1)];
		$choices = [];

		for($i=0; $i<count($dataset); $i++){
			
			if($dataset[$i][0] == $choosen[0]){
				
				continue;
			}
			
			$choices[] = $dataset[$i];
		}

		// generate grid data
		$grid = [];

		for($i=0; $i<16; $i++){
			
			if(in_array($i, $answer_pos)){
				
				$grid[] = $choosen;
			}else{
				
				$grid[] = $choices[random_int(0, count($choices) - 1)];
			}
		}

		$key = "c." . apcu_inc("captcha_gen", 1) . "." . random_int(0, 100000000);

		apcu_store(
			$key,
			[
				$grid,
				count($answer_pos),
				$choosen[0],
				false // has captcha been generated?
			],
			120 // we give user 2 minutes to get captcha, in case of network error
		);
		
		$payload = [
			"class" => "",
			"right-left" => "",
			"right-right" => "",
			"left" =>
				'<div class="infobox">' .
					'<h1>IQ test</h1>' .
					'Due to getting hit with 20,000 bot requests per day, I had to put this up. Sorry.<br><br>' .
					'Solving this captcha will allow you to make 100 searches today. I will add a way for legit users to bypass the captcha later. Sorry /g/tards!!' .
					$error .
					'<form method="POST" enctype="text/plain" autocomplete="off">' .
						'<div class="captcha-wrapper">' .
							'<div class="captcha">' .
								'<img src="captcha.php?k=' . $key . '" alt="Captcha image">' .
								'<div class="captcha-controls">' .
									'<input type="checkbox" name="c[0]" id="c0">' .
									'<label for="c0"></label>' .
									'<input type="checkbox" name="c[1]" id="c1">' .
									'<label for="c1"></label>' .
									'<input type="checkbox" name="c[2]" id="c2">' .
									'<label for="c2"></label>' .
									'<input type="checkbox" name="c[3]" id="c3">' .
									'<label for="c3"></label>' .
									'<input type="checkbox" name="c[4]" id="c4">' .
									'<label for="c4"></label>' .
									'<input type="checkbox" name="c[5]" id="c5">' .
									'<label for="c5"></label>' .
									'<input type="checkbox" name="c[6]" id="c6">' .
									'<label for="c6"></label>' .
									'<input type="checkbox" name="c[7]" id="c7">' .
									'<label for="c7"></label>' .
									'<input type="checkbox" name="c[8]" id="c8">' .
									'<label for="c8"></label>' .
									'<input type="checkbox" name="c[9]" id="c9">' .
									'<label for="c9"></label>' .
									'<input type="checkbox" name="c[10]" id="c10">' .
									'<label for="c10"></label>' .
									'<input type="checkbox" name="c[11]" id="c11">' .
									'<label for="c11"></label>' .
									'<input type="checkbox" name="c[12]" id="c12">' .
									'<label for="c12"></label>' .
									'<input type="checkbox" name="c[13]" id="c13">' .
									'<label for="c13"></label>' .
									'<input type="checkbox" name="c[14]" id="c14">' .
									'<label for="c14"></label>' .
									'<input type="checkbox" name="c[15]" id="c15">' .
									'<label for="c15"></label>' .
								'</div>' .
							'</div>' .
						'</div>' .
						'<input type="hidden" name="k" value="' . $key . '">' .
						'<input type="submit" value="Check IQ" class="captcha-submit">' .
					'</form>' .
				'</div>'
		];
		
		http_response_code(429); // too many reqs
		$frontend->loadheader(
			$get,
			$filters,
			"web"
		);
		
		echo $frontend->load("search.html", $payload);
		die();
	}
}