Newer
Older
public function getLinkedMailing(Request $request, Response $response, array $args)
{
if (!DocumentController::hasRightById(['id' => $args['id'], 'userId' => $GLOBALS['id']])) {
return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
}
$document = DocumentModel::getById(['select' => ['mailing_id'], 'id' => $args['id']]);
if (empty($document['mailing_id'])) {
return $response->withJson(['documents' => []]);
}
$substitutedUsers = UserModel::get(['select' => ['id'], 'where' => ['substitute = ?'], 'data' => [$GLOBALS['id']]]);
$users = [$GLOBALS['id']];
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'],
'where' => ['user_id in (?)', "(id) in ({$workflowSelect})"],
'data' => [$users]
]);
$ids = array_column($workflows, 'main_document_id');
$documents = DocumentModel::get([
'select' => ['id'],
'where' => ['id in (?)', 'mailing_id = ?'],
'data' => [$ids, $document['mailing_id']]
]);
$documentsId = array_column($documents, 'id');
return $response->withJson(['documents' => $documentsId]);
}
public static function endAction(array $args)
{
ValidatorModel::notEmpty($args, ['id', 'status', 'workflowId']);
ValidatorModel::intVal($args, ['id', 'workflowId']);
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
1069
1070
ValidatorModel::stringType($args, ['status', 'note']);
$set = ['process_date' => 'CURRENT_TIMESTAMP', 'status' => $args['status']];
if (!empty($args['note'])) {
$set['note'] = $args['note'];
}
WorkflowModel::update([
'set' => $set,
'where' => ['id = ?'],
'data' => [$args['workflowId']]
]);
if ($args['status'] == 'REF') {
WorkflowModel::update([
'set' => ['status' => 'END'],
'where' => ['main_document_id = ?', 'process_date is null'],
'data' => [$args['id']]
]);
}
$nextWorkflow = WorkflowModel::getCurrentStep(['select' => ['id', 'user_id'], 'documentId' => $args['id']]);
if (!empty($nextWorkflow) && empty($nextWorkflow['user_id'])) {
$workflowExternalInformations = WorkflowExternalInformationModel::get(['select' => ['*'], 'where' => ['workflow_id = ?'], 'data' => [$nextWorkflow['id']]]);
if (!empty($workflowExternalInformations[0])) {
$content = DocumentController::getContentPath(['id' => $args['id']]);
$content = file_get_contents($content['path']);
$document = DocumentModel::getById(['select' => ['title', 'description'], 'id' => $args['id']]);
$procedureCreated = YousignController::createProcedure([
'documentId' => $args['id'],
'encodedDocument' => base64_encode($content),
'name' => $document['title'],
'description' => $document['description'],
'workflowId' => $nextWorkflow['id']
]);
if (!empty($procedureCreated['errors'])) {
return ['errors' => $procedureCreated['errors']];
}
}
} else {
EmailController::sendNotification(['documentId' => $args['id'], 'status' => $args['status']]);
}
return true;
}
public static function hasRightById(array $args)
{
ValidatorModel::notEmpty($args, ['id', 'userId']);
ValidatorModel::intVal($args, ['id', 'userId']);
$document = DocumentModel::getById(['select' => ['typist'], 'id' => $args['id']]);
if ($document['typist'] == $GLOBALS['id']) {
return true;
}
$workflow = WorkflowModel::getCurrentStep(['select' => ['user_id'], 'documentId' => $args['id']]);
if ($workflow['user_id'] != $args['userId']) {
$user = UserModel::getById(['id' => $workflow['user_id'], 'select' => ['substitute']]);
if ($user['substitute'] != $args['userId']) {
return false;
}
}
public static function getEncodedDocumentFromEncodedZip(array $args)
{
ValidatorModel::notEmpty($args, ['encodedZipDocument']);
ValidatorModel::stringType($args, ['encodedZipDocument']);
$tmpPath = CoreConfigModel::getTmpPath();
$zipDocumentOnTmp = $tmpPath . mt_rand() . '_parapheur.zip';
file_put_contents($zipDocumentOnTmp, base64_decode($args['encodedZipDocument']));
$zipArchive = new \ZipArchive();
$open = $zipArchive->open($zipDocumentOnTmp);
if ($open != true) {
return ['errors' => "getDocumentFromEncodedZip : $open"];
}
$dirOnTmp = $tmpPath . mt_rand() . '_parapheur';
return ['errors' => "getDocumentFromEncodedZip : Extract failed"];
}
$filesOnTmp = scandir($dirOnTmp);
foreach ($filesOnTmp as $fileOnTmp) {
if ($fileOnTmp != '.' && $fileOnTmp != '..') {
$base64Content = base64_encode(file_get_contents("{$dirOnTmp}/{$fileOnTmp}"));
unlink($zipDocumentOnTmp);
return ['encodedDocument' => $base64Content];
}
}
return ['errors' => "getDocumentFromEncodedZip : No document was found in Zip"];
}
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
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(array $args)
if (!empty($args['type'])) {
$adr = AdrModel::getDocumentsAdr([
'select' => ['path', 'filename', 'fingerprint', 'type'],
'where' => ['main_document_id = ?', 'type = ?'],
'data' => [$args['id'], $args['type']]
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
]);
}
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];
}
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
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);
}
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
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']];
private static function processSignatures(array $args)
{
ValidatorModel::notEmpty($args, ['path', 'signature']);
ValidatorModel::stringType($args, ['path']);
$tmpPath = CoreConfigModel::getTmpPath();
if (!is_dir($tmpPath)) {
return ['errors' => 'Tmp path is not valid'];
}
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
$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];
}