/** * @param object $endpoint * @param string $type * @param array $params * * @throws Slim\Exception\Stop */ private function _handleEndpointCall($endpoint, $type, array $params) { if ($endpoint instanceof SlimBootstrap\Endpoint\InjectClientId) { $endpoint->setClientId($this->_app->router()->getCurrentRoute()->getParam('clientId')); } try { $outputWriter =& $this->_hook->getResponseOutputWriter(); if ($endpoint instanceof SlimBootstrap\Endpoint\ForceDefaultMimeType) { $csvConfig = array(); if (true === \array_key_exists('csv', $this->_applicationConfig) && true === \is_array($this->_applicationConfig['csv'])) { $csvConfig = $this->_applicationConfig['csv']; } // create output writer $responseOutputWriterFactory = new SlimBootstrap\ResponseOutputWriter\Factory($this->_app->request, $this->_app->response, $this->_app->response->headers, $this->_applicationConfig['shortName'], $csvConfig); $outputWriter = $responseOutputWriterFactory->create($endpoint->getDefaultMimeType()); } if ($endpoint instanceof SlimBootstrap\Endpoint\Streamable) { if ($outputWriter instanceof SlimBootstrap\ResponseOutputWriterStreamable) { $endpoint->setOutputWriter($outputWriter); \ob_start(); $endpoint->{$type}($params, $this->_app->request->{$type}()); \ob_end_clean(); } else { throw new SlimBootstrap\Exception('media type does not support streaming', 406, Slim\Log::WARN); } } else { $data = $endpoint->{$type}($params, $this->_app->request->{$type}()); if ($endpoint instanceof SlimBootstrap\Endpoint\PlainData) { if ($outputWriter instanceof SlimBootstrap\ResponseOutputWriterPlainData) { $outputWriter->writePlain($data); } else { throw new SlimBootstrap\Exception('media type does not support plain data writing', 406, Slim\Log::WARN); } } else { $outputWriter->write($data); } } } catch (SlimBootstrap\Exception $e) { $this->_app->getLog()->log($e->getLogLevel(), $e->getCode() . ' - ' . $e->getMessage()); $this->_app->response->setStatus($e->getCode()); $this->_app->response->setBody($e->getMessage()); $this->_app->stop(); } }
/** * Output a generic error page * * @param string $status The HTTP status code (eg 401, 404, 500) * @param string $title Optional brief title of the page, used in HTML head etc * @param string $summary Optional summary message describing the error * @param string $extendedTitle Optional extended title, used at top of main content * @param string $extendedDetails Optional extended details, conveying more details * * @throws \InvalidArgumentException if the status is not a valid HTTP status code */ protected function outputError($status, $title = null, $summary = '', $extendedTitle = '', $extendedDetails = '') { if (!is_integer($status)) { throw new \InvalidArgumentException('The $status parameter must be a valid integer HTTP status code'); } $this->app->response->setStatus($status); // slim does not set the response code in this function // setting it manually if (!headers_sent()) { http_response_code($status); // PHP 5.4+ } if (is_null($title)) { $title = 'Error ' . $status; } if ($extendedTitle === '') { $defaultMsg = \Slim\Http\Response::getMessageForCode($status); if ($defaultMsg !== null) { $extendedTitle = substr($defaultMsg, 4); } else { $extendedTitle = $title; } } // Content negotiation for the error message // callbackCheckFormats() middleware does the content negotiation. // But it was not executed, yet. Call it now to get the mime type. $route = $this->app->router()->getCurrentRoute(); if ($route) { $this->mimeBest = $this->callbackCheckFormats($route); } // if (!$this->store) { // $this->store = $route->getParam('store'); // } // display name of store in titlebar if (isset($this->storeOptions[$this->store]['shortName'])) { $u = $this->app->request()->getRootUri() . '/datasets/' . $this->store; $this->app->view()->set('titleSupplementary', '<a href="' . htmlspecialchars($u, ENT_QUOTES) . '" class="navbar-brand supplementary">' . htmlspecialchars($this->storeOptions[$this->store]['shortName']) . '</a>'); } switch ($this->mimeBest) { case 'text/plain': $error = "status => {$status}" . PHP_EOL; $error .= "title => {$extendedTitle}" . PHP_EOL; if ($summary) { $error .= "summary => {$summary}" . PHP_EOL; } if ($extendedDetails) { $error .= "details => " . preg_replace('~[\\n]+|[\\s]{2,}~', ' ', $extendedDetails); } // setBody does not work in this function $this->app->response->setBody($error); // render a blank template instead // $this->app->render('blank.twig', [ 'content' => $error ] ); break; case 'text/csv': case 'text/tab-separated-values': if ($this->mimeBest === 'text/csv') { $delimiter = ','; } else { $delimiter = "\t"; } $error = [array("status", $status), array("title", $extendedTitle)]; if ($summary) { $error[] = array("summary", $summary); } if ($extendedDetails) { $error[] = array("details", preg_replace('~[\\n]+|[\\s]{2,}~', ' ', $extendedDetails)); } ob_start(); $out = fopen('php://output', 'w'); foreach ($error as $err) { fputcsv($out, $err, $delimiter); } fclose($out); $out = ob_get_contents(); ob_end_clean(); // setBody does not work in this function $this->app->response->setBody($out); // render a blank template instead // $this->app->render('blank.twig', [ 'content' => $out ] ); break; case 'application/rdf+xml': case 'application/x-turtle': case 'text/turtle': case 'application/json': $json_error = array('status' => $status, 'title' => $extendedTitle); if ($summary) { $json_error['summary'] = $summary; } if ($extendedDetails) { $json_error['details'] = $extendedDetails; } // setBody does not work in this function $this->app->response->setBody(json_encode($json_error, JSON_PRETTY_PRINT)); // PHP 5.4+ // render a blank template instead // $this->app->render('blank.twig', [ 'content' => json_encode($json_error, JSON_PRETTY_PRINT) ] ); break; case 'text/html': case 'application/xhtml+xml': default: $summary .= '</p>' . PHP_EOL . PHP_EOL . '<p>Please try returning to <a href="' . $this->app->request()->getURL() . $this->app->request()->getRootUri() . '">the homepage</a>.'; // overwrite the previous content type definition $this->app->contentType($this->mimeBest); $this->app->render('error/error-html.twig', ['titleHTML' => ' - ' . strip_tags($title), 'titleHeader' => 'Error ' . $status, 'title' => $extendedTitle, 'summary' => $summary, 'details' => $extendedDetails]); break; } // execution stops here $this->app->stop(); }
$data['metas']['title'] = 'Internal server error'; $app->render('500', $data); }); // ================================================================== // // Cookies advise // // ------------------------------------------------------------------ $data['cookieState'] = !isset($_COOKIE[$data['cookies']['name']]) ? true : false; // ================================================================== // // Add before.dispatch and run app // // ------------------------------------------------------------------ $app->hook('slim.before.dispatch', function () use($app, $data, $pages) { $routeName = $app->router()->getCurrentRoute()->getName(); if (isset($data['langs']['metas'][$routeName])) { $data['metas']['title'] = $data['langs']['metas'][$routeName]['title']; $data['metas']['description'] = $data['langs']['metas'][$routeName]['description']; $data['metas']['keywords'] = $data['langs']['metas'][$routeName]['keywords']; } $menu_langs = []; if ($routeName) { foreach ($data['app_langs'] as $lang) { $routeItem = new stdClass(); $routeItem->lang = $lang; $routeItem->route = '/' . $lang; if (isset($pages[$routeName])) { if ($lang == $data['default_lang']) { $routeItem->route = ''; if ($pages[$routeName][$lang]['route'] == '/') {