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 SrcCore\models\ValidatorModel;
class StoreController
{
public static function storeResource(array $aArgs)
{
ValidatorModel::notEmpty($aArgs, ['encodedFile', 'format', 'status', 'type_id', 'category_id']);
ValidatorModel::stringType($aArgs, ['format', 'status', 'category_id']);
ValidatorModel::intVal($aArgs, ['type_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);
return $resId;
} catch (\Exception $e) {
return ['errors' => '[storeResource] ' . $e->getMessage()];
}
}
public static function storeAttachment(array $aArgs)
ValidatorModel::notEmpty($aArgs, ['encodedFile', 'data', 'table', 'fileFormat', 'status']);
ValidatorModel::stringType($aArgs, ['collId', 'table', 'fileFormat', 'status']);
try {
$fileContent = base64_decode(str_replace(['-', '_'], ['+', '/'], $aArgs['encodedFile']));
$storeResult = DocserverController::storeResourceOnDocServer([
'collId' => empty($aArgs['version']) ? 'attachments_coll' : 'attachments_version_coll',
'encodedResource' => base64_encode($fileContent),
'format' => $aArgs['fileFormat']
]);
if (!empty($storeResult['errors'])) {
return ['errors' => '[storeResource] ' . $storeResult['errors']];
}
$data = StoreController::prepareAttachmentStorage([
'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']
]);
if (empty($aArgs['version'])) {
$id = AttachmentModel::create($data);
} else {
$id = AttachmentModel::createVersion($data);
110
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
} 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 $args)
ValidatorModel::notEmpty($args, ['docserver_id', 'filename', 'format', 'filesize', 'path', 'fingerprint', 'status', 'res_id']);
ValidatorModel::stringType($args, ['docserver_id', 'filename', 'format', 'path', 'fingerprint', 'status']);
ValidatorModel::intVal($args, ['filesize', 'res_id']);
$args['typist'] = $GLOBALS['id'];
unset($args['alt_identifier']);
if (!empty($args['chrono'])) {
$args['alt_identifier'] = ChronoModel::getChrono(['id' => $args['category_id'], 'entityId' => $args['destination'], 'typeId' => $args['type_id'], 'resId' => $args['res_id']]);
if (empty($args['process_limit_date'])) {
$processLimitDate = ResModel::getStoredProcessLimitDate(['typeId' => $args['type_id'], 'admissionDate' => $args['admission_date']]);
$args['process_limit_date'] = $processLimitDate;
unset($args['external_id']);
if (!empty($args['externalId'])) {
if (is_array($args['externalId'])) {
$args['external_id'] = json_encode($args['externalId']);
unset($args['externalId']);
$args['creation_date'] = 'CURRENT_TIMESTAMP';
public static function prepareAttachmentStorage(array $aArgs)
ValidatorModel::notEmpty($aArgs, ['data', 'docserverId', 'fileName', 'fileFormat', 'fileSize', 'path', 'fingerPrint']);
184
185
186
187
188
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
225
226
227
228
229
230
231
232
233
234
ValidatorModel::stringType($aArgs, ['docserverId', 'status', 'fileName', 'fileFormat', 'path', 'fingerPrint']);
ValidatorModel::arrayType($aArgs, ['data']);
ValidatorModel::intVal($aArgs, ['fileSize']);
foreach ($aArgs['data'] as $key => $value) {
$aArgs['data'][$key]['column'] = strtolower($value['column']);
}
$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 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;
}
public static function getAllowedFiles()
{
$allowedFiles = [];
$loadedXml = CoreConfigModel::getXmlLoaded(['path' => 'apps/maarch_entreprise/xml/extensions.xml']);
if ($loadedXml) {
foreach ($loadedXml->FORMAT as $value) {
$allowedFiles[] = [
'extension' => (string)$value->name,
'mimeType' => (string)$value->mime,
'canConvert' => filter_var((string)$value->canConvert, FILTER_VALIDATE_BOOLEAN)
];
}
}
return $allowedFiles;
}
public static function getBytesSizeFromPhpIni(array $args)
{
if (strpos($args['size'], 'K') !== false) {
return (int)$args['size'] * 1024;
} elseif (strpos($args['size'], 'M') !== false) {
return (int)$args['size'] * 1048576;
} elseif (strpos($args['size'], 'G') !== false) {
return (int)$args['size'] * 1073741824;
}
return (int)$args['size'];
}