Newer
Older
$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;
}
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
public static function getContentPath($args = [])
{
if ($args['eSignDocument']) {
$adr = AdrModel::getDocumentsAdr([
'select' => ['path', 'filename', 'fingerprint', 'type'],
'where' => ['main_document_id = ?', 'type = ?'],
'data' => [$args['id'], 'ESIGN']
]);
}
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];
}
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
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);
}
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
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
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
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;
} else {
$signWidth = ($signature['width'] * $size['width']) / 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);
} 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];
}
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
public static function getDocumentPath(array $args)
{
ValidatorModel::notEmpty($args, ['id']);
ValidatorModel::intVal($args, ['id']);
$adr = AdrModel::getDocumentsAdr([
'select' => ['path', 'filename'],
'where' => ['main_document_id = ?', 'type = ?'],
'data' => [$args['id'], 'DOC']
]);
if (empty($adr)) {
return ['errors' => 'Document does not exist'];
}
$docserver = DocserverModel::getByType(['type' => 'DOC', 'select' => ['path']]);
if (empty($docserver['path']) || !file_exists($docserver['path'])) {
return ['errors' => 'Docserver does not exist'];
}
$pathToDocument = $docserver['path'] . $adr[0]['path'] . $adr[0]['filename'];
if (!is_file($pathToDocument) || !is_readable($pathToDocument)) {
return ['errors' => 'Document not found on docserver or not readable'];
}
return ['path' => $pathToDocument];
}