Elgg  Version master
ExceptionHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Application;
4 
13 
20 
21  use Loggable;
22 
39  public function __invoke($exception) {
40  $exception->timestamp = time();
41  $exception->uncaught = true;
42 
43  $this->log(LogLevel::CRITICAL, $exception);
44 
45  // Wipe any existing output buffer
46  ob_end_clean();
47 
48  $request = Request::createFromGlobals();
49  $headers = [
50  'Cache-Control' => 'no-store, must-revalidate',
51  'Expires' => 'Fri, 05 Feb 1982 00:00:00 -0500',
52  ];
53 
54  if ($exception instanceof InstallationException) {
55  $response = new RedirectResponse('/install.php', ELGG_HTTP_TEMPORARY_REDIRECT, $headers);
56  $response->prepare($request);
57 
58  $response->send();
59 
60  return;
61  }
62 
64 
65  if (!$app || !$app->internal_services) {
66  $msg = "Exception loading Elgg core. Check log at time {$exception->timestamp}";
67  $response = new Response($msg, ELGG_HTTP_INTERNAL_SERVER_ERROR, $headers);
68  $response->prepare($request);
69 
70  $response->send();
71 
72  return;
73  }
74 
75  $services = $app->internal_services;
76  if ($services->responseFactory->getSentResponse() !== null) {
77  return;
78  }
79 
80  try {
81  // allow custom scripts to trigger on exception
82  // value in settings.php should be a system path to a file to include
83  $exception_include = $services->config->exception_include;
84 
85  if ($exception_include && is_file($exception_include)) {
86  ob_start();
87 
88  // don't isolate, these scripts may use the local $exception var.
89  include $exception_include;
90 
91  $exception_output = ob_get_clean();
92 
93  // if content is returned from the custom handler we will output
94  // that instead of our default failsafe view
95  if (!empty($exception_output)) {
96  $response = new Response($exception_output, ELGG_HTTP_INTERNAL_SERVER_ERROR, $headers);
97  $response->prepare($request);
98 
99  $response->send();
100 
101  return;
102  }
103  }
104 
105  if (Application::isCli()) {
106  // An error has already been logged
107  return;
108  }
109 
110  if ($services->request->isXmlHttpRequest()) {
111  $services->views->setViewtype('json');
112  $response = new JsonResponse(null, ELGG_HTTP_INTERNAL_SERVER_ERROR, $headers);
113  } else {
114  $services->views->setViewtype('failsafe');
116  }
117 
118  $body = elgg_view('messages/exceptions/exception', [
119  'object' => $exception,
120  'ts' => $exception->timestamp,
121  ]);
122 
123  $response->prepare($services->request);
124  $response->setContent(elgg_view_page(elgg_echo('exception:title'), $body));
125  $response->send();
126  } catch (\Exception $e) {
127  $timestamp = time();
128 
129  $e->timestamp = $timestamp;
130 
131  $this->log(LogLevel::CRITICAL, $e);
132 
133  $msg = "Fatal error in exception handler. Check log for Exception at time $timestamp";
134 
135  $response = new Response($msg, ELGG_HTTP_INTERNAL_SERVER_ERROR, $headers);
136  $response->prepare($request);
137  $response->send();
138  }
139  }
140 }
Handler for uncaught exceptions.
static $_instance
Reference to the loaded Application.
Definition: Application.php:72
$response
Definition: content.php:10
$request
Definition: livesearch.php:12
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
$timestamp
Definition: date.php:34
Updates the basic settings for the primary site object.
trait Loggable
Enables adding a logger.
Definition: Loggable.php:14
elgg_view(string $view, array $vars=[], string $viewtype= '')
Return a parsed view.
Definition: views.php:156
elgg_view_page(string $title, string|array $body, string $page_shell= 'default', array $vars=[])
Assembles and outputs a full page.
Definition: views.php:235
static isCli()
Is application running in CLI.
Thrown when there is a major problem with the installation.
$body
Definition: useradd.php:55
log($level, $message, array $context=[])
Log a message.
Definition: Loggable.php:58
__invoke($exception)
Intercepts, logs, and displays uncaught exceptions.
const ELGG_HTTP_INTERNAL_SERVER_ERROR
Definition: constants.php:91
$exception
Definition: error.php:15
const ELGG_HTTP_TEMPORARY_REDIRECT
Definition: constants.php:62