ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilLoggingErrorFileStorage.php
Go to the documentation of this file.
1 <?php
2 
21 
28 {
29  protected const KEY_SPACE = 25;
30  protected const FILE_FORMAT = ".log";
31 
32  protected Inspector $inspector;
33  protected string $file_path;
34  protected string $file_name;
36  private array $exclusion_list = [];
37 
38  public function __construct(Inspector $inspector, string $file_path, string $file_name)
39  {
40  $this->inspector = $inspector;
41  $this->file_path = $file_path;
42  $this->file_name = $file_name;
43  }
44 
48  public function withExclusionList(array $exclusion_list): self
49  {
50  $clone = clone $this;
51  $clone->exclusion_list = $exclusion_list;
52  return $clone;
53  }
54 
55  protected function createDir(string $path): void
56  {
57  if (!is_dir($this->file_path)) {
58  ilFileUtils::makeDirParents($this->file_path);
59  }
60  }
61 
62  protected function content(): string
63  {
64  return $this->pageHeader()
65  . $this->exceptionContent()
66  . $this->tablesContent()
67  ;
68  }
69 
70  public function write(): void
71  {
72  $this->createDir($this->file_path);
73 
74  $file_name = $this->file_path . "/" . $this->file_name . self::FILE_FORMAT;
75  $stream = fopen($file_name, 'w+');
76  fwrite($stream, $this->content());
77  fclose($stream);
78  chmod($file_name, 0755);
79  }
80 
81  protected function pageHeader(): string
82  {
83  return "";
84  }
85 
89  protected function exceptionContent(): string
90  {
91  return Formatter::formatExceptionPlain($this->inspector);
92  }
93 
97  protected function tablesContent(): string
98  {
99  $ret = "";
100  foreach ($this->tables() as $title => $content) {
101  $ret .= "\n\n-- $title --\n\n";
102  if (count($content) > 0) {
103  foreach ($content as $key => $value) {
104  $key = str_pad((string) $key, self::KEY_SPACE);
105 
106  // indent multiline values, first print_r, split in lines,
107  // indent all but first line, then implode again.
108  $first = true;
109  $indentation = str_pad("", self::KEY_SPACE);
110  $value = implode("\n", array_map(function ($line) use (&$first, $indentation) {
111  if ($first) {
112  $first = false;
113  return $line;
114  }
115  return $indentation . $line;
116  }, explode("\n", print_r($value, true))));
117 
118  $ret .= "$key: $value\n";
119  }
120  } else {
121  $ret .= "empty\n";
122  }
123  }
124  return $ret;
125  }
126 
130  protected function tables(): array
131  {
132  $post = $_POST;
133  $server = $_SERVER;
134 
135  $post = $this->hideSensitiveData($post);
138 
139  return array( "GET Data" => $_GET
140  , "POST Data" => $post
141  , "Files" => $_FILES
142  , "Cookies" => $_COOKIE
143  , "Session" => $_SESSION ?? array()
144  , "Server/Request Data" => $server
145  , "Environment Variables" => $_ENV
146  );
147  }
148 
153  private function hideSensitiveData(array $super_global): array
154  {
155  foreach ($this->exclusion_list as $parameter) {
156  if (isset($super_global[$parameter])) {
157  $super_global[$parameter] = 'REMOVED FOR SECURITY';
158  }
159 
160  if (isset($super_global['post_vars'][$parameter])) {
161  $super_global['post_vars'][$parameter] = 'REMOVED FOR SECURITY';
162  }
163  }
164 
165  return $super_global;
166  }
167 
171  private function shortenPHPSessionId(array $server): array
172  {
173  if (!isset($server["HTTP_COOKIE"])) {
174  return $server;
175  }
176  $cookie_content = $server["HTTP_COOKIE"];
177  $cookie_content = explode(";", $cookie_content);
178 
179  foreach ($cookie_content as $key => $content) {
180  $content_array = explode("=", $content);
181  if (trim($content_array[0]) == session_name()) {
182  $content_array[1] = substr($content_array[1], 0, 5) . " (SHORTENED FOR SECURITY)";
183  $cookie_content[$key] = implode("=", $content_array);
184  }
185  }
186 
187  $server["HTTP_COOKIE"] = implode(";", $cookie_content);
188 
189  return $server;
190  }
191 }
__construct(Inspector $inspector, string $file_path, string $file_name)
Saves error informations into file.
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
$path
Definition: ltiservices.php:32
shortenPHPSessionId(array $server)
Shorts the php session id.
$_GET['client_id']
Definition: saml1-acs.php:21
tablesContent()
Get the header for the page.
$_SERVER['HTTP_HOST']
Definition: raiseError.php:10
exceptionContent()
Get a short info about the exception.
string $key
Consumer key/client ID value.
Definition: System.php:193
$server
tables()
Get the tables that should be rendered.
$_COOKIE[session_name()]
Definition: xapitoken.php:54
$post
Definition: ltitoken.php:49