| 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 |
|