hedera-web/rest/dms/add.php

107 lines
2.6 KiB
PHP
Raw Normal View History

2016-07-22 20:00:27 +00:00
<?php
use Vn\Lib;
/**
* Adds a document to the Document Management System.
**/
2018-05-23 10:14:20 +00:00
class Add extends Vn\Web\JsonRequest {
function run($db) {
2016-07-22 20:00:27 +00:00
// XXX: Uncomment only to test the script
//$_REQUEST['description'] = 'description';
2018-05-23 10:14:20 +00:00
$description = empty($_REQUEST['description']) ?
2016-07-22 20:00:27 +00:00
NULL : $_REQUEST['description'];
2018-05-23 10:14:20 +00:00
$baseDir = _DATA_DIR .'/'. $this->app->getName();
2016-07-22 20:00:27 +00:00
$docsDir = "$baseDir/dms";
$tempDir = "$baseDir/.dms";
$digXDir = 3;
$zerosDir = '';
for ($i = 0; $i < $digXDir; $i++)
$zerosDir .= '0';
// Checks document restrictions
2018-05-23 10:14:20 +00:00
if (empty($_FILES['doc']['name']))
throw new Lib\UserException('File not choosed');
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
$maxSize = $db->getValue('SELECT max_size FROM dms_config');
2016-07-22 20:00:27 +00:00
if ($_FILES['doc']['size'] > $maxSize * 1048576)
2018-05-23 10:14:20 +00:00
throw new Lib\UserException(sprintf('File size exceeds size: %d MB', $maxSize));
2016-07-22 20:00:27 +00:00
try {
// Registers the document in the database
2018-05-23 10:14:20 +00:00
$db->query('START TRANSACTION');
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
$db->query('INSERT INTO dms_document SET description = #', [$description]);
$docId =(string) $db->getValue('SELECT LAST_INSERT_ID()');
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
$len = strlen($docId);
$neededLevels = ceil($len / $digXDir) - 1;
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
$dirLevels = $db->getValue(
2016-07-22 20:00:27 +00:00
'SELECT dir_levels FROM dms_config LOCK IN SHARE MODE');
if ($dirLevels > $neededLevels)
$neededLevels = $dirLevels;
// Reorganizes the file repository if necessary
if ($dirLevels < $neededLevels)
2018-05-23 10:14:20 +00:00
$dirLevels = $db->getValue(
2016-07-22 20:00:27 +00:00
'SELECT dir_levels FROM dms_config FOR UPDATE');
2018-05-23 10:14:20 +00:00
if ($dirLevels < $neededLevels) {
if (is_dir($docsDir)) {
$dif =($neededLevels - $dirLevels) - 1;
2016-07-22 20:00:27 +00:00
$newDir = $docsDir;
for ($i = 0; $i < $dif; $i++)
$newDir .= "/$zerosDir";
2018-05-23 10:14:20 +00:00
$success = rename($docsDir, $tempDir)
&& mkdir($newDir, 0770, TRUE)
&& rename($tempDir, "$newDir/$zerosDir");
2016-07-22 20:00:27 +00:00
if (!$success)
2018-05-23 10:14:20 +00:00
throw new Exception('Error while reorganizing directory tree');
2016-07-22 20:00:27 +00:00
}
2018-05-23 10:14:20 +00:00
$curLevels = $db->query('UPDATE dms_config SET dir_levels = #',
2016-07-22 20:00:27 +00:00
[$neededLevels]);
}
// Saves the document to the repository
2018-05-23 10:14:20 +00:00
$padLen =($neededLevels + 1) * $digXDir;
$paddedId = str_pad($docId, $padLen, '0', STR_PAD_LEFT);
2016-07-22 20:00:27 +00:00
$saveDir = $docsDir;
for ($i = 0; $i < $neededLevels; $i++)
2018-05-23 10:14:20 +00:00
$saveDir .= '/'. substr($paddedId, $i * $digXDir, $digXDir);
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
if (!file_exists($saveDir))
mkdir($saveDir, 0770, TRUE);
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
$savePath = "$saveDir/". substr($paddedId, -$digXDir);
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
move_uploaded_file($_FILES['doc']['tmp_name'], $savePath);
2016-07-22 20:00:27 +00:00
2018-05-23 10:14:20 +00:00
$db->query('COMMIT');
2016-07-22 20:00:27 +00:00
return $docId;
}
2018-05-23 10:14:20 +00:00
catch (Exception $e) {
$db->query('ROLLBACK');
2016-07-22 20:00:27 +00:00
throw $e;
}
}
}