From f55982e9b3c7dcee976b21b99a22b3f371c0381b Mon Sep 17 00:00:00 2001 From: Slivo-fr Date: Thu, 9 May 2019 13:14:08 +0200 Subject: [PATCH] minor fixes --- Settings.php | 2 +- src/CurlWrapper.php | 38 +++++++++++++++++++++----------------- src/Killbot.php | 14 +++++++++++--- src/Logger.php | 7 +++++-- src/Utils.php | 11 +++++++++-- 5 files changed, 47 insertions(+), 25 deletions(-) diff --git a/Settings.php b/Settings.php index 527dcce..e5e1603 100644 --- a/Settings.php +++ b/Settings.php @@ -43,7 +43,7 @@ class Settings // Server configuration public static $SMTP_SERVER = 'smtp.example.com'; - public static $SMTP_PORT = '465'; + public static $SMTP_PORT = 465; public static $SMTP_USER = 'user@example.com'; public static $SMTP_PASSWORD = 'my_password'; diff --git a/src/CurlWrapper.php b/src/CurlWrapper.php index 243a692..39982bf 100644 --- a/src/CurlWrapper.php +++ b/src/CurlWrapper.php @@ -19,26 +19,30 @@ class CurlWrapper * @return bool|string * @throws Exception */ - static function curlRequest($url, $method = self::METHOD_GET, $data = null) + static public function curlRequest($url, $method = self::METHOD_GET, $data = null) { $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); - curl_setopt($ch, CURLOPT_USERAGENT, Settings::$HTTP_HEADER); + if ($ch != false) { + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); + curl_setopt($ch, CURLOPT_USERAGENT, Settings::$HTTP_HEADER); - if ($method == self::METHOD_POST) { - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); - curl_setopt($ch, CURLOPT_POSTFIELDS, $data); - } + if ($method == self::METHOD_POST) { + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); + curl_setopt($ch, CURLOPT_POSTFIELDS, $data); + } + + $output = curl_exec($ch); + self::handleErrors($output, $ch); + curl_close($ch); - $output = curl_exec($ch); - self::handleErrors($output, $ch); - curl_close($ch); + return $output; + } - return $output; + throw new Exception('Unable to initiate curl'); } /** @@ -47,7 +51,7 @@ static function curlRequest($url, $method = self::METHOD_GET, $data = null) * @return bool|string * @throws Exception */ - static function get($url) { + static public function get($url) { return self::curlRequest($url); } @@ -58,7 +62,7 @@ static function get($url) { * @return bool|string * @throws Exception */ - static function post($url, $data) { + static public function post($url, $data) { return self::curlRequest($url, self::METHOD_POST, $data); } @@ -67,7 +71,7 @@ static function post($url, $data) { * @param $ch * @throws Exception */ - static function handleErrors($output, $ch) { + static protected function handleErrors($output, $ch) { if($output === false) { diff --git a/src/Killbot.php b/src/Killbot.php index 2fdc84f..e45225e 100644 --- a/src/Killbot.php +++ b/src/Killbot.php @@ -31,6 +31,7 @@ public function run() // Processing previously failed kills $files = $this->getPendingFiles(); + foreach ($files as $file) { Logger::log('Retrying pending kill '.$file, 'INFO'); @@ -79,7 +80,7 @@ private function processKill($data = null) if ($this->isWatchedKill($data, $watchedEntities)) { if (Settings::$DEBUG) { - Logger::storeKillJson($data->{'package'}->{'killID'}, $rawOutput); + Logger::storeKillJson($data->{'package'}->{'killID'}, json_encode($data)); } $jsonAttachments = $this->formatKillData($data, $watchedEntities); @@ -100,7 +101,7 @@ private function processKill($data = null) Utils::writeFile( $rawOutput, Utils::getUnprocessedPath(), - Uuid::uuid4() . '.kill', + Uuid::uuid4()->toString() . '.kill', 'w' ); } @@ -207,6 +208,7 @@ private function isSystemWatched($killmail, $watchedEntities) * * @param $killData * @param $slackHook + * @throws Exception */ private function pushToSlack($killData, $slackHook) { @@ -333,7 +335,13 @@ protected function getPendingFiles(): array { $unprocessedPath = Utils::getUnprocessedPath(); Utils::createPath($unprocessedPath); - $files = array_diff(scandir($unprocessedPath), array('..', '.')); + $files = scandir($unprocessedPath); + + if ($files) { + $files = array_diff($files, array('..', '.')); + } else { + $files = []; + } return $files; } diff --git a/src/Logger.php b/src/Logger.php index c5a8c75..38031b1 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -3,6 +3,8 @@ namespace Killbot; +use Exception; + class Logger { @@ -11,8 +13,9 @@ class Logger * * @param $killId * @param $data + * @throws Exception */ - static function storeKillJson($killId, $data) + static public function storeKillJson($killId, $data) { $path = Utils::getKillPath(); @@ -21,7 +24,7 @@ static function storeKillJson($killId, $data) Utils::writeFile($data, $path, $filename, 'w'); } - static function log($string, $type = 'ERROR') + static public function log($string, $type = 'ERROR') { $path = Utils::getLogPath(); diff --git a/src/Utils.php b/src/Utils.php index 0a70185..b23d6fa 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -4,6 +4,7 @@ namespace Killbot; +use Exception; use Settings; class Utils @@ -14,14 +15,20 @@ class Utils * @param string $path * @param string $filename * @param string $mode + * @throws Exception */ public static function writeFile($data, string $path, string $filename, string $mode) { self::createPath($path); $file = fopen($path . $filename, $mode); - fwrite($file, $data); - fclose($file); + + if ($file) { + fwrite($file, $data); + fclose($file); + } else { + throw new Exception('Unable to write file ' .$filename); + } } public static function createPath($path)