Newer
Older
$base64Content = base64_encode(file_get_contents("{$dirOnTmp}/{$fileOnTmp}"));
unlink($zipDocumentOnTmp);
return ['encodedDocument' => $base64Content];
}
}
return ['errors' => "getDocumentFromEncodedZip : No document was found in Zip"];
}
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
public static function getLinkedDocuments(array $args)
{
ValidatorModel::notEmpty($args, ['id', 'userId', 'linkId']);
ValidatorModel::intVal($args, ['id', 'userId']);
ValidatorModel::stringType($args, ['linkId']);
$substitutedUsers = UserModel::get(['select' => ['id'], 'where' => ['substitute = ?'], 'data' => [$args['userId']]]);
$users = [$args['userId']];
foreach ($substitutedUsers as $value) {
$users[] = $value['id'];
}
$workflowSelect = "SELECT id FROM workflows ws WHERE workflows.main_document_id = main_document_id AND process_date IS NULL AND status IS NULL ORDER BY \"order\" LIMIT 1";
$workflows = WorkflowModel::get([
'select' => ['main_document_id', 'mode', 'user_id'],
'where' => ['user_id in (?)', "(id) in ({$workflowSelect})", 'main_document_id != ?'],
'data' => [$users, $args['id']]
]);
$documentIds = array_column($workflows, 'main_document_id');
$linkedDocuments = [];
if (!empty($documentIds)) {
$linkedDocuments = DocumentModel::get([
'select' => ['id', 'title', 'reference'],
'where' => ['id in (?)', 'link_id = ?'],
'data' => [$documentIds, $args['linkId']],
'orderBy' => ['creation_date desc']
]);
}
return $linkedDocuments;
}
public static function getContentPath($args = [])
{
if (!empty($args['type'])) {
$adr = AdrModel::getDocumentsAdr([
'select' => ['path', 'filename', 'fingerprint', 'type'],
'where' => ['main_document_id = ?', 'type = ?'],
'data' => [$args['id'], $args['type']]
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
]);
}
if (empty($adr)) {
$adr = AdrModel::getDocumentsAdr([
'select' => ['path', 'filename', 'fingerprint', 'type'],
'where' => ['main_document_id = ?', 'type = ?'],
'data' => [$args['id'], 'DOC']
]);
}
if (empty($adr[0])) {
return null;
}
$docserver = DocserverModel::getByType(['type' => $adr[0]['type'], 'select' => ['path']]);
if (empty($docserver['path']) || !file_exists($docserver['path'])) {
return ['errors' => 'Docserver does not exist', 'code' => 400];
}
$pathToDocument = $docserver['path'] . $adr[0]['path'] . $adr[0]['filename'];
if (!is_file($pathToDocument)) {
return ['errors' => 'Document not found on docserver', 'code' => 404];
}
$fingerprint = DocserverController::getFingerPrint(['path' => $pathToDocument]);
if ($adr[0]['fingerprint'] != $fingerprint) {
return ['errors' => 'Fingerprints do not match', 'code' => 400];
}
return ['path' => $pathToDocument];
}
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
public static function getPdfCertificate(array $args)
{
ValidatorModel::notEmpty($args, ['path', 'documentId']);
ValidatorModel::intVal($args, ['documentId']);
ValidatorModel::stringType($args, ['path']);
$path = $args['path'];
$documentId = $args['documentId'];
$tmpPath = CoreConfigModel::getTmpPath();
$signaturePath = $tmpPath . 'signature_' . $documentId . '.pkcs7';
$signatureInfoPath = $tmpPath . 'signatureInfo_' . $documentId . '.txt';
if (file_exists($signatureInfoPath)) {
$content = file_get_contents($signatureInfoPath);
if ($content !== false) {
return $content;
}
}
$content = file_get_contents($path);
$regexp = '#ByteRange\[\s*(\d+) (\d+) (\d+)#'; // subexpressions are used to extract b and c
$result = [];
preg_match_all($regexp, $content, $result);
// $result[2][0] and $result[3][0] are b and c
if (isset($result[2]) && isset($result[3]) && isset($result[2][0]) && isset($result[3][0])) {
$start = $result[2][0];
$end = $result[3][0];
if ($stream = fopen($path, 'rb')) {
$signature = stream_get_contents($stream, $end - $start - 2, $start + 1); // because we need to exclude < and > from start and end
fclose($stream);
file_put_contents($signaturePath, hex2bin($signature));
}
}
if (!file_exists($signaturePath)) {
return false;
}
exec('openssl pkcs7 -in ' . $signaturePath . ' -inform DER -print_certs > ' . $signatureInfoPath . ' 2>&1', $output, $return);
return file_get_contents($signatureInfoPath);
}
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
public static function setSignaturesOnPdf(array $args, Fpdi &$pdf)
{
ValidatorModel::notEmpty($args, ['signatures', 'pagesNumber']);
ValidatorModel::arrayType($args, ['signatures']);
ValidatorModel::intVal($args, ['pagesNumber']);
$affectedPages = [];
$tmpPath = CoreConfigModel::getTmpPath();
$pdf->setPrintHeader(false);
for ($i = 1; $i <= $args['pagesNumber']; $i++) {
$page = $pdf->importPage($i);
$size = $pdf->getTemplateSize($page);
$pdf->AddPage($size['orientation'], $size);
$pdf->useImportedPage($page);
$pdf->SetAutoPageBreak(false, 0);
$pdf->SetMargins(0, 0, 0);
$pdf->SetAutoPageBreak(false, 0);
foreach ($args['signatures'] as $signature) {
if ($signature['page'] == $i) {
if (!in_array($i, $affectedPages)) {
$affectedPages[] = $i;
}
if ($signature['positionX'] == 0 && $signature['positionY'] == 0) {
$signWidth = $size['width'];
$signPosX = 0;
$signPosY = 0;
$signHeight = null;
} else {
$signWidth = ($signature['width'] * $size['width']) / 100;
$signHeight = ($signature['height'] * $size['height']) / 100;
$signPosX = ($signature['positionX'] * $size['width']) / 100;
$signPosY = ($signature['positionY'] * $size['height']) / 100;
}
if ($signature['type'] == 'SVG') {
$image = str_replace('data:image/svg+xml;base64,', '', $signature['encodedImage']);
$image = base64_decode($image);
if ($image === false) {
return ['errors' => 'setSignaturesOnPdf : base64_decode failed for SVP type signature'];
}
$imageTmpPath = $tmpPath . $GLOBALS['id'] . '_' . rand() . '_writing.svg';
file_put_contents($imageTmpPath, $image);
$pdf->ImageSVG($imageTmpPath, $signPosX, $signPosY, $signWidth, $signHeight);
} else {
$image = base64_decode($signature['encodedImage']);
if ($image === false) {
return ['errors' => 'setSignaturesOnPdf : base64_decode failed'];
}
$imageTmpPath = $tmpPath . $GLOBALS['id'] . '_' . rand() . '_writing.png';
file_put_contents($imageTmpPath, $image);
$pdf->Image($imageTmpPath, $signPosX, $signPosY, $signWidth);
}
}
}
}
return ['affectedPages' => $affectedPages];
}
public static function getDocumentPath(array $args)
{
ValidatorModel::notEmpty($args, ['id']);
ValidatorModel::intVal($args, ['id']);
$content = DocumentController::getContentPath(['id' => $args['id']]);
if (empty($content)) {
return ['errors' => 'Document does not exist'];
} elseif (!empty($content['errors'])) {
return ['errors' => $content['errors']];
return ['path' => $content['path']];
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
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];
}