chg: [internal] Refactor FileAccessTool

pull/7800/head
Jakub Onderka 2021-10-03 23:53:03 +02:00
parent 4c7d151f2f
commit 3dd23605bc
4 changed files with 79 additions and 46 deletions

View File

@ -2168,7 +2168,7 @@ class EventsController extends AppController
$isXml = $ext === 'xml';
App::uses('FileAccessTool', 'Tools');
$data = (new FileAccessTool())->readFromFile($file['tmp_name'], $file['size']);
$data = FileAccessTool::readFromFile($file['tmp_name'], $file['size']);
$takeOwnership = Configure::read('MISP.take_ownership_xml_import')
&& (isset($this->data['Event']['takeownership']) && $this->data['Event']['takeownership'] == 1);
@ -3119,8 +3119,7 @@ class EventsController extends AppController
}
App::uses('FileAccessTool', 'Tools');
$fileAccessTool = new FileAccessTool();
$iocData = $fileAccessTool->readFromFile($this->data['Event']['submittedioc']['tmp_name'], $this->data['Event']['submittedioc']['size']);
$iocData = FileAccessTool::readFromFile($this->data['Event']['submittedioc']['tmp_name'], $this->data['Event']['submittedioc']['size']);
// write
$attachments_dir = Configure::read('MISP.attachments_dir');
@ -3140,7 +3139,7 @@ class EventsController extends AppController
// open the xml
$xmlFilePath = $destPath . DS . $this->data['Event']['submittedioc']['name'];
$xmlFileData = $fileAccessTool->readFromFile($xmlFilePath, $this->data['Event']['submittedioc']['size']);
$xmlFileData = FileAccessTool::readFromFile($xmlFilePath, $this->data['Event']['submittedioc']['size']);
// Load event and populate the event data
$this->Event->id = $id;
@ -4252,11 +4251,10 @@ class EventsController extends AppController
$successCount = 0;
$errors = array();
App::uses('FileAccessTool', 'Tools');
$fileAccessTool = new FileAccessTool();
foreach ($data['files'] as $file) {
$tmpdir = Configure::read('MISP.tmpdir') ? Configure::read('MISP.tmpdir') : APP . 'tmp';
$tmpfile = $fileAccessTool->createTempFile($tmpdir, $prefix = 'MISP_upload');
$fileAccessTool->writeToFile($tmpfile, base64_decode($file['data']));
$tmpfile = FileAccessTool::createTempFile($tmpdir, $prefix = 'MISP_upload');
FileAccessTool::writeToFile($tmpfile, base64_decode($file['data']));
$tmpfile = new File($tmpfile);
if ($advanced) {
$result = $this->Event->Attribute->advancedAddMalwareSample(
@ -4309,7 +4307,7 @@ class EventsController extends AppController
}
}
}
$fileAccessTool->deleteFile($tmpfile->path);
FileAccessTool::deleteFile($tmpfile->path);
}
if (!empty($errors)) {
$this->set('errors', $errors);
@ -4927,10 +4925,9 @@ class EventsController extends AppController
foreach ($resultArray as $key => $result) {
if (isset($result['data'])) {
App::uses('FileAccessTool', 'Tools');
$fileAccessTool = new FileAccessTool();
$tmpdir = Configure::read('MISP.tmpdir') ? Configure::read('MISP.tmpdir') : '/tmp';
$tempFile = $fileAccessTool->createTempFile($tmpdir, $prefix = 'MISP');
$fileAccessTool->writeToFile($tempFile, $result['data']);
$tempFile = FileAccessTool::createTempFile($tmpdir, $prefix = 'MISP');
FileAccessTool::writeToFile($tempFile, $result['data']);
$resultArray[$key]['data'] = basename($tempFile) . '|' . filesize($tempFile);
}
}
@ -5057,7 +5054,7 @@ class EventsController extends AppController
if ((isset($fileupload['error']) && $fileupload['error'] == 0) || (!empty($fileupload['tmp_name']) && $fileupload['tmp_name'] != 'none') && is_uploaded_file($tmpfile->path)) {
$filename = basename($fileupload['name']);
App::uses('FileAccessTool', 'Tools');
$modulePayload['data'] = (new FileAccessTool())->readFromFile($fileupload['tmp_name'], $fileupload['size']);
$modulePayload['data'] = FileAccessTool::readFromFile($fileupload['tmp_name'], $fileupload['size']);
} else {
$fail = 'Invalid file upload.';
}

View File

@ -880,7 +880,7 @@ class ServersController extends AppController
}
// read pem file data
$pemData = (new FileAccessTool())->readFromFile($server['Server'][$subm]['tmp_name'], $server['Server'][$subm]['size']);
$pemData = FileAccessTool::readFromFile($server['Server'][$subm]['tmp_name'], $server['Server'][$subm]['size']);
} else {
return true;
}

