PluginProbe
404 Solution / 4.3.0
404 Solution v4.3.0
4.3.5 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.2.0 4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.1.7 4.1.6 4.1.5 4.1.4 4.1.3 trunk 2.30.0 All 109 releases
404-solution / includes / services / ErrorDiagnosticsReporter.php

ErrorDiagnosticsReporter.php in 404 Solution 4.3.0, at includes/services/ErrorDiagnosticsReporter.php

64 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) {
4 exit;
5 }
6
7 /**
8 * Formats and persists fatal-error diagnostics without leaking sensitive SQL values.
9 */
10 class ABJ_404_Solution_ErrorDiagnosticsReporter {
11
12 /**
13 * @param mixed $value
14 * @return string
15 */
16 public function safeJsonEncode($value): string {
17 $encoded = json_encode($value, JSON_PARTIAL_OUTPUT_ON_ERROR);
18 if ($encoded === false) {
19 return '(json_encode failed) ' . print_r($value, true);
20 }
21 return $encoded;
22 }
23
24 /**
25 * @param mixed $sql
26 * @return string
27 */
28 public function redactSqlShape($sql): string {
29 if (!is_string($sql) || $sql === '') {
30 return '';
31 }
32
33 $out = $sql;
34 $out = preg_replace("~'(?:\\\\'|''|[^'])*'~", "?", $out) ?? $out;
35 $out = preg_replace('~"(?:\\\\"|""|[^"])*"~', "?", $out) ?? $out;
36 $out = preg_replace('~\\b0x[0-9A-Fa-f]+\\b~', '?', $out) ?? $out;
37 $out = preg_replace('~\\b\\d+(?:\\.\\d+)?\\b~', '?', $out) ?? $out;
38 $out = preg_replace('~\\(\\s*\\?\\s*(?:,\\s*\\?\\s*)+\\)~', '(?)', $out) ?? $out;
39 $out = preg_replace('~\\bIN\\s*\\(\\?\\)\\b~i', 'IN (?)', $out) ?? $out;
40 $out = preg_replace('~\\s+~', ' ', trim($out)) ?? $out;
41 if (strlen($out) > 4000) {
42 $out = substr($out, 0, 4000) . '...';
43 }
44 return $out;
45 }
46
47 /**
48 * @param string $line
49 * @return bool
50 */
51 public function writeLine(string $line): bool {
52 $logger = abj_service('logging');
53 if (is_object($logger) && method_exists($logger, 'writeLineToDebugFile')) {
54 $logger->writeLineToDebugFile($line);
55 return true;
56 }
57 if (is_object($logger) && method_exists($logger, 'sanitizeLogLine')) {
58 $line = $logger->sanitizeLogLine($line);
59 }
60 @file_put_contents(ABJ404_PATH . 'abj404_debug_fallback.txt', $line . "\n", FILE_APPEND);
61 return false;
62 }
63 }
64