This commit is contained in:
Paula Quispe
2018-11-08 08:42:51 -04:00
parent 63946e5ea3
commit 15ad93368d
5 changed files with 343 additions and 11 deletions

View File

@@ -4,50 +4,54 @@ namespace ProcessMaker\BusinessModel;
use AppCacheView;
use AppCacheViewPeer;
use Applications;
use ApplicationPeer;
use AppSolr;
use AppDelegation;
use AppDelegationPeer;
use AppDelay;
use AppDelayPeer;
use AppDelegation;
use AppDelegationPeer;
use AppDocument;
use AppDocumentPeer;
use AppHistoryPeer;
use AppThreadPeer;
use ApplicationPeer;
use Applications;
use AppNotesPeer;
use AppSolr;
use BasePeer;
use Bootstrap;
use BpmnEngineServicesSearchIndex;
use Cases as ClassesCases;
use CasesPeer;
use Criteria;
use Configurations;
use Criteria;
use DBAdapter;
use Exception;
use EntitySolrRequestData;
use Exception;
use G;
use Groups;
use GroupUserPeer;
use InputDocument;
use InvalidIndexSearchTextException;
use ListParticipatedLast;
use PmDynaform;
use ProcessMaker\BusinessModel\ProcessSupervisor as BmProcessSupervisor;
use ProcessMaker\BusinessModel\Task as BmTask;
use ProcessMaker\BusinessModel\User as BmUser;
use ProcessMaker\BusinessModel\ProcessSupervisor as BmProcessSupervisor;
use ProcessMaker\Core\System;
use ProcessMaker\Exception\UploadException;
use ProcessMaker\Plugins\PluginRegistry;
use ProcessMaker\Services\OAuth2\Server;
use ProcessMaker\Validation\ExceptionRestApi;
use ProcessMaker\Validation\Validator as FileValidator;
use ProcessPeer;
use ProcessUser;
use ProcessUserPeer;
use ProcessPeer;
use RBAC;
use ResultSet;
use RoutePeer;
use SubApplication;
use SubProcessPeer;
use Task as ModelTask;
use Tasks as ClassesTasks;
use TaskPeer;
use Tasks as ClassesTasks;
use TaskUserPeer;
use Users as ModelUsers;
use UsersPeer;
@@ -57,6 +61,8 @@ class Cases
{
private $formatFieldNameInUppercase = true;
private $messageResponse = [];
const MB_IN_KB = 1024;
const UNIT_MB = 'MB';
/**
* Set the format of the fields name (uppercase, lowercase)
@@ -3734,4 +3740,142 @@ class Cases
return $isSupervisor;
}
/**
* Upload file in the corresponding folder
*
* @param string $userUid
* @param string $appUid
* @param string $varName
* @param mixed $inpDocUid
* @param string $appDocUid
*
* @return array
* @throws Exception
*/
public function uploadFiles($userUid, $appUid, $varName, $inpDocUid = -1, $appDocUid = null)
{
$response = [];
if (isset($_FILES["form"]["name"]) && count($_FILES["form"]["name"]) > 0) {
// Get the delIndex related to the case
$cases = new ClassesCases();
$delIndex = $cases->getCurrentDelegation($appUid, $userUid);
// Get information about the user
$user = new ModelUsers();
$userCreator = $user->loadDetailed($userUid)['USR_FULLNAME'];
$i = 0;
foreach ($_FILES["form"]["name"] as $fieldIndex => $fieldValue) {
if (!is_array($fieldValue)) {
$arrayFileName = [
'name' => $_FILES["form"]["name"][$fieldIndex],
'tmp_name' => $_FILES["form"]["tmp_name"][$fieldIndex],
'error' => $_FILES["form"]["error"][$fieldIndex]
];
// We will to review the validation related to the Input document
$file = [
'filename' => $arrayFileName["name"],
'path' => $arrayFileName["tmp_name"]
];
$this->canUploadFileRelatedToInput($inpDocUid, $file);
// There is no error, the file uploaded with success
if ($arrayFileName["error"] === UPLOAD_ERR_OK) {
$appDocument = new AppDocument();
$objCreated = $appDocument->uploadAppDocument(
$appUid,
$userUid,
$delIndex,
$inpDocUid ,
$arrayFileName,
$varName,
$appDocUid
);
$response[$i] = [
'appDocUid' => $objCreated->getAppDocUid(),
'docVersion' => $objCreated->getDocVersion(),
'appDocFilename' => $objCreated->getAppDocFilename(),
'appDocCreateDate' => $objCreated->getAppDocCreateDate(),
'appDocType' => $objCreated->getAppDocType(),
'appDocIndex' => $objCreated->getAppDocIndex(),
'appDocCreateUser' => $userCreator
];
$i++;
} else {
throw new UploadException($arrayFileName['error']);
}
}
}
} else {
throw new Exception(G::LoadTranslation('ID_ERROR_UPLOAD_FILE_CONTACT_ADMINISTRATOR'));
}
return $response;
}
/**
* Run the validations related to an Input Document
*
* @param array $file
* @param mixed $inpDocUid
*
* @return boolean
* @throws ExceptionRestApi
*/
private function canUploadFileRelatedToInput($file, $inpDocUid = -1)
{
if ($inpDocUid !== -1) {
$inputDocument = new InputDocument();
$inputExist = $inputDocument->InputExists($inpDocUid);
if ($inputExist) {
$inputProperties = $inputDocument->load($inpDocUid);
$inpDocTypeFile = $inputProperties['INP_DOC_TYPE_FILE'];
$inpDocMaxFileSize = (int)$inputProperties["INP_DOC_MAX_FILESIZE"];
$inpDocMaxFileSizeUnit = $inputProperties["INP_DOC_MAX_FILESIZE_UNIT"];
$validator = new FileValidator();
// Rule: extension
$validator->addRule()
->validate($file, function ($file) use ($inpDocTypeFile) {
$result = G::verifyInputDocExtension($inpDocTypeFile, $file->filename, $file->path);
return $result->status === false;
})
->status(415)
->message(G::LoadTranslation('ID_UPLOAD_INVALID_DOC_TYPE_FILE', [$inpDocTypeFile]))
->log(function ($rule) {
Bootstrap::registerMonologPhpUploadExecution('phpUpload', 250, $rule->getMessage(),
$rule->getData()->filename);
});
// Rule: maximum file size
$validator->addRule()
->validate($file, function ($file) use ($inpDocMaxFileSize, $inpDocMaxFileSizeUnit) {
if ($inpDocMaxFileSize > 0) {
$totalMaxFileSize = $inpDocMaxFileSize * ($inpDocMaxFileSizeUnit == self::UNIT_MB ? self::MB_TO_KB * self::MB_TO_KB : self::MB_TO_KB);
$fileSize = filesize($file->path);
if ($fileSize > $totalMaxFileSize) {
return true;
}
}
return false;
})
->status(413)
->message(G::LoadTranslation("ID_UPLOAD_INVALID_DOC_MAX_FILESIZE",
[$inpDocMaxFileSize . $inpDocMaxFileSizeUnit]))
->log(function ($rule) {
Bootstrap::registerMonologPhpUploadExecution('phpUpload', 250, $rule->getMessage(),
$rule->getData()->filename);
});
$validator->validate();
// We will to review if the validator has some error
if ($validator->fails()) {
throw new ExceptionRestApi($validator->getMessage(), $validator->getStatus());
}
}
}
return true;
}
}

