From 49206ec4b557df8726f68de87ff89d590b5cdaa3 Mon Sep 17 00:00:00 2001 From: Jakub Onderka Date: Mon, 25 Mar 2024 14:00:06 +0100 Subject: [PATCH] fix: [UI] Showing event logo in correlation graph --- app/Controller/EventsController.php | 6 +- app/Lib/Tools/CorrelationGraphTool.php | 85 ++++++++++++------------- app/View/Helper/OrgImgHelper.php | 13 ++++ app/webroot/img/misp-org.png | Bin 0 -> 842 bytes 4 files changed, 58 insertions(+), 46 deletions(-) create mode 100644 app/webroot/img/misp-org.png diff --git a/app/Controller/EventsController.php b/app/Controller/EventsController.php index e53c437be7a..61ae6b16251 100644 --- a/app/Controller/EventsController.php +++ b/app/Controller/EventsController.php @@ -4862,16 +4862,18 @@ public function deleteNode($id) { public function updateGraph($id, $type = 'event') { $user = $this->_closeSession(); + $validTools = array('event', 'galaxy', 'tag'); if (!in_array($type, $validTools, true)) { throw new MethodNotAllowedException(__('Invalid type.')); } + $this->loadModel('Taxonomy'); $this->loadModel('GalaxyCluster'); App::uses('CorrelationGraphTool', 'Tools'); - $grapher = new CorrelationGraphTool(); + $data = $this->request->is('post') ? $this->request->data : array(); - $grapher->construct($this->Event, $this->Taxonomy, $this->GalaxyCluster, $user, $data); + $grapher = new CorrelationGraphTool($this->Event, $this->Taxonomy, $this->GalaxyCluster, $user, $data); $json = $grapher->buildGraphJson($id, $type); array_walk_recursive($json, function (&$item, $key) { if (!mb_detect_encoding($item, 'utf-8', true)) { diff --git a/app/Lib/Tools/CorrelationGraphTool.php b/app/Lib/Tools/CorrelationGraphTool.php index d089a1010cb..b663e9afd98 100644 --- a/app/Lib/Tools/CorrelationGraphTool.php +++ b/app/Lib/Tools/CorrelationGraphTool.php @@ -1,4 +1,6 @@ __eventModel = $eventModel; $this->__taxonomyModel = $taxonomyModel; $this->__galaxyClusterModel = $galaxyClusterModel; $this->__user = $user; - $this->__json = $json; + $this->data = $data; $this->__lookupTables = array( - 'analysisLevels' => $this->__eventModel->analysisLevels, - 'distributionLevels' => $this->__eventModel->Attribute->distributionLevels - ); + 'analysisLevels' => $eventModel->analysisLevels, + 'distributionLevels' => $eventModel->Attribute->distributionLevels + ); + $this->orgImgHelper = new OrgImgHelper(new View()); return true; } @@ -38,7 +44,7 @@ private function __expandEvent($id) 'sgReferenceOnly' => true, )); if (empty($event)) { - return $this->__json; + return $this->data; } $this->cleanLinks(); $event[0]['Event']['Orgc'] = $event[0]['Orgc']; @@ -75,7 +81,7 @@ private function __expandEvent($id) public function buildGraphJson($id, $type = 'event', $action = 'create') { if ($action == 'delete') { - return $this->__json; + return $this->data; } switch ($type) { case 'event': @@ -88,13 +94,13 @@ public function buildGraphJson($id, $type = 'event', $action = 'create') $this->__expandTag($id); break; } - return $this->__json; + return $this->data; } private function __deleteObject($id) { $this->cleanLinks(); - return $this->__json; + return $this->data; } private function __handleObjects($objects, $anchor_id, $full = false) @@ -193,8 +199,8 @@ private function __handleGalaxies($galaxies, $anchor_id) private function __expandGalaxy($id) { - if (!empty($this->__json['nodes'])) { - foreach ($this->__json['nodes'] as $k => $node) { + if (!empty($this->data['nodes'])) { + foreach ($this->data['nodes'] as $k => $node) { if ($node['type'] == 'galaxy' && $node['id'] == $id) { $current_galaxy_id = $k; $tag_name = $node['tag_name']; @@ -205,7 +211,7 @@ private function __expandGalaxy($id) $current_galaxy_id = $this->__addGalaxy($id); } $this->cleanLinks(); - $events = $this->__eventModel->EventTag->Tag->fetchSimpleEventsForTag($this->__json['nodes'][$current_galaxy_id]['tag_name'], $this->__user, true); + $events = $this->__eventModel->EventTag->Tag->fetchSimpleEventsForTag($this->data['nodes'][$current_galaxy_id]['tag_name'], $this->__user, true); foreach ($events as $event) { $current_event_id = $this->__createNode('event', $event); $this->__addLink($current_event_id, $current_galaxy_id); @@ -229,7 +235,7 @@ private function __addLink($from_id, $to_id, $linkDistance = 150) { $link = $this->graphJsonContainsLink($from_id, $to_id); if ($link === false) { - $this->__json['links'][] = array('source' => $from_id, 'target' => $to_id, 'linkDistance' => $linkDistance); + $this->data['links'][] = array('source' => $from_id, 'target' => $to_id, 'linkDistance' => $linkDistance); } } @@ -240,7 +246,7 @@ private function __addLinkByUuid($from_uuid, $to_uuid) if ($from_uuid == $to_uuid) { return false; } - foreach ($this->__json['nodes'] as $k => $node) { + foreach ($this->data['nodes'] as $k => $node) { if ($node['uuid'] === $from_uuid) { $from_id = $k; } @@ -277,10 +283,9 @@ private function __createNode($type, $data, $expand = false) ); break; case 'event': - if ($this->orgImgExists($data['Orgc']['name'])) { - $image = Configure::read('MISP.baseurl') . '/img/orgs/' . h($data['Orgc']['name']) . '.png'; - } else { - $image = Configure::read('MISP.baseurl') . '/img/orgs/MISP.png'; + $orgImage = $this->orgImgHelper->getOrgLogoAsBase64($data['Orgc']); + if ($orgImage === null) { + $orgImage = Configure::read('MISP.baseurl') . '/img/misp-org.png'; } $node = array( 'unique_id' => 'event-' . $data['id'], @@ -289,7 +294,7 @@ private function __createNode($type, $data, $expand = false) 'id' => $data['id'], 'expanded' => $expand, 'uuid' => $data['uuid'], - 'image' => $image, + 'image' => $orgImage, 'info' => $data['info'], 'org' => $data['Orgc']['name'], 'analysis' => $this->__lookupTables['analysisLevels'][$data['analysis']], @@ -345,11 +350,11 @@ private function __createNode($type, $data, $expand = false) ); break; } - $this->__json['nodes'][] = $node; - $current_id = count($this->__json['nodes'])-1; + $this->data['nodes'][] = $node; + $current_id = count($this->data['nodes'])-1; } else { if ($expand) { - $this->__json['nodes'][$current_id]['expanded'] = 1; + $this->data['nodes'][$current_id]['expanded'] = 1; } } return $current_id; @@ -357,11 +362,11 @@ private function __createNode($type, $data, $expand = false) public function cleanLinks() { - if (isset($this->__json['nodes']) && isset($this->__json['links'])) { + if (isset($this->data['nodes']) && isset($this->data['links'])) { $links = array(); - foreach ($this->__json['links'] as $link) { + foreach ($this->data['links'] as $link) { $temp = array(); - foreach ($this->__json['nodes'] as $k => $node) { + foreach ($this->data['nodes'] as $k => $node) { if ($link['source'] == $node) { $temp['source'] = $k; } @@ -372,32 +377,24 @@ public function cleanLinks() $temp['linkDistance'] = $link['linkDistance']; $links[] = $temp; } - $this->__json['links'] = $links; + $this->data['links'] = $links; } else { - if (!isset($this->__json['links'])) { - $this->__json['links'] = array(); + if (!isset($this->data['links'])) { + $this->data['links'] = array(); } - if (!isset($this->__json['nodes'])) { - $this->__json['nodes'] = array(); + if (!isset($this->data['nodes'])) { + $this->data['nodes'] = array(); } } return true; } - public function orgImgExists($org) - { - if (file_exists(APP . 'webroot' . DS . 'img' . DS . 'orgs' . DS . $org . '.png')) { - return true; - } - return false; - } - public function graphJsonContains($type, $element) { - if (!isset($this->__json['nodes'])) { + if (!isset($this->data['nodes'])) { return false; } - foreach ($this->__json['nodes'] as $k => $node) { + foreach ($this->data['nodes'] as $k => $node) { if ($type == 'event' && $node['type'] == 'event' && $node['id'] == $element['id']) { return $k; } @@ -418,10 +415,10 @@ public function graphJsonContains($type, $element) } public function graphJsonContainsLink($id1, $id2) { - if (!isset($this->__json['links'])) { + if (!isset($this->data['links'])) { return false; } - foreach ($this->__json['links'] as $k => $link) { + foreach ($this->data['links'] as $k => $link) { if (($link['source'] == $id1 && $link['target'] == $id2) || ($link['source'] == $id2 && $link['target'] == $id1)) { return $k; } diff --git a/app/View/Helper/OrgImgHelper.php b/app/View/Helper/OrgImgHelper.php index 798fac4a009..0d394f1f0f9 100644 --- a/app/View/Helper/OrgImgHelper.php +++ b/app/View/Helper/OrgImgHelper.php @@ -46,6 +46,19 @@ public function getOrgLogo(array $organisation, $size, $withLink = true) return $this->getOrgImg($options, true, !$withLink); } + /** + * @param array $organisation + * @return string|null + */ + public function getOrgLogoAsBase64(array $organisation) + { + $orgImgName = $this->findOrgImage($organisation); + if ($orgImgName) { + return $this->_View->Image->base64(self::IMG_PATH . $orgImgName); + } + return null; + } + /** * @deprecated */ diff --git a/app/webroot/img/misp-org.png b/app/webroot/img/misp-org.png new file mode 100644 index 0000000000000000000000000000000000000000..84df375f8434bd38041d6c6770a9a85dddac03b5 GIT binary patch literal 842 zcmV-Q1GW5#P)WFU8GbZ8()Nlj2>E@cM*00OQ_L_t(&-tCr4Xj4%T zhQDh{A0Vi>u%J{0wc=wTBz2)%rGnHds2i< zr(h)cJPG^&z+_(#X}VW*XEL1tCnLQ{!ogm#&jP=I-$1O$&Ky$ez#`X} z0j7X7Fzn@kv3e7PgNqB+B+?By&bT%5fpBoC^)P@8P;0H}1Wy0sAol<_#2x|GmpJq< zGCvM5%OgS!#D^P-jPI&2(yi9Ze*^k~3)7K?>%h7q`%==*0e56<0q`cjpvpVRYNEiQ znPblR3LUs<*>f!W2=E=KlL)MG?F+m5`+xzU8rTioD&@cvU?UD&XQt2SGO<1b3(It% zRHQqA*<~GOE>Z(xFY)+e^9pze)Ds_Rw2r?`F8H+yBG6{p&wysDe;Mcnddm2p?Uwz~ z>tyOMCXsvRK$n$1Q2-)MkqT4jjMz=UkX-h=K&ZsNE}&c572vid6oXz4I2e&Q$Js3E zvs&if`;hETQKcyG){@jwaikdC;zSp)U&f|@m3g%*twJY5U%`w4^NFR`0`1~|KYI^2 zKAgx%ZCC-+1D7mEI!dTe3WxxkfVq{akRgu4AXUV`Y00HeFqNwrhs!ON`C@n!n6UJ` z9cU40>;d{bRHy)N2eLnh2-GnB@|c%CPQF!sYk@7Ux#}{M%9p(5IJIF4W1d8tP= z#wtS1z?htLuT;}{GIz|6165_NivtgVERoPfAJ5q-DRLZGBhBV&k@WHHKuSvd*xxkD zJ|+-I0H54nPI56PUC!n|=4?(#+XgfO0l5zkR literal 0 HcmV?d00001