new: [api] CSV export using thin overlay over restsearch

pull/3766/head
iglocska 2018-10-04 22:23:32 +02:00
parent 88944cb792
commit be5b684893
1 changed files with 9 additions and 55 deletions

View File

@ -1147,6 +1147,7 @@ class EventsController extends AppController
}
$this->set('sightingTypes', $this->Sighting->type);
$this->set('currentUri', $this->params->here);
$this->layout = false;
$this->render('/Elements/eventattribute');
}
@ -2682,7 +2683,7 @@ class EventsController extends AppController
return new CakeResponse(array('body'=> implode(PHP_EOL, $rules), 'status' => 200, 'type' => 'txt'));
}
// csv function
// csv function ***DEPRECATED***
// Usage: csv($key, $eventid) - key can be a valid auth key or the string 'download'. Download requires the user to be logged in interactively and will generate a .csv file
// $eventid can be one of 3 options: left empty it will get all the visible to_ids attributes,
// $ignore is a flag that allows the export tool to ignore the ids flag. 0 = only IDS signatures, 1 = everything.
@ -2696,8 +2697,8 @@ class EventsController extends AppController
'ordered_url_params' => compact($paramArray)
);
$exception = false;
$params = $this->_harvestParameters($filterData, $exception);
if ($params === false) {
$filters = $this->_harvestParameters($filterData, $exception);
if ($filters === false) {
return $exception;
}
$list = array();
@ -2705,6 +2706,7 @@ class EventsController extends AppController
if ($user === false) {
return $exception;
}
$final = $this->Event->restSearch($user, 'csv', $filters);
// if it's a search, grab the attributeIDList from the session and get the IDs from it. Use those as the condition
// We don't need to look out for permissions since that's filtered by the search itself
// We just want all the attributes found by the search
@ -2733,46 +2735,8 @@ class EventsController extends AppController
$list[] = $attribute['Attribute']['id'];
}
}
$final = array();
if (isset($params['eventid']) && $params['eventid'] == 'all') {
unset($params['eventid']);
}
foreach ($possibleParams as $possibleParam) {
if (isset($params[$possibleParam])) {
$params[$possibleParam] = $params[$possibleParam];
}
}
$params['limit'] = 1000;
$params['page'] = 1;
$i = 0;
$continue = true;
$params = array_merge($params, array(
'requested_obj_attributes' => $requested_obj_attributes,
'requested_attributes' => $requested_attributes,
'includeContext' => $includeContext
));
App::uses('CsvExport', 'Export');
$export = new CsvExport();
$final = $export->header($params);
while ($continue) {
$attributes = $this->Event->csv($user, $params, false, $continue);
$params['page'] += 1;
$final .= $export->handler($attributes, $params);
$final .= $export->separator($attributes);
}
$export->footer();
$this->response->type('csv'); // set the content type
if (empty($params['eventid'])) {
$filename = "misp.filtered_attributes.csv";
} elseif ($params['eventid'] === 'search') {
$filename = "misp.search_result.csv";
} else {
if (is_array($params['eventid'])) {
$params['eventid'] = 'list';
}
$filename = "misp.event_" . $params['eventid'] . ".csv";
}
return $this->RestResponse->viewData($final, 'csv', false, true, $filename);
$responseType = 'csv';
return $this->RestResponse->viewData($final, $responseType, false, true, 'download.csv');
}
public function _addIOCFile($id)
@ -2993,16 +2957,6 @@ class EventsController extends AppController
'paramArray' => $paramArray,
'ordered_url_params' => compact($paramArray)
);
$validFormats = array(
'openioc' => array('xml', 'OpeniocExport', 'ioc'),
'json' => array('json', 'JsonExport', 'json'),
'xml' => array('xml', 'XmlExport', 'xml'),
'suricata' => array('txt', 'NidsSuricataExport', 'rules'),
'snort' => array('txt', 'NidsSnortExport', 'rules'),
'rpz' => array('rpz', 'RPZExport', 'rpz'),
'text' => array('text', 'TextExport', 'txt'),
'csv' => array('csv', 'CsvExport', 'csv')
);
$exception = false;
$filters = $this->_harvestParameters($filterData, $exception);
unset($filterData);
@ -3020,8 +2974,8 @@ class EventsController extends AppController
if ($returnFormat === 'download') {
$returnFormat = 'json';
}
$final = $this->Event->restSearch($user, $validFormats, $returnFormat, $filters);
$responseType = $validFormats[$returnFormat][0];
$final = $this->Event->restSearch($user, $returnFormat, $filters);
$responseType = $this->Event->validFormats[$returnFormat][0];
return $this->RestResponse->viewData($final, $responseType, false, true);
}