2024-01-13 05:54:42 +05:30
|
|
|
<?php
|
|
|
|
// Utility functions
|
2023-08-19 23:45:47 +05:30
|
|
|
|
2023-11-01 00:27:17 +05:30
|
|
|
|
|
|
|
|
2023-08-19 23:45:47 +05:30
|
|
|
// Check if request was to specified file
|
2024-03-07 22:28:39 +05:30
|
|
|
function Utils_ThisFileIsRequested (string $fullpath): bool {
|
|
|
|
return (substr($fullpath, -strlen($_SERVER["SCRIPT_NAME"])) === $_SERVER["SCRIPT_NAME"])
|
|
|
|
|| ($fullpath === $_SERVER["SCRIPT_NAME"]); // Old variant won't work on some configurations, as reported by doesnm
|
2023-08-19 23:45:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Generate secure random string
|
2023-11-01 00:27:17 +05:30
|
|
|
function Utils_GenerateRandomString (int $length, string $keyspace = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"): string {
|
2024-03-07 22:28:39 +05:30
|
|
|
if ($length < 1)
|
2023-08-19 23:45:47 +05:30
|
|
|
die("cant generate random string of size less than 1");
|
|
|
|
$pieces = [];
|
|
|
|
$max = mb_strlen($keyspace, "8bit") - 1;
|
2024-03-07 22:28:39 +05:30
|
|
|
for ($i = 0; $i < $length; ++$i)
|
2023-08-19 23:45:47 +05:30
|
|
|
$pieces []= $keyspace[random_int(0, $max)];
|
2024-01-20 22:35:45 +05:30
|
|
|
return implode("", $pieces);
|
2023-08-19 23:45:47 +05:30
|
|
|
}
|
|
|
|
|
2023-11-01 00:27:17 +05:30
|
|
|
// Get ratio from two values
|
|
|
|
function Utils_GetRatio ($x, $y) {
|
2023-09-08 01:35:23 +05:30
|
|
|
if ($x === $y)
|
|
|
|
return 1;
|
|
|
|
return max($x, $y) / min($x, $y);
|
|
|
|
}
|
|
|
|
|
2023-11-01 00:27:17 +05:30
|
|
|
// Join two or more paths pieces to single
|
|
|
|
function Utils_JoinPaths () {
|
|
|
|
$paths = array();
|
|
|
|
foreach (func_get_args() as $arg) {
|
2024-03-07 22:28:39 +05:30
|
|
|
if ($arg !== "")
|
|
|
|
$paths[] = $arg;
|
2023-11-01 00:27:17 +05:30
|
|
|
}
|
2024-02-09 02:43:23 +05:30
|
|
|
return preg_replace('#/+#', '/', join('/', $paths));
|
2023-11-01 00:27:17 +05:30
|
|
|
}
|
|
|
|
|
2024-01-20 22:35:45 +05:30
|
|
|
// Check if string is valid ASCII
|
|
|
|
function Utils_IsAscii (string $str): bool {
|
2024-02-09 02:43:23 +05:30
|
|
|
return (bool)!preg_match("/[\\x80-\\xff]+/", $str);
|
2024-01-20 22:35:45 +05:30
|
|
|
}
|
|
|
|
|
2023-11-01 00:27:17 +05:30
|
|
|
|
|
|
|
|
2023-08-19 23:45:47 +05:30
|
|
|
?>
|