-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
149 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
.idea/ | ||
/vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Killbot; | ||
|
||
use Exception; | ||
use Swift_Mailer; | ||
use Swift_Message; | ||
use Swift_SmtpTransport; | ||
use Twig_Environment; | ||
|
||
class AlertHandler | ||
{ | ||
|
||
public static function sendAlertMail(Exception $e) { | ||
|
||
$transport = (new Swift_SmtpTransport(Settings::$SMTP_SERVER, Settings::$SMTP_PORT, Settings::$SECURITY)) | ||
->setUsername(Settings::$SMTP_USER) | ||
->setPassword(Settings::$SMTP_PASSWORD); | ||
|
||
if (Settings::$SECURITY == 'ssl') { | ||
$transport->setStreamOptions( | ||
array( | ||
'ssl' => array( | ||
'allow_self_signed' => true, | ||
'verify_peer' => false | ||
) | ||
) | ||
); | ||
} | ||
|
||
$mailer = new Swift_Mailer($transport); | ||
|
||
$message = (new Swift_Message('Killbot error !')) | ||
->setFrom(Settings::$MAIL_SENDER) | ||
->setTo(Settings::$MAIL_RECIPIENT) | ||
->setBody(self::generateBody($e)); | ||
|
||
|
||
if ($mailer->send($message)) { | ||
echo "Alert mail sent\n"; | ||
} else { | ||
echo "Failed to send alert mail\n"; | ||
} | ||
} | ||
protected static function generateBody(Exception $e) | ||
{ | ||
$loader = new \Twig_Loader_Filesystem('.'); | ||
$twig = new Twig_Environment($loader); | ||
|
||
$body = $twig->render( | ||
'mail.html.twig', | ||
array( | ||
'message' => $e->getMessage(), | ||
'path' => $e->getFile(), | ||
'line' => $e->getLine(), | ||
) | ||
); | ||
|
||
return $body; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
<?php | ||
|
||
namespace Killbot; | ||
|
||
use stdClass; | ||
|
||
class Killbot { | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
<?php | ||
|
||
namespace Killbot; | ||
|
||
class Settings { | ||
|
||
/******************************************************************************************************************* | ||
|
@@ -28,6 +30,27 @@ class Settings { | |
] | ||
]; | ||
|
||
/******************************************************************************************************************* | ||
* Mail settings | ||
* Allow script to send mail alert when something goes wrong | ||
******************************************************************************************************************/ | ||
|
||
// Enable sending mails | ||
public static $SEND_MAIL = true; | ||
|
||
// Server configuration | ||
public static $SMTP_SERVER = 'smtp.example.com'; | ||
public static $SMTP_PORT = '465'; | ||
public static $SMTP_USER = '[email protected]'; | ||
public static $SMTP_PASSWORD = 'my_password'; | ||
|
||
// Use null or ssl if required | ||
public static $SECURITY = 'ssl'; | ||
|
||
// Mail addresses | ||
public static $MAIL_RECIPIENT = '[email protected]'; | ||
public static $MAIL_SENDER = '[email protected]'; | ||
|
||
/******************************************************************************************************************* | ||
* Advanced configuration | ||
* Do not edit unless you know what you are doing | ||
|
@@ -47,4 +70,4 @@ class Settings { | |
|
||
// ESI base URL | ||
public static $ESI_URL = 'https://esi.tech.ccp.is/latest'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "slivo/killbot", | ||
"type": "project", | ||
"authors": [ | ||
{ | ||
"name": "slivo-fr", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": "^7.0", | ||
"swiftmailer/swiftmailer": "^6.0", | ||
"twig/twig": "^2.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Killbot\\":"" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,24 @@ | ||
<?php | ||
|
||
include_once('Settings.php'); | ||
include_once('Killbot.php'); | ||
set_error_handler(function($code, $message, $file, $line) { | ||
echo $message . "\n"; | ||
throw new ErrorException($message, 0, $code, $file, $line); | ||
}); | ||
|
||
$killbot = new Killbot(); | ||
$killbot->run(); | ||
require_once ('vendor/autoload.php'); | ||
|
||
use Killbot\AlertHandler; | ||
use Killbot\Killbot; | ||
use Killbot\Settings; | ||
|
||
try { | ||
|
||
$killbot = new Killbot(); | ||
$killbot->run(); | ||
} | ||
catch (Exception $e) { | ||
|
||
if (Settings::$SEND_MAIL) { | ||
AlertHandler::sendAlertMail($e); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Hello there ! | ||
|
||
Unfortunately, our beloved killbot encountered an error :/ | ||
Here is more details about it : | ||
|
||
Error : {{ message }} | ||
File : {{ path }} | ||
Line : {{ line }} | ||
|
||
It may be a lonely fail, but if you keep getting more of there mail, you should double check your configuration and/or report the issue ! | ||
|
||
Fly safe o7 |