-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinclude.php
343 lines (285 loc) · 12.6 KB
/
include.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
<?php
require_once "define.php";
$isError = array(
0 => FALSE, // Tip
1 => FALSE, // Warning
2 => FALSE, // Fatal
3 => FALSE, // MoreInfo
);
$errorOutput = array(
0 => "", // Tip
1 => "", // Warn
2 => "", // Fatal
3 => "", // MoreInfo
);
//DEFINE ERRORS
define("ERROR_FATAL", "Chyba:", TRUE);
define("ERROR_WARNING", "Upozornění:", TRUE);
define("ERROR_TIP", "Tip:", TRUE);
define("ERROR_MORE_ERROR_NEAR", "Chyba je poblíž: ", TRUE);
define("ERROR_INPUT_SIZE_EXCEEDED", "Překročili jste maximální velikost vstupu ", TRUE);
define("ERROR_INPUT_EMPTY", "Nezapomněli jste na něco?", TRUE);
define("ERROR_UNHANDLED", "Please remember what you were doing and contact administrator<br />To continue reload page or press F5.", TRUE);
define("ERROR_MORSE_T2M_INPUT", "Vstup je pro morseovu abecedu neplatný.", TRUE);
define("ERROR_MORSE_M2T_UNRECOGNIZED", "Zadali jste alespoň jedno neplatné písmeno.", TRUE);
define("ERROR_MORSE_TIP_LIST", "Máte problémy s Morseovou abecedou? Podívejte se na <a href=\"help.php?type=mo\" onclick=\"return popup('help.php?type=mo')\">seznam znaků</a>!</span>", TRUE);
define("ERROR_BINARY_UNRECOGNIZED", "Neplatný vstup.", TRUE);
define("ERROR_BINARY_TIP_LIST", "Máte problémy se vstupem? Podívejte se na <a href=\"help.php?type=bi\" onclick=\"return popup('help.php?type=bi')\">seznam znaků</a>!</span>", TRUE);
function checkEverything() {
//Check Config
global $config;
if (
(!isset($config)) ||
(!is_array($config)) ||
(!array_key_exists('input_length', $config)) ||
(!array_key_exists('handle_ch', $config))
) {
doError(2, "Config broken. Shutting down.", "Config does not exist?");
}
if (!is_bool($config['handle_ch'])) {
doError(2, "Config broken. Shutting down.", "Bad input for 'handle_ch' option.");
}
if (!is_int($config['input_length'])) {
doError(2, "Config broken. Shutting down.", "Bad input for 'input_length' option.");
}
if (!is_bool($config['show_gen_time'])) {
doError(2, "Config broken. Shutting down.", "Bad input for 'show_gen_time' option.");
}
//Check inputs
if (isset($_POST['type'])) {
if (($_POST['type'] != "mo") && ($_POST['type'] != "bi"))
doError(2, "Invalid conversion type. Are you injecting via POST?");
}
}
function checkInputLength($input) {
global $config;
if ($config['input_length'] != "") {
//in utf8 "č" is 2 bytes long, but it counts as one
//if we use latin1, it is converted - so it counts as two
if (mb_strlen($input, 'latin1') > $config['input_length']) {
$lengthDiff = ((mb_strlen($input, 'latin1')) - $config['input_length']);
if ($lengthDiff < 1024) {
$lengthDiff .= " bytes";
} elseif (($lengthDiff > 1024) && ($lengthDiff < 1048576)) {
$lengthDiff = round($lengthDiff / 1024) . " kilobytes";
} else {
$lengthDiff = round($lengthDiff / 1024 / 1024) . " megabytes";
}
doError(ERROR_INPUT_SIZE_EXCEEDED . "o " . $lengthDiff, 2);
}
}
}
function diacriticFree($text) {
$array = Array('ä' => 'a', 'Ä' => 'A', 'á' => 'a', 'Á' => 'A', 'à' => 'a', 'À' => 'A', 'ã' => 'a', 'Ã' => 'A', 'â' => 'a', 'Â' => 'A', 'č' => 'c', 'Č' => 'C', 'ć' => 'c', 'Ć' => 'C', 'ď' => 'd', 'Ď' => 'D', 'ě' => 'e', 'Ě' => 'E', 'é' => 'e', 'É' => 'E', 'ë' => 'e', 'Ë' => 'E', 'è' => 'e', 'È' => 'E', 'ê' => 'e', 'Ê' => 'E', 'í' => 'i', 'Í' => 'I', 'ï' => 'i', 'Ï' => 'I', 'ì' => 'i', 'Ì' => 'I', 'î' => 'i', 'Î' => 'I', 'ľ' => 'l', 'Ľ' => 'L', 'ĺ' => 'l', 'Ĺ' => 'L', 'ń' => 'n', 'Ń' => 'N', 'ň' => 'n', 'Ň' => 'N', 'ñ' => 'n', 'Ñ' => 'N', 'ó' => 'o', 'Ó' => 'O', 'ö' => 'o', 'Ö' => 'O', 'ô' => 'o', 'Ô' => 'O', 'ò' => 'o', 'Ò' => 'O', 'õ' => 'o', 'Õ' => 'O', 'ő' => 'o', 'Ő' => 'O', 'ř' => 'r', 'Ř' => 'R', 'ŕ' => 'r', 'Ŕ' => 'R', 'š' => 's', 'Š' => 'S', 'ś' => 's', 'Ś' => 'S', 'ť' => 't', 'Ť' => 'T', 'ú' => 'u', 'Ú' => 'U', 'ů' => 'u', 'Ů' => 'U', 'ü' => 'u', 'Ü' => 'U', 'ù' => 'u', 'Ù' => 'U', 'ũ' => 'u', 'Ũ' => 'U', 'û' => 'u', 'Û' => 'U', 'ý' => 'y', 'Ý' => 'Y', 'ž' => 'z', 'Ž' => 'Z', 'ź' => 'z', 'Ź' => 'Z');
$return = strtr($text, $array);
return $return;
}
function isError($type) {
global $isError;
if ($isError[$type] == TRUE)
return TRUE;
else
return FALSE;
}
//Third argument is optional
function doError($type, $text, $more=NULL) {
global $isError;
global $errorOutput;
//If we already have same $text, dont show, but if $more differs, show only $more. else return;
if ((isError($type) == TRUE) && (strpos($errorOutput[$type], $text) !== FALSE)) {
if (strpos($errorOutput[3], $more) === FALSE) {
$errorOutput[3] .= "<li><span class=\"bold\">" . ERROR_MORE_ERROR_NEAR . "</span>" . $more . "</li>";
$isError[3] = TRUE;
return;
}
else
return;
}
switch ($type) {
case 0:
$temp = ERROR_TIP;
break;
case 1:
$temp = ERROR_WARNING;
break;
case 2:
$temp = ERROR_FATAL;
break;
default:
die("Unhandled exception #doError-1." . ERROR_UNHANDLED);
break;
}
$isError[$type] = TRUE;
if ($more != NULL) {
$errorOutput[$type] .= "<li><span class=\"bold\">" . $temp . " </span>" . $text;
$errorOutput[$type] .= " <a class = \"\" href='#' onclick =\"changeVisibility('moreinfo')\">Více info</a>";
$errorOutput[$type] .= "</li>";
$errorOutput[3] .= "<li><span class=\"bold\">" . ERROR_MORE_ERROR_NEAR . "</span>" . $more . "</li>";
$isError[3] = TRUE;
} else {
$errorOutput[$type] .= "<li><span class=\"bold\">" . $temp . " </span>" . $text . "</li>";
}
}
function showError() {
global $errorOutput;
$return = "";
if ((isError(2) == TRUE) || (isError(1) == TRUE)) {
$return = "<div id=\"error\"><ul>";
if (isError(1) == TRUE)
$return .= $errorOutput[1];
if (isError(2) == TRUE)
$return .= $errorOutput[2];
$return .= "</ul></div>\n";
if (isError(3) == TRUE)
$return .= "<div id=\"moreinfo\" class=\"hidden\"><ul>" . $errorOutput[3] . "</ul><a href=\"#\" class=\"hide\" onclick=\"changeVisibility('moreinfo')\">(Skrýt)</a></div>\n";
}
if (isError(0) == TRUE)
$return .= "<div id=\"tip\"><ul>" . $errorOutput[0] . "</ul></div>\n";
return $return;
}
function morseCode($input, $encode) {
//text = input text ; encode==TRUE =>text2morse, FALSE=>morse2text
global $morse;
global $config;
$return = NULL;
$i = 0;
if ($encode == TRUE) {
//MORSECODE ENCODE
$input = diacriticFree($input);
$input = strtolower($input);
$input = preg_replace('!\s+!', ' ', $input); //multiple spaces into one space
if ($config['handle_ch'] == TRUE)
$getCh = strpos($input, "ch"); //do this before the string is splitted
$input = str_split($input);
//handling Ch character, skip if ch not found
if (($getCh !== FALSE) && ($config['handle_ch'] == TRUE)) {
for ($j = 0; $j < count($input) - 1; $j++) {
if (($input[$j] == "c") && ($input[$j + 1] == "h")) {
$input[$j] = "ch";
//delete H and re-index array
unset($input[$j + 1]);
$input = array_values($input);
}
}
}
unset($getCh);
foreach ($input as $temp) {
//spaces need to have two slashes, make them without spaces - str_replace hack else return character in morseCode
if (!array_key_exists($temp, $morse)) {
doError(1, ERROR_MORSE_T2M_INPUT, ($i + 1) . ". písmeno (Neznámý znak <span class=\"red\">" . $temp . "</span>)");
doError(0, ERROR_MORSE_TIP_LIST);
$return .= "<span class=\"red bold\" title = '" . ERROR_MORE_ERROR_NEAR . ($i + 1) . ". písmeno (Neznámý znak: " . $temp . ")'>*</span> / ";
} elseif ($temp == " ") {
$return .= "/ ";
} else {
$return.= $morse[$temp] . " / ";
}
$i++;
}
$return = str_replace("/ /", "//", $return); //formatting, easier than if next char is space do this....
} elseif ($encode == FALSE) {
//MORSECODE DECODE, input check is handled in showOutput()
if (strpos($input, "/") === FALSE)
$input = explode(" ", $input); // explode by space
else {
$input = str_replace(" ", "", $input); //get rid of all spaces
$input = explode("/", $input); //and explode by /
}
foreach ($input as $temp) {
if ((!in_array($temp, $morse))) {
doError(1, ERROR_MORSE_M2T_UNRECOGNIZED, "<span class=\"error\">" . ERROR_MORE_ERROR_NEAR . "</span>" . ($i + 1) . ". písmeno: (Neznámý vstup: <span class=\"red\">" . $temp . "</span>)");
doError(0, ERROR_MORSE_TIP_LIST, 0);
$return .= "<span class=\"red bold\" title = '" . ERROR_MORE_ERROR_NEAR . " " . ($i + 1) . ". pismeno (Neznámý vstup: " . $temp . "'>*</span>";
} else {
$return.= array_search($temp, $morse);
}
$i++;
}
} else {
die("Unhandled exception #MorseCode-1." . ERROR_UNHANDLED);
}
return $return;
}
function binaryCode($input, $encode) {
global $binary;
$return = NULL;
$i = 0;
if ($encode == TRUE) {
$input = diacriticFree($input);
$input = preg_replace('!\s+!', ' ', $input); //multiple spaces into one space
$input = str_split($input);
foreach ($input as $temp) {
if (!array_key_exists($temp, $binary)) {
//TODO: MOREINFO
doError(1, ERROR_BINARY_UNRECOGNIZED);
doError(0, ERROR_BINARY_TIP_LIST);
$return .= "<span class=\"red\">ERR</span> ";
} else {
$return.= $binary[$temp] . " ";
}
$i++;
}
} elseif ($encode == FALSE) {
//Binary DECODE, input check is handled in showOutput()
//delete spaces and explode every 8 chars
$input = str_replace(" ", "", $input);
$input2 = $input;
$input = array();
for ($j = 0; $j < strlen($input2); $j += 8)
$input[] = substr($input2, $j, 8);
foreach ($input as $temp) {
if ((!in_array($temp, $binary))) {
//TODO: MOREINFO
doError(0, ERROR_BINARY_UNRECOGNIZED);
doError(1, ERROR_BINARY_TIP_LIST);
$return .= "<span class=\"red\">ERR</span> ";
} else {
$return.= array_search($temp, $binary);
}
}
} else {
die("Unhandled #binaryCode-1" . ERROR_UNHANDLED);
}
return $return;
}
function showOutput($input, $type) {
$return = NULL;
global $config;
checkEverything();
checkInputLength($input);
if ($input == NULL)
doError(2, ERROR_INPUT_EMPTY);
//Don't bother doing anything if already fatal error
if (isError(2) == TRUE)
return;
//Trim whitespaces at start/end and newlines
$input = trim($input);
$input = str_replace(array("\r\n", "\r", "\n"), ' ', $input);
switch ($type) {
/*
* mo - text2morsecode
* bi - text2binary
*/
case "mo":
//check for the only chars available in MorseCode (.-/) + \r\n (newline)
if (preg_match('/^[\ \-\.\/\n\r]+$/i', $input)) {
$return = morseCode($input, FALSE);
} else {
$return = morseCode($input, TRUE);
}
break;
case "bi":
//check for the only chars available in binary (0, 1 and \s )
if (preg_match('/^[01\ ]+$/i', $input)) {
$return = binaryCode($input, FALSE);
} else {
$return = binaryCode($input, TRUE);
}
break;
default:
die("Unhandled exception #showOutput-1." . ERROR_UNHANDLED);
break;
}
return $return;
}
?>