2015-02-16 17:31:32 +01:00
|
|
|
<?php
|
|
|
|
class JSONConverterTool {
|
|
|
|
public function event2JSON($event, $isSiteAdmin=false) {
|
2015-12-24 15:22:05 +01:00
|
|
|
$event['Event']['Org'] = $event['Org'];
|
|
|
|
$event['Event']['Orgc'] = $event['Orgc'];
|
2015-04-13 17:52:04 +02:00
|
|
|
if (isset($event['SharingGroup']['SharingGroupOrg'])) {
|
|
|
|
foreach ($event['SharingGroup']['SharingGroupOrg'] as $key => $sgo) {
|
2015-12-24 15:22:05 +01:00
|
|
|
$event['SharingGroup']['SharingGroupOrg'][$key]['Organisation'] = $event['SharingGroup']['SharingGroupOrg'][$key]['Organisation'];
|
2015-04-13 17:52:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isset($event['SharingGroup']['SharingGroupServer'])) {
|
|
|
|
foreach ($event['SharingGroup']['SharingGroupServer'] as $key => $sgs) {
|
2015-12-24 15:22:05 +01:00
|
|
|
$event['SharingGroup']['SharingGroupServer'][$key]['Server'] = $event['SharingGroup']['SharingGroupServer'][$key]['Server'];
|
2015-04-13 17:52:04 +02:00
|
|
|
}
|
|
|
|
}
|
2015-12-24 15:22:05 +01:00
|
|
|
if (isset($event['SharingGroup'])) $event['Event']['SharingGroup'] = $event['SharingGroup'];
|
2015-02-16 17:31:32 +01:00
|
|
|
$event['Event']['Attribute'] = $event['Attribute'];
|
|
|
|
$event['Event']['ShadowAttribute'] = $event['ShadowAttribute'];
|
|
|
|
$event['Event']['RelatedEvent'] = $event['RelatedEvent'];
|
|
|
|
|
2015-04-13 17:52:04 +02:00
|
|
|
if (isset($event['EventTag'])) {
|
|
|
|
foreach ($event['EventTag'] as $k => $tag) {
|
|
|
|
$event['Event']['Tag'][$k] = $tag['Tag'];
|
|
|
|
}
|
|
|
|
}
|
2015-10-22 09:59:00 +02:00
|
|
|
|
2015-10-15 17:39:17 +02:00
|
|
|
if (isset($event['RelatedAttribute'])) $event['Event']['RelatedAttribute'] = $event['RelatedAttribute'];
|
|
|
|
else $event['Event']['RelatedAttribute'] = array();
|
2015-02-16 17:31:32 +01:00
|
|
|
//
|
|
|
|
// cleanup the array from things we do not want to expose
|
|
|
|
//
|
|
|
|
unset($event['Event']['user_id']);
|
|
|
|
// hide the org field is we are not in showorg mode
|
|
|
|
if (!Configure::read('MISP.showorg') && !$isSiteAdmin) {
|
|
|
|
unset($event['Event']['org']);
|
|
|
|
unset($event['Event']['orgc']);
|
|
|
|
unset($event['Event']['from']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($event['Event']['Attribute'])) {
|
|
|
|
// remove value1 and value2 from the output and remove invalid utf8 characters for the xml parser
|
|
|
|
foreach ($event['Event']['Attribute'] as $key => $value) {
|
|
|
|
unset($event['Event']['Attribute'][$key]['value1']);
|
|
|
|
unset($event['Event']['Attribute'][$key]['value2']);
|
|
|
|
unset($event['Event']['Attribute'][$key]['category_order']);
|
2015-10-15 17:39:17 +02:00
|
|
|
if (isset($event['Event']['RelatedAttribute'][$value['id']])) $event['Event']['Attribute'][$key]['RelatedAttribute'] = $event['Event']['RelatedAttribute'][$value['id']];
|
2015-02-16 17:31:32 +01:00
|
|
|
}
|
|
|
|
}
|
2015-10-15 17:39:17 +02:00
|
|
|
unset($event['Event']['RelatedAttribute']);
|
|
|
|
|
2015-02-16 17:31:32 +01:00
|
|
|
if (isset($event['Event']['RelatedEvent'])) {
|
|
|
|
foreach ($event['Event']['RelatedEvent'] as $key => $value) {
|
|
|
|
$temp = $value['Event'];
|
|
|
|
unset($event['Event']['RelatedEvent'][$key]['Event']);
|
|
|
|
$event['Event']['RelatedEvent'][$key]['Event'][0] = $temp;
|
|
|
|
unset($event['Event']['RelatedEvent'][$key]['Event'][0]['user_id']);
|
|
|
|
if (!Configure::read('MISP.showorg') && !$isSiteAdmin) {
|
|
|
|
unset($event['Event']['RelatedEvent'][$key]['Event'][0]['org']);
|
|
|
|
unset($event['Event']['RelatedEvent'][$key]['Event'][0]['orgc']);
|
|
|
|
}
|
|
|
|
unset($temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$result = array('Event' => $event['Event']);
|
2015-12-01 15:39:12 +01:00
|
|
|
if (isset($event['errors']))$result = array_merge($result, array('errors' => $event['errors']));
|
2015-02-16 17:31:32 +01:00
|
|
|
return json_encode($result);
|
|
|
|
}
|
2015-11-30 02:28:07 +01:00
|
|
|
|
2015-12-01 15:39:12 +01:00
|
|
|
public function arrayPrinter($array, $root = true) {
|
2015-11-30 02:28:07 +01:00
|
|
|
if (is_array($array)) {
|
|
|
|
$resultArray = array();
|
|
|
|
foreach ($array as $k => $element) {
|
2015-12-01 15:39:12 +01:00
|
|
|
$temp = $this->arrayPrinter($element, false);
|
2015-11-30 02:28:07 +01:00
|
|
|
if (!is_array($temp)) {
|
|
|
|
$resultArray[] = '[' . $k .']' . $temp;
|
|
|
|
} else {
|
|
|
|
foreach ($temp as &$t) $resultArray[] = '[' . $k . ']' . $t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else $resultArray = ': ' . $array . PHP_EOL;
|
|
|
|
if ($root) {
|
|
|
|
$text = '';
|
|
|
|
foreach ($resultArray as &$r) $text .= $r;
|
|
|
|
return $text;
|
|
|
|
} else return $resultArray;
|
|
|
|
}
|
2015-02-16 17:31:32 +01:00
|
|
|
}
|