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 Docserver Controller
* @author dev@maarch.org
* @ingroup core
*/
use Core\Models\ServiceModel;
use Core\Models\ValidatorModel;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Docserver\models\DocserverModel;
use SrcCore\controllers\StoreController;
public function get(RequestInterface $request, ResponseInterface $response)
if (!ServiceModel::hasService(['id' => 'admin_docservers', 'userId' => $_SESSION['user']['UserId'], 'location' => 'apps', 'type' => 'admin'])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
return $response->withJson(['docservers' => DocserverModel::get()]);
}
public function getById(RequestInterface $request, ResponseInterface $response, $aArgs)
{
if (!ServiceModel::hasService(['id' => 'admin_docservers', 'userId' => $_SESSION['user']['UserId'], 'location' => 'apps', 'type' => 'admin'])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
$docserver = DocserverModel::getById(['id' => $aArgs['id']]);
if(empty($docserver)){
return $response->withStatus(400)->withJson(['errors' => 'Docserver not found']);
return $response->withJson($docserver);
}
public function delete(RequestInterface $request, ResponseInterface $response, $aArgs)
{
if (!ServiceModel::hasService(['id' => 'admin_docservers', 'userId' => $_SESSION['user']['UserId'], 'location' => 'apps', 'type' => 'admin'])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
$docserver = DocserverModel::getById(['id' => $aArgs['id']]);
if(empty($docserver)){
return $response->withStatus(400)->withJson(['errors' => 'Docserver does not exist']);
DocserverModel::delete(['id' => $aArgs['id']]);
return $response->withJson(['docservers' => DocserverModel::get()]);
public static function createPathOnDocServer(array $aArgs)
{
ValidatorModel::notEmpty($aArgs, ['path']);
ValidatorModel::stringType($aArgs, ['path']);
if (!is_dir($aArgs['path'])) {
return ['errors' => '[createPathOnDocServer] Path does not exist ' . $aArgs['path']];
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
107
108
109
110
}
error_reporting(0);
umask(0022);
$yearPath = $aArgs['path'] . date('Y') . '/';
if (!is_dir($yearPath)) {
mkdir($yearPath, 0770);
if (DIRECTORY_SEPARATOR == '/' && !empty($GLOBALS['apacheUserAndGroup'])) {
exec('chown ' . escapeshellarg($GLOBALS['apacheUserAndGroup']) . ' ' . escapeshellarg($yearPath));
}
umask(0022);
chmod($yearPath, 0770);
}
$monthPath = $yearPath . date('m') . '/';
if (!is_dir($monthPath)) {
mkdir($monthPath, 0770);
if (DIRECTORY_SEPARATOR == '/' && !empty($GLOBALS['apacheUserAndGroup'])) {
exec('chown ' . escapeshellarg($GLOBALS['apacheUserAndGroup']) . ' ' . escapeshellarg($monthPath));
}
umask(0022);
chmod($monthPath, 0770);
}
$pathToDS = $monthPath;
if (!empty($GLOBALS['wb'])) {
$pathToDS = "{$monthPath}BATCH/{$GLOBALS['wb']}/";
if (!is_dir($pathToDS)) {
mkdir($pathToDS, 0770, true);
if (DIRECTORY_SEPARATOR == '/' && !empty($GLOBALS['apacheUserAndGroup'])) {
exec('chown ' . escapeshellarg($GLOBALS['apacheUserAndGroup']) . ' ' . escapeshellarg($monthPath));
}
umask(0022);
chmod($monthPath, 0770);
} else {
//return ['errors' => '[createPathOnDocServer] Folder alreay exists, workbatch already exist:' . $pathToDS];
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
}
}
return $pathToDS;
}
public static function getNextFileNameInDocServer(array $aArgs)
{
ValidatorModel::notEmpty($aArgs, ['pathOnDocserver']);
ValidatorModel::stringType($aArgs, ['pathOnDocserver']);
if (!is_dir($aArgs['pathOnDocserver'])) {
return ['errors' => '[getNextFileNameInDocServer] PathOnDocserver does not exist'];
}
umask(0022);
$aFiles = scandir($aArgs['pathOnDocserver']);
array_shift($aFiles); // Remove . line
array_shift($aFiles); // Remove .. line
if (file_exists($aArgs['pathOnDocserver'] . '/package_information')) {
unset($aFiles[array_search('package_information', $aFiles)]);
}
if (is_dir($aArgs['pathOnDocserver'] . '/BATCH')) {
unset($aFiles[array_search('BATCH', $aFiles)]);
}
$filesNb = count($aFiles);
if ($filesNb == 0) {
$zeroOnePath = $aArgs['pathOnDocserver'] . '0001/';
if (!mkdir($zeroOnePath, 0770)) {
return ['errors' => '[getNextFileNameInDocServer] Directory creation failed: ' . $zeroOnePath];
} else {
if (DIRECTORY_SEPARATOR == '/' && !empty($GLOBALS['apacheUserAndGroup'])) {
exec('chown ' . escapeshellarg($GLOBALS['apacheUserAndGroup']) . ' ' . escapeshellarg($zeroOnePath));
}
umask(0022);
chmod($zeroOnePath, 0770);
return [
'destinationDir' => $zeroOnePath,
'fileDestinationName' => '0001_' . mt_rand(),
];
}
} else {
$destinationDir = $aArgs['pathOnDocserver'] . str_pad(count($aFiles), 4, '0', STR_PAD_LEFT) . '/';
$aFilesBis = scandir($aArgs['pathOnDocserver'] . strval(str_pad(count($aFiles), 4, '0', STR_PAD_LEFT)));
array_shift($aFilesBis); // Remove . line
array_shift($aFilesBis); // Remove .. line
$filesNbBis = count($aFilesBis);
if ($filesNbBis >= 1000) { //If number of files >= 1000 then creates a new subdirectory
$zeroNumberPath = $aArgs['pathOnDocserver'] . str_pad($filesNb + 1, 4, '0', STR_PAD_LEFT) . '/';
if (!mkdir($zeroNumberPath, 0770)) {
return ['errors' => '[getNextFileNameInDocServer] Directory creation failed: ' . $zeroNumberPath];
} else {
if (DIRECTORY_SEPARATOR == '/' && !empty($GLOBALS['apacheUserAndGroup'])) {
exec('chown ' . escapeshellarg($GLOBALS['apacheUserAndGroup']) . ' ' . escapeshellarg($zeroNumberPath));
}
umask(0022);
chmod($zeroNumberPath, 0770);
return [
'destinationDir' => $zeroNumberPath,
'fileDestinationName' => '0001_' . mt_rand(),
];
}
} else {
$higher = $filesNbBis + 1;
foreach ($aFilesBis as $value) {
$currentFileName = explode('.', $value);
if ($higher <= (int)$currentFileName[0]) {
$higher = (int)$currentFileName[0] + 1;
}
}
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
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
return [
'destinationDir' => $destinationDir,
'fileDestinationName' => str_pad($higher, 4, '0', STR_PAD_LEFT) . '_' . mt_rand(),
];
}
}
}
public static function copyOnDocServer(array $aArgs)
{
ValidatorModel::notEmpty($aArgs, ['destinationDir', 'fileDestinationName', 'sourceFilePath']);
ValidatorModel::stringType($aArgs, ['destinationDir', 'fileDestinationName', 'sourceFilePath']);
if (file_exists($aArgs['destinationDir'] . $aArgs['fileDestinationName'])) {
return ['errors' => '[copyOnDocserver] File already exists: ' . $aArgs['destinationDir'] . $aArgs['fileDestinationName']];
}
if (!file_exists($aArgs['sourceFilePath'])) {
return ['errors' => '[copyOnDocserver] File does not exist'];
}
error_reporting(0);
$aArgs['sourceFilePath'] = str_replace('\\\\', '\\', $aArgs['sourceFilePath']);
if (!is_dir($aArgs['destinationDir'])) {
mkdir($aArgs['destinationDir'], 0770, true);
if (DIRECTORY_SEPARATOR == '/' && !empty($GLOBALS['apacheUserAndGroup'])) {
exec('chown ' . escapeshellarg($GLOBALS['apacheUserAndGroup']) . ' ' . escapeshellarg($aArgs['destinationDir']));
}
umask(0022);
chmod($aArgs['destinationDir'], 0770);
}
if (!copy($aArgs['sourceFilePath'], $aArgs['destinationDir'] . $aArgs['fileDestinationName'])) {
return ['errors' => '[copyOnDocserver] Copy on the docserver failed'];
}
if (DIRECTORY_SEPARATOR == '/' && !empty($GLOBALS['apacheUserAndGroup'])) {
exec('chown ' . escapeshellarg($GLOBALS['apacheUserAndGroup']) . ' ' . escapeshellarg($aArgs['destinationDir'] . $aArgs['fileDestinationName']));
}
umask(0022);
chmod($aArgs['destinationDir'] . $aArgs['fileDestinationName'], 0770);
$fingerprintControl = StoreController::controlFingerPrint([
'pathInit' => $aArgs['sourceFilePath'],
'pathTarget' => $aArgs['destinationDir'] . $aArgs['fileDestinationName'],
'fingerprintMode' => $aArgs['docserverSourceFingerprint'],
]);
if (!empty($fingerprintControl['errors'])) {
return ['errors' => '[copyOnDocserver] ' . $fingerprintControl['errors']];
}
if (!empty($GLOBALS['currentStep'])) { // For batch like life cycle
$aArgs['destinationDir'] = str_replace($GLOBALS['docservers'][$GLOBALS['currentStep']]['docserver']['path_template'], '', $aArgs['destinationDir']);
}
$aArgs['destinationDir'] = str_replace(DIRECTORY_SEPARATOR, '#', $aArgs['destinationDir']);
$dataToReturn = [
'copyOnDocserver' =>
[
'destinationDir' => $aArgs['destinationDir'],
'fileDestinationName' => $aArgs['fileDestinationName'],
'fileSize' => filesize(str_replace('#', '/', $aArgs['destinationDir']) . $aArgs['fileDestinationName']),
]
];
if (!empty($GLOBALS['TmpDirectory'])) {
DocserverController::directoryWasher(['path' => $GLOBALS['TmpDirectory']]);
}
return $dataToReturn;
}
private static function directoryWasher(array $aArgs)
{
ValidatorModel::notEmpty($aArgs, ['path']);
ValidatorModel::stringType($aArgs, ['path']);
if (!is_dir($aArgs['path'])) {
return ['errors' => '[directoryWasher] Path does not exist'];
}
$aFiles = scandir($aArgs['path']);
foreach ($aFiles as $file) {
if ($file != '.' && $file != '..') {
if (filetype($aArgs['path'] . '/' . $file) == 'dir') {
DocserverController::directoryWasher(['path' => $aArgs['path'] . '/' . $file]);
} else {
unlink($aArgs['path'] . '/' . $file);
}
}
}
reset($aFiles);
return true;
}