ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilLoggingErrorFileStorage.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 2016 Stefan Hecken, Extended GPL, see docs/LICENSE */
3require_once './libs/composer/vendor/autoload.php';
4
5use Whoops\Exception\Formatter;
6
13{
14 const KEY_SPACE = 25;
15 const FILE_FORMAT = ".log";
16
18 private $exclusion_list = [];
19
20 public function __construct($inspector, $file_path, $file_name)
21 {
22 $this->inspector = $inspector;
23 $this->file_path = $file_path;
24 $this->file_name = $file_name;
25 }
26
30 public function withExclusionList(array $exclusion_list) : self
31 {
32 $clone = clone $this;
33 $clone->exclusion_list = $exclusion_list;
34 return $clone;
35 }
36
37 protected function createDir($path)
38 {
39 if (!is_dir($this->file_path)) {
40 ilUtil::makeDirParents($this->file_path);
41 }
42 }
43
44 protected function content()
45 {
46 return $this->pageHeader()
47 . $this->exceptionContent()
48 . $this->tablesContent()
49 ;
50 }
51
52 public function write()
53 {
54 $this->createDir($this->file_path);
55
56 $file_name = $this->file_path . "/" . $this->file_name . self::FILE_FORMAT;
57 $stream = fopen($file_name, 'w+');
58 fwrite($stream, $this->content());
59 fclose($stream);
60 chmod($file_name, 0755);
61 }
62
68 protected function pageHeader()
69 {
70 return "";
71 }
72
78 protected function exceptionContent()
79 {
80 return Formatter::formatExceptionPlain($this->inspector);
81 }
82
88 protected function tablesContent()
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($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("\n", array_map(function ($line) use (&$first, $indentation) {
102 if ($first) {
103 $first = false;
104 return $line;
105 }
106 return $indentation . $line;
107 }, explode("\n", print_r($value, true))));
108
109 $ret .= "$key: $value\n";
110 }
111 } else {
112 $ret .= "empty\n";
113 }
114 }
115 return $ret;
116 }
117
123 protected function tables()
124 {
125 $post = $_POST;
127
128 $post = $this->hideSensitiveData($post);
131
132 return array( "GET Data" => $_GET
133 , "POST Data" => $post
134 , "Files" => $_FILES
135 , "Cookies" => $_COOKIE
136 , "Session" => isset($_SESSION) ? $_SESSION : array()
137 , "Server/Request Data" => $server
138 , "Environment Variables" => $_ENV
139 );
140 }
141
146 private function hideSensitiveData(array $super_global) : array
147 {
148 foreach ($this->exclusion_list as $parameter) {
149 if (isset($super_global[$parameter])) {
150 $super_global[$parameter] = 'REMOVED FOR SECURITY';
151 }
152
153 if (isset($super_global['post_vars'][$parameter])) {
154 $super_global['post_vars'][$parameter] = 'REMOVED FOR SECURITY';
155 }
156 }
157
158 return $super_global;
159 }
160
168 private function shortenPHPSessionId(array $server)
169 {
170 $cookie_content = $server["HTTP_COOKIE"];
171 $cookie_content = explode(";", $cookie_content);
172
173 foreach ($cookie_content as $key => $content) {
174 $content_array = explode("=", $content);
175 if (trim($content_array[0]) == session_name()) {
176 $content_array[1] = substr($content_array[1], 0, 5) . " (SHORTENED FOR SECURITY)";
177 $cookie_content[$key] = implode("=", $content_array);
178 }
179 }
180
181 $server["HTTP_COOKIE"] = implode(";", $cookie_content);
182
183 return $server;
184 }
185}
$_GET["client_id"]
$_POST["username"]
$_SESSION["AccountId"]
An exception for terminatinating execution or to throw for unit testing.
Saves error informations into file.
__construct($inspector, $file_path, $file_name)
pageHeader()
Get the header for the page.
exceptionContent()
Get a short info about the exception.
tablesContent()
Get the header for the page.
tables()
Get the tables that should be rendered.
shortenPHPSessionId(array $server)
Shorts the php session id.
static makeDirParents($a_dir)
Create a new directory and all parent directories.
$server
$ret
Definition: parser.php:6
$_SERVER['HTTP_HOST']
Definition: raiseError.php:10
$_COOKIE[session_name()]
Definition: xapitoken.php:37