Commit 0f7fa47d authored by Quentin Ribac's avatar Quentin Ribac
Browse files

FIX #17437 TIME 1:30 revert to finfo::buffer where the new method was useless,...

FIX #17437 TIME 1:30 revert to finfo::buffer where the new method was useless, added error handling for new method, removed “resource” mode for getMimeTypeAndFileSize()
parent 055c5d52
......@@ -510,7 +510,11 @@ class AttachmentController
return $response->withStatus(404)->withJson(['errors' => 'Thumbnail not found on docserver']);
}
$mimeType = CoreController::getMimeTypeAndFileSize(['path' => $pathToThumbnail])['mime'];
$mimeAndSize = CoreController::getMimeTypeAndFileSize(['path' => $pathToThumbnail]);
if (!empty($mimeAndSize['errors'])) {
return $response->withStatus(400)->withJson(['errors' => $mimeAndSize['errors']]);
}
$mimeType = $mimeAndSize['mime'];
$response->write($fileContent);
$response = $response->withAddedHeader('Content-Disposition', "inline; filename=maarch.{$pathInfo['extension']}");
......@@ -678,7 +682,11 @@ class AttachmentController
$data = $request->getQueryParams();
$mimeType = CoreController::getMimeTypeAndFileSize(['path' => $pathToDocument])['mime'];
$mimeAndSize = CoreController::getMimeTypeAndFileSize(['path' => $pathToDocument]);
if (!empty($mimeAndSize['errors'])) {
return $response->withStatus(400)->withJson(['errors' => $mimeAndSize['errors']]);
}
$mimeType = $mimeAndSize['mime'];
$filename = TextFormatModel::formatFilename(['filename' => $attachment['title'], 'maxLength' => 250]);
if ($data['mode'] == 'base64') {
......@@ -762,7 +770,11 @@ class AttachmentController
return $response->withStatus(400)->withJson(['errors' => 'Document not found on docserver']);
}
$mimeType = CoreController::getMimeTypeAndFileSize(['path' => $pathToDocument])['mime'];
$mimeAndSize = CoreController::getMimeTypeAndFileSize(['path' => $pathToDocument]);
if (!empty($mimeAndSize['errors'])) {
return $response->withStatus(400)->withJson(['errors' => $mimeAndSize['errors']]);
}
$mimeType = $mimeAndSize['mime'];
$pathInfo = pathinfo($pathToDocument);
$data = $request->getQueryParams();
$filename = TextFormatModel::formatFilename(['filename' => $attachmentTodisplay['title'], 'maxLength' => 250]);
......
......@@ -116,7 +116,8 @@ class CollaboraOnlineController
$fileContent = base64_decode($content);
}
$mimeType = CoreController::getMimeTypeAndFileSize(['path' => $pathToDocument])['mime'];
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$mimeType = $finfo->buffer($fileContent);
$pathInfo = pathinfo($pathToDocument);
if ($tokenCheckResult['type'] == 'templateEncoded') {
......@@ -446,7 +447,8 @@ class CollaboraOnlineController
}
$fileContent = base64_decode($body['content']);
$mimeType = CoreController::getMimeTypeAndFileSize(['encodedFile' => $body['content']])['mime'];
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$mimeType = $finfo->buffer($fileContent);
if (!StoreController::isFileAllowed(['extension' => $body['format'], 'type' => $mimeType]) || !in_array($mimeType, TemplateController::AUTHORIZED_MIMETYPES)) {
return $response->withStatus(400)->withJson(['errors' => _WRONG_FILE_TYPE . ' : '.$mimeType]);
}
......
......@@ -240,7 +240,8 @@ class OnlyOfficeController
return $response->withStatus(400)->withJson(['errors' => 'No content found']);
}
$mimeType = CoreController::getMimeTypeAndFileSize(['path' => $tmpPath . $filename])['mime'];
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$mimeType = $finfo->buffer($fileContent);
$extension = pathinfo($tmpPath . $filename, PATHINFO_EXTENSION);
unlink($tmpPath . $filename);
......@@ -493,7 +494,8 @@ class OnlyOfficeController
return $response->withStatus(404)->withJson(['errors' => 'Document not found']);
}
$mimeType = CoreController::getMimeTypeAndFileSize(['path' => $jwt->fullFilename])['mime'];
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$mimeType = $finfo->buffer($fileContent);
$pathInfo = pathinfo($jwt->fullFilename);
$response->write($fileContent);
......
......@@ -327,6 +327,9 @@ class ConvertPdfController
$ext = substr($body['name'], strrpos($body['name'], '.') + 1);
$file = base64_decode($body['base64']);
$mimeAndSize = CoreController::getMimeTypeAndFileSize(['encodedFile' => $body['base64']]);
if (!empty($mimeAndSize['errors'])) {
return $response->withStatus(400)->withJson(['errors' => $mimeAndSize['errors']]);
}
$mimeType = $mimeAndSize['mime'];
$size = $mimeAndSize['size'];
......
......@@ -228,10 +228,13 @@ class MessageExchangeController
return $response->withStatus(400)->withJson(['errors' => 'Document not found on docserver']);
}
$fileContent = file_get_contents($pathToDocument);
$mimeType = CoreController::getMimeTypeAndFileSize(['path' => $pathToDocument])['mime'];
$mimeAndSize = CoreController::getMimeTypeAndFileSize(['path' => $pathToDocument]);
if (!empty($mimeAndSize['errors'])) {
return $response->withStatus(400)->withJson(['errors' => $mimeAndSize['errors']]);
}
$mimeType = $mimeAndSize['mime'];
$fileContent = file_get_contents($pathToDocument);
$response->write($fileContent);
$response = $response->withAddedHeader('Content-Disposition', "attachment; filename=maarch.zip");
return $response->withHeader('Content-Type', $mimeType);
......
......@@ -158,9 +158,11 @@ class ReceiveMessageExchangeController
return ['errors' => 'Bad Request'];
}
$file = base64_decode($aArgs['base64']);
$mimeType = CoreController::getMimeTypeAndFileSize(['encodedFile' => $aArgs['base64']])['mime'];
$mimeAndSize = CoreController::getMimeTypeAndFileSize(['encodedFile' => $aArgs['base64']]);
if (!empty($mimeAndSize['errors'])) {
return ['errors' => $mimeAndSize['errors']];
}
$mimeType = $mimeAndSize['mime'];
$ext = $aArgs['extension'];
$tmpName = 'tmp_file_' .$GLOBALS['login']. '_ArchiveTransfer_' .rand(). '.' . $ext;
......@@ -172,6 +174,8 @@ class ReceiveMessageExchangeController
return ['errors' => 'Filetype is not allowed'];
}
$file = base64_decode($aArgs['base64']);
$tmpPath = CoreConfigModel::getTmpPath();
file_put_contents($tmpPath . $tmpName, $file);
......
......@@ -508,7 +508,11 @@ class FolderPrintController
if (!file_exists($filePathOnTmp)) {
return $response->withStatus(500)->withJson(['errors' => 'Merged file not created']);
} else {
$mimeType = CoreController::getMimeTypeAndFileSize(['path' => $filePathOnTmp])['mime'];
$mimeAndSize = CoreController::getMimeTypeAndFileSize(['path' => $filePathOnTmp]);
if (!empty($mimeAndSize['errors'])) {
return $response->withStatus(400)->withJson(['errors' => $mimeAndSize['errors']]);
}
$mimeType = $mimeAndSize['mime'];
$response->write(file_get_contents($filePathOnTmp));
......
......@@ -501,7 +501,11 @@ class ResController extends ResourceControlController
$data = $request->getQueryParams();
$mimeType = CoreController::getMimeTypeAndFileSize(['path' => $pathToDocument])['mime'];
$mimeAndSize = CoreController::getMimeTypeAndFileSize(['path' => $pathToDocument]);
if (!empty($mimeAndSize['errors'])) {
return $response->withStatus(400)->withJson(['errors' => $mimeAndSize['errors']]);
}
$mimeType = $mimeAndSize['mime'];
$filename = TextFormatModel::formatFilename(['filename' => $subject, 'maxLength' => 250]);
if ($data['mode'] == 'base64') {
......@@ -704,7 +708,11 @@ class ResController extends ResourceControlController
'eventId' => 'resview',
]);
$mimeType = CoreController::getMimeTypeAndFileSize(['path' => $pathToDocument])['mime'];
$mimeAndSize = CoreController::getMimeTypeAndFileSize(['path' => $pathToDocument]);
if (!empty($mimeAndSize['errors'])) {
return $response->withStatus(400)->withJson(['errors' => $mimeAndSize['errors']]);
}
$mimeType = $mimeAndSize['mime'];
$pathInfo = pathinfo($pathToDocument);
$data = $request->getQueryParams();
$filename = TextFormatModel::formatFilename(['filename' => $subject, 'maxLength' => 250]);
......@@ -757,7 +765,11 @@ class ResController extends ResourceControlController
$fileContent = @file_get_contents($pathToThumbnail);
}
$mimeType = CoreController::getMimeTypeAndFileSize(['path' => $pathToThumbnail])['mime'];
$mimeAndSize = CoreController::getMimeTypeAndFileSize(['path' => $pathToThumbnail]);
if (!empty($mimeAndSize['errors'])) {
return $response->withStatus(400)->withJson(['errors' => $mimeAndSize['errors']]);
}
$mimeType = $mimeAndSize['mime'];
$pathInfo = pathinfo($pathToThumbnail);
$response->write($fileContent);
......
......@@ -243,8 +243,8 @@ class ResourceControlController
}
$mimeAndSize = CoreController::getMimeTypeAndFileSize(['encodedFile' => $body['encodedFile']]);
if (isset($mimeAndSize['errors'])) {
return $mimeAndSize['errors'];
if (!empty($mimeAndSize['errors'])) {
return ['errors' => $mimeAndSize['errors']];
}
if (!StoreController::isFileAllowed(['extension' => $body['format'], 'type' => $mimeAndSize['mime']])) {
......
......@@ -954,7 +954,8 @@ class UserController
return $response->withStatus(404)->withJson(['errors' => 'Signature not found on docserver']);
}
$mimeType = CoreController::getMimeTypeAndFileSize(['path' => $pathToSignature])['mime'];
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$mimeType = $finfo->buffer($image);
$response->write($image);
......@@ -982,9 +983,9 @@ class UserController
$file = base64_decode($data['base64']);
$tmpName = "tmp_file_{$aArgs['id']}_" .rand(). "_{$data['name']}";
$mimeAndSize = CoreController::getMimeTypeAndFileSize(['encodedFile' => $data['base64']]);
$mimeType = $mimeAndSize['mime'];
$size = $mimeAndSize['size'];
$finfo = new \finfo(FILEINFO_MIME_TYPE);
$mimeType = $finfo->buffer($file);
$size = strlen($file);
$type = explode('/', $mimeType);
$ext = strtoupper(substr($data['name'], strrpos($data['name'], '.') + 1));
......
......@@ -149,10 +149,15 @@ class CoreController
/**
* getMimeTypeAndFileSize
*
* @param args array with either an 'encodedFile' (base64 string), a 'resource' (resource), or a 'path' (file path as string)
* @return array with 'mime' and 'size' entries
* @param args array with either an 'encodedFile' (base64 string), or a 'path' (file path as string)
* @return array with 'mime' and 'size' entries or array with 'errors' entry
*/
public static function getMimeTypeAndFileSize(array $args) {
ValidatorModel::stringType($args, ['encodedFile', 'path']);
if (empty($args['encodedFile']) && empty($args['path'])) {
return ['errors' => 'args needs one of encodedFile or path'];
}
$resource = null;
$size = null;
if (!empty($args['encodedFile'])) {
......@@ -164,13 +169,6 @@ class CoreController
stream_set_chunk_size($resource, 1024*1024);
$size = fwrite($resource, $args['encodedFile']);
stream_filter_remove($streamFilterBase64);
} elseif (!empty($args['resource'])) {
if (!is_resource($args['resource'])) {
return ['errors' => 'args resource is not a resource'];
}
$resource = $args['resource'];
$devNull = fopen('/dev/null', 'a');
$size = stream_copy_to_stream($resource, $devNull);
} elseif (!empty($args['path'])) {
if (!is_file($args['path']) || !is_readable($args['path'])) {
return ['errors' => 'args filename does not refer to a regular file or said file is not readable'];
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment