ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables 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->getExceptionOutput() . $this->tablesContent() . "\n";
53  }
54 
58  protected function getExceptionOutput(): string
59  {
60  return Formatter::formatExceptionPlain($this->getInspector());
61  }
62 
66  protected function tablesContent(): string
67  {
68  $ret = '';
69  foreach ($this->tables() as $title => $content) {
70  $ret .= "\n\n-- $title --\n\n";
71  if (count($content) > 0) {
72  foreach ($content as $key => $value) {
73  $key = str_pad((string) $key, self::KEY_SPACE);
74 
75  // indent multiline values, first print_r, split in lines,
76  // indent all but first line, then implode again.
77  $first = true;
78  $indentation = str_pad('', self::KEY_SPACE);
79  $value = implode(
80  "\n",
81  array_map(
82  static function ($line) use (&$first, $indentation): string {
83  if ($first) {
84  $first = false;
85  return $line;
86  }
87  return $indentation . $line;
88  },
89  explode("\n", print_r($value, true))
90  )
91  );
92 
93  $ret .= "$key: $value\n";
94  }
95  } else {
96  $ret .= "empty\n";
97  }
98  }
99 
100  return $this->stripNullBytes($ret);
101  }
102 
106  protected function tables(): array
107  {
108  $post = $_POST;
109  $server = $_SERVER;
110 
111  $post = $this->hideSensitiveData($post);
114 
115  return [
116  'GET Data' => $_GET,
117  'POST Data' => $post,
118  'Files' => $_FILES,
119  'Cookies' => $_COOKIE,
120  'Session' => $_SESSION ?? [],
121  'Server/Request Data' => $server,
122  'Environment Variables' => $_ENV,
123  ];
124  }
125 
130  private function hideSensitiveData(array $super_global): array
131  {
132  foreach ($this->exclusion_list as $parameter) {
133  if (isset($super_global[$parameter])) {
134  $super_global[$parameter] = 'REMOVED FOR SECURITY';
135  }
136 
137  if (isset($super_global['post_vars'][$parameter])) {
138  $super_global['post_vars'][$parameter] = 'REMOVED FOR SECURITY';
139  }
140  }
141 
142  return $super_global;
143  }
144 
149  private function shortenPHPSessionId(array $server): array
150  {
151  $cookie_content = $server['HTTP_COOKIE'];
152  $cookie_content = explode(';', $cookie_content);
153 
154  foreach ($cookie_content as $key => $content) {
155  $content_array = explode('=', $content);
156  if (trim($content_array[0]) === session_name()) {
157  $content_array[1] = substr($content_array[1], 0, 5) . ' (SHORTENED FOR SECURITY)';
158  $cookie_content[$key] = implode('=', $content_array);
159  }
160  }
161 
162  $server['HTTP_COOKIE'] = implode(';', $cookie_content);
163 
164  return $server;
165  }
166 }
withExclusionList(array $exclusion_list)
tablesContent()
Get the header for the page.
hideSensitiveData(array $super_global)
$_GET['client_id']
Definition: saml1-acs.php:21
$_SERVER['HTTP_HOST']
Definition: raiseError.php:10
string $key
Consumer key/client ID value.
Definition: System.php:193
tables()
Get the tables that should be rendered.
A Whoops error handler that prints the same content as the PrettyPageHandler but as plain text...
$server
getExceptionOutput()
Get a short info about the exception.
$_COOKIE[session_name()]
Definition: xapitoken.php:54
$post
Definition: ltitoken.php:49