/**
  * Outputs the error popup, or a plain message, depending on the response content type.
  *
  * @param \Exception|\Error      $exception Note: can't be type hinted, for PHP7 compat.
  * @param ResponseInterface|null $response  If null, it outputs directly to the client. Otherwise, it assumes the
  *                                          object is a new blank response.
  * @return ResponseInterface|null
  */
 static function display($exception, ResponseInterface $response = null)
 {
     // For HTML pages, output the error popup
     if (strpos(get($_SERVER, 'HTTP_ACCEPT'), 'text/html') !== false) {
         ob_start();
         ErrorConsoleRenderer::renderStyles();
         $stackTrace = self::getStackTrace($exception->getPrevious() ?: $exception);
         ErrorConsoleRenderer::renderPopup($exception, self::$appName, $stackTrace);
         $popup = ob_get_clean();
         // PSR-7 output
         if ($response) {
             $response->getBody()->write($popup);
             return $response->withStatus(500);
         }
         // Direct output
         echo $popup;
     } else {
         // PSR-7 output
         if ($response) {
             $response->getBody()->write($exception->getMessage());
             if (self::$devEnv) {
                 $response->getBody()->write("\n\nStack trace:\n" . $exception->getTraceAsString());
             }
             return $response->withoutHeader('Content-Type')->withHeader('Content-Type', 'text-plain')->withStatus(500);
         }
         // Direct output
         header("Content-Type: text/plain");
         http_response_code(500);
         echo $exception->getMessage();
         if (self::$devEnv) {
             echo "\n\nStack trace:\n" . $exception->getTraceAsString();
         }
     }
     return null;
 }
Example #2
0
    public function dispatch(ServerRequestInterface $request, ResponseInterface $response) : ResponseInterface
    {
        $lang = LanguageNegotiator::getLanguage($request);
        $query = $this->db->prepare('SELECT n.id, n.title, n.parent_id from nodes n
INNER JOIN node_attributes na ON na.node_id = n.id
INNER JOIN attribute_values av on na.`attribute_value_id` = av.`id`
where av.`attribute_value` = :language;');
        $query->bindParam(':language', $lang);
        $query->execute();
        $data = $query->fetchAll(\PDO::FETCH_ASSOC);
        $neg = new FormatNegotiator();
        $contentType = $neg->getFormat($request);
        switch ($contentType) {
            case 'xml':
                $xml = new \SimpleXMLElement('<root/>');
                array_walk_recursive($this->treeBuilder->build($data), array($xml, 'addChild'));
                $response->getBody()->write($xml->asXML());
                break;
            default:
                $response->getBody()->write(json_encode($this->treeBuilder->build($data)));
                break;
        }
        return $response->withoutHeader('Content-Type');
        // Remove the Content-Type Header
    }
 /**
  * Proxy to PsrResponseInterface::withoutHeader()
  *
  * {@inheritdoc}
  * @throws RuntimeException if response is already completed
  */
 public function withoutHeader($header)
 {
     if ($this->complete) {
         throw $this->responseIsAlreadyCompleted(__METHOD__);
     }
     $new = $this->psrResponse->withoutHeader($header);
     return new self($new);
 }
 /**
  * Proxy to PsrResponseInterface::withoutHeader()
  *
  * {@inheritdoc}
  */
 public function withoutHeader($header)
 {
     if ($this->complete) {
         return $this;
     }
     $new = $this->psrResponse->withoutHeader($header);
     return new self($new);
 }
 /**
  * {@inheritDoc}
  */
 public function isAuthentic(ResponseInterface $response)
 {
     if (!$response->hasHeader('X-Server-Authorization-HMAC-SHA256')) {
         throw new MalformedResponseException('Response is missing required X-Server-Authorization-HMAC-SHA256 header.', null, 0, $response);
     }
     $responseSigner = new ResponseSigner($this->key, $this->request);
     $compareResponse = $responseSigner->signResponse($response->withoutHeader('X-Server-Authorization-HMAC-SHA256'));
     $responseSignature = $response->getHeaderLine('X-Server-Authorization-HMAC-SHA256');
     $compareSignature = $compareResponse->getHeaderLine('X-Server-Authorization-HMAC-SHA256');
     return hash_equals($compareSignature, $responseSignature);
 }
Example #6
0
 /**
  * {@inheritDoc}
  */
 public function withoutHeader($name)
 {
     return new self($this->app, $this->decorated->withoutHeader($name));
 }
Example #7
0
 /**
  * Return an instance without the specified header.
  *
  * Header resolution MUST be done without case-sensitivity.
  *
  * This method MUST be implemented in such a way as to retain the
  * immutability of the message, and MUST return an instance that removes
  * the named header.
  *
  * @param string $name Case-insensitive header field name to remove.
  * @return self
  */
 function withoutHeader($name)
 {
     return $this->response->withoutHeader($name);
 }
Example #8
0
 /**
  * {@inheritdoc}
  */
 public function withoutHeader($name)
 {
     return new HttpException($this->response->withoutHeader($name), $this->attributes);
 }
Example #9
0
 /**
  * @inheritDoc
  */
 public function withoutHeader($name)
 {
     return new self($this->psrResponse->withoutHeader($name));
 }
Example #10
0
 public function withoutHeader($name)
 {
     $response = clone $this;
     $response->response = $this->response->withoutHeader($name);
     return $response;
 }
Example #11
0
 /**
  * @param ResponseInterface $response
  */
 public function send(ResponseInterface $response)
 {
     $empty = \false;
     if (in_array($response->getStatusCode(), [204, 205, 304])) {
         $empty = \true;
     }
     if ($empty === \true) {
         $response->withoutHeader('Content-Type')->withoutHeader('Content-Length');
     }
     $size = $response->getBody()->getSize();
     if ($size !== \null && !$response->hasHeader('Content-Length')) {
         $response->withHeader('Content-Length', (string) $size);
     }
     // Send response
     if (!\headers_sent()) {
         // Status
         \header(\sprintf('HTTP/%s %s %s', $response->getProtocolVersion(), $response->getStatusCode(), $response->getReasonPhrase()));
         // Headers
         foreach ($response->getHeaders() as $name => $values) {
             foreach ($values as $value) {
                 \header(\sprintf('%s: %s', $name, $value), false);
             }
         }
     }
     // Body
     if ($empty === \false) {
         $body = $response->getBody();
         if ($body->isSeekable()) {
             $body->rewind();
         }
         $chunkSize = $this->container->get('config')->get('response.responseChunkSize', 4096);
         $contentLength = $response->getHeaderLine('Content-Length');
         if (!$contentLength) {
             $contentLength = $body->getSize();
         }
         $totalChunks = \ceil($contentLength / $chunkSize);
         $lastChunkSize = $contentLength % $chunkSize;
         $currentChunk = 0;
         while (!$body->eof() && $currentChunk < $totalChunks) {
             if (++$currentChunk == $totalChunks && $lastChunkSize > 0) {
                 $chunkSize = $lastChunkSize;
             }
             echo $body->read($chunkSize);
             if (\connection_status() != \CONNECTION_NORMAL) {
                 break;
             }
         }
     }
 }