death-notifier/src/main/php/com/fwdekker/deathnotifier/Util.php

72 lines
1.5 KiB
PHP

<?php
namespace com\fwdekker\deathnotifier;
use Exception;
use InvalidArgumentException;
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
*/
static function http_exit(int $status): never
{
http_response_code($status);
exit(1);
}
/**
* Parses POST values from JSON-based inputs.
*
* @return array<int|string, mixed>|null the parsed POST-ed values
*/
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;
}
/**
* Parses `$argv` into an array, similar to `$_GET`.
*
* Code from https://www.php.net/manual/en/features.commandline.php#108883.
*
* @return array<int|string, mixed> the parsed CLI inputs
*/
static function parse_cli(): array
{
parse_str(implode("&", array_slice($_SERVER["argv"], 1)), $output);
return $output;
}
/**
* Generates an appropriate CSRF token.
*
* @return string the generated CSRF token
* @throws Exception if the CSRF token could not be generated
*/
static function generate_csrf_token(): string
{
return bin2hex(random_bytes(32));
}
}