2 namespace wcf\acp\page
;
3 use wcf\page\AbstractPage
;
4 use wcf\page\MultipleLinkPage
;
5 use wcf\system\event\EventHandler
;
6 use wcf\system\exception\IllegalLinkException
;
9 use wcf\util\DirectoryUtil
;
11 use wcf\util\StringUtil
;
14 * Shows the exception log.
16 * @author Tim Duesterhus
17 * @copyright 2001-2018 WoltLab GmbH
18 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
19 * @package WoltLabSuite\Core\Acp\Page
21 class ExceptionLogViewPage
extends MultipleLinkPage
{
25 public $activeMenuItem = 'wcf.acp.menu.link.log.exception';
30 public $neededPermissions = ['admin.management.canViewLog'];
35 public $itemsPerPage = 10;
41 public $exceptionID = '';
53 public $logFiles = [];
59 public $exceptions = [];
64 public function readParameters() {
65 parent
::readParameters();
67 if (isset($_REQUEST['exceptionID'])) $this->exceptionID
= StringUtil
::trim($_REQUEST['exceptionID']);
68 if (isset($_REQUEST['logFile'])) $this->logFile
= StringUtil
::trim($_REQUEST['logFile']);
74 public function readData() {
75 AbstractPage
::readData();
77 $fileNameRegex = new Regex('(?:^|/)\d{4}-\d{2}-\d{2}\.txt$');
78 $this->logFiles
= DirectoryUtil
::getInstance(WCF_DIR
.'log/')->getFiles(SORT_DESC
, $fileNameRegex);
80 if ($this->exceptionID
) {
81 // search the appropriate file
82 foreach ($this->logFiles
as $logFile) {
83 $contents = file_get_contents($logFile);
85 if (mb_strpos($contents, '<<<<<<<<'.$this->exceptionID
.'<<<<') !== false) {
86 $fileNameRegex->match($logFile);
87 $matches = $fileNameRegex->getMatches();
88 $this->logFile
= $matches[0];
95 if (!isset($contents)) {
100 else if ($this->logFile
) {
101 if (!$fileNameRegex->match(basename($this->logFile
))) throw new IllegalLinkException();
102 if (!file_exists(WCF_DIR
.'log/'.$this->logFile
)) throw new IllegalLinkException();
104 $contents = file_get_contents(WCF_DIR
.'log/'.$this->logFile
);
111 $contents = StringUtil
::unifyNewlines($contents);
114 $split = new Regex('(?:^|\n<<<<\n\n)(?:<<<<<<<<([a-f0-9]{40})<<<<\n|$)');
115 $contents = $split->split($contents, Regex
::SPLIT_NON_EMPTY_ONLY | Regex
::CAPTURE_SPLIT_DELIMITER
);
117 // even items become keys, odd items become values
119 $this->exceptions
= call_user_func_array('array_merge', array_map(
121 return [$v[0] => $v[1]];
123 array_chunk($contents, 2)
126 catch (\Exception
$e) {
127 // logfile contents are pretty malformed, abort
131 if ($this->exceptionID
) $this->searchPage($this->exceptionID
);
132 $this->calculateNumberOfPages();
135 $exceptionRegex = new Regex("(?P<date>[MTWFS][a-z]{2}, \d{1,2} [JFMASOND][a-z]{2} \d{4} \d{2}:\d{2}:\d{2} [+-]\d{4})\s*\n".
136 "Message: (?P<message>.*?)\s*\n".
137 "PHP version: (?P<phpVersion>.*?)\s*\n".
138 "WoltLab Suite version: (?P<wcfVersion>.*?)\s*\n".
139 "Request URI: (?P<requestURI>.*?)\s*\n".
140 "Referrer: (?P<referrer>.*?)\s*\n".
141 "User Agent: (?P<userAgent>.*?)\s*\n".
142 "Peak Memory Usage: (?<peakMemory>\d+)/(?<maxMemory>\d+)\s*\n".
144 ".*)", Regex
::DOT_ALL
);
145 $chainRegex = new Regex("======\n".
146 "Error Class: (?P<class>.*?)\s*\n".
147 "Error Message: (?P<message>.*?)\s*\n".
148 "Error Code: (?P<code>\d+)\s*\n".
149 "File: (?P<file>.*?) \((?P<line>\d+)\)\s*\n".
150 "Extra Information: (?P<information>(?:-|[a-zA-Z0-9+/]+={0,2}))\s*\n".
151 "Stack Trace: (?P<stack>\[[^\n]+\])", Regex
::DOT_ALL
);
153 $isPhp7 = version_compare(PHP_VERSION
, '7.0.0') >= 0;
154 foreach ($this->exceptions
as $key => $val) {
156 if ($i < $this->startIndex ||
$i > $this->endIndex
) {
157 unset($this->exceptions
[$key]);
161 if (!$exceptionRegex->match($val)) {
162 unset($this->exceptions
[$key]);
165 $matches = $exceptionRegex->getMatches();
166 $chainRegex->match($matches['chain'], true, Regex
::ORDER_MATCH_BY_SET
);
168 $chainMatches = array_map(function ($item) use ($isPhp7) {
169 if ($item['information'] === '-') $item['information'] = null;
172 $item['information'] = unserialize(base64_decode($item['information']), ['allowed_classes' => false]);
175 $item['information'] = unserialize(base64_decode($item['information']));
179 $item['stack'] = JSON
::decode($item['stack']);
182 }, $chainRegex->getMatches());
184 $matches['chain'] = $chainMatches;
185 $this->exceptions
[$key] = $matches;
192 public function countItems() {
193 // call countItems event
194 EventHandler
::getInstance()->fireAction($this, 'countItems');
196 return count($this->exceptions
);
200 * Switches to the page containing the exception with the given ID.
202 * @param string $exceptionID
204 public function searchPage($exceptionID) {
207 foreach ($this->exceptions
as $key => $val) {
208 if ($key == $exceptionID) break;
212 $this->pageNo
= ceil($i / $this->itemsPerPage
);
218 public function assignVariables() {
219 parent
::assignVariables();
221 WCF
::getTPL()->assign([
222 'exceptionID' => $this->exceptionID
,
223 'logFiles' => array_flip(array_map('basename', $this->logFiles
)),
224 'logFile' => $this->logFile
,
225 'exceptions' => $this->exceptions