Newer
Older
<?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 Store Controller
* @author dev@maarch.org
* @ingroup core
*/
namespace Resource\controllers;
use Slim\Http\Request;
use Slim\Http\Response;
use SrcCore\models\ValidatorModel;
use Respect\Validation\Validator;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
public function checkFileUpload(Request $request, Response $response, array $aArgs)
{
$body = $request->getParsedBody();
if (!Validator::notEmpty()->validate($body['size'])) {
return $response->withStatus(400)->withJson(['errors' => 'filesize is empty']);
} else if (!Validator::notEmpty()->validate($body['type'])) {
return $response->withStatus(400)->withJson(['errors' => 'no mime type detected']);
} else if (!Validator::notEmpty()->validate($body['extension'])) {
return $response->withStatus(400)->withJson(['errors' => 'this filename has no extension']);
}
if (!StoreController::isFileAllowed($body)) {
return $response->withStatus(400)->withJson(['errors' => _FILE_NOT_ALLOWED_INFO_1.' "'.$body['extension'].'" '._FILE_NOT_ALLOWED_INFO_2.' "'. $body['type']. '" '._FILE_NOT_ALLOWED_INFO_3]);
}
$maxFilesizeMo = ini_get('upload_max_filesize');
$maxFilesizeKo = ini_get('upload_max_filesize')*1024;
if ($body['size']/1024 > $maxFilesizeKo) {
return $response->withStatus(400)->withJson(['errors' => _MAX_SIZE_UPLOAD_REACHED.' ('.round($maxFilesizeMo).'Mo Max.)']);
}
return $response->withJson(['success']);
}
private static function isFileAllowed(array $args)
{
ValidatorModel::notEmpty($args, ['extension', 'type']);
ValidatorModel::stringType($args, ['extension', 'type']);
$loadedXml = CoreConfigModel::getXmlLoaded(['path' => 'apps/maarch_entreprise/xml/extensions.xml']);
if ($loadedXml) {
foreach ($loadedXml->FORMAT as $value) {
if (strtolower((string)$value->name) == strtolower($args['extension']) && strtolower((string)$value->mime) == strtolower($args['type'])) {
return true;
}
}
}
return false;
}
private static function getAllowedMime()
{
$loadedXml = CoreConfigModel::getXmlLoaded(['path' => 'apps/maarch_entreprise/xml/extensions.xml']);
$mimeList = [];
if ($loadedXml) {
foreach ($loadedXml->FORMAT as $value) {
$mimeList[] = (string)$value->mime;
}
}
return array_unique($mimeList);
}
public static function storeResource(array $aArgs)
{
ValidatorModel::notEmpty($aArgs, ['encodedFile', 'format', 'status', 'type_id', 'category_id']);
ValidatorModel::stringType($aArgs, ['format', 'status']);
$mlbColumns = [
'category_id', 'exp_contact_id', 'exp_user_id', 'dest_contact_id', 'dest_user_id',
'nature_id', 'alt_identifier', 'admission_date', 'process_limit_date', 'closing_date', 'address_id'
foreach ($aArgs as $column => $value) {
if (empty($value)) {
unset($aArgs[$column]);
}
}
$fileContent = base64_decode(str_replace(['-', '_'], ['+', '/'], $aArgs['encodedFile']));
$storeResult = DocserverController::storeResourceOnDocServer([
'collId' => 'letterbox_coll',
'docserverTypeId' => 'DOC',
'encodedResource' => base64_encode($fileContent),
'format' => $aArgs['format']
]);
if (!empty($storeResult['errors'])) {
return ['errors' => '[storeResource] ' . $storeResult['errors']];
}
unset($aArgs['encodedFile']);
$resId = DatabaseModel::getNextSequenceValue(['sequenceId' => 'res_id_mlb_seq']);
$data = [
'docserver_id' => $storeResult['docserver_id'],
'filename' => $storeResult['file_destination_name'],
'filesize' => $storeResult['fileSize'],
'path' => $storeResult['destination_dir'],
'fingerprint' => $storeResult['fingerPrint'],
'res_id' => $resId
];
$data = array_merge($aArgs, $data);
$data = StoreController::prepareStorage($data);
$dataMlb = [];
foreach ($data as $key => $value) {
if (in_array($key, $mlbColumns)) {
$dataMlb[$key] = $value;
unset($data[$key]);
}
}
$dataMlb['res_id'] = $resId;
ResModel::createExt($dataMlb);
return $resId;
} catch (\Exception $e) {
return ['errors' => '[storeResource] ' . $e->getMessage()];
}
}
public static function storeResourceRes(array $aArgs)
{
ValidatorModel::notEmpty($aArgs, ['encodedFile', 'data', 'collId', 'table', 'fileFormat', 'status']);
ValidatorModel::stringType($aArgs, ['collId', 'table', 'fileFormat', 'status']);
ValidatorModel::arrayType($aArgs, ['data']);
if (!in_array($aArgs['table'], ['res_letterbox', 'res_attachments', 'res_version_attachments'])) {
return ['errors' => '[storeResource] Table not valid'];
}
try {
$fileContent = base64_decode(str_replace(['-', '_'], ['+', '/'], $aArgs['encodedFile']));
$storeResult = DocserverController::storeResourceOnDocServer([
'collId' => $aArgs['collId'],
'docserverTypeId' => 'DOC',
'encodedResource' => base64_encode($fileContent),
'format' => $aArgs['fileFormat']
]);
if (!empty($storeResult['errors'])) {
return ['errors' => '[storeResource] ' . $storeResult['errors']];
}
'data' => $aArgs['data'],
'docserverId' => $storeResult['docserver_id'],
'status' => $aArgs['status'],
'fileName' => $storeResult['file_destination_name'],
'fileFormat' => $aArgs['fileFormat'],
'fileSize' => $storeResult['fileSize'],
'path' => $storeResult['destination_dir'],
'fingerPrint' => $storeResult['fingerPrint']
]);
$resId = false;
if ($aArgs['table'] == 'res_letterbox') {
$resId = ResModel::create($data);
} elseif ($aArgs['table'] == 'res_attachments') {
} elseif ($aArgs['table'] == 'res_version_attachments') {
$resId = AttachmentModel::createVersion($data);
189
190
191
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
}
return $resId;
} catch (\Exception $e) {
return ['errors' => '[storeResource] ' . $e->getMessage()];
}
}
public static function controlFingerPrint(array $aArgs)
{
ValidatorModel::notEmpty($aArgs, ['pathInit', 'pathTarget']);
ValidatorModel::stringType($aArgs, ['pathInit', 'pathTarget', 'fingerprintMode']);
if (!file_exists($aArgs['pathInit'])) {
return ['errors' => '[controlFingerprint] PathInit does not exist'];
}
if (!file_exists($aArgs['pathTarget'])) {
return ['errors' => '[controlFingerprint] PathTarget does not exist'];
}
$fingerprint1 = StoreController::getFingerPrint(['filePath' => $aArgs['pathInit'], 'mode' => $aArgs['fingerprintMode']]);
$fingerprint2 = StoreController::getFingerPrint(['filePath' => $aArgs['pathTarget'], 'mode' => $aArgs['fingerprintMode']]);
if ($fingerprint1 != $fingerprint2) {
return ['errors' => '[controlFingerprint] Fingerprints do not match: ' . $aArgs['pathInit'] . ' and ' . $aArgs['pathTarget']];
}
return true;
}
public static function getFingerPrint(array $aArgs)
{
ValidatorModel::notEmpty($aArgs, ['filePath']);
ValidatorModel::stringType($aArgs, ['filePath', 'mode']);
if (empty($aArgs['mode']) || $aArgs['mode'] == 'NONE') {
}
return hash_file(strtolower($aArgs['mode']), $aArgs['filePath']);
}
public static function prepareStorage(array $aArgs)
ValidatorModel::notEmpty($aArgs, ['docserver_id', 'filename', 'format', 'filesize', 'path', 'fingerprint', 'status', 'res_id']);
ValidatorModel::stringType($aArgs, ['docserver_id', 'filename', 'format', 'path', 'fingerprint', 'status']);
ValidatorModel::intVal($aArgs, ['filesize', 'res_id']);
if (empty($aArgs['typist'])) {
$aArgs['typist'] = 'auto';
unset($aArgs['alt_identifier']);
if (!empty($aArgs['chrono'])) {
$aArgs['alt_identifier'] = ChronoModel::getChrono(['id' => $aArgs['category_id'], 'entityId' => $aArgs['destination'], 'typeId' => $aArgs['type_id'], 'resId' => $aArgs['res_id']]);
unset($aArgs['chrono']);
if (empty($aArgs['process_limit_date'])) {
$processLimitDate = ResModel::getStoredProcessLimitDate(['typeId' => $aArgs['type_id'], 'admissionDate' => $aArgs['admission_date']]);
$aArgs['process_limit_date'] = $processLimitDate;
}
if (!empty($aArgs['exp_contact_id']) && !is_numeric($aArgs['exp_contact_id'])) {
$mail = explode('<', str_replace('>', '', $aArgs['exp_contact_id']));
$contact = ContactModel::getByEmail(['email' => $mail[count($mail) - 1], 'select' => ['contacts_v2.contact_id']]);
if (!empty($contact['contact_id'])) {
$aArgs['exp_contact_id'] = $contact['contact_id'];
} else {
$aArgs['exp_contact_id'] = 0;
}
}
if (!empty($aArgs['address_id']) && !is_numeric($aArgs['address_id'])) {
$mail = explode('<', str_replace('>', '', $aArgs['address_id']));
$contact = ContactModel::getByEmail(['email' => $mail[count($mail) - 1], 'select' => ['contact_addresses.id']]);
if (!empty($contact['id'])) {
$aArgs['address_id'] = $contact['id'];
} else {
$aArgs['address_id'] = 0;
}
}
unset($aArgs['external_id']);
if (!empty($aArgs['externalId'])) {
if (is_array($aArgs['externalId'])) {
$aArgs['external_id'] = json_encode($aArgs['externalId']);
}
unset($aArgs['externalId']);
}
$aArgs['creation_date'] = 'CURRENT_TIMESTAMP';
return $aArgs;
}
public static function prepareStorageRes(array $aArgs)
ValidatorModel::notEmpty($aArgs, ['data', 'docserverId', 'fileName', 'fileFormat', 'fileSize', 'path', 'fingerPrint']);
ValidatorModel::stringType($aArgs, ['docserverId', 'status', 'fileName', 'fileFormat', 'path', 'fingerPrint']);
ValidatorModel::arrayType($aArgs, ['data']);
ValidatorModel::intVal($aArgs, ['fileSize']);
$statusFound = false;
$typistFound = false;
$toAddressFound = false;
$userPrimaryEntity = false;
foreach ($aArgs['data'] as $key => $value) {
$aArgs['data'][$key]['column'] = strtolower($value['column']);
}
foreach ($aArgs['data'] as $key => $value) {
if (strtolower($value['type']) == 'integer' || strtolower($value['type']) == 'float') {
if (empty($value['value'])) {
$aArgs['data'][$key]['value'] = '0';
}
} elseif (strtolower($value['type']) == 'string') {
$aArgs['data'][$key]['value'] = str_replace(';', '', $value['value']);
$aArgs['data'][$key]['value'] = str_replace('--', '', $value['value']);
}
if ($value['column'] == 'status') {
$statusFound = true;
} elseif ($value['column'] == 'typist') {
} elseif ($value['column'] == 'custom_t10') {
$theString = str_replace('>', '', $value['value']);
$mail = explode("<", $theString);
$user = UserModel::getByEmail(['mail' => $mail[count($mail) -1], 'select' => ['user_id']]);
if (!empty($user[0]['user_id'])) {
$toAddressFound = true;
$destUser = $user[0]['user_id'];
$entity = EntityModel::getByLogin(['login' => $destUser, 'select' => ['entity_id']]);
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
354
355
356
357
358
if (!empty($entity[0]['entity_id'])) {
$userEntity = $entity[0]['entity_id'];
$userPrimaryEntity = true;
}
} else {
$entity = EntityModel::getByEmail(['email' => $mail[count($mail) -1], 'select' => ['entity_id']]);
if (!empty($entity[0]['entity_id'])) {
$userPrimaryEntity = true;
}
}
}
}
$destUser = empty($destUser) ? '' : $destUser;
$userEntity = empty($userEntity) ? '' : $userEntity;
if (!$typistFound && !$toAddressFound) {
$aArgs['data'][] = [
'column' => 'typist',
'value' => 'auto',
'type' => 'string'
];
}
if (!$statusFound) {
$aArgs['data'][] = [
'column' => 'status',
'value' => $aArgs['status'],
'type' => 'string'
];
}
if ($toAddressFound) {
$aArgs['data'][] = [
'column' => 'dest_user',
'value' => $destUser,
'type' => 'string'
];
if (!$typistFound) {
$aArgs['data'][] = [
'column' => 'typist',
'value' => $destUser,
'type' => 'string'
];
}
}
if ($userPrimaryEntity) {
$destinationFound = false;
$initiatorFound = false;
foreach ($aArgs['data'] as $key => $value) {
if ($value['column'] == 'destination') {
if (empty($value['value'])) {
$aArgs['data'][$key]['value'] = $userEntity;
}
$destinationFound = true;
} elseif ($value['column'] == 'initiator') {
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
if (empty($value['value'])) {
$aArgs['data'][$key]['value'] = $userEntity;
}
$initiatorFound = true;
}
}
if (!$destinationFound) {
$aArgs['data'][] = [
'column' => 'destination',
'value' => $userEntity,
'type' => 'string'
];
}
if (!$initiatorFound) {
$aArgs['data'][] = [
'column' => 'initiator',
'value' => $userEntity,
'type' => 'string'
];
}
}
$aArgs['data'][] = [
'column' => 'docserver_id',
'value' => $aArgs['docserverId'],
'type' => 'string'
];
$aArgs['data'][] = [
'column' => 'creation_date',
'value' => 'CURRENT_TIMESTAMP',
'type' => 'function'
];
$aArgs['data'][] = [
'column' => 'path',
'value' => $aArgs['path'],
'type' => 'string'
];
$aArgs['data'][] = [
'column' => 'fingerprint',
'value' => $aArgs['fingerPrint'],
'type' => 'string'
];
$aArgs['data'][] = [
'column' => 'filename',
'value' => $aArgs['fileName'],
'type' => 'string'
];
$aArgs['data'][] = [
'column' => 'format',
'value' => $aArgs['fileFormat'],
'type' => 'string'
];
$aArgs['data'][] = [
'column' => 'filesize',
'value' => $aArgs['fileSize'],
'type' => 'int'
];
$formatedData = [];
foreach ($aArgs['data'] as $value) {
$formatedData[$value['column']] = $value['value'];
}
return $formatedData;
}
public static function prepareExtStorage(array $aArgs)
{
ValidatorModel::notEmpty($aArgs, ['data', 'resId']);
ValidatorModel::arrayType($aArgs, ['data']);
ValidatorModel::intVal($aArgs, ['resId']);
$processLimitDateFound = false;
foreach ($aArgs['data'] as $key => $value) {
$aArgs['data'][$key]['column'] = strtolower($value['column']);
}
foreach ($aArgs['data'] as $value) {
if ($value['column'] == 'process_limit_date') {
$processLimitDateFound = true;
}
if ($value['column'] == 'category_id') {
$categoryId = $value['value'];
}
if ($value['column'] == 'admission_date') {
$admissionDate = $value['value'];
}
$processLimitDate = ResModel::getStoredProcessLimitDate(['resId' => $aArgs['resId'], 'admissionDate' => $admissionDate]);
$aArgs['data'][] = [
'column' => 'process_limit_date',
'value' => $processLimitDate,
'type' => 'date'
];
}
foreach ($aArgs['data'] as $key => $value) {
if (strtolower($value['type']) == 'integer' || strtolower($value['type']) == 'float') {
if ($value['value'] == '') {
$aArgs['data'][$key]['value'] = '0';
}
$aArgs['data'][$key]['value'] = str_replace(',', '.', $value['value']);
}
if ($value['column'] == 'alt_identifier' && empty($value['value']) && !empty($categoryId)) {
$document = ResModel::getById(['resId' => $aArgs['resId'], 'select' => ['destination, type_id']]);
$aArgs['data'][$key]['value'] = ChronoModel::getChrono(['id' => $categoryId, 'entityId' => $document['destination'], 'typeId' => $document['type_id']]);
} elseif ($value['column'] == 'exp_contact_id' && !empty($value['value']) && !is_numeric($value['value'])) {
$mail = explode('<', str_replace('>', '', $value['value']));
$contact = ContactModel::getByEmail(['email' => $mail[count($mail) - 1], 'select' => ['contacts_v2.contact_id']]);
if (!empty($contact['contact_id'])) {
$aArgs['data'][$key]['value'] = $contact['contact_id'];
} else {
$aArgs['data'][$key]['value'] = 0;
}
} elseif ($value['column'] == 'address_id' && !empty($value['value']) && !is_numeric($value['value'])) {
$mail = explode('<', str_replace('>', '', $value['value']));
$contact = ContactModel::getByEmail(['email' => $mail[count($mail) - 1], 'select' => ['contact_addresses.id']]);
if (!empty($contact['id'])) {
$aArgs['data'][$key]['value'] = $contact['ca_id'];
} else {
$aArgs['data'][$key]['value'] = 0;
}
}
}
$formatedData = [];
foreach ($aArgs['data'] as $value) {
$formatedData[$value['column']] = $value['value'];
}
$formatedData['res_id'] = $aArgs['resId'];
return $formatedData;
}