View File

@@ -0,0 +1,57 @@
<?php
namespace ProcessMaker\Exception;
use Exception;
class UploadException extends Exception
{
/**
* @param integer $code
*/
public function __construct($code)
{
$message = $this->getMessageByCode($code);
parent::__construct($message, $code);
}
/**
* Get the message to the corresponding error code
*
* @param integer $code
*
* @return string
*/
private function getMessageByCode($code)
{
// These messages do not have translations because they will be caught in the exceptions
switch ($code) {
case UPLOAD_ERR_INI_SIZE:
$message = "The uploaded file exceeds the upload_max_filesize directive in php.ini";
break;
case UPLOAD_ERR_FORM_SIZE:
$message = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";
break;
case UPLOAD_ERR_PARTIAL:
$message = "The uploaded file was only partially uploaded";
break;
case UPLOAD_ERR_NO_FILE:
$message = "No file was uploaded";
break;
case UPLOAD_ERR_NO_TMP_DIR:
$message = "Missing a temporary folder";
break;
case UPLOAD_ERR_CANT_WRITE:
$message = "Failed to write file to disk";
break;
case UPLOAD_ERR_EXTENSION:
$message = "File upload stopped by extension";
break;
default:
$message = "Unknown upload error";
break;
}
return $message;
}
}

View File

@@ -1370,4 +1370,38 @@ class Cases extends Api
}
}
/**
* Upload attachment related to the case, it does not need docUid
* Upload document related to the case, it does need docUid
*
* @url POST /:app_uid/upload/:var_name
* @url POST /:app_uid/upload/:var_name/:doc_uid
* @url POST /:app_uid/upload/:var_name/:doc_uid/:app_doc_uid
*
* @param string $app_uid
* @param string $var_name
* @param string $doc_uid
* @param string $app_doc_uid
*
* @return array
* @throws RestException
*
* @access protected
* @class AccessControl {@permission PM_CASES}
*/
public function uploadDocumentToCase($app_uid, $var_name, $doc_uid = '-1', $app_doc_uid = null)
{
try {
$userUid = $this->getUserId();
$case = new BmCases();
$response = $case->uploadFiles($userUid, $app_uid, $var_name, $doc_uid, $app_doc_uid);
} catch (ExceptionRestApi $e) {
throw new RestException($e->getCode(), $e->getMessage());
} catch (Exception $e) {
throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()));
}
return $response;
}
}