File: /home/www/web115/wordpress/wp-content/plugins/digimember/application/model/logic/http_response.php
<?php
/**
* Class digimember_HttpResponseLogic
*/
class digimember_HttpResponseLogic extends ncore_BaseLogic
{
public $version = '1.1';
/** @var array */
private $parameters = [];
/** @var array */
private $httpHeaders = [];
/** @var int */
private $statusCode;
/** @var string */
private $statusText;
/** @var array */
public static $statusTexts = [
100 => 'Continue',
101 => 'Switching Protocols',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
418 => 'I\'m a teapot',
422 => 'Unprocessable Entity',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
];
/**
* Resets the request in case some other process tried to fill it first
*/
public function init()
{
$this->parameters = [];
$this->httpHeaders = [];
$this->statusCode = 200;
}
/**
* @return int
*/
public function getStatusCode()
{
return $this->statusCode;
}
/**
* @param int $statusCode
* @param null|string $text
*/
public function setStatusCode($statusCode, $text = null)
{
$this->statusCode = (int)$statusCode;
if ($this->isInvalid()) {
throw new \InvalidArgumentException(sprintf('The HTTP status code "%s" is not valid.', $statusCode));
}
$this->statusText = false == $text ? '' : (null == $text ? self::$statusTexts[$this->statusCode] : $text);
}
/**
* @return string
*/
public function getStatusText()
{
return $this->statusText;
}
/**
* @return array
*/
public function getParameters()
{
return $this->parameters;
}
/**
* @param array $parameters
*/
public function setParameters($parameters)
{
$this->parameters = $parameters;
}
/**
* @param array $parameters
*/
public function addParameters($parameters)
{
$this->parameters = array_merge($this->parameters, $parameters);
}
/**
* @param string $name
* @param mixed $default
* @return mixed
*/
public function getParameter($name, $default = null)
{
return ncore_retrieve($this->parameters, $name, $default);
}
/**
* @param string $name
* @param mixed $value
*/
public function setParameter($name, $value)
{
$this->parameters[$name] = $value;
}
/**
* @param array $httpHeaders
*/
public function setHttpHeaders($httpHeaders)
{
$this->httpHeaders = $httpHeaders;
}
/**
* @param string $name
* @param string $value
*/
public function setHttpHeader($name, $value)
{
$this->httpHeaders[$name] = $value;
}
/**
* @param array $httpHeaders
*/
public function addHttpHeaders($httpHeaders)
{
$this->httpHeaders = array_merge($this->httpHeaders, $httpHeaders);
}
/**
* @return array
*/
public function getHttpHeaders()
{
return $this->httpHeaders;
}
/**
* @param string $name
* @param string $default
* @return string|null
*/
public function getHttpHeader($name, $default = null)
{
return ncore_retrieve($this->httpHeaders, $name, $default);
}
/**
* @param string $format
* @return string
* @throws InvalidArgumentException
*/
public function getResponseBody($format = 'json')
{
switch ($format) {
case 'json':
return json_encode($this->parameters);
case 'xml':
// this only works for single-level arrays
$xml = new SimpleXMLElement('<response/>');
array_walk($this->parameters, [$xml, 'addChild']);
return $xml->asXML();
}
throw new InvalidArgumentException(sprintf('The format %s is not supported', $format));
}
/**
* @param string $format
*/
public function send($format = 'json')
{
// headers have already been sent by the developer
if (headers_sent()) {
return;
}
switch ($format) {
case 'json':
$this->setHttpHeader('Content-Type', 'application/json');
break;
case 'xml':
$this->setHttpHeader('Content-Type', 'text/xml');
break;
}
// status
header(sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText));
foreach ($this->getHttpHeaders() as $name => $header) {
header($this->buildHeader($name, $header));
}
echo $this->getResponseBody($format);
$this->terminate();
}
/**
* Function that exits the script, should be mocked in unit tests
*/
public function terminate()
{
exit;
}
/**
* @param int $statusCode
* @param string $error
* @param null $errorDescription
* @param null $errorUri
*/
public function setError($statusCode, $error, $errorDescription = null, $errorUri = null)
{
$parameters = [
'error' => $error,
'error_description' => $errorDescription,
];
if (!is_null($errorUri)) {
if (strlen($errorUri) > 0 && $errorUri[0] == '#') {
$errorUri = 'http://tools.ietf.org/html/rfc6749' . $errorUri;
}
$parameters['error_uri'] = $errorUri;
}
$httpHeaders = [
'Cache-Control' => 'no-store',
];
$this->setStatusCode($statusCode);
$this->addParameters($parameters);
$this->addHttpHeaders($httpHeaders);
if (!$this->isClientError() && !$this->isServerError()) {
throw new \InvalidArgumentException(sprintf('The HTTP status code is not an error ("%s" given).', $statusCode));
}
}
/**
* @return bool
*/
public function isInvalid()
{
return $this->statusCode < 100 || $this->statusCode >= 600;
}
/**
* @return bool
*/
public function isInformational()
{
return $this->statusCode >= 100 && $this->statusCode < 200;
}
/**
* @return bool
*/
public function isRedirection()
{
return $this->statusCode >= 300 && $this->statusCode < 400;
}
/**
* @return bool
*/
public function isClientError()
{
return $this->statusCode >= 400 && $this->statusCode < 500;
}
/**
* @return bool
*/
public function isServerError()
{
return $this->statusCode >= 500 && $this->statusCode < 600;
}
/**
* @return bool
*/
public function isSuccessful()
{
return $this->statusCode >= 200 && $this->statusCode < 300;
}
/**
* @param string $name
* @param string $value
* @return string
*/
private function buildHeader($name, $value)
{
return sprintf("%s: %s\n", $name, $value);
}
}