ILIAS  trunk Revision v11.0_alpha-1753-gb21ca8c4367
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilPlainTextHandler.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
28 class ilPlainTextHandler extends \Whoops\Handler\PlainTextHandler
29 {
30  protected const KEY_SPACE = 25;
31 
33  private array $exclusion_list = [];
34 
38  public function withExclusionList(array $exclusion_list): self
39  {
40  $clone = clone $this;
41  $clone->exclusion_list = $exclusion_list;
42  return $clone;
43  }
44 
45  private function stripNullBytes(string $ret): string
46  {
47  return str_replace("\0", '', $ret);
48  }
49 
50  public function generateResponse(): string
51  {
52  return $this->getPlainTextExceptionOutput() . $this->tablesContent() . "\n";
53  }
54 
55  protected function getSimpleExceptionOutput(Throwable $exception): string
56  {
57  return sprintf(
58  '%s: %s in file %s on line %d',
59  get_class($exception),
60  $exception->getMessage(),
61  $exception->getFile(),
62  $exception->getLine()
63  );
64  }
65 
69  protected function getPlainTextExceptionOutput(bool $with_previous = true): string
70  {
71  $message = Formatter::formatExceptionPlain($this->getInspector());
72 
73  if ($with_previous) {
74  $exception = $this->getInspector()->getException();
75  $previous = $exception->getPrevious();
76  while ($previous) {
77  $message .= "\n\nCaused by\n" . $this->getSimpleExceptionOutput($previous);
78  $previous = $previous->getPrevious();
79  }
80  }
81 
82  return $message;
83  }
84 
88  protected function tablesContent(): string
89  {
90  $ret = '';
91  foreach ($this->tables() as $title => $content) {
92  $ret .= "\n\n-- $title --\n\n";
93  if (count($content) > 0) {
94  foreach ($content as $key => $value) {
95  $key = str_pad((string) $key, self::KEY_SPACE);
96 
97  // indent multiline values, first print_r, split in lines,
98  // indent all but first line, then implode again.
99  $first = true;
100  $indentation = str_pad('', self::KEY_SPACE);
101  $value = implode(
102  "\n",
103  array_map(
104  static function ($line) use (&$first, $indentation): string {
105  if ($first) {
106  $first = false;
107  return $line;
108  }
109  return $indentation . $line;
110  },
111  explode("\n", print_r($value, true))
112  )
113  );
114 
115  $ret .= "$key: $value\n";
116  }
117  } else {
118  $ret .= "empty\n";
119  }
120  }
121 
122  return $this->stripNullBytes($ret);
123  }
124 
128  protected function tables(): array
129  {
130  $post = $_POST;
131  $server = $_SERVER;
132 
133  $post = $this->hideSensitiveData($post);
136 
137  return [
138  'GET Data' => $_GET,
139  'POST Data' => $post,
140  'Files' => $_FILES,
141  'Cookies' => $_COOKIE,
142  'Session' => $_SESSION ?? [],
143  'Server/Request Data' => $server,
144  'Environment Variables' => $_ENV,
145  ];
146  }
147 
152  private function hideSensitiveData(array $super_global): array
153  {
154  foreach ($this->exclusion_list as $parameter) {
155  if (isset($super_global[$parameter])) {
156  $super_global[$parameter] = 'REMOVED FOR SECURITY';
157  }
158 
159  if (isset($super_global['post_vars'][$parameter])) {
160  $super_global['post_vars'][$parameter] = 'REMOVED FOR SECURITY';
161  }
162  }
163 
164  return $super_global;
165  }
166 
171  private function shortenPHPSessionId(array $server): array
172  {
173  $cookie_content = $server['HTTP_COOKIE'];
174  $cookie_content = explode(';', $cookie_content);
175 
176  foreach ($cookie_content as $key => $content) {
177  $content_array = explode('=', $content);
178  if (trim($content_array[0]) === session_name()) {
179  $content_array[1] = substr($content_array[1], 0, 5) . ' (SHORTENED FOR SECURITY)';
180  $cookie_content[$key] = implode('=', $content_array);
181  }
182  }
183 
184  $server['HTTP_COOKIE'] = implode(';', $cookie_content);
185 
186  return $server;
187  }
188 }
getSimpleExceptionOutput(Throwable $exception)
$_GET["client_id"]
Definition: webdav.php:30
withExclusionList(array $exclusion_list)
tablesContent()
Get the header for the page.
hideSensitiveData(array $super_global)
$_SERVER['HTTP_HOST']
Definition: raiseError.php:26
tables()
Get the tables that should be rendered.
A Whoops error handler that prints the same content as the PrettyPageHandler but as plain text...
getPlainTextExceptionOutput(bool $with_previous=true)
Get a short info about the exception.
$message
Definition: xapiexit.php:31
$server
Definition: shib_login.php:24
$_COOKIE[session_name()]
Definition: xapitoken.php:54
$post
Definition: ltitoken.php:46