6 * Contains header-related functions.
8 * @author Tim Duesterhus
9 * @copyright 2001-2019 WoltLab GmbH
10 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11 * @package WoltLabSuite\Core\Util
14 final class ExceptionLogUtil
{
16 * Splits the given string of Exceptions into an array.
18 * @param string $contents
21 public static function splitLog($contents) {
23 $contents = StringUtil
::unifyNewlines($contents);
26 $split = new Regex('(?:^|\n<<<<\n\n)(?:<<<<<<<<([a-f0-9]{40})<<<<\n|$)');
27 $contents = $split->split($contents, Regex
::SPLIT_NON_EMPTY_ONLY | Regex
::CAPTURE_SPLIT_DELIMITER
);
29 // even items become keys, odd items become values
30 return array_merge(...array_map(
32 return [$v[0] => $v[1]];
34 array_chunk($contents, 2)
39 * Parses the given log entry.
41 * @param string $entry
44 public static function parseException($entry) {
46 static $chainRegex = null;
47 if ($regex === null ||
$chainRegex === null) {
48 $regex = 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".
49 "Message: (?P<message>.*?)\s*\n".
50 "PHP version: (?P<phpVersion>.*?)\s*\n".
51 "WoltLab Suite version: (?P<wcfVersion>.*?)\s*\n".
52 "Request URI: (?P<requestURI>.*?)\s*\n".
53 "Referrer: (?P<referrer>.*?)\s*\n".
54 "User Agent: (?P<userAgent>.*?)\s*\n".
55 "Peak Memory Usage: (?<peakMemory>\d+)/(?<maxMemory>(?:\d+|-1))\s*\n".
57 ".*)", Regex
::DOT_ALL
);
58 $chainRegex = new Regex("======\n".
59 "Error Class: (?P<class>.*?)\s*\n".
60 "Error Message: (?P<message>.*?)\s*\n".
61 "Error Code: (?P<code>[a-zA-Z0-9]+)\s*\n".
62 "File: (?P<file>.*?) \((?P<line>\d+)\)\s*\n".
63 "Extra Information: (?P<information>(?:-|[a-zA-Z0-9+/]+={0,2}))\s*\n".
64 "Stack Trace: (?P<stack>\[[^\n]+\])", Regex
::DOT_ALL
);
67 if (!$regex->match($entry)) {
68 throw new \
InvalidArgumentException('The given entry is malformed');
70 $matches = $regex->getMatches();
71 $chainRegex->match($matches['chain'], true, Regex
::ORDER_MATCH_BY_SET
);
73 $chainMatches = array_map(function ($item) {
74 if ($item['information'] === '-') {
75 $item['information'] = null;
78 $item['information'] = unserialize(base64_decode($item['information']), ['allowed_classes' => false]);
81 $item['stack'] = JSON
::decode($item['stack']);
84 }, $chainRegex->getMatches());
86 $matches['date'] = strtotime($matches['date']);
87 $matches['chain'] = $chainMatches;
93 * Forbid creation of ExceptionLogUtil objects.
95 private function __construct() {