2020-02-09 23:02:19 +01:00
|
|
|
<?php
|
|
|
|
|
2022-06-17 20:53:35 +02:00
|
|
|
/**
|
|
|
|
* This file contains functions used globally without a namespace and should not require
|
|
|
|
* any other files. It also loads the composer vendor libraries.
|
|
|
|
*/
|
|
|
|
|
2022-02-21 00:32:40 +01:00
|
|
|
$autoLoad = implode(DIRECTORY_SEPARATOR, [__DIR__, "External", "vendor", "autoload.php"]);
|
|
|
|
if (is_file($autoLoad)) {
|
|
|
|
require_once $autoLoad;
|
2022-02-20 23:17:17 +01:00
|
|
|
}
|
2021-12-08 16:53:43 +01:00
|
|
|
|
2024-05-16 17:07:22 +02:00
|
|
|
const WEBBASE_VERSION = "2.4.3";
|
2020-06-22 19:09:02 +02:00
|
|
|
|
2021-04-06 16:34:12 +02:00
|
|
|
spl_autoload_extensions(".php");
|
2022-06-14 10:30:35 +02:00
|
|
|
spl_autoload_register(function ($class) {
|
2021-12-08 16:53:43 +01:00
|
|
|
if (!class_exists($class)) {
|
2022-11-20 17:13:53 +01:00
|
|
|
$suffixes = ["", ".class", ".trait", ".interface"];
|
2022-02-20 16:53:26 +01:00
|
|
|
foreach ($suffixes as $suffix) {
|
|
|
|
$full_path = WEBROOT . "/" . getClassPath($class, $suffix);
|
|
|
|
if (file_exists($full_path)) {
|
|
|
|
include_once $full_path;
|
|
|
|
return;
|
|
|
|
}
|
2021-12-08 16:53:43 +01:00
|
|
|
}
|
2022-02-20 16:53:26 +01:00
|
|
|
|
|
|
|
throw new Exception("Class or Trait not found: $class");
|
2021-04-06 16:34:12 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-03 10:39:13 +02:00
|
|
|
function getProtocol(): string {
|
2021-04-07 00:03:14 +02:00
|
|
|
$isSecure = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ||
|
2022-06-14 10:30:35 +02:00
|
|
|
(!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') ||
|
|
|
|
(!empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on');
|
2021-04-07 00:03:14 +02:00
|
|
|
|
|
|
|
return $isSecure ? 'https' : 'http';
|
2020-06-22 19:09:02 +02:00
|
|
|
}
|
|
|
|
|
2024-04-09 14:59:19 +02:00
|
|
|
function getCurrentHostName(): string {
|
|
|
|
$hostname = $_SERVER["SERVER_NAME"] ?? null;
|
|
|
|
if (empty($hostname)) {
|
|
|
|
$hostname = $_SERVER["HTTP_HOST"] ?? null;
|
|
|
|
if (empty($hostname)) {
|
|
|
|
$hostname = gethostname();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $hostname;
|
|
|
|
}
|
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
function uuidv4(): string {
|
|
|
|
$data = random_bytes(16);
|
|
|
|
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
|
|
|
|
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
|
|
|
|
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
|
|
|
|
}
|
|
|
|
|
2023-01-18 14:37:34 +01:00
|
|
|
function generateRandomString(int $length, $type = "ascii"): string {
|
2020-06-22 19:09:02 +02:00
|
|
|
$randomString = '';
|
2021-11-11 14:25:26 +01:00
|
|
|
|
|
|
|
$lowercase = "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
$uppercase = strtoupper($lowercase);
|
2022-06-14 10:30:35 +02:00
|
|
|
$digits = "0123456789";
|
|
|
|
$hex = $digits . substr($lowercase, 0, 6);
|
|
|
|
$ascii = $uppercase . $lowercase . $digits;
|
2021-11-11 14:25:26 +01:00
|
|
|
|
2020-06-22 19:09:02 +02:00
|
|
|
if ($length > 0) {
|
2021-11-11 14:25:26 +01:00
|
|
|
$type = strtolower($type);
|
|
|
|
if ($type === "hex") {
|
|
|
|
$charset = $hex;
|
2021-12-08 16:53:43 +01:00
|
|
|
} else if ($type === "base64") {
|
|
|
|
$charset = $ascii . "/+";
|
2022-03-08 11:50:18 +01:00
|
|
|
} else if ($type === "base58") {
|
|
|
|
$charset = preg_replace("/[0Oo1Il]/", "", $ascii);
|
2022-02-20 16:53:26 +01:00
|
|
|
} else if ($type === "base32") {
|
|
|
|
$charset = $uppercase . substr($digits, 2, 6);
|
2021-11-11 14:25:26 +01:00
|
|
|
} else {
|
|
|
|
$charset = $ascii;
|
|
|
|
}
|
|
|
|
|
2022-02-20 16:53:26 +01:00
|
|
|
$numCharacters = $type === "raw" ? 256 : strlen($charset);
|
2020-06-22 19:09:02 +02:00
|
|
|
for ($i = 0; $i < $length; $i++) {
|
|
|
|
try {
|
|
|
|
$num = random_int(0, $numCharacters - 1);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$num = rand(0, $numCharacters - 1);
|
|
|
|
}
|
2020-06-20 15:49:53 +02:00
|
|
|
|
2022-02-20 16:53:26 +01:00
|
|
|
$randomString .= $type === "raw" ? chr($num) : $charset[$num];
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 19:09:02 +02:00
|
|
|
return $randomString;
|
|
|
|
}
|
2020-02-10 00:52:25 +01:00
|
|
|
|
2022-11-23 23:36:30 +01:00
|
|
|
function base64url_decode($data): bool|string {
|
2022-02-20 16:53:26 +01:00
|
|
|
$base64 = strtr($data, '-_', '+/');
|
|
|
|
return base64_decode($base64);
|
|
|
|
}
|
|
|
|
|
2021-04-03 11:07:56 +02:00
|
|
|
function startsWith($haystack, $needle, bool $ignoreCase = false): bool {
|
|
|
|
|
2020-06-22 19:09:02 +02:00
|
|
|
$length = strlen($needle);
|
2021-04-03 11:07:56 +02:00
|
|
|
if ($length === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($ignoreCase) {
|
|
|
|
$haystack = strtolower($haystack);
|
2021-04-03 12:05:32 +02:00
|
|
|
$needle = strtolower($needle);
|
2021-04-03 11:07:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PHP 8.0 support
|
|
|
|
if (function_exists("str_starts_with")) {
|
|
|
|
return str_starts_with($haystack, $needle);
|
|
|
|
} else {
|
|
|
|
return (substr($haystack, 0, $length) === $needle);
|
|
|
|
}
|
2020-06-22 19:09:02 +02:00
|
|
|
}
|
|
|
|
|
2022-03-08 11:50:18 +01:00
|
|
|
function startsWithAny($haystack, array $needles, bool $ignoreCase = false): bool {
|
|
|
|
foreach ($needles as $needle) {
|
|
|
|
if (startsWith($haystack, $needle, $ignoreCase)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-03 11:07:56 +02:00
|
|
|
function endsWith($haystack, $needle, bool $ignoreCase = false): bool {
|
|
|
|
|
2020-06-22 19:09:02 +02:00
|
|
|
$length = strlen($needle);
|
2021-04-03 11:07:56 +02:00
|
|
|
if ($length === 0) {
|
2020-06-22 19:09:02 +02:00
|
|
|
return true;
|
2021-04-03 11:07:56 +02:00
|
|
|
}
|
2020-06-22 19:09:02 +02:00
|
|
|
|
2021-04-03 11:07:56 +02:00
|
|
|
if ($ignoreCase) {
|
|
|
|
$haystack = strtolower($haystack);
|
2021-04-03 12:05:32 +02:00
|
|
|
$needle = strtolower($needle);
|
2021-04-03 11:07:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PHP 8.0 support
|
|
|
|
if (function_exists("str_ends_with")) {
|
|
|
|
return str_ends_with($haystack, $needle);
|
|
|
|
} else {
|
|
|
|
return (substr($haystack, -$length) === $needle);
|
|
|
|
}
|
2020-06-22 19:09:02 +02:00
|
|
|
}
|
|
|
|
|
2022-02-20 16:53:26 +01:00
|
|
|
|
|
|
|
function contains($haystack, $needle, bool $ignoreCase = false): bool {
|
|
|
|
|
|
|
|
if (is_array($haystack)) {
|
|
|
|
return in_array($needle, $haystack);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($ignoreCase) {
|
|
|
|
$haystack = strtolower($haystack);
|
|
|
|
$needle = strtolower($needle);
|
|
|
|
}
|
|
|
|
|
|
|
|
// PHP 8.0 support
|
|
|
|
if (function_exists("str_contains")) {
|
|
|
|
return str_contains($haystack, $needle);
|
|
|
|
} else {
|
|
|
|
return strpos($haystack, $needle) !== false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 11:50:18 +01:00
|
|
|
function intendCode($code, $escape = true): string {
|
2020-06-22 19:09:02 +02:00
|
|
|
$newCode = "";
|
|
|
|
$first = true;
|
|
|
|
$brackets = array();
|
|
|
|
$intend = 0;
|
|
|
|
|
|
|
|
foreach (explode("\n", $code) as $line) {
|
|
|
|
if (!$first) $newCode .= "\n";
|
|
|
|
if ($escape) $line = htmlspecialchars($line);
|
|
|
|
$line = trim($line);
|
|
|
|
|
|
|
|
if (count($brackets) > 0 && startsWith($line, current($brackets))) {
|
|
|
|
$intend = max(0, $intend - 2);
|
|
|
|
array_pop($brackets);
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
|
|
|
|
2020-06-22 19:09:02 +02:00
|
|
|
$newCode .= str_repeat(" ", $intend);
|
|
|
|
$newCode .= $line;
|
|
|
|
$first = false;
|
2020-02-09 23:02:19 +01:00
|
|
|
|
2020-06-22 19:09:02 +02:00
|
|
|
if (endsWith($line, "{")) {
|
|
|
|
$intend += 2;
|
2022-03-08 11:50:18 +01:00
|
|
|
$brackets[] = "}";
|
2020-06-22 19:09:02 +02:00
|
|
|
} else if (endsWith($line, "(")) {
|
|
|
|
$intend += 2;
|
2022-03-08 11:50:18 +01:00
|
|
|
$brackets[] = ")";
|
2020-06-22 19:09:02 +02:00
|
|
|
}
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
|
|
|
|
2020-06-22 19:09:02 +02:00
|
|
|
return $newCode;
|
|
|
|
}
|
2020-02-09 23:02:19 +01:00
|
|
|
|
2021-12-08 16:53:43 +01:00
|
|
|
function html_attributes(array $attributes): string {
|
|
|
|
return implode(" ", array_map(function ($key) use ($attributes) {
|
2024-05-04 12:23:14 +02:00
|
|
|
$value = is_array($attributes[$key]) ? implode(" ", $attributes[$key]) : $attributes[$key];
|
|
|
|
$value = htmlspecialchars($value);
|
2021-12-08 16:53:43 +01:00
|
|
|
return "$key=\"$value\"";
|
|
|
|
}, array_keys($attributes)));
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:30:35 +02:00
|
|
|
function html_tag_short(string $tag, array $attributes = []): string {
|
|
|
|
return html_tag_ex($tag, $attributes, "", true, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
function html_tag(string $tag, array $attributes = [], $content = "", bool $escapeContent = true): string {
|
|
|
|
return html_tag_ex($tag, $attributes, $content, $escapeContent, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function html_tag_ex(string $tag, array $attributes, $content = "", bool $escapeContent = true, bool $short = false): string {
|
|
|
|
$attrs = html_attributes($attributes);
|
|
|
|
if (!empty($attrs)) {
|
|
|
|
$attrs = " " . $attrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($content)) {
|
|
|
|
$content = implode("", $content);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($escapeContent) {
|
|
|
|
$content = htmlspecialchars($content);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ($short && !empty($content)) ? "<$tag$attrs/>" : "<$tag$attrs>$content</$tag>";
|
|
|
|
}
|
|
|
|
|
2022-02-20 16:53:26 +01:00
|
|
|
function getClassPath($class, string $suffix = ".class"): string {
|
2020-06-22 19:09:02 +02:00
|
|
|
$path = str_replace('\\', '/', $class);
|
2022-11-18 18:06:46 +01:00
|
|
|
$pathParts = array_values(array_filter(explode("/", $path)));
|
|
|
|
$pathCount = count($pathParts);
|
|
|
|
|
|
|
|
if ($pathCount >= 3) {
|
|
|
|
if (strcasecmp($pathParts[$pathCount - 3], "API") === 0) {
|
|
|
|
$group = $pathParts[$pathCount - 2];
|
2023-01-05 22:47:17 +01:00
|
|
|
if (strcasecmp($group, "Parameter") !== 0 && strcasecmp($group, "Traits") !== 0) {
|
2022-11-18 18:06:46 +01:00
|
|
|
$pathParts = array_slice($pathParts, 0, $pathCount - 2);
|
2023-01-16 21:47:23 +01:00
|
|
|
$pathParts[] = "{$group}API";
|
2022-11-18 18:06:46 +01:00
|
|
|
}
|
|
|
|
}
|
2020-06-22 19:09:02 +02:00
|
|
|
}
|
2020-02-09 23:02:19 +01:00
|
|
|
|
2022-11-18 18:06:46 +01:00
|
|
|
$path = implode("/", $pathParts);
|
|
|
|
return "$path$suffix.php";
|
2020-06-22 19:09:02 +02:00
|
|
|
}
|
2020-02-09 23:02:19 +01:00
|
|
|
|
2022-05-31 16:14:49 +02:00
|
|
|
function getClassName($class, bool $short = true): string {
|
|
|
|
$reflection = new \ReflectionClass($class);
|
|
|
|
if ($short) {
|
|
|
|
return $reflection->getShortName();
|
|
|
|
} else {
|
|
|
|
return $reflection->getName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-23 20:14:32 +02:00
|
|
|
function isDocker(): bool {
|
|
|
|
return file_exists("/.dockerenv");
|
|
|
|
}
|
|
|
|
|
2022-11-23 23:36:30 +01:00
|
|
|
function createError($msg): array {
|
2022-06-20 19:52:31 +02:00
|
|
|
return ["success" => false, "msg" => $msg];
|
2020-06-22 19:09:02 +02:00
|
|
|
}
|
2020-02-09 23:02:19 +01:00
|
|
|
|
2022-02-20 16:53:26 +01:00
|
|
|
function downloadFile($handle, $offset = 0, $length = null): bool {
|
2022-06-14 10:30:35 +02:00
|
|
|
if ($handle === false) {
|
2022-02-20 16:53:26 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($offset > 0) {
|
|
|
|
fseek($handle, $offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
$bytesRead = 0;
|
2022-06-14 10:30:35 +02:00
|
|
|
$bufferSize = 1024 * 16;
|
2022-02-20 16:53:26 +01:00
|
|
|
while (!feof($handle) && ($length === null || $bytesRead < $length)) {
|
|
|
|
$chunkSize = ($length === null ? $bufferSize : min($length - $bytesRead, $bufferSize));
|
|
|
|
echo fread($handle, $chunkSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose($handle);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-08 11:50:18 +01:00
|
|
|
function parseClass($class): string {
|
2020-07-15 15:05:54 +02:00
|
|
|
if (!startsWith($class, "\\")) {
|
|
|
|
$class = "\\$class";
|
|
|
|
}
|
|
|
|
|
|
|
|
$parts = explode("\\", $class);
|
|
|
|
$parts = array_map('ucfirst', $parts);
|
|
|
|
return implode("\\", $parts);
|
2021-01-07 15:54:19 +01:00
|
|
|
}
|
2022-06-20 19:52:31 +02:00
|
|
|
|
|
|
|
function isClass(string $str): bool {
|
|
|
|
$path = getClassPath($str);
|
|
|
|
return is_file($path) && class_exists($str);
|
2023-01-12 20:13:20 +01:00
|
|
|
}
|
|
|
|
|
2024-04-22 12:03:24 +02:00
|
|
|
function getCurrentUsername(): string {
|
|
|
|
if (function_exists("posix_getpwuid") && function_exists("posix_geteuid")) {
|
|
|
|
return posix_getpwuid(posix_geteuid())['name'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return exec('whoami') ?? "Unknown";
|
|
|
|
}
|
|
|
|
|
2023-01-12 20:13:20 +01:00
|
|
|
function rrmdir(string $dir): void {
|
|
|
|
if (is_dir($dir)) {
|
|
|
|
$objects = scandir($dir);
|
|
|
|
foreach ($objects as $object) {
|
|
|
|
if ($object !== "." && $object !== "..") {
|
|
|
|
$path = $dir . DIRECTORY_SEPARATOR . $object;
|
|
|
|
if (is_dir($path) && !is_link($path)) {
|
|
|
|
rrmdir($path);
|
|
|
|
} else {
|
|
|
|
unlink($path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rmdir($dir);
|
|
|
|
}
|
2024-05-04 12:23:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function loadEnv(?string $file = NULL, bool $putEnv = false): array|null {
|
|
|
|
if ($file === NULL) {
|
|
|
|
$file = WEBROOT . DIRECTORY_SEPARATOR . ".env";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_file($file)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$env = parse_ini_file('.env');
|
|
|
|
if ($putEnv) {
|
|
|
|
foreach ($env as $key => $value) {
|
|
|
|
putenv("$key=$value");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $env;
|
2022-06-20 19:52:31 +02:00
|
|
|
}
|