ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilLoggingErrorFileStorage.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
25 {
26  protected const KEY_SPACE = 25;
27  protected const FILE_FORMAT = '.log';
28 
29  protected Inspector $inspector;
30  protected string $file_path;
31  protected string $file_name;
33  private array $exclusion_list = [];
34 
35  public function __construct(Inspector $inspector, string $file_path, string $file_name)
36  {
37  $this->inspector = $inspector;
38  $this->file_path = $file_path;
39  $this->file_name = $file_name;
40  }
41 
45  public function withExclusionList(array $exclusion_list): self
46  {
47  $clone = clone $this;
48  $clone->exclusion_list = $exclusion_list;
49  return $clone;
50  }
51 
52  private function stripNullBytes(string $ret): string
53  {
54  return str_replace("\0", '', $ret);
55  }
56 
57  protected function createDir(): void
58  {
59  if (!is_dir($this->file_path)) {
60  ilFileUtils::makeDirParents($this->file_path);
61  }
62  }
63 
64  protected function content(): string
65  {
66  return $this->pageHeader()
67  . $this->exceptionContent()
68  . $this->tablesContent();
69  }
70 
71  public function write(): void
72  {
73  $this->createDir();
74 
75  $file_name = $this->file_path . '/' . $this->file_name . self::FILE_FORMAT;
76  $stream = fopen($file_name, 'wb+');
77  fwrite($stream, $this->content());
78  fclose($stream);
79  chmod($file_name, 0755);
80  }
81 
82  protected function pageHeader(): string
83  {
84  return '';
85  }
86 
87  protected function exceptionContent(): string
88  {
89  $message = Formatter::formatExceptionPlain($this->inspector);
90 
91  $exception = $this->inspector->getException();
92  $previous = $exception->getPrevious();
93  while ($previous) {
94  $message .= "\n\nCaused by\n" . sprintf(
95  '%s: %s in file %s on line %d',
96  get_class($previous),
97  $previous->getMessage(),
98  $previous->getFile(),
99  $previous->getLine()
100  );
101  $previous = $previous->getPrevious();
102  }
103 
104  return $message;
105  }
106 
107  protected function tablesContent(): string
108  {
109  $ret = '';
110  foreach ($this->tables() as $title => $content) {
111  $ret .= "\n\n-- $title --\n\n";
112  if (count($content) > 0) {
113  foreach ($content as $key => $value) {
114  $key = str_pad((string) $key, self::KEY_SPACE);
115 
116  // indent multiline values, first print_r, split in lines,
117  // indent all but first line, then implode again.
118  $first = true;
119  $indentation = str_pad('', self::KEY_SPACE);
120  $value = implode("\n", array_map(static function ($line) use (&$first, $indentation): string {
121  if ($first) {
122  $first = false;
123  return $line;
124  }
125  return $indentation . $line;
126  }, explode("\n", print_r($value, true))));
127 
128  $ret .= "$key: $value\n";
129  }
130  } else {
131  $ret .= "empty\n";
132  }
133  }
134 
135  return $this->stripNullBytes($ret);
136  }
137 
138  protected function tables(): array
139  {
140  $post = $_POST;
141  $server = $_SERVER;
142 
143  $post = $this->hideSensitiveData($post);
146 
147  return [
148  'GET Data' => $_GET,
149  'POST Data' => $post,
150  'Files' => $_FILES,
151  'Cookies' => $_COOKIE,
152  'Session' => $_SESSION ?? [],
153  'Server/Request Data' => $server,
154  'Environment Variables' => $_ENV
155  ];
156  }
157 
162  private function hideSensitiveData(array $super_global): array
163  {
164  foreach ($this->exclusion_list as $parameter) {
165  if (isset($super_global[$parameter])) {
166  $super_global[$parameter] = 'REMOVED FOR SECURITY';
167  }
168 
169  if (isset($super_global['post_vars'][$parameter])) {
170  $super_global['post_vars'][$parameter] = 'REMOVED FOR SECURITY';
171  }
172  }
173 
174  return $super_global;
175  }
176 
177  private function shortenPHPSessionId(array $server): array
178  {
179  if (!isset($server['HTTP_COOKIE'])) {
180  return $server;
181  }
182  $cookie_content = $server['HTTP_COOKIE'];
183  $cookie_content = explode(';', $cookie_content);
184 
185  foreach ($cookie_content as $key => $content) {
186  $content_array = explode('=', $content);
187  if (trim($content_array[0]) === session_name()) {
188  $content_array[1] = substr($content_array[1], 0, 5) . ' (SHORTENED FOR SECURITY)';
189  $cookie_content[$key] = implode('=', $content_array);
190  }
191  }
192 
193  $server['HTTP_COOKIE'] = implode(';', $cookie_content);
194 
195  return $server;
196  }
197 }
$_GET["client_id"]
Definition: webdav.php:30
__construct(Inspector $inspector, string $file_path, string $file_name)
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
$_SERVER['HTTP_HOST']
Definition: raiseError.php:26
$message
Definition: xapiexit.php:31
$server
Definition: shib_login.php:28
$_COOKIE[session_name()]
Definition: xapitoken.php:54
$post
Definition: ltitoken.php:46