Merge pull request #9640 from JakubOnderka/event-log-correlation-graph

fix: [UI] Showing event logo in correlation graph
pull/9649/head
Jakub Onderka 2024-03-25 15:23:37 +01:00 committed by GitHub
commit 95e5faa911
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 58 additions and 46 deletions

View File

@ -4862,16 +4862,18 @@ class EventsController extends AppController
public function updateGraph($id, $type = 'event') public function updateGraph($id, $type = 'event')
{ {
$user = $this->_closeSession(); $user = $this->_closeSession();
$validTools = array('event', 'galaxy', 'tag'); $validTools = array('event', 'galaxy', 'tag');
if (!in_array($type, $validTools, true)) { if (!in_array($type, $validTools, true)) {
throw new MethodNotAllowedException(__('Invalid type.')); throw new MethodNotAllowedException(__('Invalid type.'));
} }
$this->loadModel('Taxonomy'); $this->loadModel('Taxonomy');
$this->loadModel('GalaxyCluster'); $this->loadModel('GalaxyCluster');
App::uses('CorrelationGraphTool', 'Tools'); App::uses('CorrelationGraphTool', 'Tools');
$grapher = new CorrelationGraphTool();
$data = $this->request->is('post') ? $this->request->data : array(); $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); $json = $grapher->buildGraphJson($id, $type);
array_walk_recursive($json, function (&$item, $key) { array_walk_recursive($json, function (&$item, $key) {
if (!mb_detect_encoding($item, 'utf-8', true)) { if (!mb_detect_encoding($item, 'utf-8', true)) {

View File

@ -1,4 +1,6 @@
<?php <?php
App::uses('OrgImgHelper', 'View/Helper');
class CorrelationGraphTool class CorrelationGraphTool
{ {
private $__lookupTables = array(); private $__lookupTables = array();
@ -9,20 +11,24 @@
/** @var Taxonomy */ /** @var Taxonomy */
private $__taxonomyModel; private $__taxonomyModel;
private $__galaxyClusterModel = false; private $__galaxyClusterModel = false;
private $__user = false; /** @var User */
private $__json = array(); private $__user;
/** @var array */
private $data;
private $orgImgHelper;
public function construct(Event $eventModel, $taxonomyModel, $galaxyClusterModel, $user, $json) public function __construct(Event $eventModel, $taxonomyModel, $galaxyClusterModel, array $user, array $data)
{ {
$this->__eventModel = $eventModel; $this->__eventModel = $eventModel;
$this->__taxonomyModel = $taxonomyModel; $this->__taxonomyModel = $taxonomyModel;
$this->__galaxyClusterModel = $galaxyClusterModel; $this->__galaxyClusterModel = $galaxyClusterModel;
$this->__user = $user; $this->__user = $user;
$this->__json = $json; $this->data = $data;
$this->__lookupTables = array( $this->__lookupTables = array(
'analysisLevels' => $this->__eventModel->analysisLevels, 'analysisLevels' => $eventModel->analysisLevels,
'distributionLevels' => $this->__eventModel->Attribute->distributionLevels 'distributionLevels' => $eventModel->Attribute->distributionLevels
); );
$this->orgImgHelper = new OrgImgHelper(new View());
return true; return true;
} }
@ -38,7 +44,7 @@
'sgReferenceOnly' => true, 'sgReferenceOnly' => true,
)); ));
if (empty($event)) { if (empty($event)) {
return $this->__json; return $this->data;
} }
$this->cleanLinks(); $this->cleanLinks();
$event[0]['Event']['Orgc'] = $event[0]['Orgc']; $event[0]['Event']['Orgc'] = $event[0]['Orgc'];
@ -75,7 +81,7 @@
public function buildGraphJson($id, $type = 'event', $action = 'create') public function buildGraphJson($id, $type = 'event', $action = 'create')
{ {
if ($action == 'delete') { if ($action == 'delete') {
return $this->__json; return $this->data;
} }
switch ($type) { switch ($type) {
case 'event': case 'event':
@ -88,13 +94,13 @@
$this->__expandTag($id); $this->__expandTag($id);
break; break;
} }
return $this->__json; return $this->data;
} }
private function __deleteObject($id) private function __deleteObject($id)
{ {
$this->cleanLinks(); $this->cleanLinks();
return $this->__json; return $this->data;
} }
private function __handleObjects($objects, $anchor_id, $full = false) private function __handleObjects($objects, $anchor_id, $full = false)
@ -193,8 +199,8 @@
private function __expandGalaxy($id) private function __expandGalaxy($id)
{ {
if (!empty($this->__json['nodes'])) { if (!empty($this->data['nodes'])) {
foreach ($this->__json['nodes'] as $k => $node) { foreach ($this->data['nodes'] as $k => $node) {
if ($node['type'] == 'galaxy' && $node['id'] == $id) { if ($node['type'] == 'galaxy' && $node['id'] == $id) {
$current_galaxy_id = $k; $current_galaxy_id = $k;
$tag_name = $node['tag_name']; $tag_name = $node['tag_name'];
@ -205,7 +211,7 @@
$current_galaxy_id = $this->__addGalaxy($id); $current_galaxy_id = $this->__addGalaxy($id);
} }
$this->cleanLinks(); $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) { foreach ($events as $event) {
$current_event_id = $this->__createNode('event', $event); $current_event_id = $this->__createNode('event', $event);
$this->__addLink($current_event_id, $current_galaxy_id); $this->__addLink($current_event_id, $current_galaxy_id);
@ -229,7 +235,7 @@
{ {
$link = $this->graphJsonContainsLink($from_id, $to_id); $link = $this->graphJsonContainsLink($from_id, $to_id);
if ($link === false) { 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 @@
if ($from_uuid == $to_uuid) { if ($from_uuid == $to_uuid) {
return false; return false;
} }
foreach ($this->__json['nodes'] as $k => $node) { foreach ($this->data['nodes'] as $k => $node) {
if ($node['uuid'] === $from_uuid) { if ($node['uuid'] === $from_uuid) {
$from_id = $k; $from_id = $k;
} }
@ -277,10 +283,9 @@
); );
break; break;
case 'event': case 'event':
if ($this->orgImgExists($data['Orgc']['name'])) { $orgImage = $this->orgImgHelper->getOrgLogoAsBase64($data['Orgc']);
$image = Configure::read('MISP.baseurl') . '/img/orgs/' . h($data['Orgc']['name']) . '.png'; if ($orgImage === null) {
} else { $orgImage = Configure::read('MISP.baseurl') . '/img/misp-org.png';
$image = Configure::read('MISP.baseurl') . '/img/orgs/MISP.png';
} }
$node = array( $node = array(
'unique_id' => 'event-' . $data['id'], 'unique_id' => 'event-' . $data['id'],
@ -289,7 +294,7 @@
'id' => $data['id'], 'id' => $data['id'],
'expanded' => $expand, 'expanded' => $expand,
'uuid' => $data['uuid'], 'uuid' => $data['uuid'],
'image' => $image, 'image' => $orgImage,
'info' => $data['info'], 'info' => $data['info'],
'org' => $data['Orgc']['name'], 'org' => $data['Orgc']['name'],
'analysis' => $this->__lookupTables['analysisLevels'][$data['analysis']], 'analysis' => $this->__lookupTables['analysisLevels'][$data['analysis']],
@ -345,11 +350,11 @@
); );
break; break;
} }
$this->__json['nodes'][] = $node; $this->data['nodes'][] = $node;
$current_id = count($this->__json['nodes'])-1; $current_id = count($this->data['nodes'])-1;
} else { } else {
if ($expand) { if ($expand) {
$this->__json['nodes'][$current_id]['expanded'] = 1; $this->data['nodes'][$current_id]['expanded'] = 1;
} }
} }
return $current_id; return $current_id;
@ -357,11 +362,11 @@
public function cleanLinks() public function cleanLinks()
{ {
if (isset($this->__json['nodes']) && isset($this->__json['links'])) { if (isset($this->data['nodes']) && isset($this->data['links'])) {
$links = array(); $links = array();
foreach ($this->__json['links'] as $link) { foreach ($this->data['links'] as $link) {
$temp = array(); $temp = array();
foreach ($this->__json['nodes'] as $k => $node) { foreach ($this->data['nodes'] as $k => $node) {
if ($link['source'] == $node) { if ($link['source'] == $node) {
$temp['source'] = $k; $temp['source'] = $k;
} }
@ -372,32 +377,24 @@
$temp['linkDistance'] = $link['linkDistance']; $temp['linkDistance'] = $link['linkDistance'];
$links[] = $temp; $links[] = $temp;
} }
$this->__json['links'] = $links; $this->data['links'] = $links;
} else { } else {
if (!isset($this->__json['links'])) { if (!isset($this->data['links'])) {
$this->__json['links'] = array(); $this->data['links'] = array();
} }
if (!isset($this->__json['nodes'])) { if (!isset($this->data['nodes'])) {
$this->__json['nodes'] = array(); $this->data['nodes'] = array();
} }
} }
return true; 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) public function graphJsonContains($type, $element)
{ {
if (!isset($this->__json['nodes'])) { if (!isset($this->data['nodes'])) {
return false; 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']) { if ($type == 'event' && $node['type'] == 'event' && $node['id'] == $element['id']) {
return $k; return $k;
} }
@ -418,10 +415,10 @@
} }
public function graphJsonContainsLink($id1, $id2) public function graphJsonContainsLink($id1, $id2)
{ {
if (!isset($this->__json['links'])) { if (!isset($this->data['links'])) {
return false; 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)) { if (($link['source'] == $id1 && $link['target'] == $id2) || ($link['source'] == $id2 && $link['target'] == $id1)) {
return $k; return $k;
} }

View File

@ -46,6 +46,19 @@ class OrgImgHelper extends AppHelper
return $this->getOrgImg($options, true, !$withLink); 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 * @deprecated
*/ */

Binary file not shown.

After

Width:  |  Height:  |  Size: 842 B