diff --git a/app/Lib/Export/Stix1Export.php b/app/Lib/Export/Stix1Export.php index 69cff7631..df880f9ca 100644 --- a/app/Lib/Export/Stix1Export.php +++ b/app/Lib/Export/Stix1Export.php @@ -18,9 +18,10 @@ class Stix1Export extends StixExport return $this->pythonBin() . ' ' . $this->__framing_script . ' stix1 -v ' . $this->__version . ' -n ' . $this->__baseurl . ' -o ' . $this->__org . ' -f ' . $this->__return_format . ' ' . $this->__end_of_cmd; } - protected function __parse_misp_events($filenames) + protected function __parse_misp_events(array $filenames) { + $filenames = implode(' ', $filenames); $scriptFile = $this->__scripts_dir . $this->__script_name; - return shell_exec($this->pythonBin() . ' ' . $scriptFile . '-v ' . $this->__version . ' -f ' . $this->__return_format . ' -o ' . $this->__org . ' -i ' . $this->__tmp_dir . $filenames . $this->__end_of_cmd); + return shell_exec($this->pythonBin() . ' ' . $scriptFile . '-v ' . $this->__version . ' -f ' . $this->__return_format . ' -o ' . $this->__org . ' -i ' . $filenames . $this->__end_of_cmd); } } diff --git a/app/Lib/Export/Stix2Export.php b/app/Lib/Export/Stix2Export.php index 9686fe49d..789d80cff 100644 --- a/app/Lib/Export/Stix2Export.php +++ b/app/Lib/Export/Stix2Export.php @@ -14,11 +14,11 @@ class Stix2Export extends StixExport return $this->pythonBin() . ' ' . $this->__framing_script . ' stix2 -v ' . $this->__version . ' --uuid ' . escapeshellarg(CakeText::uuid()) . $this->__end_of_cmd; } - protected function __parse_misp_events($filenames) + protected function __parse_misp_events(array $filenames) { $scriptFile = $this->__scripts_dir . $this->__script_name; - $filenames = implode(' ' . $this->__tmp_dir, $this->__filenames); - $result = shell_exec($this->pythonBin() . ' ' . $scriptFile . '-v ' . $this->__version . ' -i ' . $this->__tmp_dir . $filenames . $this->__end_of_cmd); + $filenames = implode(' ', $filenames); + $result = shell_exec($this->pythonBin() . ' ' . $scriptFile . '-v ' . $this->__version . ' -i ' . $filenames . $this->__end_of_cmd); $result = preg_split("/\r\n|\n|\r/", trim($result)); return end($result); } diff --git a/app/Lib/Export/StixExport.php b/app/Lib/Export/StixExport.php index 3655ac2b0..f12d9f92d 100644 --- a/app/Lib/Export/StixExport.php +++ b/app/Lib/Export/StixExport.php @@ -10,10 +10,11 @@ abstract class StixExport ); protected $__return_format = 'json'; protected $__scripts_dir = APP . 'files/scripts/'; - protected $__tmp_dir = APP . 'files/scripts/tmp/'; protected $__framing_script = APP . 'files/scripts/misp_framing.py'; protected $__end_of_cmd = ' 2>' . APP . 'tmp/logs/exec-errors.log'; protected $__return_type = null; + + /** @var array Full paths to files to convert */ protected $__filenames = array(); protected $__default_filters = null; protected $__version = null; @@ -34,7 +35,7 @@ abstract class StixExport public function setDefaultFilters($filters) { - $sane_version = (!empty($filters['stix-version']) && in_array($filters['stix-version'], $this->__sane_versions)); + $sane_version = !empty($filters['stix-version']) && in_array($filters['stix-version'], $this->__sane_versions, true); $this->__version = $sane_version ? $filters['stix-version'] : $this->__default_version; } @@ -54,9 +55,8 @@ abstract class StixExport $this->__n_attributes += $attributes_count; $this->__empty_file = false; } elseif ($attributes_count > $this->__attributes_limit) { - $randomFileName = $this->__generateRandomFileName(); - FileAccessTool::writeToFile($this->__tmp_dir . $randomFileName, $event); - $this->__filenames[] = $randomFileName; + $filePath = FileAccessTool::writeToTempFile( $event); + $this->__filenames[] = $filePath; } else { $this->__tmp_file->append(']}'); $this->__tmp_file->close(); @@ -71,9 +71,9 @@ abstract class StixExport public function header($options = array()) { $this->__return_type = $options['returnFormat']; - if ($this->__return_type == 'stix-json') { + if ($this->__return_type === 'stix-json') { $this->__return_type = 'stix'; - } else if ($this->__return_type == 'stix') { + } else if ($this->__return_type === 'stix') { $this->__return_format = 'xml'; } $framing_cmd = $this->__initiate_framing_params(); @@ -85,7 +85,7 @@ abstract class StixExport } /** - * @return string|TmpFileTool + * @return TmpFileTool * @throws Exception */ public function footer() @@ -98,18 +98,16 @@ abstract class StixExport $this->__tmp_file->close(); $this->__filenames[] = $this->__current_filename; } - $filenames = implode(' ' . $this->__tmp_dir, $this->__filenames); - $result = $this->__parse_misp_events($filenames); + $result = $this->__parse_misp_events($this->__filenames); $decoded = json_decode($result, true); if (!isset($decoded['success']) || !$decoded['success']) { $this->__delete_temporary_files(); $error = $decoded && !empty($decoded['error']) ? $decoded['error'] : $result; - return 'Error while processing your query: ' . $error; + throw new Exception('Error while processing your query during STIX export: ' . $error); } foreach ($this->__filenames as $filename) { - $stix_event = FileAccessTool::readFromFile($this->__tmp_dir . $filename . '.out'); + $stix_event = FileAccessTool::readAndDelete($filename . '.out'); $stix_event = $this->__return_type === 'stix' ? $stix_event : substr($stix_event, 1, -1); - FileAccessTool::deleteFile($this->__tmp_dir . $filename . '.out'); $this->__stix_file->writeWithSeparator($stix_event, $this->__framing['separator']); } @@ -124,8 +122,8 @@ abstract class StixExport private function __initialize_misp_file() { - $this->__current_filename = $this->__generateRandomFileName(); - $this->__tmp_file = new File($this->__tmp_dir . $this->__current_filename, true, 0644); + $this->__current_filename = FileAccessTool::createTempFile(); + $this->__tmp_file = new File($this->__current_filename); $this->__tmp_file->write('{"response": ['); $this->__empty_file = true; } @@ -138,7 +136,7 @@ abstract class StixExport private function __delete_temporary_files() { foreach ($this->__filenames as $filename) { - @unlink($this->__tmp_dir . $filename); + FileAccessTool::deleteFileIfExists($filename); } } @@ -153,7 +151,14 @@ abstract class StixExport return $this->Server->getPythonVersion(); } - abstract protected function __parse_misp_events($filenames); + /** + * @param array $filenames Paths to files to process + * @return string|false|null + */ + abstract protected function __parse_misp_events(array $filenames); + /** + * @return string + */ abstract protected function __initiate_framing_params(); } diff --git a/app/Lib/Tools/FileAccessTool.php b/app/Lib/Tools/FileAccessTool.php index c6e8a8342..438d471f1 100644 --- a/app/Lib/Tools/FileAccessTool.php +++ b/app/Lib/Tools/FileAccessTool.php @@ -47,6 +47,18 @@ class FileAccessTool return $content; } + /** + * @param string $file + * @return string + * @throws Exception + */ + public static function readAndDelete($file) + { + $content = self::readFromFile($file); + self::deleteFile($file); + return $content; + } + /** * @param string $file * @param mixed $content