mirror of https://github.com/MISP/MISP
Added downloading of an event from the index, better error handling
parent
113d9cd82d
commit
f767ddaec6
|
@ -98,7 +98,31 @@ class FeedsController extends AppController {
|
|||
$HttpSocket = $syncTool->setupHttpSocketFeed($this->Feed->data);
|
||||
$actions = $this->Feed->getNewEventUuids($this->Feed->data, $HttpSocket);
|
||||
$result = $this->Feed->downloadFromFeed($actions, $this->Feed->data, $HttpSocket, $this->Auth->user());
|
||||
return new CakeResponse(array('body'=> json_encode(array('saved' => true, 'success' => 'Attribute added.')),'status'=>200));
|
||||
$message = 'Fetching the feed has successfuly completed.';
|
||||
if (isset($result['add'])) $message .= ' Downloaded ' . count($result['add']) . ' new event(s).';
|
||||
if (isset($result['edit'])) $message .= ' Updated ' . count($result['edit']) . ' event(s).';
|
||||
$this->Session->setFlash($message);
|
||||
$this->redirect(array('action' => 'index'));
|
||||
}
|
||||
|
||||
public function getEvent($feedId, $eventUuid, $all = false) {
|
||||
$this->Feed->id = $feedId;
|
||||
if (!$this->Feed->exists()) throw new NotFoundException('Invalid feed.');
|
||||
$this->Feed->read();
|
||||
$result = $this->Feed->downloadAndSaveEventFromFeed($this->Feed->data, $eventUuid, $this->Auth->user());
|
||||
if (isset($result['action'])) {
|
||||
if ($result['result']) {
|
||||
if ($result['action'] == 'add') $message = 'Event added.';
|
||||
else {
|
||||
if ($result['result'] === 'No change') $message = 'Event already up to date.';
|
||||
else $message = 'Event updated.';
|
||||
}
|
||||
} else {
|
||||
$message = 'Could not ' . $result['action'] . ' event.';
|
||||
}
|
||||
} else $message = 'Download failed.';
|
||||
$this->Session->setFlash($message);
|
||||
$this->redirect(array('action' => 'previewIndex', $feedId));
|
||||
}
|
||||
|
||||
public function previewIndex($feedId) {
|
||||
|
@ -114,7 +138,7 @@ class FeedsController extends AppController {
|
|||
$this->Feed->read();
|
||||
$HttpSocket = $syncTool->setupHttpSocketFeed($this->Feed->data);
|
||||
$events = $this->Feed->getManifest($this->Feed->data, $HttpSocket);
|
||||
|
||||
if (isset($events['code'])) throw new NotFoundException('Feed could not be fetched. The HTTP error code returned was: ' .$events['code']);
|
||||
$pageCount = count($events);
|
||||
App::uses('CustomPaginationTool', 'Tools');
|
||||
$customPagination = new CustomPaginationTool();
|
||||
|
|
|
@ -1978,7 +1978,7 @@ class Event extends AppModel {
|
|||
} else {
|
||||
// If a field is not set in the request, just reuse the old value
|
||||
$recoverFields = array('value', 'to_ids', 'distribution', 'category', 'type', 'comment', 'sharing_group_id');
|
||||
foreach ($recoverFields as $rF) if (!isset($attribute[$rF])) $data['Event']['Attribute'][$c][$rF] = $existingAttribute['Attribute'][$rF];
|
||||
foreach ($recoverFields as $rF) if (!isset($attribute[$rF])) $data['Event']['Attribute'][$k][$rF] = $existingAttribute['Attribute'][$rF];
|
||||
$data['Event']['Attribute'][$k]['id'] = $existingAttribute['Attribute']['id'];
|
||||
// Check if the attribute's timestamp is bigger than the one that already exists.
|
||||
// If yes, it means that it's newer, so insert it. If no, it means that it's the same attribute or older - don't insert it, insert the old attribute.
|
||||
|
|
|
@ -83,7 +83,7 @@ class Feed extends AppModel {
|
|||
if (isset($actions['add']) && !empty($actions['add'])) {
|
||||
foreach ($actions['add'] as $uuid) {
|
||||
$result = $this->__addEventFromFeed($HttpSocket, $feed, $uuid, $user, $filterRules);
|
||||
if ($result === 'blocked') debug('blocked: ' . $uuid);
|
||||
if ($result === 'blocked') continue;
|
||||
if ($result === true) {
|
||||
$results['add']['success'] = $uuid;
|
||||
} else {
|
||||
|
@ -94,6 +94,7 @@ class Feed extends AppModel {
|
|||
if (isset($actions['edit']) && !empty($actions['edit'])) {
|
||||
foreach ($actions['edit'] as $editTarget) {
|
||||
$result = $this->__updateEventFromFeed($HttpSocket, $feed, $editTarget['uuid'], $editTarget['id'], $user, $filterRules);
|
||||
if ($result === 'blocked') continue;
|
||||
if ($result === true) {
|
||||
$results['edit']['success'] = $uuid;
|
||||
} else {
|
||||
|
@ -101,7 +102,6 @@ class Feed extends AppModel {
|
|||
}
|
||||
}
|
||||
}
|
||||
throw new Exception();
|
||||
return $results;
|
||||
}
|
||||
|
||||
|
@ -181,6 +181,12 @@ class Feed extends AppModel {
|
|||
return $events;
|
||||
}
|
||||
|
||||
public function downloadAndSaveEventFromFeed($feed, $uuid, $user) {
|
||||
$event = $this->downloadEventFromFeed($feed, $uuid, $user);
|
||||
if (!is_array($event) || isset($event['code'])) return false;
|
||||
return $this->__saveEvent($event, $user);
|
||||
}
|
||||
|
||||
public function downloadEventFromFeed($feed, $uuid, $user) {
|
||||
$HttpSocket = $this->__setupHttpSocket($feed);
|
||||
$request = $this->__createFeedRequest();
|
||||
|
@ -190,18 +196,37 @@ class Feed extends AppModel {
|
|||
return false;
|
||||
} else {
|
||||
$filterRules = $this->__prepareFilterRules($feed);
|
||||
$event = $this->__prepareEvent($response->body, $filterRules);
|
||||
if ($event !== true) return $event;
|
||||
$this->Event = ClassRegistry::init('Event');
|
||||
return $this->Event->_add($event, true, $user);
|
||||
return $this->__prepareEvent($response->body, $filterRules);
|
||||
}
|
||||
}
|
||||
|
||||
private function __saveEvent($event, $user) {
|
||||
$this->Event = ClassRegistry::init('Event');
|
||||
$existingEvent = $this->Event->find('first', array(
|
||||
'conditions' => array('Event.uuid' => $event['Event']['uuid']),
|
||||
'recursive' => -1,
|
||||
'fields' => array('Event.uuid', 'Event.id', 'Event.timestamp')
|
||||
));
|
||||
$result = array();
|
||||
if (!empty($existingEvent)) {
|
||||
$result['action'] = 'edit';
|
||||
if ($existingEvent['Event']['timestamp'] < $event['Event']['timestamp']) {
|
||||
$result['result'] = $this->Event->_edit($event, true, $user);
|
||||
} else $result['result'] = 'No change';
|
||||
} else {
|
||||
$result['action'] = 'add';
|
||||
$result['result'] = $this->Event->_add($event, true, $user);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function __prepareEvent($body, $filterRules) {
|
||||
$event = json_decode($body, true);
|
||||
if (isset($event['response'])) $event = $event['response'];
|
||||
if (isset($event[0])) $event = $event[0];
|
||||
if (!isset($event['Event']['uuid'])) return false;
|
||||
$event['Event']['distribution'] = 3;
|
||||
foreach ($event['Event']['Attribute'] as &$attribute) $attribute['distribution'] = 5;
|
||||
if (!$this->__checkIfEventBlockedByFilter($event, $filterRules)) return 'blocked';
|
||||
return $event;
|
||||
}
|
||||
|
@ -227,13 +252,14 @@ class Feed extends AppModel {
|
|||
} else {
|
||||
$filterRules = $this->__prepareFilterRules($feed);
|
||||
$event = $this->__prepareEvent($response->body, $filterRules);
|
||||
$this->Event = ClassRegistry::init('Event');
|
||||
return $this->Event->_add($event, true, $user);
|
||||
if (is_array($event)) {
|
||||
$this->Event = ClassRegistry::init('Event');
|
||||
return $this->Event->_add($event, true, $user);
|
||||
} else return $event;
|
||||
}
|
||||
}
|
||||
|
||||
private function __updateEventFromFeed($HttpSocket, $feed, $uuid, $eventId, $user, $filterRules) {
|
||||
debug($uuid);
|
||||
$request = $this->__createFeedRequest();
|
||||
$uri = $feed['Feed']['url'] . '/' . $uuid . '.json';
|
||||
$response = $HttpSocket->get($uri, '', $request);
|
||||
|
|
Loading…
Reference in New Issue