Files
crm.clientright.ru/catch_request_debug.php
Fedor 01c4fe80b5 chore: snapshot current working tree changes
Save all currently accumulated repository changes as a backup snapshot for Gitea so no local work is lost.
2026-03-26 14:19:01 +03:00

97 lines
3.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* Отладочный эндпоинт: перехватывает входящий запрос (заголовки + multipart/body)
* и сохраняет в logs/caught_requests/ для просмотра. Вызов: POST на этот скрипт
* вместо Debex/n8n, потом смотреть файлы в logs/caught_requests/.
*
* Использование: в GetUrl() или в n8n временно подставить URL этого скрипта,
* отправить запрос — в логах появится что реально ушло.
*/
$logDir = __DIR__ . '/logs/caught_requests';
if (!is_dir($logDir)) {
@mkdir($logDir, 0755, true);
}
$uploadsDir = $logDir . '/uploads';
if (!is_dir($uploadsDir)) {
@mkdir($uploadsDir, 0755, true);
}
$id = date('Y-m-d_H-i-s') . '_' . substr(bin2hex(random_bytes(4)), 0, 8);
// Заголовки
$headers = [];
if (function_exists('getallheaders')) {
$headers = getallheaders();
} else {
foreach ($_SERVER as $k => $v) {
if (strpos($k, 'HTTP_') === 0) {
$name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($k, 5)))));
$headers[$name] = $v;
}
}
}
// Тело: для multipart PHP уже разобрал в $_POST и $_FILES
$summary = [
'id' => $id,
'method' => $_SERVER['REQUEST_METHOD'] ?? '?',
'content_type' => $_SERVER['CONTENT_TYPE'] ?? null,
'content_length' => $_SERVER['CONTENT_LENGTH'] ?? null,
'headers' => $headers,
'post_keys' => array_keys($_POST),
'post_preview' => [],
'files' => [],
];
foreach ($_POST as $k => $v) {
$len = is_string($v) ? strlen($v) : strlen(json_encode($v));
$summary['post_preview'][$k] = [
'length' => $len,
'preview' => is_string($v) ? mb_substr($v, 0, 200) . ($len > 200 ? '...' : '') : '[array/object]',
];
}
foreach ($_FILES as $name => $file) {
$summary['files'][$name] = [
'name' => $file['name'] ?? '',
'type' => $file['type'] ?? '',
'size' => $file['size'] ?? 0,
'error' => $file['error'] ?? 0,
'tmp_name' => $file['tmp_name'] ?? '',
];
// копируем загруженный файл в нашу папку с читаемым именем
if (!empty($file['tmp_name']) && is_uploaded_file($file['tmp_name'])) {
$safeName = preg_replace('/[^a-zA-Z0-9._-]/', '_', $file['name'] ?: $name);
$dest = $uploadsDir . '/' . $id . '_' . $name . '_' . $safeName;
if (copy($file['tmp_name'], $dest)) {
$summary['files'][$name]['saved_to'] = $dest;
}
}
}
// Сохраняем отчёт
$reportFile = $logDir . '/' . $id . '_report.json';
file_put_contents($reportFile, json_encode($summary, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
// Отдельно сохраняем сырые заголовки и краткий текст (для быстрого просмотра)
$txt = $logDir . '/' . $id . '_headers.txt';
$lines = ["=== $id ===\n", "METHOD: " . $summary['method'], "CONTENT-TYPE: " . ($summary['content_type'] ?? ''), "CONTENT-LENGTH: " . ($summary['content_length'] ?? ''), "\n--- Headers ---\n"];
foreach ($headers as $k => $v) {
$lines[] = "$k: $v";
}
$lines[] = "\n--- POST keys ---\n" . implode(", ", $summary['post_keys']);
$lines[] = "\n--- FILES ---\n" . json_encode($summary['files'], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
file_put_contents($txt, implode("\n", $lines));
header('Content-Type: application/json; charset=utf-8');
echo json_encode([
'ok' => true,
'message' => 'Request caught for debug',
'id' => $id,
'report_file' => $reportFile,
'headers_file' => $txt,
'files_count' => count($_FILES),
'post_keys' => $summary['post_keys'],
], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);