2014-08-31 16:56:00 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Helpers;
|
|
|
|
|
|
|
|
class HttpHelper
|
|
|
|
{
|
|
|
|
public function setResponseCode($code)
|
|
|
|
{
|
|
|
|
http_response_code($code);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setHeader($key, $value)
|
|
|
|
{
|
2014-09-09 12:34:57 +02:00
|
|
|
header($key . ': ' . $value);
|
2014-08-31 16:56:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function outputJSON($data)
|
|
|
|
{
|
2014-09-16 14:42:25 +02:00
|
|
|
$encodedJson = json_encode((array) $data);
|
|
|
|
$lastError = json_last_error();
|
|
|
|
if ($lastError !== JSON_ERROR_NONE)
|
|
|
|
$this->output('Fatal error while encoding JSON: ' . $lastError . PHP_EOL . PHP_EOL . print_r($data, true));
|
|
|
|
else
|
|
|
|
$this->output($encodedJson);
|
2014-08-31 16:56:00 +02:00
|
|
|
}
|
|
|
|
|
2014-10-08 19:47:21 +02:00
|
|
|
public function output($data)
|
|
|
|
{
|
|
|
|
echo $data;
|
|
|
|
}
|
|
|
|
|
2014-09-04 19:21:18 +02:00
|
|
|
public function getRequestHeaders()
|
|
|
|
{
|
|
|
|
return getallheaders();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRequestHeader($key)
|
|
|
|
{
|
|
|
|
$headers = $this->getRequestHeaders();
|
|
|
|
return isset($headers[$key]) ? $headers[$key] : null;
|
|
|
|
}
|
|
|
|
|
2014-08-31 16:56:00 +02:00
|
|
|
public function getRequestMethod()
|
|
|
|
{
|
|
|
|
return $_SERVER['REQUEST_METHOD'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRequestUri()
|
|
|
|
{
|
2014-09-03 19:07:53 +02:00
|
|
|
$requestUri = $_SERVER['REQUEST_URI'];
|
|
|
|
$requestUri = preg_replace('/\?.*$/', '', $requestUri);
|
|
|
|
return $requestUri;
|
2014-08-31 16:56:00 +02:00
|
|
|
}
|
|
|
|
}
|