new: [sighting/api] xml output format + improved error feedback

pull/3789/head
Sami Mokaddem 2018-10-23 13:06:37 +02:00
parent 01cba114f2
commit ff5f5faf02
3 changed files with 21 additions and 19 deletions

View File

@ -222,6 +222,7 @@ class SightingsController extends AppController
public function restSearch($context = false)
{
$allowedContext = array(false, 'event', 'attribute');
$paramArray = array('returnFormat', 'id', 'type', 'from', 'to', 'last', 'org_id', 'includeAttribute', 'includeEvent');
$filterData = array(
'request' => $this->request,
@ -230,6 +231,14 @@ class SightingsController extends AppController
'ordered_url_params' => compact($paramArray)
);
$filters = $this->_harvestParameters($filterData, $exception);
// ensure that an id is provided if context is set
if (!in_array($context, $allowedContext, true)) {
throw new MethodNotAllowedException(_('Invalid context.'));
}
if ($context !== false && !isset($filters['id'])) {
throw new MethodNotAllowedException(_('An id must be provided if the context is set.'));
}
$filters['context'] = $context;
if (isset($filters['returnFormat'])) {
@ -239,11 +248,6 @@ class SightingsController extends AppController
$returnFormat = 'json';
}
// ensure that an id is provided if context is set
if ($context !== false && !isset($filters['id'])) {
throw new MethodNotAllowedException(_('An id must be provided if the context is set.'));
}
$sightings = $this->Sighting->restSearch($this->Auth->user(), $returnFormat, $filters);
$validFormats = $this->Sighting->validFormats;

View File

@ -9,8 +9,10 @@ class XmlExport
{
if ($options['scope'] === 'Attribute') {
return $this->__attributeHandler($data, $options);
} else {
return $this->__eventHandler($data, $options);
} else if($options['scope'] === 'Event') {
return $this->__eventsHandler($data, $options);
} else if($options['scope'] === 'Sighting') {
return $this->__sightingsHandler($data, $options);
}
}
@ -50,6 +52,12 @@ class XmlExport
return substr($xmlString, strpos($xmlString, "\n") + 1);
}
private function __sightingsHandler($sighting, $options = array()) {
$xmlObject = Xml::fromArray(array('Sighting' => $sighting), array('format' => 'tags'));
$xmlString = $xmlObject->asXML();
return substr($xmlString, strpos($xmlString, "\n") + 1);
}
public function header($options = array())
{
return '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL . '<response>';

View File

@ -40,16 +40,8 @@ class Sighting extends AppModel
public $validFormats = array(
'json' => array('json', 'JsonExport', 'json'),
//'openioc' => array('xml', 'OpeniocExport', 'ioc'),
//'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'),
//'stix' => array('xml', 'Stix1Export', 'xml'),
//'stix2' => array('json', 'Stix2Export', 'json'),
//'cache' => array('txt', 'CacheExport', 'cache')
'xml' => array('xml', 'XmlExport', 'xml'),
'csv' => array('csv', 'CsvExport', 'csv')
);
public function beforeValidate($options = array())
@ -581,7 +573,5 @@ class Sighting extends AppModel
$final = fread($tmpfile, fstat($tmpfile)['size']);
fclose($tmpfile);
return $final;
//return $allowedSightings;
}
}