2022-08-29 01:09:33 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace php;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use Monolog\ErrorHandler;
|
|
|
|
use Monolog\Handler\StreamHandler;
|
|
|
|
use Monolog\Logger;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper functions.
|
|
|
|
*/
|
|
|
|
class Util
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Sets HTTP status code and exits the script.
|
|
|
|
*
|
|
|
|
* @param int $status the HTTP status code to set
|
|
|
|
* @return never-returns
|
|
|
|
*/
|
|
|
|
static function http_exit(int $status): void
|
|
|
|
{
|
|
|
|
http_response_code($status);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2022-11-14 15:06:39 +01:00
|
|
|
|
2022-08-29 01:09:33 +02:00
|
|
|
/**
|
|
|
|
* Reads the configuration file and overrides it with the user's custom values.
|
|
|
|
*
|
|
|
|
* @return array<string, array<string, mixed>>|null the configuration
|
|
|
|
*/
|
|
|
|
static function read_config(): ?array
|
|
|
|
{
|
|
|
|
// TODO: Check permissions, return `null` if too permissive
|
|
|
|
$config = parse_ini_file("config.default.ini.php", process_sections: true, scanner_mode: INI_SCANNER_TYPED);
|
|
|
|
if ($config === false) return null;
|
|
|
|
|
|
|
|
if (file_exists("config.ini.php")) {
|
|
|
|
$config_custom = parse_ini_file("config.ini.php", process_sections: true, scanner_mode: INI_SCANNER_TYPED);
|
|
|
|
if ($config_custom === false) return null;
|
|
|
|
|
2022-11-18 16:25:08 +01:00
|
|
|
$config = array_replace_recursive($config, $config_custom);
|
2022-08-29 01:09:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the main logger instance.
|
|
|
|
*
|
|
|
|
* @param array<string, mixed> $logger_config the logger section of the configuration
|
|
|
|
* @return Logger the main logger instance
|
|
|
|
*/
|
|
|
|
static function create_logger(array $logger_config): Logger
|
|
|
|
{
|
|
|
|
$logger = new Logger("main");
|
|
|
|
$logger->pushHandler(new StreamHandler($logger_config["file"], $logger_config["level"]));
|
|
|
|
ErrorHandler::register($logger);
|
|
|
|
|
|
|
|
return $logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses POST values from JSON-based inputs.
|
|
|
|
*
|
2022-11-15 22:26:34 +01:00
|
|
|
* @return array<string, mixed>|null the parsed POST-ed values
|
2022-08-29 01:09:33 +02:00
|
|
|
*/
|
|
|
|
static function parse_post(): ?array
|
|
|
|
{
|
|
|
|
$output = $_POST;
|
|
|
|
|
|
|
|
$post_input = file_get_contents("php://input");
|
|
|
|
if ($post_input !== false)
|
|
|
|
$output = json_decode($post_input, associative: true);
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2022-11-14 15:06:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates an appropriate CSRF token.
|
|
|
|
*
|
|
|
|
* @param Logger $logger the logger to use if something goes wrong
|
|
|
|
* @return string|null the CSRF token, or `null` if no CSRF token could be created
|
|
|
|
*/
|
|
|
|
static function generate_csrf_token(Logger $logger): ?string
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return bin2hex(random_bytes(32));
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
$logger->emergency("Failed to generate token.", ["cause" => $exception]);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2022-08-29 01:09:33 +02:00
|
|
|
}
|