Skip to content

Commit

Permalink
Merge pull request #9640 from JakubOnderka/event-log-correlation-graph
Browse files Browse the repository at this point in the history
fix: [UI] Showing event logo in correlation graph
  • Loading branch information
JakubOnderka committed Mar 25, 2024
2 parents c946d7c + 90a2e3a commit 95e5faa
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 46 deletions.
6 changes: 4 additions & 2 deletions app/Controller/EventsController.php
Expand Up @@ -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)) {
Expand Down
85 changes: 41 additions & 44 deletions app/Lib/Tools/CorrelationGraphTool.php
@@ -1,4 +1,6 @@
<?php
App::uses('OrgImgHelper', 'View/Helper');

class CorrelationGraphTool
{
private $__lookupTables = array();
Expand All @@ -9,20 +11,24 @@ class CorrelationGraphTool
/** @var Taxonomy */
private $__taxonomyModel;
private $__galaxyClusterModel = false;
private $__user = false;
private $__json = array();
/** @var User */
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->__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;
}

Expand All @@ -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'];
Expand Down Expand Up @@ -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':
Expand All @@ -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)
Expand Down Expand Up @@ -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'];
Expand All @@ -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);
Expand All @@ -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);
}
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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'],
Expand All @@ -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']],
Expand Down Expand Up @@ -345,23 +350,23 @@ 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;
}

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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
13 changes: 13 additions & 0 deletions app/View/Helper/OrgImgHelper.php
Expand Up @@ -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
*/
Expand Down
Binary file added app/webroot/img/misp-org.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 95e5faa

Please sign in to comment.