Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
/**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
* This file is part of Maarch software.
*
*/
/**
* @brief fastParapheur Controller
* @author nathan.cheval@edissyum.com
* @author dev@maarch.org
*/
namespace ExternalSignatoryBook\controllers;
use Attachment\models\AttachmentModel;
use Convert\models\AdrModel;
use Docserver\models\DocserverModel;
use Resource\models\ResModel;
use SrcCore\models\CoreConfigModel;
use SrcCore\models\CurlModel;
use SrcCore\models\DatabaseModel;
class FastParapheurController
{
public static function retrieveSignedMails($aArgs)
{
foreach (['noVersion', 'resLetterbox'] as $version) {
foreach ($aArgs['idsToRetrieve'][$version] as $resId => $value) {
$xmlPostString = '<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sei="http://sei.ws.fast.cdc.com/">
<soapenv:Header/>
<soapenv:Body>
<sei:history>
<documentId>' . $value->external_id . '</documentId>
</sei:history>
</soapenv:Body>
</soapenv:Envelope>';
$curlReturn = CurlModel::execSOAP([
'xmlPostString' => $xmlPostString,
'url' => $aArgs['config']['data']['url'],
'options' => [
CURLOPT_SSLCERT => $aArgs['config']['data']['certPath'],
CURLOPT_SSLCERTPASSWD => $aArgs['config']['data']['certPass'],
CURLOPT_SSLCERTTYPE => $aArgs['config']['data']['certType']
]
]);
$isError = $curlReturn['response']->children('http://schemas.xmlsoap.org/soap/envelope/')->Body;
if (!empty($isError ->Fault[0]) && !empty($value->res_id_master)) {
echo 'PJ n° ' . $resId . ' et document original n° ' . $value->res_id_master . ' : ' . (string)$curlReturn['response']->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->Fault[0]->children()->faultstring . PHP_EOL;
continue;
} elseif (!empty($isError ->Fault[0])) {
echo 'Document principal n° ' . $resId . ' : ' . (string)$curlReturn['response']->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->Fault[0]->children()->faultstring . PHP_EOL;
continue;
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
}
$response = $curlReturn['response']->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://sei.ws.fast.cdc.com/')->historyResponse->children();
foreach ($response->return as $res) { // Loop on all steps of the documents (prepared, send to signature, signed etc...)
$state = (string) $res->stateName;
if ($state == $aArgs['config']['data']['validatedState']) {
$response = FastParapheurController::download(['config' => $aArgs['config'], 'documentId' => $value->external_id]);
$aArgs['idsToRetrieve'][$version][$resId]->status = 'validated';
$aArgs['idsToRetrieve'][$version][$resId]->format = 'pdf';
$aArgs['idsToRetrieve'][$version][$resId]->encodedFile = $response['b64FileContent'];
break;
} elseif ($state == $aArgs['config']['data']['refusedState']) {
$res = DatabaseModel::select([
'select' => ['firstname', 'lastname'],
'table' => ['listinstance', 'users'],
'left_join' => ['listinstance.item_id = users.user_id'],
'where' => ['res_id = ?', 'item_mode = ?'],
'data' => [$aArgs['idsToRetrieve'][$version][$resId]->res_id_master, 'sign']
])[0];
$response = FastParapheurController::getRefusalMessage(['config' => $aArgs['config'], 'documentId' => $value->external_id]);
$aArgs['idsToRetrieve'][$version][$resId]->status = 'refused';
$aArgs['idsToRetrieve'][$version][$resId]->noteContent = $res['lastname'] . ' ' . $res['firstname'] . ' : ' . $response;
break;
} else {
$aArgs['idsToRetrieve'][$version][$resId]->status = 'waiting';
}
}
}
}
return $aArgs['idsToRetrieve'];
}
public static function upload($aArgs)
{
$circuitId = $aArgs['circuitId'];
$label = $aArgs['label'];
$subscriberId = $aArgs['businessId'];
// Retrieve the annexes of the attachemnt to sign (other attachment and the original document)
$annexes = [];
$annexes['letterbox'] = ResModel::get([
'select' => ['res_id', 'path', 'filename', 'docserver_id','format', 'category_id', 'external_id', 'integrations'],
'where' => ['res_id = ?'],
'data' => [$aArgs['resIdMaster']]
]);
if (!empty($annexes['letterbox'][0]['docserver_id'])) {
$letterboxPath = DocserverModel::getByDocserverId(['docserverId' => $annexes['letterbox'][0]['docserver_id'], 'select' => ['path_template']]);
$annexes['letterbox'][0]['filePath'] = $letterboxPath['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $annexes['letterbox'][0]['path']) . $annexes['letterbox'][0]['filename'];
}
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
$attachments = AttachmentModel::get([
'select' => [
'res_id', 'docserver_id', 'path', 'filename', 'format', 'attachment_type'
],
'where' => ["res_id_master = ?", "attachment_type not in (?)", "status not in ('DEL', 'OBS', 'FRZ', 'TMP', 'SEND_MASS')", "in_signature_book = 'true'"],
'data' => [$aArgs['resIdMaster'], ['converted_pdf', 'print_folder', 'signed_response']]
]);
$attachmentTypes = AttachmentModel::getAttachmentsTypesByXML();
foreach ($attachments as $key => $value) {
if (!$attachmentTypes[$value['attachment_type']]['sign']) {
$annexeAttachmentPath = DocserverModel::getByDocserverId(['docserverId' => $value['docserver_id'], 'select' => ['path_template']]);
$value['filePath'] = $annexeAttachmentPath['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $value['path']) . $value['filename'];
unset($attachments[$key]);
$annexes['attachments'][] = $value;
}
}
// END annexes
$attachmentToFreeze = [];
foreach ($attachments as $attachment) {
$resId = $attachment['res_id'];
$collId = 'attachments_coll';
$curlReturn = FastParapheurController::uploadFile([
'resId' => $resId,
'collId' => $collId,
'resIdMaster' => $aArgs['resIdMaster'],
'annexes' => $annexes,
'circuitId' => $circuitId,
'label' => $label,
'subscriberId' => $subscriberId,
'config' => $aArgs['config']
]);
if ($curlReturn['infos']['http_code'] == 404) {
return ['error' => 'Erreur 404 : ' . $curlReturn['raw']];
} elseif (!empty($curlReturn['error'])) {
return ['error' => $curlReturn['error']];
} elseif (!empty($curlReturn['response']->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->Fault[0])) {
$error = (string)$curlReturn['response']->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->Fault[0]->children()->faultstring . PHP_EOL;
return ['error' => $error];
} else {
$documentId = $curlReturn['response']->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://sei.ws.fast.cdc.com/')->uploadResponse->children();
$attachmentToFreeze[$collId][$resId] = (string) $documentId;
}
}
// Send main document if in signature book
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
if (!empty($annexes['letterbox'][0])) {
$mainDocumentIntegration = json_decode($annexes['letterbox'][0]['integrations'], true);
$externalId = json_decode($annexes['letterbox'][0]['external_id'], true);
// $externalId = empty($externalId) ? [] : $externalId;
if ($mainDocumentIntegration['inSignatureBook'] && empty($externalId['signatureBookId'])) {
$resId = $annexes['letterbox'][0]['res_id'];
$collId = 'letterbox_coll';
unset($annexes['letterbox']);
$curlReturn = FastParapheurController::uploadFile([
'resId' => $resId,
'collId' => $collId,
'resIdMaster' => $aArgs['resIdMaster'],
'annexes' => $annexes,
'circuitId' => $circuitId,
'label' => $label,
'subscriberId' => $subscriberId,
'config' => $aArgs['config']
]);
if ($curlReturn['infos']['http_code'] == 404) {
return ['error' => 'Erreur 404 : ' . $curlReturn['raw']];
} elseif (!empty($curlReturn['error'])) {
return ['error' => $curlReturn['error']];
} elseif (!empty($curlReturn['response']->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->Fault[0])) {
$error = (string)$curlReturn['response']->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->Fault[0]->children()->faultstring . PHP_EOL;
return ['error' => $error];
} else {
$documentId = $curlReturn['response']->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://sei.ws.fast.cdc.com/')->uploadResponse->children();
$attachmentToFreeze[$collId][$resId] = (string) $documentId;
}
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
}
}
return ['sended' => $attachmentToFreeze];
}
public static function uploadFile($aArgs)
{
$adrInfo = AdrModel::getConvertedDocumentById(['resId' => $aArgs['resId'], 'collId' => $aArgs['collId'], 'type' => 'PDF']);
$attachmentPath = DocserverModel::getByDocserverId(['docserverId' => $adrInfo['docserver_id'], 'select' => ['path_template']]);
$attachmentFilePath = $attachmentPath['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $adrInfo['path']) . $adrInfo['filename'];
$attachmentFileName = 'projet_courrier_' . $aArgs['resIdMaster'] . '_' . rand(0001, 9999) . '.pdf';
$zip = new \ZipArchive();
$tmpPath = CoreConfigModel::getTmpPath();
$zipFilePath = $tmpPath . DIRECTORY_SEPARATOR
. $attachmentFileName . '.zip'; // The zip file need to have the same name as the attachment we want to sign
if ($zip->open($zipFilePath, \ZipArchive::CREATE)!==true) {
exit(_ERROR_CREATE_ZIP . "<$zipFilePath>\n");
}
$zip->addFile($attachmentFilePath, $attachmentFileName);
if (!empty($aArgs['annexes']['letterbox'])) {
$zip->addFile($aArgs['annexes']['letterbox'][0]['filePath'], 'document_principal.' . $aArgs['annexes']['letterbox'][0]['format']);
}
if (isset($aArgs['annexes']['attachments'])) {
for ($j = 0; $j < count($aArgs['annexes']['attachments']); $j++) {
$zip->addFile(
$aArgs['annexes']['attachments'][$j]['filePath'],
'PJ_' . ($j + 1) . '.' . $aArgs['annexes']['attachments'][$j]['format']
);
}
}
$zip->close();
$b64Attachment = base64_encode(file_get_contents($zipFilePath));
$fileName = $attachmentFileName . '.zip';
$xmlPostString = '<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sei="http://sei.ws.fast.cdc.com/">
<soapenv:Header/>
<soapenv:Body>
<sei:upload>
<label>' . $aArgs['label'] . '</label>
<comment></comment>
<subscriberId>' . $aArgs['subscriberId'] . '</subscriberId>
<circuitId>' . $aArgs['circuitId'] . '</circuitId>
<dataFileVO>
<dataHandler>' . $b64Attachment . '</dataHandler>
<filename>' . $fileName . '</filename>
</dataFileVO>
</sei:upload>
</soapenv:Body>
</soapenv:Envelope>';
$curlReturn = CurlModel::execSOAP([
'xmlPostString' => $xmlPostString,
'url' => $aArgs['config']['data']['url'],
'options' => [
CURLOPT_SSLCERT => $aArgs['config']['data']['certPath'],
CURLOPT_SSLCERTPASSWD => $aArgs['config']['data']['certPass'],
CURLOPT_SSLCERTTYPE => $aArgs['config']['data']['certType']
]
]);
return $curlReturn;
}
public static function download($aArgs)
{
$xmlPostString = '<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sei="http://sei.ws.fast.cdc.com/">
<soapenv:Header/>
<soapenv:Body>
<sei:download>
<documentId>' . $aArgs['documentId'] . '</documentId>
</sei:download>
</soapenv:Body>
</soapenv:Envelope>';
$curlReturn = CurlModel::execSOAP([
'xmlPostString' => $xmlPostString,
'url' => $aArgs['config']['data']['url'],
'options' => [
CURLOPT_SSLCERT => $aArgs['config']['data']['certPath'],
CURLOPT_SSLCERTPASSWD => $aArgs['config']['data']['certPass'],
CURLOPT_SSLCERTTYPE => $aArgs['config']['data']['certType']
]
]);
$isError = $curlReturn['response']->children('http://schemas.xmlsoap.org/soap/envelope/')->Body;
if (!empty($isError ->Fault[0])) {
echo (string)$curlReturn['response']->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->Fault[0]->children()->faultstring . PHP_EOL;
return false;
} else {
$response = $curlReturn['response']->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://sei.ws.fast.cdc.com/')->downloadResponse->children()->return;
$returnedDocumentId = (string) $response->documentId;
if ($aArgs['documentId'] !== $returnedDocumentId) {
return false;
} else {
$b64FileContent = $response->content;
return ['b64FileContent' => (string)$b64FileContent, 'documentId' => $returnedDocumentId];
}
}
}
public static function sendDatas($aArgs)
{
$config = $aArgs['config'];
// We need the SIRET field and the user_id of the signatory user's primary entity
$signatory = DatabaseModel::select([
'select' => ['user_id', 'business_id', 'entities.entity_label'],
'table' => ['listinstance', 'users_entities', 'entities'],
'left_join' => ['item_id = user_id', 'users_entities.entity_id = entities.entity_id'],
'where' => ['res_id = ?', 'item_mode = ?'],
'data' => [$aArgs['resIdMaster'], 'sign']
])[0];
$redactor = DatabaseModel::select([
'select' => ['short_label'],
'table' => ['res_view_letterbox', 'users_entities', 'entities'],
'left_join' => ['dest_user = user_id', 'users_entities.entity_id = entities.entity_id'],
'where' => ['res_id = ?'],
'data' => [$aArgs['resIdMaster']]
])[0];
if (empty($signatory['business_id']) || substr($signatory['business_id'], 0, 3) == 'org') {
$signatory['business_id'] = $config['data']['subscriberId'];
}
return FastParapheurController::upload(['config' => $config, 'resIdMaster' => $aArgs['resIdMaster'], 'businessId' => $signatory['business_id'], 'circuitId' => $signatory['user_id'], 'label' => $redactor['short_label']]);
}
public static function getRefusalMessage($aArgs)
{
$xmlPostString = '<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sei="http://sei.ws.fast.cdc.com/">
<soapenv:Header/>
<soapenv:Body>
<sei:getRefusalMessage>
<nodeRefId>' . $aArgs['documentId'] . '</nodeRefId>
</sei:getRefusalMessage>
</soapenv:Body>
</soapenv:Envelope>';
$curlReturn = CurlModel::execSOAP([
'xmlPostString' => $xmlPostString,
'url' => $aArgs['config']['data']['url'],
'options' => [
CURLOPT_SSLCERT => $aArgs['config']['data']['certPath'],
CURLOPT_SSLCERTPASSWD => $aArgs['config']['data']['certPass'],
CURLOPT_SSLCERTTYPE => $aArgs['config']['data']['certType']
]
]);
$response = $curlReturn['response']->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://sei.ws.fast.cdc.com/')->getRefusalMessageResponse->children()->return;
return $response;
}
}