FusionDirectory
class_FatalError.inc
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4 
5  Copyright (C) 2019-2020 FusionDirectory
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21 
25 class FatalError extends Error
26 {
27  protected $htmlMessage;
28 
29  public function __construct (string $htmlMessage = '', int $code = 0, Throwable $previous = NULL)
30  {
31  $this->htmlMessage = $htmlMessage;
32  parent::__construct(htmlunescape(strip_tags($htmlMessage)), $code, $previous);
33  }
34 
35  public function getHtmlMessage ()
36  {
37  return $this->htmlMessage;
38  }
39 
40  public function toArray (): array
41  {
42  return [
43  'message' => $this->getMessage(),
44  'line' => $this->getLine(),
45  'file' => $this->getFile(),
46  'fatal' => TRUE,
47  ];
48  }
49 
50  public function display ()
51  {
52  restore_error_handler();
53  error_reporting(E_ALL);
54  echo $this->renderFatalErrorDialog();
55  }
56 
60  protected function renderFatalErrorDialog ()
61  {
62  global $config;
63 
64  $display =
65  '<!DOCTYPE html>
66  <html><head>
67  <title>'.htmlescape(_('FusionDirectory Fatal Error')).'</title>
68  </head><body>';
69 
70  $display .=
71  '<table style="width:100%; border:2px solid red;">
72  <tr>
73  <td style="vertical-align:top;padding:10px">
74  <img src="geticon.php?context=status&amp;icon=dialog-error&amp;size=32" alt="'.htmlescape(_('Error')).'"/>
75  </td>
76  <td style="width:100%">
77  <h3>'.htmlescape(_('Fatal Error')).'</h3>
78  '.$this->getHtmlMessage().'
79  </td>
80  </tr>
81  </table>';
82 
83  if (isset($config) && is_object($config) &&
84  $config->get_cfg_value('displayerrors') == 'TRUE') {
85  $trace = FusionDirectoryError::formatTrace($this);
86  $display .= print_a($trace, TRUE);
87  }
88 
89  $display .= '</body></html>';
90 
91  return $display;
92  }
93 }
htmlescape(string $str)
Escape string for HTML output.
Definition: php_setup.inc:32
Fatal error class. Does not extend FusionDirectoryError.
htmlunescape(string $html)
Unescape string for HTML output, reverse of htmlescape.
Definition: php_setup.inc:40
renderFatalErrorDialog()
Render fatal error screen.