diff --git a/app/main/lib/Monolog.php b/app/main/lib/Monolog.php index 9fd71659..b3fc008c 100644 --- a/app/main/lib/Monolog.php +++ b/app/main/lib/Monolog.php @@ -36,8 +36,8 @@ class Monolog extends \Prefab { 'mail' => 'Monolog\Handler\SwiftMailerHandler', 'slackMap' => 'lib\logging\handler\SlackMapWebhookHandler', 'slackRally' => 'lib\logging\handler\SlackRallyWebhookHandler', - 'discordMap' => 'lib\logging\handler\SlackMapWebhookHandler', // use Slack handler for Discord - 'discordRally' => 'lib\logging\handler\SlackRallyWebhookHandler', // use Slack handler for Discord + 'discordMap' => 'lib\logging\handler\DiscordMapWebhookHandler', + 'discordRally' => 'lib\logging\handler\DiscordRallyWebhookHandler', 'zmq' => 'lib\logging\handler\ZMQHandler' ]; diff --git a/app/main/lib/logging/handler/AbstractMapWebhookHandler.php b/app/main/lib/logging/handler/AbstractMapWebhookHandler.php new file mode 100644 index 00000000..0a01ff32 --- /dev/null +++ b/app/main/lib/logging/handler/AbstractMapWebhookHandler.php @@ -0,0 +1,99 @@ +getTimestamp(); + $text = ''; + + if ( + $this->useAttachment && + !empty( $attachmentsData = $record['context']['data']) + ) { + + // convert non grouped data (associative array) to multi dimensional (sequential) array + // -> see "group" records + $attachmentsData = Util::is_assoc($attachmentsData) ? [$attachmentsData] : $attachmentsData; + + $thumbData = (array)$record['extra']['thumb']; + + $postData['attachments'] = []; + + foreach($attachmentsData as $attachmentData){ + $channelData = (array)$attachmentData['channel']; + $characterData = (array)$attachmentData['character']; + $formatted = (string)$attachmentData['formatted']; + + // get "message" from $formatted + $msgParts = explode('|', $formatted, 2); + + // build main text from first Attachment (they belong to same channel) + if(!empty($channelData)){ + $text = "*Map '" . $channelData['channelName'] . "'* _#" . $channelData['channelId'] . "_ *changed*"; + } + + $attachment = [ + 'title' => !empty($msgParts[0]) ? $msgParts[0] : 'No Title', + //'pretext' => '', + 'text' => !empty($msgParts[1]) ? sprintf('```%s```', $msgParts[1]) : '', + 'fallback' => !empty($msgParts[1]) ? $msgParts[1] : 'No Fallback', + 'color' => $this->getAttachmentColor($tag), + 'fields' => [], + 'mrkdwn_in' => ['fields', 'text'], + 'footer' => 'Pathfinder API', + //'footer_icon'=> '', + 'ts' => $timestamp + ]; + + $attachment = $this->setAuthor($attachment, $characterData); + $attachment = $this->setThumb($attachment, $thumbData); + + + // set 'field' array ---------------------------------------------------------------------------------- + if ($this->includeExtra) { + $attachment['fields'][] = $this->generateAttachmentField('', 'Meta data:', false, false); + + if(!empty($record['extra']['path'])){ + $attachment['fields'][] = $this->generateAttachmentField('Path', $record['extra']['path'], true); + } + + if(!empty($tag)){ + $attachment['fields'][] = $this->generateAttachmentField('Tag', $tag, true); + } + + if(!empty($record['level_name'])){ + $attachment['fields'][] = $this->generateAttachmentField('Level', $record['level_name'], true); + } + + if(!empty($record['extra']['ip'])){ + $attachment['fields'][] = $this->generateAttachmentField('IP', $record['extra']['ip'], true); + } + } + + $postData['attachments'][] = $attachment; + } + } + + $postData['text'] = empty($text) ? $postData['text'] : $text; + + + return $postData; + } +} \ No newline at end of file diff --git a/app/main/lib/logging/handler/AbstractRallyWebhookHandler.php b/app/main/lib/logging/handler/AbstractRallyWebhookHandler.php new file mode 100644 index 00000000..f77d4dd2 --- /dev/null +++ b/app/main/lib/logging/handler/AbstractRallyWebhookHandler.php @@ -0,0 +1,156 @@ +getTimestamp(); + $text = ''; + + if ( + $this->useAttachment && + !empty( $attachmentsData = $record['context']['data']) + ){ + // convert non grouped data (associative array) to multi dimensional (sequential) array + // -> see "group" records + $attachmentsData = Util::is_assoc($attachmentsData) ? [$attachmentsData] : $attachmentsData; + + $thumbData = (array)$record['extra']['thumb']; + + $postData['attachments'] = []; + + foreach($attachmentsData as $attachmentData){ + $characterData = (array)$attachmentData['character']; + + $text = 'No Title'; + if( !empty($attachmentData['formatted']) ){ + $text = $attachmentData['formatted']; + } + + $attachment = [ + 'title' => !empty($attachmentData['main']['message']) ? 'Message' : '', + //'pretext' => '', + 'text' => !empty($attachmentData['main']['message']) ? sprintf('```%s```', $attachmentData['main']['message']) : '', + 'fallback' => !empty($attachmentData['main']['message']) ? $attachmentData['main']['message'] : 'No Fallback', + 'color' => $this->getAttachmentColor($tag), + 'fields' => [], + 'mrkdwn_in' => ['fields', 'text'], + 'footer' => 'Pathfinder API', + //'footer_icon'=> '', + 'ts' => $timestamp + ]; + + $attachment = $this->setAuthor($attachment, $characterData); + $attachment = $this->setThumb($attachment, $thumbData); + + // set 'field' array ---------------------------------------------------------------------------------- + if ($this->includeContext) { + if(!empty($objectData = $attachmentData['object'])){ + if(!empty($objectData['objAlias'])){ + // System alias + $attachment['fields'][] = $this->generateAttachmentField('Alias', $objectData['objAlias']); + } + + if(!empty($objectData['objName'])){ + // System name + $attachment['fields'][] = $this->generateAttachmentField('System', $objectData['objName']); + } + + if(!empty($objectData['objRegion'])){ + // System region + $attachment['fields'][] = $this->generateAttachmentField('Region', $objectData['objRegion']); + } + + if(isset($objectData['objIsWormhole'])){ + // Is wormhole + $attachment['fields'][] = $this->generateAttachmentField('Wormhole', $objectData['objIsWormhole'] ? 'Yes' : 'No'); + } + + if(!empty($objectData['objSecurity'])){ + // System security + $attachment['fields'][] = $this->generateAttachmentField('Security', $objectData['objSecurity']); + } + + if(!empty($objectData['objEffect'])){ + // System effect + $attachment['fields'][] = $this->generateAttachmentField('Effect', $objectData['objEffect']); + } + + if(!empty($objectData['objTrueSec'])){ + // System trueSec + $attachment['fields'][] = $this->generateAttachmentField('TrueSec', $objectData['objTrueSec']); + } + + if(!empty($objectData['objCountPlanets'])){ + // System planet count + $attachment['fields'][] = $this->generateAttachmentField('Planets', $objectData['objCountPlanets']); + } + + if(!empty($objectData['objDescription'])){ + // System description + $attachment['fields'][] = $this->generateAttachmentField('System description', '```' . $this->htmlToMarkdown($objectData['objDescription']) . '```', false, false); + } + + if(!empty($objectData['objUrl'])){ + // System deeeplink + $attachment['fields'][] = $this->generateAttachmentField('', $objectData['objUrl'] , false, false); + } + } + } + + if($this->includeExtra){ + if(!empty($record['extra']['path'])){ + $attachment['fields'][] = $this->generateAttachmentField('Path', $record['extra']['path'], true); + } + + if(!empty($tag)){ + $attachment['fields'][] = $this->generateAttachmentField('Tag', $tag, true); + } + + if(!empty($record['level_name'])){ + $attachment['fields'][] = $this->generateAttachmentField('Level', $record['level_name'], true); + } + + if(!empty($record['extra']['ip'])){ + $attachment['fields'][] = $this->generateAttachmentField('IP', $record['extra']['ip'], true); + } + } + + $postData['attachments'][] = $attachment; + } + } + + $postData['text'] = empty($text) ? $postData['text'] : $text; + + return $postData; + } + + /** + * convert $html into Markdown + * @param $html + * @return string + */ + protected function htmlToMarkdown($html){ + $converter = new HtmlConverter(); + $converter->getConfig()->setOption('strip_tags', true); + $markdown = $converter->convert($html); + return $markdown; + } +} \ No newline at end of file diff --git a/app/main/lib/logging/handler/AbstractSlackWebhookHandler.php b/app/main/lib/logging/handler/AbstractWebhookHandler.php similarity index 98% rename from app/main/lib/logging/handler/AbstractSlackWebhookHandler.php rename to app/main/lib/logging/handler/AbstractWebhookHandler.php index 5e348ce7..8c7d4adf 100644 --- a/app/main/lib/logging/handler/AbstractSlackWebhookHandler.php +++ b/app/main/lib/logging/handler/AbstractWebhookHandler.php @@ -12,7 +12,7 @@ use lib\Config; use Monolog\Handler; use Monolog\Logger; -abstract class AbstractSlackWebhookHandler extends Handler\AbstractProcessingHandler { +abstract class AbstractWebhookHandler extends Handler\AbstractProcessingHandler { /** * @var string diff --git a/app/main/lib/logging/handler/DiscordMapWebhookHandler.php b/app/main/lib/logging/handler/DiscordMapWebhookHandler.php new file mode 100644 index 00000000..1c6b34c0 --- /dev/null +++ b/app/main/lib/logging/handler/DiscordMapWebhookHandler.php @@ -0,0 +1,14 @@ +getTimestamp(); - $text = ''; - - if ( - $this->useAttachment && - !empty( $attachmentsData = $record['context']['data']) - ) { - - // convert non grouped data (associative array) to multi dimensional (sequential) array - // -> see "group" records - $attachmentsData = Util::is_assoc($attachmentsData) ? [$attachmentsData] : $attachmentsData; - - $thumbData = (array)$record['extra']['thumb']; - - $postData['attachments'] = []; - - foreach($attachmentsData as $attachmentData){ - $channelData = (array)$attachmentData['channel']; - $characterData = (array)$attachmentData['character']; - $formatted = (string)$attachmentData['formatted']; - - // get "message" from $formatted - $msgParts = explode('|', $formatted, 2); - - // build main text from first Attachment (they belong to same channel) - if(!empty($channelData)){ - $text = "*Map '" . $channelData['channelName'] . "'* _#" . $channelData['channelId'] . "_ *changed*"; - } - - $attachment = [ - 'title' => !empty($msgParts[0]) ? $msgParts[0] : 'No Title', - //'pretext' => '', - 'text' => !empty($msgParts[1]) ? sprintf('```%s```', $msgParts[1]) : '', - 'fallback' => !empty($msgParts[1]) ? $msgParts[1] : 'No Fallback', - 'color' => $this->getAttachmentColor($tag), - 'fields' => [], - 'mrkdwn_in' => ['fields', 'text'], - 'footer' => 'Pathfinder API', - //'footer_icon'=> '', - 'ts' => $timestamp - ]; - - $attachment = $this->setAuthor($attachment, $characterData); - $attachment = $this->setThumb($attachment, $thumbData); - - - // set 'field' array ---------------------------------------------------------------------------------- - if ($this->includeExtra) { - $attachment['fields'][] = $this->generateAttachmentField('', 'Meta data:', false, false); - - if(!empty($record['extra']['path'])){ - $attachment['fields'][] = $this->generateAttachmentField('Path', $record['extra']['path'], true); - } - - if(!empty($tag)){ - $attachment['fields'][] = $this->generateAttachmentField('Tag', $tag, true); - } - - if(!empty($record['level_name'])){ - $attachment['fields'][] = $this->generateAttachmentField('Level', $record['level_name'], true); - } - - if(!empty($record['extra']['ip'])){ - $attachment['fields'][] = $this->generateAttachmentField('IP', $record['extra']['ip'], true); - } - } - - $postData['attachments'][] = $attachment; - } - } - - $postData['text'] = empty($text) ? $postData['text'] : $text; - - - return $postData; - } +class SlackMapWebhookHandler extends AbstractMapWebhookHandler { } \ No newline at end of file diff --git a/app/main/lib/logging/handler/SlackRallyWebhookHandler.php b/app/main/lib/logging/handler/SlackRallyWebhookHandler.php index db3d0ceb..40f77cbd 100644 --- a/app/main/lib/logging/handler/SlackRallyWebhookHandler.php +++ b/app/main/lib/logging/handler/SlackRallyWebhookHandler.php @@ -8,138 +8,10 @@ namespace lib\logging\handler; -use lib\Util; -class SlackRallyWebhookHandler extends AbstractSlackWebhookHandler { +class SlackRallyWebhookHandler extends AbstractRallyWebhookHandler { - /** - * @param array $record - * @return array - */ - protected function getSlackData(array $record) : array{ - $postData = parent::getSlackData($record); - $tag = (string)$record['context']['tag']; - $timestamp = (int)$record['datetime']->getTimestamp(); - $text = ''; - - if ( - $this->useAttachment && - !empty( $attachmentsData = $record['context']['data']) - ){ - // convert non grouped data (associative array) to multi dimensional (sequential) array - // -> see "group" records - $attachmentsData = Util::is_assoc($attachmentsData) ? [$attachmentsData] : $attachmentsData; - - $thumbData = (array)$record['extra']['thumb']; - - $postData['attachments'] = []; - - foreach($attachmentsData as $attachmentData){ - $characterData = (array)$attachmentData['character']; - - $text = 'No Title'; - if( !empty($attachmentData['formatted']) ){ - $text = $attachmentData['formatted']; - } - - $attachment = [ - 'title' => !empty($attachmentData['main']['message']) ? 'Message' : '', - //'pretext' => '', - 'text' => !empty($attachmentData['main']['message']) ? sprintf('```%s```', $attachmentData['main']['message']) : '', - 'fallback' => !empty($attachmentData['main']['message']) ? $attachmentData['main']['message'] : 'No Fallback', - 'color' => $this->getAttachmentColor($tag), - 'fields' => [], - 'mrkdwn_in' => ['fields', 'text'], - 'footer' => 'Pathfinder API', - //'footer_icon'=> '', - 'ts' => $timestamp - ]; - - $attachment = $this->setAuthor($attachment, $characterData); - $attachment = $this->setThumb($attachment, $thumbData); - - // set 'field' array ---------------------------------------------------------------------------------- - if ($this->includeContext) { - if(!empty($objectData = $attachmentData['object'])){ - if(!empty($objectData['objAlias'])){ - // System alias - $attachment['fields'][] = $this->generateAttachmentField('Alias', $objectData['objAlias']); - } - - if(!empty($objectData['objName'])){ - // System name - $attachment['fields'][] = $this->generateAttachmentField('System', $objectData['objName']); - } - - if(!empty($objectData['objRegion'])){ - // System region - $attachment['fields'][] = $this->generateAttachmentField('Region', $objectData['objRegion']); - } - - if(isset($objectData['objIsWormhole'])){ - // Is wormhole - $attachment['fields'][] = $this->generateAttachmentField('Wormhole', $objectData['objIsWormhole'] ? 'Yes' : 'No'); - } - - if(!empty($objectData['objSecurity'])){ - // System security - $attachment['fields'][] = $this->generateAttachmentField('Security', $objectData['objSecurity']); - } - - if(!empty($objectData['objEffect'])){ - // System effect - $attachment['fields'][] = $this->generateAttachmentField('Effect', $objectData['objEffect']); - } - - if(!empty($objectData['objTrueSec'])){ - // System trueSec - $attachment['fields'][] = $this->generateAttachmentField('TrueSec', $objectData['objTrueSec']); - } - - if(!empty($objectData['objCountPlanets'])){ - // System planet count - $attachment['fields'][] = $this->generateAttachmentField('Planets', $objectData['objCountPlanets']); - } - - if(!empty($objectData['objDescription'])){ - // System trueSec - $attachment['fields'][] = $this->generateAttachmentField('System description', '```' . $objectData['objDescription'] . '```', false, false); - } - - if(!empty($objectData['objUrl'])){ - // System deeeplink - $attachment['fields'][] = $this->generateAttachmentField('', $objectData['objUrl'] , false, false); - } - } - } - - if($this->includeExtra){ - if(!empty($record['extra']['path'])){ - $attachment['fields'][] = $this->generateAttachmentField('Path', $record['extra']['path'], true); - } - - if(!empty($tag)){ - $attachment['fields'][] = $this->generateAttachmentField('Tag', $tag, true); - } - - if(!empty($record['level_name'])){ - $attachment['fields'][] = $this->generateAttachmentField('Level', $record['level_name'], true); - } - - if(!empty($record['extra']['ip'])){ - $attachment['fields'][] = $this->generateAttachmentField('IP', $record['extra']['ip'], true); - } - } - - $postData['attachments'][] = $attachment; - } - } - - $postData['text'] = empty($text) ? $postData['text'] : $text; - - return $postData; - } } \ No newline at end of file diff --git a/composer-dev.json b/composer-dev.json index d90eaa1a..3f1a5f0e 100644 --- a/composer-dev.json +++ b/composer-dev.json @@ -32,6 +32,7 @@ "monolog/monolog": "1.*", "websoftwares/monolog-zmq-handler": "0.2.*", "swiftmailer/swiftmailer": "^6.0", + "league/html-to-markdown": "4.8.*", "exodus4d/pathfinder_esi": "dev-develop as 0.0.x-dev" } } diff --git a/composer.json b/composer.json index 7afbda72..ebef794e 100644 --- a/composer.json +++ b/composer.json @@ -32,6 +32,7 @@ "monolog/monolog": "1.*", "websoftwares/monolog-zmq-handler": "0.2.*", "swiftmailer/swiftmailer": "^6.0", + "league/html-to-markdown": "4.8.*", "exodus4d/pathfinder_esi": "dev-master#v1.2.5" } }