-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
351 lines (332 loc) · 11.1 KB
/
index.php
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
<?php
require_once __DIR__ . '/vendor/autoload.php';
$alphabets = [
0 => [
'a' => 'a',
'b' => 'b',
'c' => 'c',
'd' => 'd',
'e' => 'e',
'f' => 'f',
'g' => 'g',
'h' => 'h',
'i' => 'i',
'j' => 'j',
'k' => 'k',
'l' => 'l',
'm' => 'm',
'n' => 'n',
'o' => 'o',
'p' => 'p',
'q' => 'q',
'r' => 'r',
's' => 's',
't' => 't',
'u' => 'u',
'v' => 'v',
'w' => 'w',
'x' => 'x',
'y' => 'y',
'z' => 'z',
],
1 => [
"a"=>".-",
"b"=>"-...",
"c"=>"-.-.",
"d"=>"-..",
"e"=>".",
"f"=>"..-.",
"g"=>"--.",
"h"=>"....",
"i"=>"..",
"j"=>".---",
"k"=>"-.-",
"l"=>".-..",
"m"=>"--",
"n"=>"-.",
"o"=>"---",
"p"=>".--.",
"q"=>"--.-",
"r"=>".-.",
"s"=>"...",
"t"=>"-",
"u"=>"..-",
"v"=>"...-",
"w"=>".--",
"x"=>"-..-",
"y"=>"-.--",
"z"=>"--..",
],
2 => [
'a' => '.',
'b' => '-',
'c' => '..',
'd' => '.-',
'e' => '-.',
'f' => '--',
'g' => '...',
'h' => '..-',
'i' => '.-.',
'j' => '.--',
'k' => '-..',
'l' => '-.-',
'm' => '--.',
'n' => '---',
'o' => '....',
'p' => '...-',
'q' => '..-.',
'r' => '..--',
's' => '.-..',
't' => '.-.-',
'u' => '.--.',
'v' => '.---',
'w' => '-...',
'x' => '-..-',
'y' => '-.-.',
'z' => '-.--',
],
3 => [
'a' => '..',
'b' => '.--.',
'c' => '--.',
'd' => '---',
'e' => '.',
'f' => '.---',
'g' => '-...',
'h' => '.-..',
'i' => '.-',
'j' => '.-.-',
'k' => '-.-',
'l' => '-..',
'm' => '...-',
'n' => '-.',
'o' => '-',
'p' => '....',
'q' => '-.--',
'r' => '..-',
's' => '...',
't' => '--',
'u' => '.--',
'v' => '.-.',
'w' => '-.-.',
'x' => '-..-',
'y' => '..--',
'z' => '..-.',
],
4 => [
'a' => '-..',
'b' => '-..-',
'c' => '.---',
'd' => '--.',
'e' => '..',
'f' => '---',
'g' => '....',
'h' => '...-',
'i' => '..-',
'j' => '..-.',
'k' => '..--',
'l' => '.-..',
'm' => '-.--',
'n' => '-.-.',
'o' => '.-.',
'p' => '.--',
'q' => '.',
'r' => '.-',
's' => '-.-',
't' => '-.',
'u' => '...',
'v' => '-...',
'w' => '-',
'x' => '.--.',
'y' => '.-.-',
'z' => '--',
]
];
// morse
// abecedni morse
// cesta frekvencka
// qwertz
$code = 's tri';
$title = 'S3 Systematická';
$password = 'demokracie';
$prompts = [
0 => 'Ahoj, ja jsem los Bjarte, ale ostatni mi rikaji ParanoioS...',
1 => 'Kdo jste vy?',
2 => 'Ahoj tyme %s! Vitam te u ulohy '. $code . '... dostal jsem za ukol ti sdelit heslo, ale nekdo by nas mohl odposlouchavat, prejdeme radeji na nejaky kod. Napadl me jeden, ktery zna kazdy namornik.',
3 => 'Super, vidim ze uz pises v morseovce, ale pro jistotu mi napis slovo otaznik.',
4 => 'Hura, mame ustanovenou komunikaci, aby rec nestala vis ze ma druha prezdivka je ABCDOS? Kdyz jsem byl mensi strasne rad jsem vytvarel sve alternativy vseho ktere byly systematicky preskladane podle abecedy. No kazdopadne mam pocit ze nas odposlouchavaji, pojdme si vytvorit novy kod. Napis mi v nem slovo hruska.',
5 => 'Sladka hruska, uplne jsem na ni dostal chut. Mimochodem, vedeli jste ze morseovka odpovida frekvenci vyskytu pismen v anglictine, aby zpravy byly co nejkratsi. Cool tabulka, mrkej na algoritmy tecka net, jen tak premyslim ze v cestine by se asi musela secist cetnost znaku s diakritikou dohromady hmm a ch vlastne zapocitam dvakrat. Vis co, pouzijme novy kod, stejne nas uz zase odposlouchavaji, hned jak napises slovo svihak, budu vedet ze jsme zase na jedne lodi.',
6 => 'Trochu ti to zabralo, ze? Ja se mezitim nudil, takze jsem si naprogramoval vlastni rozlozeni klavesnice, ale musim rict to vychozi ceske je stejne nejlepsi, miluji Q, takze sorry za jeste jednu zmenu kodovani, tentokrat napis paranoik.',
7 => 'No moc jsme si nepopovidali, ale uz se na me vali dalsi tymy, tedy heslo teto ulohy je ' . $password,
8 => 'Hele, stejne nas odposlouchavaji, radsi to tu smazme.'
];
$wrongs = [
'unknown_team' => ['Neznam tym takoveho jmena', 'To nezni moc jako Interlosi tym.', 'Divny nazev tymu, jste vlastne vubec zaregistrovani?'],
'no_morse' => 'To neni ono, pojdme zacit pouzivat ten znamy kod, tenhle musis znat.',
'misunderstanding' => 'Tomu jsem nerozumel.',
'no_password' => 'Tohle neni to spravne slovo. Zkus to znovu.',
];
$teams = explode("\n", file_get_contents('teams.csv'));
$teamsLowered = array_map('mb_strtolower', $teams);
$teamsLoweredStripped = array_map('stripMessage', $teamsLowered);
$guards = [
1 => function ($plaintext) use ($teamsLoweredStripped, $teamsLowered, $teams, $prompts, $wrongs) {
if (in_array(trim(mb_strtolower($plaintext)), $teamsLowered, true) || in_array(stripMessage(trim(mb_strtolower($plaintext))), $teamsLoweredStripped, true)) {
return [true, false, stripMessage(sprintf($prompts[2], stripMessage($plaintext)))];
}
return [false, false, stripMessage($wrongs['unknown_team'][array_rand($wrongs['unknown_team'], 1)])];
},
2 => function ($plaintext, $original) use (&$currentAlphabet, $wrongs, $prompts) {
if (!isInMorse($original)) {
return [false, false, stripMessage($wrongs['no_morse'])];
}
return [true, true, stripMessage($prompts[3])];
},
3 => function ($plaintext, $original) use (&$currentAlphabet, $wrongs, $prompts) {
if (str_replace(' ', '', $plaintext) !== 'otaznik') {
return [false, false, stripMessage($wrongs['no_password'])];
}
return [true, true, stripMessage($prompts[4])];
},
4 => function ($plaintext, $original) use (&$currentAlphabet, $wrongs, $prompts) {
if (str_replace(' ', '', $plaintext) !== 'hruska') {
return [false, false, stripMessage($wrongs['no_password'])];
}
return [true, true, stripMessage($prompts[5])];
},
5 => function ($plaintext, $original) use (&$currentAlphabet, $wrongs, $prompts) {
if (str_replace(' ', '', $plaintext) !== 'svihak') {
return [false, false, stripMessage($wrongs['no_password'])];
}
return [true, true, stripMessage($prompts[6])];
},
6 => function ($plaintext, $original) use ($wrongs, $prompts) {
if (str_replace(' ', '', $plaintext) !== 'paranoik') {
return [false, false, stripMessage($wrongs['no_password'])];
}
return [true, false, stripMessage($prompts[7])];
},
7 => function ($plaintext, $original) use ($wrongs, $prompts) {
return [true, false, stripMessage($prompts[8])];
}
];
// Application
$requestFactory = new \Nette\Http\RequestFactory();
$response = new \Nette\Http\Response();
$session = new \Nette\Http\Session($requestFactory->createHttpRequest(), $response);
$section = $session->getSection('messages');
$session->start();
$messages = $section->offsetGet('messages');
$currentAlphabet = $section->offsetGet('currentAlphabet');
$currentLevel = $section->offsetGet('currentLevel');
$currentAlphabet = !$currentAlphabet ? 0 : $currentAlphabet;
$currentLevel = !$currentLevel ? 1 : $currentLevel;
if (!$messages) {
$messages[] = [date('j. m. Y H:i:s'), 'Bjarte', stripMessage($prompts[0])];
$messages[] = [date('j. m. Y H:i:s'), 'Bjarte', stripMessage($prompts[1])];
}
$form = new \Nette\Forms\Form();
$form->addText('message', 'Zpráva:', 100)
->setAttribute('autofocus', 'true');
$form->addSubmit('send', 'Odeslat');
$redirect = false;
$form->onSuccess[] = function (\Nette\Forms\Form $form, $values) use (
$alphabets,
&$currentAlphabet,
&$currentLevel,
$guards,
&$messages,
&$redirect
) {
// Always add user input
$messages[] = [date('j. m. Y H:i:s'), 'Vy', $values->message];
// Add evaluation
if (isset($guards[$currentLevel])) {
$sanitized = $currentAlphabet === 0 ? stripMessage($values->message) : fromAlphabet(stripMessageMorse($values->message), $currentAlphabet, $alphabets);
list($status, $levelUp, $message) = $guards[$currentLevel]($sanitized, $values->message);
if ($currentLevel === 2 && $levelUp) {
$finalMessage = toAlphabet($message, 1, $alphabets);
} else {
$finalMessage = $currentAlphabet === 0 ? $message : toAlphabet($message, $currentAlphabet, $alphabets);
}
if ($status) {
$currentLevel++;
}
if ($levelUp) {
$currentAlphabet++;
}
$messages[] = [date('j. m. Y H:i:s'), 'Bjarte', $finalMessage];
}
$redirect = true;
};
$form->fireEvents();
// Storing output
$section->offsetSet('messages', $messages);
$section->offsetSet('currentLevel', $currentLevel);
$section->offsetSet('currentAlphabet', $currentAlphabet);
if ($redirect) {
header('Location: index.php');
exit;
}
// Output
echo <<<EOT
<!DOCTYPE html>
<html lang="cs">
<head>
<meta charset="utf-8">
<title>$title</title>
<style>body, input { font-family: "Trebuchet MS"; font-size: 1.2em; } form { margin-top: 1cm; }</style>
</head>
<body>
EOT;
foreach ($messages as list($time, $who, $message)) {
echo $time . ' [' . $who . ']: ' . $message . "<br>\n";
}
echo $form;
echo <<<EOT
</body>
</html>
EOT;
if (isset($_GET['reset']) || $currentLevel === 8) {
session_destroy();
}
/// functions
function stripMessage($input)
{
$input = iconv('UTF-8', 'ASCII//TRANSLIT', $input);
return mb_strtolower(preg_replace('#[^a-zA-Z ]#', '', $input));
}
function stripMessageMorse($input)
{
$input = iconv('UTF-8', 'ASCII//TRANSLIT', $input);
$input = str_replace('|', ' ', $input);
return mb_strtolower(preg_replace('#[^\-\. ]#', '', $input));
}
function isInMorse($input)
{
return (bool) preg_match('#^[\.\- ]+$#', $input);
}
function toAlphabet($input, $currentAlphabet, $alphabets) {
return array_reduce(str_split($input), function($carry, $item) use (
$currentAlphabet,
$alphabets)
{
$table = $alphabets[$currentAlphabet];
// Get the correspondent value for the given letter
$morse = isset($table[$item]) ? $table[$item] . ' ' : ' ';
// Return the string with appended morse character
return $carry . $morse;
});
};
function fromAlphabet($input, $currentAlphabet, $alphabets) {
return array_reduce(explode(' ', $input), function($carry, $item) use (
$currentAlphabet,
$alphabets)
{
$table = array_flip($alphabets[$currentAlphabet]);
// Get the correspondent value for the given letter
$morse = $table[$item] ?? ' ';
// Return the string with appended morse character
return $carry . $morse;
});
};