-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathajax.main1_nodb.php
More file actions
executable file
·395 lines (326 loc) · 13.1 KB
/
ajax.main1_nodb.php
File metadata and controls
executable file
·395 lines (326 loc) · 13.1 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
define("DB_NAME",'useraccess_gc7b724.sqlite');
$db = new SQLite3(DB_NAME);
$db->busyTimeout(5000);
$ip_adress = $_SERVER['REMOTE_ADDR'];// zufallszahl zum loggen
$cross = rand(1000,9999);
$ts = time(); // zum Zeitstempel aufdrücken und vergleichen
// WAL mode has better control over concurrency.
// Source: https://www.sqlite.org/wal.html
//$db->exec('PRAGMA journal_mode = wal;');
$action = '';
////////////////////// POST or GET requests //////////////////////
if (isset($_POST['a']))
{
$action = $_POST['a'];
}
////////////////////// Functions //////////////////////
// wandelt eine CSV datei in ein Array, Erste Zeile ist Indexname
function csv_to_array($filename='', $delimiter=';')
{
if(!file_exists($filename) || !is_readable($filename))
return FALSE;
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
{
if(!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
fclose($handle);
}
return $data;
}
// berechnet die Quersumme einer Zahl
function crossfoot ( $digits )
{
// Typcast falls Integer uebergeben
$strDigits = ( string ) $digits;
for( $intCrossfoot = $i = 0; $i < strlen ( $strDigits ); $i++ )
{
$intCrossfoot += $strDigits{$i};
}
return $intCrossfoot;
}
// baut Ajaxheader und gibt es aus
function send_ajax($arr)
{
header('Content-Type: application/json; charset=UTF-8');
print json_encode($arr);
}
// Hilfsfunktionen zur Abfrage
function sqlitequery($query)
{
$arr_result = array();
$db = new SQLite3(DB_NAME);
$r = $db->query($query);
while ($row = $r->fetchArray()) {
$arr_result[] = $row;
}
$db->close();
return $arr_result;
}
// Hilfsfunktion zum schreiben
function sqliteexec($query)
{
$db = new SQLite3(DB_NAME);
$r = $db->exec($query);
$db->close();
return $r;
}
// Codecheck 1 + 2
function compare_code($code,$which)
{
global $ip_adress; // Identifikation des Users
$ts = time(); // zum Zeitstempel aufdrücken und vergleichen
$result = 0; // default: erster code falsch
$code1 = '1931035';
$code2 = 'Gobius';
$forbidden = array(".", ",", " ");
$code_sauber = str_replace($forbidden, "", $code);
//print $code . " - " .$which . " - ".$code1;
if($which == 1)
{
if( strtolower($code_sauber) == strtolower($code1))
{
$result = 1; // 1. code passt
}else{
//
}
}else
if($which == 2)
{
if(strtolower($code_sauber) == strtolower($code2))
{
$result = 3; // 2. code passt
}else{
$result = 2; // 2.code passt nicht
}
}
// $q_write = "UPDATE access_list SET pw = '$code' ,try = 0, ts = $ts, step = $compare_result, cross = 0000 WHERE id = $id";
return $result;
}
function init()
{
global $ip_adress; // Identifikation des Users
$ts = time(); // zum Zeitstempel aufdrücken und vergleichen
// $max_time = $ts - 60; // 1 Minute
$max_time = $ts - 600; // 10 Minuten Timeout
// erstmal schauen, ob der Benutzer schon mal probiert hat, und gleich die id holen
$q = "SELECT * FROM access_list WHERE ip_adress = '$ip_adress' AND ts > $max_time";
$arr = sqlitequery($q);
// positiv:
// wenn ein datensatz kommt, war der user innerhalb der letzten Zeit mal am probieren
if(count($arr) != 0)
{
$arr_row = $arr[0]; // erste Zeile reicht, es kann ja nur einen Datansatz geben
$id = $arr_row['id'];
$q_write = "UPDATE access_list SET pw = 'init' ,try = 0, step = 0 WHERE id = $id";
sqliteexec($q_write);
return true;
}
return false;
}
// die eigentliche Codeauswertung mit Blick auf die Fehlversuche:
// Rückgabe: 0=erster code falsch; 1=erster Code richtig; 2=2.code falsch; 3= 2.code richtig; 5= Zu viele falsche versuche
/*
function check_code($code)
{
global $ip_adress; // Identifikation des Users
global $cross; // Zufallszahl, die der Logger eingeben muss
$ts = time(); // zum Zeitstempel aufdrücken und vergleichen
//$max_time = $ts - 60; // die letzte Minute
$max_time = $ts - 600; // 10 Minuten Timeout
$compare_result = 5; // default: Zu viele ungültige Versuche
// erstmal schauen, ob der Benutzer schon mal probiert hat, und gleich die id holen
$q = "SELECT * FROM access_list WHERE ip_adress = '$ip_adress' AND ts > $max_time";
$arr = sqlitequery($q);
// positiv:
// wenn ein datensatz kommt, war der user innerhalb der letzten Zeit mal am probieren
if(count($arr) != 0)
{
$arr_row = $arr[0]; // erste Zeile reicht, es kann ja nur einen Datansatz geben
$id = $arr_row['id'];
$try = $arr_row['try'];
$step = $arr_row['step']; // welcher Versuch war schonmal richtig?
// Bis zu 10 Versuche lassen wir zu
if($try < 9)
{
$textnr = 1;
if($step == 0){$textnr = 1;}
if($step == 1){$textnr = 2;}
if($step == 2){$textnr = 2;}
//$cross = crossfoot($ts);
$cross = rand(1000,9999);
//if($step == 3){$textnr = 2;}
$compare_result = compare_code($code,$textnr);
if(($compare_result == 1)||($compare_result == 3))
{
$q_write = "UPDATE access_list SET pw = '$code' ,try = 0, ts = $ts, step = $compare_result, cross = $cross WHERE id = $id";
}
else
{
$try = $try+1;
$q_write = "UPDATE access_list SET pw = '$code' ,try = $try, step = $compare_result, cross = $cross WHERE id = $id";
}
sqliteexec($q_write);
}
}
else
{
// hier isses ohnehin der erste Versuch nach einer gewissen Zeit
$id = 0;
$try = 0;
$step = 0;
$compare_result = compare_code($code,1);
// Egal ob richtig oder falsch, ersten Versuch registrieren
if($compare_result == 1){$try = 0;}else{$try = 1;} // wenn falsch, dann Versuchszähler eins hoch
$q_write = "INSERT INTO access_list (ip_adress,ts,pw,try,step) VALUES ('$ip_adress',$ts,'$code',$try,$compare_result)";
sqliteexec($q_write);
}
return $compare_result;
}
*/
/*
function write_codeword($codeword)
{
global $ip_adress; // Identifikation des Users
$ts = time(); // zum Zeitstempel aufdrücken und vergleichen
$max_time = $ts - 600; // 10 Minuten Timeout
// anhand der Benutzer-ip die id holen
$q = "SELECT * FROM access_list WHERE ip_adress = '$ip_adress' AND ts > $max_time";
$arr = sqlitequery($q);
if(count($arr) != 0)
{
$arr_row = $arr[0]; // erste Zeile reicht, es kann ja nur einen Datansatz geben
$id = $arr_row['id'];
if($id > 0)
{
$q_write = "UPDATE access_list SET codeword = '$codeword' WHERE id = $id";
sqliteexec($q_write);
return true;
}
}
return false;
}
*/
function check_bruteforce(){
global $ip_adress; // Identifikation des Users
$ts = time(); // zum Zeitstempel aufdrücken und vergleichen
$max_time = $ts - 300; // 5 Minuten Timeout
// anhand der Benutzer-ip die id holen
$q = "SELECT * FROM access_list WHERE ip_adress = '$ip_adress' AND ts > $max_time";
$arr = sqlitequery($q);
return count($arr);
}
// Wörterliste laden und Array draus machen
function load_csv()
{
$filename = 'woerterliste.csv';
$arr_csv = csv_to_array($filename);
$rand = rand(0,count($arr_csv)-1);
$arr_result = $arr_csv[$rand];
//write_codeword($arr_result['de-gr']);
return $arr_result;
}
////////////////////// MAIN //////////////////////
if($action == 'check_code')
{
$code = '';
$which = '';
//$code = mysql_real_escape_string($_REQUEST['code']);
if(isset($_POST['code'])){$code = trim($_POST['code']);}
if(isset($_POST['wh'])){$which = trim($_POST['wh']);}
$arr_result = array();
// GER
$txt0_ger = 'Nein, der Code "'.$code.'" ist leider falsch.<br/>Versuchs nochmal von Vorn!<br/>Gib die Zahl <b>[X]</b> ins Eingabefeld ein.';
$txt1_ger = '<div class="answer_correct"><b >Richtig! <img src="img/ghost_happy.jpeg" style="width:50px;"/>Du hast mich im "Flüsterbogen" , Untermarkt 22 gefunden!</b>';
//$txt1 .= '<p><br/>Solltest Du zu Zweit sein, kannst Du mal versuchen, auf einer Seite in die Rille des Bogens zu flüstern, während auf der anderen Seite Jemand versucht, das geflüsterte Wort zu hören!</p>';
$txt1_ger .= '<br/>Geh doch mal hinein und suche auf der linken Seite hinter der Wand mein Klingelschild! <br/>Klingel bei mir,<b> finde meinen Namen heraus und gib Ihn hier ein!</b><br/><div id="descript_remark">Sollte die Tür im Winter zu sein, drücke vorsichtig dagegen, und schließe sie wieder hinter Dir!<br/>Sollte leider komplett verschlossen sein, dann nutze bitte die Logbedingung per Mail an <a href="mailto:geistvomuntermarkt-1931035@s-drum.de">geistvomuntermarkt-1931035@s-drum.de</a> !</div>';
$txt1_ger .= '</div>';
$txt2_ger = 'Nein, "'.$code.'" heiße ich leider nicht!<br/>Gehe hinein, suche auf der linken Seite hinter der Tür hinter der Wand und <br/><b>finde meinen Namen heraus, indem Du mal bei mir klingelst!</b>!';
$txt3_ger = '<b>Gratulation, so heiße ich!</b>';
$txt3_ger .= '<p >Danke, dass Du mich, den Geist des Untermarkts besucht hast. Du darfst nun natürlich loggen, wenn Du die Logbedingung respektierst.</p>';
$txt3_ger .= '<p >Nun zur Logbedingung: <br/>Bitte baue in Deinen Online-Log diesen typischen Görlitzer Ausdruck zusammen mit der 4-Stelligen Kontrollzahl ein: </p>';
$txt4_ger = '<b>Sinnloses rumprobieren bringt doch nichts ;)<br/>Zu viele Falscheingaben!</b>';
$txt4_ger .= '<p>Bitte ein paar Minuten warten zum weitermachen.</p>';
// ENG
$txt0_eng = 'No, Sorry. the code "'.$code.'" was wrong or you have been waiting too long.<br/>Try again!Enter the number <b>[X]</b> in the input field to get the next step';
$txt1_eng = '<b>Correct, you have found me! I\'m living in the "Flüsterbogen"</b>';
//$txt1 .= '<p><br/>Solltest Du zu Zweit sein, kannst Du mal versuchen, auf einer Seite in die Rille des Bogens zu flüstern, während auf der anderen Seite Jemand versucht, das geflüsterte Wort zu hören!</p>';
$txt1_eng .= '<p>Enter the entrance portal and search my doorbell to <b>find out my name !</b><br/> Enter the Name in the Input-field<br/><br/>';
$txt1_eng .= 'The doorbell you will find on the left side behind the wall</p>';
$txt2_eng = 'No, my name isn\'t "'.$code.'", sorry!<br/>Enter the entrance portal, search on the left side behind the door and ring on my bell to <b>find out my name!</b><br/>';
$txt3_eng = '<b>Yes, it\'s my Name, congratulations!</b>';
$txt3_eng .= '<p >Thank you for visiting me, the ghost of the Untermarkt. Of course, you may log, when you respect my log-condition! I hope, you had a lot of fun with me.</p>';
$txt3_eng .= '<p >Here comes the log-condition:<br/>Please insert this typical dialect word and the 4Digit check-number into your Online-Log:</p>';
$txt4_eng = '<b>Too many wrong inputs!</b>';
$txt4_eng .= '<p>Please wait some minutes to go forward.</p>';
$coderesult = 0;
$trys = check_bruteforce();
if ($trys > 20){
$coderesult = 4; // BRUTEFORCE!
}else{
if (preg_match("/^([a-zA-Z0-9öäüßÖÄÜß?!,.;:_() \n\r-]+)$/is", $code))
{
//$coderesult = check_code($code);
$coderesult = compare_code($code,$which);
}
}
$snip = '';
$arr_result['result'] = $coderesult;
$arr_result['try'] = $trys;
if($coderesult == 0){ // Nummer falsch
$arr_result['answer_ger'] = $txt0_ger;
$arr_result['answer_eng'] = $txt0_eng;
$arr_result['pic'] = 'umarkt_2.jpg';
}else if($coderesult == 1){ // Nummer richtig
$arr_result['answer_ger'] = $txt1_ger;
$arr_result['answer_eng'] = $txt1_eng;
$arr_result['pic'] = 'fbogen.jpg';
}else if($coderesult == 2){ // Gobius falsch
$arr_result['answer_ger'] = $txt2_ger;
$arr_result['answer_eng'] = $txt2_eng;
$arr_result['pic'] = 'fbogen.jpg';
}else if($coderesult == 3){ // Gobius richtig
$arr_snipplet = load_csv();
$arr_result['answer_ger'] = $txt3_ger;
$arr_result['answer_eng'] = $txt3_eng;
$arr_result['pic'] = '100629069_8.jpg';
$arr_result['snipplet'] = $arr_snipplet;
$arr_result['crossfoot'] = $cross;
$snip = $arr_snipplet['de-gr'];
}else if($coderesult == 4){ // Bruteforce Versuch
$arr_result['answer_ger'] = $txt4_ger;
$arr_result['answer_eng'] = $txt4_eng;
$arr_result['pic'] = 'umarkt_2.jpg';
}
send_ajax( $arr_result );
//log
$q_write = "INSERT INTO access_list (ip_adress,ts,pw,try,step,cross, codeword) VALUES (
'$ip_adress',
$ts,
'$code',
'$trys',
'$coderesult',
'$cross',
'$snip')";
sqliteexec($q_write);
}
if($action == 'init')
{
//$txt0_ger = 'Willkommen, edler Fremder! <br/>In nur 2 Schritten bist Du am Ziel. <br/>Finde zuerst heraus, wo ich wohne! Gib den Namen des Hauses gefolgt von der Zahl [X] ein.<br/>Beispiel: Geisterhaus12345';
$txt0_ger = 'Willkommen auf dem Untermarkt, edler Fremder! <br/>In nur 2 Schritten bist Du am Ziel. <br/>Gib zur Kontrolle die 7-Stellige Zahl <b>[X]</b> ins Eingabefeld ein.';
$txt0_eng = 'Welcome to the Untermarket, dear visitor!<br/>In only 2 steps you are at the goal.<br/>Enter the number <b>[X]</b> (7 digits) to get the next step!';
//init();
$arr_result = array();
$arr_result['result'] = -1;
$arr_result['answer_ger'] = $txt0_ger;
$arr_result['answer_eng'] = $txt0_eng;
send_ajax( $arr_result );
}
?>