Compare commits
4 Commits
c0197c259f
...
d3e6100d17
Author | SHA1 | Date | |
---|---|---|---|
|
d3e6100d17 | ||
|
bec3c36657 | ||
|
a7dcdf4ee7 | ||
|
ec873145d2 |
@ -10,5 +10,10 @@
|
|||||||
"email": "info@mcnd.ca"
|
"email": "info@mcnd.ca"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {}
|
"require": {},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"TheBugs\\": "src/"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
32
src/Email/EmailConfiguration.php
Normal file
32
src/Email/EmailConfiguration.php
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace TheBugs\Email;
|
||||||
|
|
||||||
|
class EmailConfiguration
|
||||||
|
{
|
||||||
|
const AUTH_TYPE_SMTP = 1;
|
||||||
|
|
||||||
|
protected $type;
|
||||||
|
|
||||||
|
public $smtpHost = "";
|
||||||
|
|
||||||
|
public $smtpUsername = "";
|
||||||
|
|
||||||
|
public $smtpPassword = "";
|
||||||
|
|
||||||
|
public $smtpPort = 25;
|
||||||
|
|
||||||
|
public $smtpUseTLS = false;
|
||||||
|
|
||||||
|
public $toAddress = "";
|
||||||
|
|
||||||
|
public $fromAddress = "";
|
||||||
|
|
||||||
|
public $fromName = "";
|
||||||
|
|
||||||
|
public function __construct($type = self::AUTH_TYPE_SMTP)
|
||||||
|
{
|
||||||
|
$this->type = $type;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
12
src/Email/MailerInterface.php
Normal file
12
src/Email/MailerInterface.php
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace TheBugs\Email;
|
||||||
|
|
||||||
|
interface MailerInterface
|
||||||
|
{
|
||||||
|
public function send(string $subject, string $message, bool $html = true) : bool;
|
||||||
|
|
||||||
|
public function setFrom($from) : self;
|
||||||
|
|
||||||
|
public function setTo($to) : self;
|
||||||
|
}
|
45
src/Email/SwiftMailer.php
Normal file
45
src/Email/SwiftMailer.php
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace TheBugs\Email;
|
||||||
|
|
||||||
|
class SwiftMailer implements MailerInterface
|
||||||
|
{
|
||||||
|
protected $emailConfiguration;
|
||||||
|
|
||||||
|
protected $transport;
|
||||||
|
|
||||||
|
protected $from;
|
||||||
|
|
||||||
|
protected $to;
|
||||||
|
|
||||||
|
public function __construct(EmailConfiguration $configuration) {
|
||||||
|
$this->emailConfiguration = $configuration;
|
||||||
|
|
||||||
|
$this->transport = ( new \Swift_SmtpTransport($this->emailConfiguration->smtpHost, $this->emailConfiguration->smtpPort) )
|
||||||
|
->setUsername($this->emailConfiguration->smtpUsername)
|
||||||
|
->setPassword($this->emailConfiguration->smtpPassword);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function send(string $subject, string $message, bool $html = true) : bool
|
||||||
|
{
|
||||||
|
$swiftObj = ( new \Swift_Message($subject) )
|
||||||
|
->setFrom( $this->from )
|
||||||
|
->setTo( $this->to )
|
||||||
|
->setBody( $message, $html ? 'text/html' : 'text/plain' );
|
||||||
|
|
||||||
|
return ( new \Swift_Mailer($this->transport) )->send($swiftObj);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setFrom($from) : MailerInterface
|
||||||
|
{
|
||||||
|
$this->from = $from;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setTo($to) : MailerInterface
|
||||||
|
{
|
||||||
|
$this->to = $to;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
44
src/EmailErrorMiddleware.php
Normal file
44
src/EmailErrorMiddleware.php
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace TheBugs;
|
||||||
|
|
||||||
|
use Psr\Http\Message\ResponseInterface,
|
||||||
|
Psr\Http\Message\ServerRequestInterface,
|
||||||
|
Psr\Http\Server\MiddlewareInterface,
|
||||||
|
Psr\Http\Server\RequestHandlerInterface;
|
||||||
|
|
||||||
|
class EmailErrorMiddleware implements MiddlewareInterface
|
||||||
|
{
|
||||||
|
protected /* EmailConfiguration */ $emailConfiguration;
|
||||||
|
|
||||||
|
protected /* Callable */ $callable;
|
||||||
|
|
||||||
|
protected /* Mailer */ $mailer;
|
||||||
|
|
||||||
|
public function __construct(Email\EmailConfiguration $emailConfiguration, Email\MailerInterface $mailer, Callable $callable)
|
||||||
|
{
|
||||||
|
$this->emailConfiguration = $emailConfiguration;
|
||||||
|
$this->callable = $callable;
|
||||||
|
$this->mailer = $mailer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
|
||||||
|
{
|
||||||
|
$response = $handler->handle($request);
|
||||||
|
|
||||||
|
if ( $response->getStatusCode() === 500 ) {
|
||||||
|
$this->mailer->setTo($this->emailConfiguration->toAddress);
|
||||||
|
$this->mailer->setFrom([$this->emailConfiguration->fromAddress => $this->emailConfiguration->fromName]);
|
||||||
|
|
||||||
|
$bugReport = $response->getBody()->__toString();
|
||||||
|
|
||||||
|
if ( false === ( $this->mailer->send(error_get_last()['message'], $bugReport, true) ) ) {
|
||||||
|
error_log("Impossile to send an email bug report from " . static::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->callable->call($this);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
}
|
@ -21,10 +21,12 @@ class JavascriptMiddleware implements MiddlewareInterface
|
|||||||
"headers" => [
|
"headers" => [
|
||||||
"User-Agent" => "TheBugs/1.0",
|
"User-Agent" => "TheBugs/1.0",
|
||||||
],
|
],
|
||||||
|
# Callable functions
|
||||||
|
"functions" => []
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter list, allows custom invokable
|
* Filter list, allows custom invokable too
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $filters = [];
|
protected $filters = [];
|
||||||
@ -46,9 +48,9 @@ class JavascriptMiddleware implements MiddlewareInterface
|
|||||||
*/
|
*/
|
||||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||||
{
|
{
|
||||||
if ( ( $this->filters['code'] ?? false ) && ($this->filters['code'] !== $request->getCode())) {
|
# if ( ( $this->filters['code'] ?? false ) && ($this->filters['code'] !== $request->getCode())) {
|
||||||
return $handler->handle($request);
|
# return $handler->handle($request);
|
||||||
}
|
# }
|
||||||
|
|
||||||
foreach($this->filters['headers'] as $key => $value) {
|
foreach($this->filters['headers'] as $key => $value) {
|
||||||
if ( ! in_array($value, $request->getHeader($key)) ) {
|
if ( ! in_array($value, $request->getHeader($key)) ) {
|
||||||
@ -56,6 +58,12 @@ class JavascriptMiddleware implements MiddlewareInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
foreach($this->filters['functions'] as $func) {
|
||||||
|
if ( $func->call($this) ) {
|
||||||
|
return $handler->handle($request);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$this->throwError(json_decode($request->getBody(), true));
|
$this->throwError(json_decode($request->getBody(), true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,15 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
use Whoops\Run as Whoops;
|
|
||||||
use Whoops\Handler\PrettyPageHandler as Handler;
|
|
||||||
|
|
||||||
$whoops = new Whoops();
|
|
||||||
$whoops->allowQuit(false);
|
|
||||||
$whoops->writeToOutput(false);
|
|
||||||
$whoops->pushHandler(new Handler());
|
|
||||||
|
|
||||||
IF CLI {
|
|
||||||
$handler->handleUnconditionally(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
$body = $whoops->handleException($this->exception);
|
|
46
templates/js_debug.js
Normal file
46
templates/js_debug.js
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
|
||||||
|
class ErrorHandler
|
||||||
|
{
|
||||||
|
constructor(options) {
|
||||||
|
if ( options ) {
|
||||||
|
if ( "url" in options ) {
|
||||||
|
this.url = options['url'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.catchError();
|
||||||
|
}
|
||||||
|
|
||||||
|
catchError() {
|
||||||
|
window.onerror = function(message, url, line, column, error) {
|
||||||
|
fetch(this.url ? this.url : window.location.href, {
|
||||||
|
method: "post",
|
||||||
|
headers: {
|
||||||
|
'Accept': "application/json",
|
||||||
|
'Content-Type': "application/json",
|
||||||
|
'User-Agent': "TheBugs/1.0"
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
message: message,
|
||||||
|
url: url,
|
||||||
|
line: line,
|
||||||
|
column: column,
|
||||||
|
stack: error.stack,
|
||||||
|
location: window.location.toString()
|
||||||
|
})
|
||||||
|
}).then( response => response ).then(data => {
|
||||||
|
console.info("Error reported", data);
|
||||||
|
});
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}.bind(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
get url() {
|
||||||
|
return this._url;
|
||||||
|
}
|
||||||
|
|
||||||
|
set url(set) {
|
||||||
|
return this._url = set;
|
||||||
|
}
|
||||||
|
}
|
34
templates/xhr_debug.php
Normal file
34
templates/xhr_debug.php
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<script>
|
||||||
|
(send => {
|
||||||
|
XMLHttpRequest.prototype.send = function() {
|
||||||
|
this.addEventListener('load', function(e) {
|
||||||
|
switch(this.status) {
|
||||||
|
case 500:
|
||||||
|
let response = JSON.parse(this.response),
|
||||||
|
content = JSON.stringify(response.error.trace, null, 2),
|
||||||
|
element = document.createElement('div'),
|
||||||
|
body = document.querySelector("body");
|
||||||
|
|
||||||
|
body.appendChild(element);
|
||||||
|
element.outerHTML = `
|
||||||
|
<div ondblclick="this.parentNode.removeChild(this);">
|
||||||
|
<dialog style="top:10vh;width:75%;z-index:1500;background:#444;color:#444;border:0px solid #2f2a2a;padding:2px;position:fixed; max-height:80vh;">
|
||||||
|
<div style="background:#fff">
|
||||||
|
<div style="padding:15px; color:#fff; border-bottom:2px solid #444;background:#c61f1f;font-weight:bold">${response.error.message}</div>
|
||||||
|
<pre style="overflow-y:auto;max-height:65vh;padding:15px;">${content}</pre>
|
||||||
|
<div style="padding:8px 15px; color:#fff; border-top:2px solid #444;text-align:right;background:#c61f1f;font-weight:bold">${response.error.file}:${response.error.line}</div>
|
||||||
|
</div>
|
||||||
|
</dialog>
|
||||||
|
<div style="position:fixed;top:0;left:0;width:100vw;height:100vh;background:#000;opacity:0.5"></div>
|
||||||
|
</div>
|
||||||
|
`.trim();
|
||||||
|
|
||||||
|
body.querySelector("dialog").showModal();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
send.apply(this, arguments);
|
||||||
|
};
|
||||||
|
})(XMLHttpRequest.prototype.send);
|
||||||
|
</script>
|
Loading…
x
Reference in New Issue
Block a user