<?php
// Utility functions



// Check if request was to specified file
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
}

// Generate secure random string
function Utils_GenerateRandomString (int $length, string $keyspace = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"): string {
	if ($length < 1)
		die("cant generate random string of size less than 1");
	$pieces = [];
	$max = mb_strlen($keyspace, "8bit") - 1;
	for ($i = 0; $i < $length; ++$i)
		$pieces []= $keyspace[random_int(0, $max)];
	return implode("", $pieces);
}

// Get ratio from two values
function Utils_GetRatio ($x, $y) {
	if ($x === $y)
		return 1;
	return max($x, $y) / min($x, $y);
}

// Join two or more paths pieces to single
function Utils_JoinPaths () {
	$paths = array();
	foreach (func_get_args() as $arg) {
		if ($arg !== "")
			$paths[] = $arg;
	}
	return preg_replace('#/+#', '/', join('/', $paths));
}

// Check if string is valid ASCII
function Utils_IsAscii (string $str): bool {
	return (bool)!preg_match("/[\\x80-\\xff]+/", $str);
}



?>