-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunky.php
635 lines (563 loc) · 18.5 KB
/
funky.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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
<?php
function clean($data)
{
$data = htmlspecialchars($data);
$data = strip_tags($data);
$data = stripslashes($data);
$data = trim($data);
return $data;
}
function sanitizeText($text)
{
// Convert special characters to HTML entities
$text = htmlspecialchars($text, ENT_QUOTES | ENT_HTML5, "UTF-8");
// Strip out any remaining tags that may have gotten through
$text = strip_tags($text);
// Return the sanitized text
return $text;
}
function stripNumbers($input)
{
return preg_replace("/[^0-9]/", "", json_encode($input));
}
function platformSlashes($path)
{
return str_replace("/", DIRECTORY_SEPARATOR, $path);
}
function genToken()
{
return md5(rand());
}
function now()
{
return date("d-m-Y h:i:s");
}
function jd($text)
{
return json_decode($text, true);
}
function je($text)
{
return json_encode($text, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}
function bbcodeLink($text)
{
// Convert BBCode URLs and stuff to HTML links
$text = preg_replace('/\[url\](.*?)\[\/url\]/is', '<a href="$1" target="_blank" class="link">$1</a>', $text);
$text = preg_replace('/\[i\](.*?)\[\/i\]/is', '<em>$1</em>', $text);
$text = preg_replace('/\[spoiler\](.*?)\[\/spoiler\]/is', '<span class="spoiler">$1</span>', $text);
$text = preg_replace('/\[b\](.*?)\[\/b\]/is', '<strong>$1</strong>', $text);
$text = preg_replace('/\[s\](.*?)\[\/s\]/is', '<del>$1</del>', $text);
// Return the converted text
return $text;
}
function doLog($action, bool $success, $value = null, $user = null)
{
require "config.php";
if ($config["logs"]) {
require_once platformSlashes(__DIR__ . "/library/SleekDB/Store.php");
$db = new \SleekDB\Store("logs", platformSlashes($config["db"]["path"]), $config["db"]["config"]); // Logs
if (empty($action)) return false;
if (!empty($user) && !is_numeric($user)) return false;
$data = array(
"action" => clean($action),
"success" => $success,
"value" => clean($value),
"user" => $user,
"ip" => clean($_SERVER["REMOTE_ADDR"]),
"timestamp" => now()
);
$db->insert($data);
}
return true;
}
function logTags(int $post, string $rating, string $before, string $after, string $source, string $title, int $user, string $username)
{
require "config.php";
require_once platformSlashes(__DIR__ . "/library/SleekDB/Store.php");
$db = new \SleekDB\Store("tagLogs", platformSlashes($config["db"]["path"]), $config["db"]["config"]); // Tag-Logs
if (empty($post) || !is_numeric($post)) return false;
if (empty($after)) return false;
if (empty($rating)) return false;
if (!empty($user) && !is_numeric($user)) return false;
$data = array(
"post" => stripNumbers($post),
"rating" => clean(strtolower($rating)),
"before" => clean(strtolower($before)),
"after" => clean(strtolower($after)),
"source" => clean($source),
"title" => clean($title),
"user" => $user,
"username" => clean($username),
"ip" => clean($_SERVER["REMOTE_ADDR"]),
"timestamp" => now()
);
$db->insert($data);
return true;
}
function logTerm(int $term, string $before, string $after, int $user, string $username)
{
require "config.php";
require_once platformSlashes(__DIR__ . "/library/SleekDB/Store.php");
$db = new \SleekDB\Store("wikiLogs", platformSlashes($config["db"]["path"]), $config["db"]["config"]); // Tag-Logs
if (empty($term) || !is_numeric($term)) return false;
if (empty($after)) return false;
if (!empty($user) && !is_numeric($user)) return false;
$data = array(
"term" => stripNumbers($term),
"before" => clean($before),
"after" => clean($after),
"user" => $user,
"username" => clean($username),
"ip" => clean($_SERVER["REMOTE_ADDR"]),
"timestamp" => now()
);
$db->insert($data);
return true;
}
function cutTextAtCharacter($inputString, $character, $cutBefore = true)
{
// Find the position of the character in the input string
$position = strpos($inputString, $character);
// Check if the character was found
if ($position !== false) {
if ($cutBefore) {
// If cutBefore is true, cut off text before and including the character
$result = substr($inputString, $position + 1);
} else {
// If cutBefore is false, cut off text after the character
$result = substr($inputString, 0, $position);
}
return $result;
} else {
// If the character is not found, return the original string
return $inputString;
}
}
function visit()
{
require "config.php";
require_once platformSlashes(__DIR__ . "/library/SleekDB/Store.php");
$db = new \SleekDB\Store("visitLogs", platformSlashes($config["db"]["path"]), $config["db"]["config"]); // Besucher-Logs
if (empty($db->findOneBy(["ip", "==", clean($_SERVER["REMOTE_ADDR"])]))) {
$data = array(
"ip" => clean($_SERVER["REMOTE_ADDR"]),
"timestamp" => now()
);
$db->insert($data);
}
}
function hCaptcha($response)
{
require "config.php";
$data = array(
"secret" => $config["captcha"]["hcaptcha"]["secret"],
"response" => $response
);
$verify = curl_init();
curl_setopt($verify, CURLOPT_URL, "https://hcaptcha.com/siteverify");
curl_setopt($verify, CURLOPT_POST, true);
curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
$responseData = json_decode(curl_exec($verify));
return $responseData->success ? true : false;
}
function formatBytes($size, $precision = 2)
{
$base = log($size, 1024);
$suffixes = array("B", "KB", "MB", "GB", "TB");
return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
}
function genUuid()
{
return sprintf(
"%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0x0C2f) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0x2Aff),
mt_rand(0, 0xffD3),
mt_rand(0, 0xff4B)
);
}
function startsWith($string, $startString)
{
$len = strlen($startString);
return (substr($string, 0, $len) == $startString);
}
function toStringWithSpaces(array $array): string
{
if (empty($array)) {
return "";
}
$items = array_map(function ($item) {
return substr(strtolower(trim(clean($item))), 0, 75);
}, $array);
return implode(" ", $items);
}
function toArrayFromSpaces(string $string): array
{
$trimmedString = trim(clean($string));
if (empty($trimmedString)) {
return [];
}
$items = explode(" ", $trimmedString);
$result = [];
foreach ($items as $item) {
$result[] = substr(strtolower(clean(trim($item))), 0, 75);
}
return $result;
}
function getTagAmount($tags)
{
if (!is_array($tags)) {
$tags = toArrayFromSpaces($tags);
}
return count($tags);
}
function parseTag($tag)
{
$type = "tag";
$prefixes = ["artist:", "a:", "character:", "char:", "c:", "meta:", "m:", "copyright:", "copy:", "cp:", "tag:", "t:"];
foreach ($prefixes as $prefix) {
if (startsWith($tag, $prefix)) {
$tag = substr($tag, strlen($prefix));
switch ($prefix) {
case "artist:":
case "a:":
$type = "artist";
break;
case "character:":
case "char:":
case "c:":
$type = "character";
break;
case "meta:":
case "m:":
$type = "meta";
break;
case "copyright:":
case "copy:":
case "cp:":
$type = "copyright";
break;
default:
$type = "tag";
}
break;
}
}
return ["type" => $type, "tag" => $tag, "full" => $type . ":" . $tag];
}
function parseTag2($tag)
{
require "config.php";
require_once platformSlashes(__DIR__ . "/library/SleekDB/Store.php");
$db["tags"] = new \SleekDB\Store("tags", platformSlashes($config["db"]["path"]), $config["db"]["config"]); // Tags
$searchables = ["copyright:{$tag}", "character:{$tag}", "artist:{$tag}", "meta:{$tag}", "tag:{$tag}"];
foreach ($searchables as $sa) {
$search = $db["tags"]->findOneBy(["full", "==", $sa]);
if (!empty($search)) {
$tag = clean($sa);
break;
}
}
if (empty($search)) {
$tag = "tag:{$tag}";
}
$type = "tag";
$prefixes = ["artist:", "a:", "character:", "char:", "c:", "meta:", "m:", "copyright:", "copy:", "cp:", "tag:", "t:"];
foreach ($prefixes as $prefix) {
if (startsWith($tag, $prefix)) {
$tag = substr($tag, strlen($prefix));
switch ($prefix) {
case "artist:":
case "a:":
$type = "artist";
break;
case "character:":
case "char:":
case "c:":
$type = "character";
break;
case "meta:":
case "m:":
$type = "meta";
break;
case "copyright:":
case "copy:":
case "cp:":
$type = "copyright";
break;
default:
$type = "tag";
}
break;
}
}
return ["type" => $type, "tag" => $tag, "full" => $type . ":" . $tag];
}
function ps($path)
{
return str_replace("/", DIRECTORY_SEPARATOR, $path);
}
function getOrder($type)
{
$orderMap = [
"copyright" => 1,
"character" => 2,
"artist" => 3,
"tag" => 4
];
return $orderMap[$type] ?? 5;
}
function processTags(int $postId, $tags)
{
if (!is_array($tags)) $tags = toArrayFromSpaces($tags);
require "config.php";
require_once platformSlashes(__DIR__ . "/library/SleekDB/Store.php");
$db["tags"] = new \SleekDB\Store("tags", platformSlashes($config["db"]["path"]), $config["db"]["config"]); // Tags
$db["tagRelations"] = new \SleekDB\Store("tagRelations", platformSlashes($config["db"]["path"]), $config["db"]["config"]); // Tags-Verknüpfungen
// $post = $db["tags"]->findById($postId);
// if (empty($post)) return "Post does not exist";
$finalTags = array();
$relations = $db["tagRelations"]->findBy(["post", "==", $postId]);
$rels = array();
foreach ($relations as $rel) {
array_push($rels, $rel["full"]);
}
foreach ($tags as $_tag) {
$tag = parseTag($_tag);
$order = getOrder($tag["type"]);
$exTag = $db["tags"]->findOneBy([["name", "==", $tag["tag"]], "AND", ["type", "==", $tag["type"]]]);
$data = [
"name" => clean($tag["tag"]),
"type" => $tag["type"],
"full" => clean($tag["full"]),
"order" => $order,
"modified" => now(),
"timestamp" => now()
];
if (empty($exTag)) {
$db["tags"]->insert($data);
$exTag = $db["tags"]->findOneBy([["name", "==", $tag["tag"]], "AND", ["type", "==", $tag["type"]]]);
}
$data = [
"tag" => $exTag["_id"],
"name" => clean($tag["tag"]),
"type" => $tag["type"],
"full" => clean($tag["full"]),
"order" => $order,
"post" => $postId,
"timestamp" => now()
];
$relation = array();
$relation = $db["tagRelations"]->findOneBy([["post", "==", $postId], "AND", ["full", "==", $tag["full"]]]);
if (empty($relation))
$db["tagRelations"]->insert($data);
array_push($finalTags, $tag["full"]);
}
foreach ($rels as $relation) {
if (!in_array($relation, $finalTags))
$db["tagRelations"]->deleteBy(["full", "==", clean($relation)]);
}
return toStringWithSpaces($finalTags);
}
function altProcessTags(int $postId, $tags)
{
if (!is_array($tags)) $tags = toArrayFromSpaces($tags);
require "config.php";
require_once platformSlashes(__DIR__ . "/library/SleekDB/Store.php");
$db["tags"] = new \SleekDB\Store("tags", platformSlashes($config["db"]["path"]), $config["db"]["config"]); // Tags
$db["tagRelations"] = new \SleekDB\Store("tagRelations", platformSlashes($config["db"]["path"]), $config["db"]["config"]); // Tags-Verknüpfungen
// $post = $db["tags"]->findById($postId);
// if (empty($post)) return "Post does not exist";
$finalTags = array();
$relations = $db["tagRelations"]->findBy(["post", "==", $postId]);
$rels = array();
foreach ($relations as $rel) {
array_push($rels, $rel["name"]);
}
foreach ($tags as $_tag) {
$tag = parseTag2($_tag);
$order = getOrder($tag["type"]);
$exTag = $db["tags"]->findOneBy([["name", "==", $tag["tag"]], "AND", ["type", "==", $tag["type"]]]);
$data = [
"name" => clean($tag["tag"]),
"type" => $tag["type"],
"full" => clean($tag["full"]),
"order" => $order,
"modified" => now(),
"timestamp" => now()
];
if (empty($exTag)) {
$db["tags"]->insert($data);
$exTag = $db["tags"]->findOneBy([["name", "==", $tag["tag"]], "AND", ["type", "==", $tag["type"]]]);
}
$data = [
"tag" => $exTag["_id"],
"name" => clean($tag["tag"]),
"type" => $tag["type"],
"full" => clean($tag["full"]),
"order" => $order,
"post" => $postId,
"timestamp" => now()
];
$relation = array();
$relation = $db["tagRelations"]->findOneBy([["post", "==", $postId], "AND", ["name", "==", $tag["tag"]]]);
if (empty($relation))
$db["tagRelations"]->insert($data);
array_push($finalTags, $tag["full"]);
}
foreach ($rels as $relation) {
if (!in_array($relation, $finalTags))
$db["tagRelations"]->deleteBy(["name", "==", clean($relation)]);
}
return toStringWithSpaces($finalTags);
}
function isLandsape($file)
{
list($width, $height) = getimagesize($file);
return $width > $height ? "landscape" : "portrait";
}
function encryptString($message, $key)
{
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
$salt = random_bytes(SODIUM_CRYPTO_PWHASH_SALTBYTES);
$key = sodium_crypto_pwhash(
SODIUM_CRYPTO_SECRETBOX_KEYBYTES,
$key,
$salt,
SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE,
SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE
);
$ciphertext = sodium_crypto_secretbox($message, $nonce, $key);
return base64_encode($salt . $nonce . $ciphertext);
}
function decryptString($ciphertext, $key)
{
$decoded = base64_decode($ciphertext);
$salt = substr($decoded, 0, SODIUM_CRYPTO_PWHASH_SALTBYTES);
$nonce = substr($decoded, SODIUM_CRYPTO_PWHASH_SALTBYTES, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
$ciphertext = substr($decoded, SODIUM_CRYPTO_PWHASH_SALTBYTES + SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
$key = sodium_crypto_pwhash(
SODIUM_CRYPTO_SECRETBOX_KEYBYTES,
$key,
$salt,
SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE,
SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE
);
$plaintext = sodium_crypto_secretbox_open($ciphertext, $nonce, $key);
if ($plaintext === false) {
throw new Exception("Unable to decrypt message");
}
return $plaintext;
}
function removeDuplicatesByValue($array, $value)
{
$count = array_count_values(array_column($array, $value));
$result = array_filter($array, function ($item) use ($count, $value) {
return $count[$item[$value]] === 1;
});
return array_values($result);
}
function removeDuplicates($tags)
{
$nameCounts = array();
foreach ($tags as $index => $subArray) {
foreach ($subArray as $key => $value) {
$name = $value["name"];
if (isset($nameCounts[$name])) {
unset($tags[$index][$key]);
} else {
$nameCounts[$name] = 1;
}
}
}
return $tags;
}
function removeDuplicateByName($arr, $value)
{
if (empty($arr) || !is_array($arr)) return $arr;
$uniqueValues = array();
foreach ($arr as $key => $item) {
if (!in_array($item[$value], $uniqueValues)) {
$uniqueValues[] = $item[$value];
} else {
unset($arr[$key]);
}
}
return $arr;
}
function callAPI($url)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return "cURL Error #:" . $err;
} else {
return $response;
}
}
function xmlToArray($xml)
{
$array = array();
foreach ($xml->children() as $element) {
$tag = $element->getName();
if (!$element->children()) {
$array[$tag] = trim((string) $element);
} else {
$array[$tag][] = xmlToArray($element);
}
}
return $array;
}
function getFileExtension($filename)
{
return pathinfo($filename, PATHINFO_EXTENSION);
}
function getFileType($filename)
{
$path_parts = pathinfo($filename);
$extension = $path_parts["extension"];
$type = mime_content_type($filename);
return array(
"extension" => $extension,
"type" => $type
);
}
function containsXSS($string)
{
// Define XSS tags
$xss_tags = array('<script>', '</script>', '<embed>', '<iframe>', '<object>', '<applet>');
// Loop through XSS tags
foreach ($xss_tags as $tag) {
// Check if tag is found in string
if (stripos($string, $tag) !== false) {
return true;
}
}
// Check for onclick or onload events
if (preg_match('/(?:onclick|onload)[^>]\s=\s*(?:"[^"]"|\'[^\']\')/i', $string)) {
return true;
}
// Check for style/onmouseover tags
if (preg_match('/<[\w:]+[^>]style="([^"])("|\'|>)/i', $string)) {
return true;
}
// No XSS tags found
return false;
}