- First commit of Negundo

This commit is contained in:
mcndave committed 2023-03-29 13:52:15 +00:00
commit 69ed176b1c
18 files changed
+846

No files matched your search

+69
View File
@@ -0,0 +1,69 @@
<?php
namespace Negundo\Client\Util;
use Negundo\Client\DataInterface;
class DumpHandler {
public /*DataInterface*/ $dataManipulator;
public function __construct(DataInterface $dataManipulator = null)
{
$this->dataManipulator = $dataManipulator;
}
public function dumpData(...$content) : array
{
$backtrace = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS);
array_shift($backtrace);
array_shift($backtrace);
$trace = $backtrace[0] ?? [
"line" => -1,
"file" => "unknown",
];
$message = ( new SourceCodeFormatter() )->generateFromFile($trace['file'] === "unknown" ? "" : $trace['file'], $trace['line'] - 1, 1);
$post = [
'file' => $trace['file'],
'line' => $trace['line'],
'title' => substr(strip_tags(trim(str_replace(['&nbsp;'], [''], $message))), 0, 255),
'url' => ( ( 'on' === $_SERVER['HTTPS'] ?? false ) ? 'https' : 'http' ) . '://' . ( $_SERVER['HTTP_HOST'] ?? "") . ( $_SERVER["REQUEST_URI"] ?? "" ),
'backtrace' => json_encode($backtrace),
'source' => ( new SourceCodeFormatter() )->generateFromFile($trace['file'] === "unknown" ? "" : $trace['file'], $trace['line']),
'data' => [
'content' => $this->extractDumpContent(...$content),
'sent_at' => date('Y-m-d H:i:s'),
'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? null,
'request_body' => json_encode($_POST),
'remote_addr' => $_SERVER['REMOTE_ADDR'] ?? null,
],
];
if ( $this->dataManipulator ?? false ) {
$post['data'] += $this->dataManipulator->getData();
$this->dataManipulator->run($post);
}
$post['data'] = json_encode($post['data'] ?? null);
return $post;
}
protected function extractDumpContent(...$content) : string
{
ob_start();
var_dump( ...( $content !== [] ? $content : [null] ) );
return ob_get_clean();
}
public function hash(array $content) : string
{
return md5(implode('-', [ $content['file'], $content['line'], $content['title'] ]));
}
}
+61
View File
@@ -0,0 +1,61 @@
<?php
namespace Negundo\Client\Util;
use Negundo\Client\DataInterface;
class ExceptionHandler {
public /*DataInterface*/ $dataManipulator;
public function __construct(DataInterface $dataManipulator = null)
{
$this->dataManipulator = $dataManipulator;
}
public function extractExceptionData(\Throwable $ex, array $serverData, array $postData, $dataManipulator = null) : array
{
$serverData = [
'HTTPS' => $serverData['HTTPS'] ?? false,
'HTTP_HOST' => $serverData['HTTP_HOST'] ?? "",
'REQUEST_URI' => $serverData['REQUEST_URI'] ?? "",
'HTTP_USER_AGENT' => $serverData['HTTP_USER_AGENT'] ?? null,
'REMOTE_ADDR' => $serverData['REMOTE_ADDR'] ?? null,
];
$post = [
'code' => $ex->getCode(),
'file' => $ex->getFile(),
'line' => $ex->getLine(),
'message' => $ex->getMessage(),
'url' => ( ( 'on' === $serverData['HTTPS'] ) ? 'https' : 'http' ) . '://' . $serverData['HTTP_HOST'] . $serverData["REQUEST_URI"],
'backtrace' => json_encode($ex->getTrace()),
'backtrace_string' => $ex->getTraceAsString(),
'source' => ( new SourceCodeFormatter() )->generateFromException($ex),
'hits' => [
'user_agent' => $serverData['HTTP_USER_AGENT'] ?? "???",
'sent_at' => date('Y-m-d H:i:s'),
'request_body_vars' => array_keys($postData),
'remote_addr' => $serverData['REMOTE_ADDR'],
],
'data' => [],
];
if ( $dataManipulator ) {
$post['data'] = $dataManipulator->getData();
$post['hits'] = array_merge($post['hits'], $dataManipulator->getHits());
$dataManipulator->run($post);
}
$post['data'] = json_encode($post['data'] ?? null);
$post['hits'] = json_encode($post['hits'] ?? null);
return $post;
}
public function hash(\Throwable $ex) : string
{
return md5(implode('-', [ $ex->getMessage(), $ex->getFile(), $ex->getLine(), $ex->getCode() ]));
}
}
+65
View File
@@ -0,0 +1,65 @@
<?php
namespace Negundo\Client\Util;
class SourceCodeFormatter {
public function generateFromException(\Throwable $ex, int $lineCount = 12) : string
{
return $this->generateFromFile($ex->getFile(), $ex->getLine(), $lineCount);
}
public function generateFromFile(string $filePath, int $sourceLine, int $lineCount = 12) : string
{
if ( empty( $filePath ) ){
return "";
}
ini_set('highlight.default', "#0072bc");
ini_set('highlight.string' , "#bc0000");
ini_set('highlight.comment', "#b69832");
$lc2 = $lineCount / 2;
$seek = $sourceLine - (int) floor($sourceLine >= $lc2 ? $lc2 : $sourceLine);
$fileSpl = new \SplFileObject($filePath);
$fileSpl->seek($seek);
$html = "";
$err = error_reporting();
error_reporting(0);
for($line = $seek; $line < $seek + $lineCount; $line++) {
try {
# Highlighting code
$content = highlight_string("<?php ".$fileSpl->current(), true);
}
catch(\Throwable $e) {
$content = $fileSpl->current();
}
# Get code between <code></code> tags
$content = substr($content, strpos($content, "<code>") + 6, strrpos($content, "</code>") - 6);
# Removing the added <?php tag
$content = str_replace("&lt;?php", "", $content);
$html .= "<div data-line='" . ($line + 1) . "' class='source-line'>" . $content . "</div>";
$fileSpl->next();
if ( $fileSpl->eof() ) {
$html .= "<div class='source-line eof'></div>";
break;
}
}
error_reporting($err);
return $html;
}
}
+56
View File
@@ -0,0 +1,56 @@
<?php
namespace Negundo\Client\Util;
use Negundo\Client\DataInterface;
class TaskHandler {
public /*DataInterface*/ $dataManipulator;
public function __construct(DataInterface $dataManipulator = null)
{
$this->dataManipulator = $dataManipulator;
}
public function sendReport(string $message, ? string $title = null, array $data = []) : array
{
$backtrace = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS);
array_shift($backtrace);
array_shift($backtrace);
$trace = $backtrace[0] ?? [
"line" => -1,
"file" => "unknown",
];
$post = [
'file' => $trace['file'],
'line' => $trace['line'],
'title' => $title ?: substr($trace['file'], 0, 255),
'message' => $message,
'url' => ( ( 'on' === ($_SERVER['HTTPS'] ?? false) ) ? 'https' : 'http' ) . '://' . ( $_SERVER['HTTP_HOST'] ?? "") . ( $_SERVER["REQUEST_URI"] ?? "" ),
'data' => $data + [
'sent_at' => date('Y-m-d H:i:s'),
'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? null,
'request_body' => json_encode($_POST),
'remote_addr' => $_SERVER['REMOTE_ADDR'] ?? null,
],
];
if ( $this->dataManipulator ?? false ) {
$post['data'] += $this->dataManipulator->getData();
$this->dataManipulator->run($post);
}
$post['data'] = json_encode($post['data'] ?? null);
return $post;
}
public function hash(array $content) : string
{
return md5(implode('-', [ $content['file'], $content['line'], $content['title'] ]));
}
}