public static function respond($httpResponseCode, $errorDetails = NULL, $contentType = NULL) { self::$isShutdownHandlerEnabled = FALSE; if ($errorDetails == NULL) { $errorDetails = ''; } // Check if any technical error has occurred (ignore certain errors). $lastError = error_get_last(); if (isset($lastError['message']) and stripos($lastError['message'], 'magic_quotes_gpc') === FALSE) { // If so, then log it. $logMsg = $lastError['message'] . " in " . $lastError['file'] . '(' . $lastError['line'] . ')'; error_log("LAST ERROR - {$logMsg} {$errorDetails}"); // ...and adjust the response to make sure it replects an error. if ($httpResponseCode < HttpResponseCodes::errorCodesBeginAt) { $httpResponseCode = HttpResponseCodes::HTTP_INTERNAL_SERVER_ERROR; } $errorDetails .= "\n<!-- " . $lastError['message'] . "\n<br/>## " . $lastError['file'] . '(' . $lastError['line'] . ") -->"; // Append any available data. $bufferedOutput = ob_get_clean(); if (strlen($bufferedOutput) > 0) { $errorDetails .= "<br/>{$bufferedOutput}"; } $contentType = 'text/html'; } $responseMessage = HttpResponseCodes::getMessage($httpResponseCode); header("HTTP/1.1 {$httpResponseCode} {$responseMessage}", true, $httpResponseCode); if (!HttpResponseCodes::isSuccessCode($httpResponseCode)) { if (HttpResponseCodes::canHaveBody($httpResponseCode)) { $contentTypeTag = InternetMediaTypes::getTagOfType($contentType); if ($contentTypeTag == 'xml') { $response = '<?xml version="1.0"?>' . "\n<error>" . "<code>{$httpResponseCode}</code>" . "<message>{$responseMessage}</message>"; if (strlen($errorDetails) > 0) { $response .= "<details>{$errorDetails}</details>"; } $response .= '</error>'; } else { $contentType = 'text/html'; $response = '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">' . "\n<html><head>" . "<title>{$httpResponseCode} - {$responseMessage}</title>" . '</head><body>' . "<h1>{$responseMessage}</h1>"; if (strlen($errorDetails) > 0) { if (stripos($errorDetails, '</html>') !== FALSE) { $errorDetails = html_entity_decode($errorDetails); } $response .= "<p>{$errorDetails}</p>"; } $response .= '</body></html>'; } header('Content-type: ' . $contentType); echo $response; } exit; } }
<?php error_reporting(E_ALL | E_NOTICE | E_WARNING); require_once realpath(dirname(__FILE__) . '/../../bransom/php/Bootstrap.class.php'); Bootstrap::initConfig(dirname(__FILE__) . '/../../bransom/config/config.ini'); Bootstrap::import('nl.bransom.http.HttpRequestHandler'); Bootstrap::import('nl.bransom.http.HttpResponder'); HttpResponder::handleFatalErrorsOnShutdown(); // e.g. /webitems/newsreader/elsvanwijnen/image/330/data.jpg $restTarget = HttpRequestHandler::getRestTarget($_SERVER['REQUEST_URI'], $_SERVER['PHP_SELF']); // e.g [ elsvanwijnen, image, 330, data.jpg ] if (count($restTarget) > 0) { $restTarget[0] = 'webitems'; } $requestHandler = new HttpRequestHandler(); $requestHandler->dispatchRequest($_SERVER['REQUEST_METHOD'], $restTarget, $_SERVER['QUERY_STRING'], $_SERVER['HTTP_ACCEPT'], 'max-age=3600', $_REQUEST);
public function respond($expectedResponseTypeTag = NULL, $httpAccept = NULL, $cacheControl = NULL) { if ($this->expectedResponseTypeTag == NULL) { $this->expectedResponseTypeTag = $expectedResponseTypeTag; } $contentType = $this->getHttpMediaType($httpAccept); // Reply HTTP response code and headers. $httpResponseCode = $this->getHttpResponseCode(); HttpResponder::respond($httpResponseCode, $this->getResponseContentHttp(), $contentType); if (InternetMediaTypes::requiresEncoding($contentType)) { $this->characterEncoding = 'utf-8'; header("Content-type: {$contentType}; charset=" . $this->characterEncoding); } else { header("Content-type: {$contentType}"); } // Set cache-control header. if ($cacheControl != NULL) { header("Cache-Control: {$cacheControl}"); } else { // No caching! header("Cache-Control: no-cache, no-store, max-age=0"); header("Pragma: no-cache"); } // Reply the content. echo $this->getResponseContentHttp(); }