misuzu/src/WhoopsReporter.php

103 lines
2.9 KiB
PHP
Raw Normal View History

<?php
namespace Misuzu;
use Whoops\Exception\Formatter;
use Whoops\Handler\Handler;
2019-06-10 19:04:53 +02:00
final class WhoopsReporter extends Handler {
private $reportUrl;
private $reportSign;
2019-06-10 19:04:53 +02:00
public function __construct(?string $reportUrl = null, ?string $reportSign = null) {
$this->setReportInfo($reportUrl, $reportSign);
}
2019-06-10 19:04:53 +02:00
public function setReportInfo(?string $reportUrl = null, ?string $reportSign = null): void {
$this->reportUrl = $reportUrl;
$this->reportSign = $reportSign;
}
2019-06-10 19:04:53 +02:00
public function handle() {
echo $this->html(
$this->report()
? 'Information about this error has been sent to the devs.'
: 'Report what you were trying to a developer.'
);
return Handler::QUIT;
}
2019-06-10 19:04:53 +02:00
public function contentType(): string {
return 'text/html';
}
2019-06-10 19:04:53 +02:00
public function html(string $text): string {
return <<<HTML
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Something broke!</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
2018-09-23 16:53:00 +02:00
<link href="/css/style.css" rel="stylesheet">
</head>
<body class="main" style="justify-content: center; --site-max-width: 400px">
<div class="main__wrapper" style="flex: 0 0 auto">
<div class="container">
2018-10-22 21:53:21 +02:00
<div class="container__title">
<div class="container__title__background"></div>
<div class="container__title__text"><i class="fas fa-bug fa-fw"></i> Something broke!</div>
2018-10-22 21:53:21 +02:00
</div>
<div class="container__content">
<p>{$text}</p>
</div>
</div>
</div>
</body>
</html>
HTML;
}
2019-06-10 19:04:53 +02:00
private function report(): bool {
if(!mb_strlen($this->reportUrl) || !($curl = curl_init($this->reportUrl))) {
return false;
}
$json = json_encode([
'git' => [
'tag' => git_tag(),
'hash' => git_commit_hash(true),
],
'misuzu' => [
'trace_txt' => $this->getException()->getTraceAsString(),
'directory' => MSZ_ROOT,
],
'exception' => Formatter::formatExceptionAsDataArray(
$this->getInspector(),
$this
),
]);
$headers = [
'Content-Type: application/json;charset=utf-8',
];
2019-06-10 19:04:53 +02:00
if(mb_strlen($this->reportSign)) {
$headers[] = 'X-Misuzu-Signature: sha256=' . hash_hmac('sha256', $json, $this->reportSign);
}
$setOpts = curl_setopt_array($curl, [
CURLOPT_TCP_NODELAY => true,
CURLOPT_POSTFIELDS => $json,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_RETURNTRANSFER => false,
]);
2019-06-10 19:04:53 +02:00
if(!$setOpts) {
return false;
}
return curl_exec($curl) !== false;
}
}