-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
funky.php
398 lines (363 loc) · 11.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
<?php
function clean($data, $title = false)
{
// This function is used, to completely sanitize user-input and make any form of scripts harmless and displayable
$data = htmlspecialchars($data);
$data = strip_tags($data);
$data = stripslashes($data);
$data = trim($data);
$data = str_replace("'", "\'", $data);
if ($title)
$data = str_replace(".", "", $data);
return $data;
}
function cat($title)
{
// This function is used, to make all titles readable for the URL, Cookies and links
return preg_replace('/[^A-Za-z0-9\-_,.]/', '', str_replace("&", "et", str_replace(' ', '-', strtolower($title))));
}
function namba($data)
{
return preg_replace("/[^0-9.]/i", "", str_replace(",", ".", $data));
}
function now()
{
return date('Y-m-d 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 ps($path)
{
return str_replace('/', DIRECTORY_SEPARATOR, $path);
}
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 genToken()
{
return md5(rand());
}
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)];
return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
}
function doLog($action, bool $success, $value = null, $user = null)
{
require "config.php";
if ($config["logs"]) {
require_once ps(__DIR__ . $config["path"]["sleek"] . "/Store.php");
$db = new \SleekDB\Store("logs", ps(__DIR__ . $config["db"]["sleek"]["dir"]), $config["db"]["sleek"]["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 visit()
{
require "config.php";
require_once ps(__DIR__ . $config["path"]["sleek"] . "/Store.php");
$db = new \SleekDB\Store("visitLogs", ps(__DIR__ . $config["db"]["sleek"]["dir"]), $config["db"]["sleek"]["config"]); // Logs
if (empty($db->findOneBy(["ip", "=", clean($_SERVER["REMOTE_ADDR"])]))) {
$data = array(
"ip" => clean($_SERVER["REMOTE_ADDR"]),
"timestamp" => now()
);
$db->insert($data);
}
}
function titleVisit($title)
{
require "config.php";
require_once ps(__DIR__ . $config["path"]["sleek"] . "/Store.php");
$db = new \SleekDB\Store("titleViews", ps(__DIR__ . $config["db"]["sleek"]["dir"]), $config["db"]["sleek"]["config"]); // Logs
if (empty($db->findOneBy([["ip", "=", clean($_SERVER["REMOTE_ADDR"])], "AND", ["title.id", "=", $title["id"]]]))) {
$data = array(
"title" => $title,
"ip" => clean($_SERVER["REMOTE_ADDR"]),
"timestamp" => now()
);
$db->insert($data);
}
}
function chapterVisit($chapter)
{
require "config.php";
require_once ps(__DIR__ . $config["path"]["sleek"] . "/Store.php");
$db = new \SleekDB\Store("chapterViews", ps(__DIR__ . $config["db"]["sleek"]["dir"]), $config["db"]["sleek"]["config"]); // Logs
if (empty($db->findOneBy([["ip", "=", clean($_SERVER["REMOTE_ADDR"])], "AND", ["chapter.id", "=", $chapter["id"]]]))) {
$data = array(
"chapter" => $chapter,
"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 valCustom($file)
{
require "config.php";
require_once ps(__DIR__ . $config["path"]["sleek"] . "/Store.php");
$db = new \SleekDB\Store("custom_" . $file, ps(__DIR__ . $config["db"]["sleek"]["dir"]), $config["db"]["sleek"]["config"]);
$file = file_exists(ps(__DIR__ . "/custom/{$file}.json")) ? file_get_contents(ps(__DIR__ . "/custom/{$file}.json")) : "";
$data = jd($file);
$_data = $db->findAll();
if (!empty($data)) {
sort($data);
foreach ($data as $dat) {
$dta = array(
"name" => $dat,
"timestamp" => now()
);
if (empty($db->findOneBy(["name", "=", $dat]))) $db->insert($dta);
}
}
if (!empty($_data)) {
sort($_data);
foreach ($_data as $dat) {
if (!in_array($dat["name"], $data)) {
$db->deleteBy(["name", "=", $dat["name"]]);
}
}
return $_data;
} else {
return true;
}
}
function valCLang($file)
{
require "config.php";
require_once ps(__DIR__ . $config["path"]["sleek"] . "/Store.php");
$db = new \SleekDB\Store("custom_" . $file, ps(__DIR__ . $config["db"]["sleek"]["dir"]), $config["db"]["sleek"]["config"]);
$file = file_exists(ps(__DIR__ . "/custom/{$file}.json")) ? file_get_contents(ps(__DIR__ . "/custom/{$file}.json")) : "";
$data = jd($file);
$_data = $db->findAll();
if (!empty($_data)) {
sort($_data);
$langs = array_column($data, 0);
$langs = array_reverse($langs);
foreach ($_data as $dat) {
$found = false;
foreach ($langs as $l) {
if ($dat["code"] == $l) $found = true;
}
if (!$found) $db->deleteBy(["code", "=", $dat["code"]]);
}
}
if (!empty($data)) {
sort($data);
foreach ($data as $dat) {
$dta = array(
"code" => $dat[0],
"name" => $dat[1],
"flag" => $dat[2],
"timestamp" => now()
);
if (empty($db->findOneBy(["code", "=", $dat[0]]))) $db->insert($dta);
}
}
return !empty($_data) ? $_data : true;
}
function getTag($file, $id)
{
$tags = valCustom($file);
foreach ($tags as $tg) {
if ($tg["id"] == $id) return $tg;
}
return null;
}
function rmrf($dir)
{
foreach (glob($dir) as $file) {
if (is_dir($file)) {
rmrf("$file/*");
rmdir($file);
} else {
unlink($file);
}
}
}
function valImage($location, $file)
{
// Image?
$check = getimagesize($location);
if ($check == false) return "Fake image.";
// In array?
$filetypes = array("jpg", "jpeg", "png", "gif", "webp");
if (!in_array(strtolower($file), $filetypes)) return "Invalid Image format.";
// Yes!
return true;
}
function unzip($file, $tmp, $target)
{
$zip = new ZipArchive;
$res = $zip->open($file);
if ($res !== true) die("Something!");
// extract it to the path we determined above
if (file_exists($tmp)) rmrf($tmp);
if (file_exists($target)) rmrf($target);
mkdir($tmp, 0755, true);
mkdir($target, 0755, true);
$zip->extractTo($tmp);
$files = glob("$tmp/*.{jpg,jpeg,webp,gif,png}", GLOB_BRACE);
if (empty($files)) return "No images in ZIP!";
foreach ($files as $f) {
$check = getimagesize($f);
if ($check == false) {
return "An error occured.";
rmrf($tmp);
}
}
sort($files, SORT_STRING);
$c = 1;
foreach ($files as $_file) {
rename($_file, ps($target . "/{$c}." . strtolower(pathinfo($_file, PATHINFO_EXTENSION))));
$c++;
}
$zip->close();
rmrf($tmp);
unlink($file);
return "success";
}
function formatChapterTitle($volume, $chapter, $type = "short")
{
switch ($type) {
case "full":
$type = "full";
break;
case "short":
default:
$type = "short";
}
$out = "";
if (substr($chapter, -2) == ".00") {
$chapter = substr($chapter, 0, -3);
} elseif (substr($chapter, -1) == "0") {
$chapter = substr($chapter, 0, -1);
}
if ($type == "short") {
if (!empty($volume)) $out .= "Vol." . $volume . " ";
$out .= "Ch." . $chapter;
}
if ($type == "full") {
if (!empty($volume)) $out .= "Volume " . $volume . " ";
$out .= "Chapter " . $chapter;
}
return $out;
}
function formatDate($date, $full = false)
{
$date = clean($date);
$s = $date;
$date = strtotime($s);
if ($full == false) {
return date('d. M Y', $date);
} else {
return date('d. M Y H:m:i', $date);
}
}
function getUserLang($logged = false, $userlang = "")
{
require "config.php";
$userlang = $logged ? $userlang : cat($_COOKIE[cat($config["title"]) . "_lang"] ?? $config["default"]["lang"]);
if (!file_exists(ps(__DIR__ . $config["path"]["langs"] . "/{$userlang}.php")))
$userlang = $config["default"]["lang"];
return $userlang;
}
function getUserTheme($logged = false, $usertheme = "")
{
require "config.php";
$usertheme = $logged ? $usertheme : cat($_COOKIE[cat($config["title"]) . "_theme"] ?? $config["default"]["theme"]);
if (!file_exists(ps(__DIR__ . $config["smarty"]["template"] . "/{$usertheme}/info.php")))
$usertheme = $config["default"]["theme"];
return $usertheme;
}
function getPrefLang()
{
require "config.php";
return isset($_COOKIE[cat($config["title"]) . "_preflang"]) && !empty($_COOKIE[cat($config["title"]) . "_preflang"]) ? cat($_COOKIE[cat($config["title"]) . "_preflang"]) : $config["default"]["lang"];
}
function timeAgo($datetime, $full = false)
{
// Thx - https://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php-e-g-1-day-ago-2-days-ago
$now = new DateTime;
$ago = new DateTime($datetime);
$diff = $now->diff($ago);
$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;
$string = array(
'y' => 'year',
'm' => 'month',
'w' => 'week',
'd' => 'day',
'h' => 'hour',
'i' => 'min',
's' => 'second',
);
foreach ($string as $k => &$v) {
if ($diff->$k) {
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
} else {
unset($string[$k]);
}
}
if (!$full) $string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . ' ago' : 'just now';
}
// Only available in PHP 8.x which I don't like at all
if (!function_exists("str_contains")) {
function str_contains($haystack, $needle)
{
return $needle !== "" && mb_strpos($haystack, $needle) !== false;
}
}
function shorten($text, $maxlength = 25)
{
// This function is used, to display only a certain amount of characters
if (strlen($text) > $maxlength)
return substr($text, 0, $maxlength) . "...";
return $text;
}