View File

@ -2,51 +2,88 @@
class FileAccessTool
{
private $__fileErrorMsgPrefix = 'An error has occurred while attempting to ';
public function createTempFile($dir, $prefix = 'MISP')
/**
* Creates temporary file, but you have to delete it after use.
* @param string $dir
* @param string $prefix
* @return string
* @throws Exception
*/
public static function createTempFile($dir, $prefix = 'MISP')
{
$tempFile = tempnam($dir, $prefix);
$this->__checkForFalse($tempFile, 'create a temporary file in path "' . $dir);
if ($tempFile === false) {
throw new Exception("An error has occurred while attempt to create a temporary file in path `$dir`.");
}
return $tempFile;
}
public function readFromFile($file, $fileSize = -1)
/**
* @param string $file
* @param int $fileSize
* @return string
* @throws Exception
*/
public static function readFromFile($file, $fileSize = -1)
{
$this->__checkForFalse($file, 'create file "' . $file);
$fileHandle = fopen($file, 'rb');
$this->__checkForFalse($fileHandle, 'access file "' . $file);
if ($fileSize === -1) {
$fileSize = filesize($file);
$this->__checkForFalse($fileHandle, 'get filesize from file "' . $file);
$fileSize = $fileSize === -1 ? null : $fileSize;
$content = file_get_contents($file, false, null, 0, $fileSize);
if ($content === false) {
throw new Exception("An error has occurred while attempt to read file `$file`.");
}
$readResult = fread($fileHandle, $fileSize);
$this->__checkForFalse($fileHandle, 'read from file "' . $file);
fclose($fileHandle);
return $readResult;
return $content;
}
public function writeToFile($file, $content)
/**
* @param string $file
* @param mixed $content
* @throws Exception
*/
public static function writeToFile($file, $content)
{
$this->__checkForFalse($file, 'create file "' . $file);
$fileHandle = fopen($file, 'wb');
$this->__checkForFalse($fileHandle, 'access file "' . $file);
$writeResult = fwrite($fileHandle, $content);
$this->__checkForFalse($writeResult, 'write to file "' . $file);
fclose($fileHandle);
return $file;
if (file_put_contents($file, $content, LOCK_EX) === false) {
throw new Exception("An error has occurred while attempt to write to file `$file`.");
}
}
private function __checkForFalse($result, $errorMsgPart)
/**
* @param string $file
* @param mixed $content
* @throws Exception
*/
public static function writeCompressedFile($file, $content)
{
$res = gzopen($file, 'wb1');
if ($res === false) {
throw new Exception("An error has occurred while attempt to open file `$file` for writing.");
}
$result = gzwrite($res, $content);
if ($result === false) {
throw new MethodNotAllowedException($this->__fileErrorMsgPrefix . $errorMsgPart . '".');
throw new Exception("An error has occurred while attempt to write into file `$file`.");
}
gzclose($res);
return $result;
}
public function deleteFile($file)
/**
* @param string $file
* @return bool
*/
public static function deleteFile($file)
{
unlink($file);
return true;
return unlink($file);
}
/**
* @param string $file
* @return bool
*/
public static function deleteFileIfExists($file)
{
if (file_exists($file)) {
return unlink($file);
} else {
return true;
}
}
}

View File

@ -885,7 +885,7 @@ class SendEmail
* @param string $content
* @return File[]
* @throws SendEmailException
* @throws MethodNotAllowedException
* @throws Exception
*/
private function createInputOutputFiles($content)
{
@ -897,11 +897,10 @@ class SendEmail
}
App::uses('FileAccessTool', 'Tools');
$fileAccessTool = new FileAccessTool();
$inputFile = $fileAccessTool->createTempFile($dir, 'SMIME');
$fileAccessTool->writeToFile($inputFile, $content);
$inputFile = FileAccessTool::createTempFile($dir, 'SMIME');
FileAccessTool::writeToFile($inputFile, $content);
$outputFile = $fileAccessTool->createTempFile($dir, 'SMIME');
$outputFile = FileAccessTool::createTempFile($dir, 'SMIME');
return array(new File($inputFile), new File($outputFile));
}