diff --git a/src/app/document/controllers/CertificateSignatureController.php b/src/app/document/controllers/CertificateSignatureController.php index 57faaca2c3d163f5ee18c6865084576c960a4d1b..47deffc95fdd990e03bfa346d44098019ed16ffb 100755 --- a/src/app/document/controllers/CertificateSignatureController.php +++ b/src/app/document/controllers/CertificateSignatureController.php @@ -34,8 +34,6 @@ class CertificateSignatureController if (!empty($args['body']['tmpUniqueId'])) { if (is_file("{$tmpPath}tmpSignatureEsign_{$GLOBALS['id']}_{$args['body']['tmpUniqueId']}.pdf")) { $pathToDocument = "{$tmpPath}tmpSignatureEsign_{$GLOBALS['id']}_{$args['body']['tmpUniqueId']}.pdf"; - } elseif (is_file("{$tmpPath}tmpSignatureDoc_{$GLOBALS['id']}_{$args['body']['tmpUniqueId']}.pdf")) { - $pathToDocument = "{$tmpPath}tmpSignatureDoc_{$GLOBALS['id']}_{$args['body']['tmpUniqueId']}.pdf"; } } if (empty($pathToDocument)) { @@ -210,8 +208,6 @@ class CertificateSignatureController if (!empty($args['tmpUniqueId'])) { if (is_file("{$tmpPath}tmpSignatureEsign_{$GLOBALS['id']}_{$args['tmpUniqueId']}.pdf")) { $pathToDocument = "{$tmpPath}tmpSignatureEsign_{$GLOBALS['id']}_{$args['tmpUniqueId']}.pdf"; - } elseif (is_file("{$tmpPath}tmpSignatureDoc_{$GLOBALS['id']}_{$args['tmpUniqueId']}.pdf")) { - $pathToDocument = "{$tmpPath}tmpSignatureDoc_{$GLOBALS['id']}_{$args['tmpUniqueId']}.pdf"; } } diff --git a/src/app/document/controllers/DocumentController.php b/src/app/document/controllers/DocumentController.php index 8fde98e528bd6f26c4cd4dd47bc8e4ce5909d989..4103285b09fd9bab0ecc0e384b14d766d892895a 100755 --- a/src/app/document/controllers/DocumentController.php +++ b/src/app/document/controllers/DocumentController.php @@ -548,40 +548,23 @@ class DocumentController if (in_array($workflow['signature_mode'], ['rgs_2stars', 'rgs_2stars_timestamped', 'inca_card', 'inca_card_eidas'])) { if (!empty($body['step']) && $body['step'] == 'hashCertificate') { + $signWithServerCertificate = false; + if (DocumentController::ACTIONS[$args['actionId']] == 'VAL' && $workflow['mode'] == 'sign' && $loadedXml->electronicSignature->enable == 'true') { + $signWithServerCertificate = true; + } + if (empty($body['tmpUniqueId'])) { $control = DocumentController::getDocumentPath(['id' => $args['id']]); if (!empty($control['errors'])) { return $response->withStatus(400)->withJson(['errors' => $control['errors']]); } - $pathToDocument = $control['path']; - - $tmpFilename = $tmpPath . $GLOBALS['id'] . '_' . rand() . 'adr.pdf'; - copy($pathToDocument, $tmpFilename); - - $configPath = CoreConfigModel::getConfigPath(); - $overrideFile = "{$configPath}/override/setasign/fpdi_pdf-parser/src/autoload.php"; - if (file_exists($overrideFile)) { - require_once($overrideFile); - } - $pdf = new Fpdi('P'); - $pagesNumber = $pdf->setSourceFile($tmpFilename); - $control = DocumentController::setSignaturesOnPdf(['signatures' => [$body['signatures'][0]], 'pagesNumber' => $pagesNumber], $pdf); + $control = DocumentController::processSignatures(['path' => $control['path'], 'signature' => $body['signatures'][0], 'signWithServerCertificate' => $signWithServerCertificate]); if (!empty($control['errors'])) { return $response->withStatus(400)->withJson(['errors' => $control['errors']]); } - if (DocumentController::ACTIONS[$args['actionId']] == 'VAL' && $workflow['mode'] == 'sign' && $loadedXml->electronicSignature->enable == 'true') { - $control = CertificateSignatureController::signWithServerCertificate($pdf); - if (!empty($control['errors'])) { - return $response->withStatus(400)->withJson(['errors' => $control['errors']]); - } - } - - $fileContent = $pdf->Output('', 'S'); - $uniqueId = CoreConfigModel::getUniqueId(); - file_put_contents("{$tmpPath}tmpSignatureDoc_{$GLOBALS['id']}_{$uniqueId}.pdf", $fileContent); $body['tmpUniqueId'] = $uniqueId; } else { $pathToDocument = "{$tmpPath}tmpSignatureDoc_{$GLOBALS['id']}_{$body['tmpUniqueId']}.pdf"; @@ -589,33 +572,12 @@ class DocumentController return $response->withStatus(400)->withJson(['errors' => 'Unique temporary file does not exist']); } - $tmpFilename = $tmpPath . $GLOBALS['id'] . '_' . rand() . 'adr.pdf'; - copy($pathToDocument, $tmpFilename); - - $configPath = CoreConfigModel::getConfigPath(); - $overrideFile = "{$configPath}/override/setasign/fpdi_pdf-parser/src/autoload.php"; - if (file_exists($overrideFile)) { - require_once($overrideFile); - } - $pdf = new Fpdi('P'); - $pagesNumber = $pdf->setSourceFile($tmpFilename); - - $control = DocumentController::setSignaturesOnPdf(['signatures' => [$body['signatures'][0]], 'pagesNumber' => $pagesNumber], $pdf); + $control = DocumentController::processSignatures(['path' => $pathToDocument, 'signature' => $body['signatures'][0], 'signWithServerCertificate' => $signWithServerCertificate]); if (!empty($control['errors'])) { return $response->withStatus(400)->withJson(['errors' => $control['errors']]); } - - if (DocumentController::ACTIONS[$args['actionId']] == 'VAL' && $workflow['mode'] == 'sign' && $loadedXml->electronicSignature->enable == 'true') { - $control = CertificateSignatureController::signWithServerCertificate($pdf); - if (!empty($control['errors'])) { - return $response->withStatus(400)->withJson(['errors' => $control['errors']]); - } - } - - $fileContent = $pdf->Output('', 'S'); - - file_put_contents("{$tmpPath}tmpSignatureDoc_{$GLOBALS['id']}_{$body['tmpUniqueId']}.pdf", $fileContent); } + file_put_contents("{$tmpPath}tmpSignatureDoc_{$GLOBALS['id']}_{$body['tmpUniqueId']}.pdf", $control['fileContent']); } } else { $control = DocumentController::getDocumentPath(['id' => $args['id']]); @@ -1197,4 +1159,40 @@ class DocumentController return ['path' => $pathToDocument]; } + + private static function processSignatures(array $args) + { + ValidatorModel::notEmpty($args, ['path', 'signature']); + ValidatorModel::stringType($args, ['path']); + + $tmpPath = CoreConfigModel::getTmpPath(); + + $tmpFilename = $tmpPath . $GLOBALS['id'] . '_' . rand() . 'adr.pdf'; + copy($args['path'], $tmpFilename); + + $configPath = CoreConfigModel::getConfigPath(); + $overrideFile = "{$configPath}/override/setasign/fpdi_pdf-parser/src/autoload.php"; + if (file_exists($overrideFile)) { + require_once($overrideFile); + } + $pdf = new Fpdi('P'); + $pagesNumber = $pdf->setSourceFile($tmpFilename); + + $control = DocumentController::setSignaturesOnPdf(['signatures' => [$args['signature']], 'pagesNumber' => $pagesNumber], $pdf); + if (!empty($control['errors'])) { + return ['errors' => $control['errors']]; + } + + if ($args['signWithServerCertificate']) { + $control = CertificateSignatureController::signWithServerCertificate($pdf); + if (!empty($control['errors'])) { + return ['errors' => $control['errors']]; + } + } + + $fileContent = $pdf->Output('', 'S'); + unlink($tmpFilename); + + return ['fileContent' => $fileContent]; + } }