diff --git a/crm/Core/ErrorReporter.php b/crm/Core/ErrorReporter.php index 8c00c7d..72f28ce 100644 --- a/crm/Core/ErrorReporter.php +++ b/crm/Core/ErrorReporter.php @@ -29,11 +29,10 @@ class ErrorReporter // Mostrar erro visual em desenvolvimento/localhost self::displayErrorIfDebug($exception, $context); - // Desativar temporariamente o sistema completo para evitar loops - // self::logError($exception, $context); - // self::captureWithErrorMonitoring($exception, $context); - // self::checkErrorThreshold($exception); - // self::notifyIfCritical($exception, $context); + // Hub ingest via canonical drop-in (BIT-25023); keep local path lightweight + if (class_exists(\BitReport\ErrorReporter::class)) { + \BitReport\ErrorReporter::reportThrowable($exception, $context); + } } catch (Throwable $e) { // Fallback para evitar loops infinitos error_log("ErrorReporter failed: " . $e->getMessage()); diff --git a/crm/config.env.example b/crm/config.env.example index d159aa7..310d298 100644 --- a/crm/config.env.example +++ b/crm/config.env.example @@ -99,4 +99,14 @@ COINMARKETCAP_API_KEY=your-coinmarketcap-api-key-here # DB_HOST=your-production-db-host # DB_NAME=your-production-db-name # DB_USER=your-production-db-user -# DB_PASS=your-production-db-password \ No newline at end of file +# DB_PASS=your-production-db-password + +# ============================================================================= +# ERROR HUB (errors.bitreport.pt) — vault only; DevOps prod tokens: BIT-25022 +# ============================================================================= +# ERROR_REPORTING_ENDPOINT=https://errors.bitreport.pt +# ERROR_REPORTING_TOKEN= +ERROR_REPORTING_PROJECT_NAME=MoneyFlix +ERROR_REPORTING_PROJECT_SLUG=moneyflix +ERROR_REPORTING_GITEA_REPO=BitReport.pt/MoneyFlix +# ERROR_REPORTING_DISABLED=1 \ No newline at end of file diff --git a/crm/index.php b/crm/index.php index 32450e9..02a5f0a 100644 --- a/crm/index.php +++ b/crm/index.php @@ -20,6 +20,13 @@ require_once __DIR__ . '/vendor/autoload.php'; use App\Core\EnvironmentLoader; EnvironmentLoader::load(); +// BitReport error hub (errors.bitreport.pt) +$mfHubReporter = __DIR__ . '/lib/BitReport/ErrorReporter.php'; +if (is_readable($mfHubReporter)) { + require_once $mfHubReporter; + BitReport\ErrorReporter::registerGlobalHandlers(); +} + // Load configuration require_once __DIR__ . '/config.php'; diff --git a/crm/lib/BitReport/ErrorReporter.php b/crm/lib/BitReport/ErrorReporter.php new file mode 100644 index 0000000..db6521d --- /dev/null +++ b/crm/lib/BitReport/ErrorReporter.php @@ -0,0 +1,222 @@ + 0 + ? "[fatal shutdown] {$file}:{$line}" + : '[fatal shutdown] (location unknown)'; + self::reportMessage( + (string) $err['message'], + 'critical', + $file, + $line, + $syn, + ['shutdown' => true, 'errno' => $err['type']] + ); + }); + + set_exception_handler(static function (\Throwable $e): void { + self::reportThrowable($e, ['uncaught' => true]); + error_log('Uncaught ' . $e::class . ': ' . $e->getMessage() . ' @ ' . $e->getFile() . ':' . $e->getLine()); + if (!headers_sent()) { + header('HTTP/1.1 500 Internal Server Error'); + } + echo 'Internal Server Error'; + exit(1); + }); + } + + public static function reportThrowable(\Throwable $e, array $context = []): void + { + $severity = ($e instanceof \Error) ? 'critical' : 'error'; + self::reportMessage( + $e->getMessage(), + $severity, + $e->getFile(), + $e->getLine(), + $e->getTraceAsString(), + array_merge(['error_type' => $e::class], $context) + ); + } + + /** + * @param array $context + */ + public static function reportMessage( + string $message, + string $level = 'error', + string $filePath = '', + int $lineNumber = 0, + string $stackTrace = '', + array $context = [] + ): void { + if (!self::isConfigured()) { + return; + } + + $ctx = array_merge([ + 'file_path' => $filePath, + 'line_number' => $lineNumber ?: null, + 'php_sapi' => PHP_SAPI, + 'request_uri' => $_SERVER['REQUEST_URI'] ?? null, + 'http_method' => $_SERVER['REQUEST_METHOD'] ?? null, + 'ip_address' => $_SERVER['REMOTE_ADDR'] ?? null, + 'project_name' => self::projectName() !== '' ? self::projectName() : null, + 'project_slug' => self::projectSlug() !== '' ? self::projectSlug() : null, + 'gitea_repo' => self::giteaRepo() !== '' ? self::giteaRepo() : null, + ], $context); + + // Remove null values + $ctx = array_filter($ctx, static fn($v) => $v !== null && $v !== ''); + + $event = [ + 'schema_version' => 1, + 'occurred_at' => gmdate('Y-m-d\TH:i:s.v\Z'), + 'level' => $level, + 'message' => $message, + ]; + if ($stackTrace !== '') { + $event['stacktrace'] = $stackTrace; + } + if (!empty($ctx)) { + $event['context'] = $ctx; + } + + $payload = json_encode(['events' => [$event]], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); + if ($payload === false) { + return; + } + + self::post($payload); + } + + private static function post(string $json): void + { + $url = self::endpoint(); + $token = self::token(); + $headers = [ + 'Content-Type: application/json', + 'Authorization: Bearer ' . $token, + ]; + + try { + if (function_exists('curl_init')) { + $ch = curl_init($url); + if ($ch === false) return; + curl_setopt_array($ch, [ + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => $json, + CURLOPT_HTTPHEADER => $headers, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_TIMEOUT => 4, + CURLOPT_CONNECTTIMEOUT => 2, + CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS, + ]); + @curl_exec($ch); + curl_close($ch); + return; + } + + $headerStr = implode("\r\n", $headers); + $ctx = stream_context_create([ + 'http' => [ + 'method' => 'POST', + 'header' => $headerStr . "\r\n", + 'content' => $json, + 'timeout' => 4, + 'ignore_errors' => true, + ], + ]); + @file_get_contents($url, false, $ctx); + } catch (\Throwable $e) { + error_log('BitReport\\ErrorReporter: send failed: ' . $e->getMessage()); + } + } +}