interfaccia tra i portali e IS della Regione per l'autenticazione CNS SPID e CIE
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

109 righe
3.4 KiB

<?php
namespace MwgAuth\Core;
class Controller
{
private static $charset = 'UTF-8';
private static $contentType = 'application/json';
private static $responseCode = 200;
public static function run(string $function): void
{
if ($function === '') {
$function = 'index';
} else {
$function = self::camelize($function);
}
if (method_exists(static::class, $function)) {
$html = static::$function();
$header = 'Content-Type: ' . self::$contentType;
if (null !== self::$charset) {
$header .= "; charset=" . self::$charset;
}
header($header, true, self::$responseCode);
echo $html;
} else {
self::page404();
}
}
protected static function camelize(string $input): string
{
$ret = [];
foreach (explode('_', $input) as $part) {
$ret[] = ucfirst($part);
}
return implode('', $ret);
}
protected static function getPost(): ?array
{
$input = file_get_contents('php://input');
return json_decode($input, true);
}
protected static function requireMethod(string $method): void
{
if ($method !== $_SERVER['REQUEST_METHOD']) {
header($_SERVER['SERVER_PROTOCOL'] . ' 405 Method Not Allowed', true, 405);
die("<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" .
"<html><head>\n" .
"<title>405 Method Not Allowed</title>\n" .
"</head><body>\n" .
"<h1>Method Not Allowed</h1>\n" .
"<p>The requested method " . $_SERVER['REQUEST_METHOD'] . " is not allowed for this resource.</p>\n" .
self::serverSignature());
}
}
protected static function page404(): string
{
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found', true, 404);
die("<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" .
"<html><head>\n" .
"<title>404 Not Found</title>\n" .
"</head><body>\n" .
"<h1>Not Found</h1>\n" .
"<p>The requested URL was not found on this server.</p>\n" .
self::serverSignature());
}
public static function redirect(string $location)
{
header($_SERVER['SERVER_PROTOCOL'] . ' 302 Found', true, 302);
header('Location: ' . $location, true);
die("<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" .
"<html><head>\n" .
"<title>302 Found</title>\n" .
"</head><body>\n" .
"<h1>Found</h1>\n" .
"<p>The document has moved <a href=\"" . $location . "\">here</a>.</p>\n" .
self::serverSignature());
}
private static function serverSignature(): string
{
return
"<hr>\n" .
"<address>" . $_SERVER['SERVER_SOFTWARE'] . " Server at " . $_SERVER['SERVER_NAME'] . " Port " .
$_SERVER['SERVER_PORT'] . "</address>\n" .
"</body></html>\n";
}
protected static function setCharset(string $charset): void
{
self::$charset = $charset;
}
protected static function setContentType(string $contentType): void
{
self::$contentType = $contentType;
}
protected static function setResponseCode(string $responseCode): void
{
self::$responseCode = $responseCode;
}
}