From bab69e7a33395b980b04cc02ba2e4d75d2d28f3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aliz=C3=A9e?= Date: Tue, 5 Jun 2018 10:59:43 -0400 Subject: [PATCH] Fixed multiple files with same name for document repository --- SQL/0000-00-00-schema.sql | 1 + ...Add_UUID_column_to_document_repository.sql | 1 + .../ajax/documentDelete.php | 19 +++++ .../ajax/documentEditUpload.php | 84 +++++++++++++++---- .../test/document_repositoryTest.php | 1 + test/RBdata.sql | 2 +- 6 files changed, 92 insertions(+), 16 deletions(-) create mode 100644 SQL/Archive/19.2/2018-06-05_Add_UUID_column_to_document_repository.sql diff --git a/SQL/0000-00-00-schema.sql b/SQL/0000-00-00-schema.sql index 63e0305a499..6be0fef5608 100644 --- a/SQL/0000-00-00-schema.sql +++ b/SQL/0000-00-00-schema.sql @@ -864,6 +864,7 @@ CREATE TABLE `document_repository` ( `EARLI` tinyint(1) DEFAULT '0', `hide_video` tinyint(1) DEFAULT '0', `File_category` int(3) unsigned DEFAULT NULL, + `UUID` varchar(36) DEFAULT '', PRIMARY KEY (`record_id`), KEY `fk_document_repository_1_idx` (`File_category`), CONSTRAINT `fk_document_repository_1` FOREIGN KEY (`File_category`) REFERENCES `document_repository_categories` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION diff --git a/SQL/Archive/19.2/2018-06-05_Add_UUID_column_to_document_repository.sql b/SQL/Archive/19.2/2018-06-05_Add_UUID_column_to_document_repository.sql new file mode 100644 index 00000000000..a9d542e8efe --- /dev/null +++ b/SQL/Archive/19.2/2018-06-05_Add_UUID_column_to_document_repository.sql @@ -0,0 +1 @@ +ALTER TABLE document_repository ADD `UUID` varchar(36) DEFAULT ''; \ No newline at end of file diff --git a/modules/document_repository/ajax/documentDelete.php b/modules/document_repository/ajax/documentDelete.php index 542a9f1dac9..44a2ccd5d49 100644 --- a/modules/document_repository/ajax/documentDelete.php +++ b/modules/document_repository/ajax/documentDelete.php @@ -10,18 +10,23 @@ * @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 * @link https://github.com/aces/Loris */ + $user =& User::singleton(); + if (!$user->hasPermission('document_repository_delete')) { header("HTTP/1.1 403 Forbidden"); exit; } set_include_path(get_include_path().":../../project/libraries:../../php/libraries:"); + require_once "NDB_Client.class.inc"; require_once "NDB_Config.class.inc"; require_once "Email.class.inc"; + $client = new NDB_Client(); $client->initialize("../../project/config.xml"); + $factory = NDB_Factory::singleton(); $baseURL = $factory->settings()->getBaseURL(); @@ -67,4 +72,18 @@ unlink($path); } +// Cleanup empty directories +set_error_handler( + function () { + // Silence the E_WARNING when files exist in the directory. + } +); +$rm_directory = __DIR__ . '/../user_uploads/' + . substr($dataDir, 0, strlen($dataDir)-(strlen($fileName)+1)); +rmdir($rm_directory); +$rm_directory = __DIR__ . '/../user_uploads/' + . $userName . '/' . $fileName; +rmdir($rm_directory); +restore_error_handler(); + ?> diff --git a/modules/document_repository/ajax/documentEditUpload.php b/modules/document_repository/ajax/documentEditUpload.php index e50c3125652..cbcd41c0243 100644 --- a/modules/document_repository/ajax/documentEditUpload.php +++ b/modules/document_repository/ajax/documentEditUpload.php @@ -25,15 +25,17 @@ require_once "NDB_Client.class.inc"; require_once "NDB_Config.class.inc"; require_once "Email.class.inc"; + $client = new NDB_Client(); $client->initialize("../../project/config.xml"); + $factory = NDB_Factory::singleton(); $baseURL = $factory->settings()->getBaseURL(); -$config = NDB_Config::singleton(); - -// create Database object -$DB =& Database::singleton(); +// Setup Database object. +$config =& \NDB_Config::singleton(); +$db_config = $config->getSetting('database'); +$db =& \Database::singleton(); $editNotifier = new NDB_Notifier( "document_repository", @@ -58,26 +60,61 @@ $instrument = $_POST['instrument'] !== '' ? $_POST['instrument'] : null; $pscid = $_POST['pscid'] !== '' ? $_POST['pscid'] : null; $visit = $_POST['visit'] !== '' ? $_POST['visit'] : null; - $comments = $_POST['comments'] !== '' ? $_POST['commnets'] : null; + $comments = $_POST['comments'] !== '' ? $_POST['comments'] : null; $version = $_POST['version'] !== '' ? $_POST['version'] : null; + $uuid = uuid4(); $fileSize = $_FILES["file"]["size"]; $fileName = $_FILES["file"]["name"]; - $fileType = end((explode(".", $fileName))); + $fileType = ''; + // Handle retrieving the file type. + if (preg_match('/\./', $fileName)) { + $pos = strrpos($fileName, '.', -1); + if ($pos+1 != strlen($fileName)) { + $fileType = substr( + $fileName, + strrpos($fileName, '.', -1)+1 + ); + } + } + $sql_statement = $db->prepare( + 'SELECT File_name, version FROM document_repository ' + .'WHERE File_name=? AND uploaded_by=?' + ); + $sql_statement->bindParam(1, $fileName, PDO::PARAM_STR); + $sql_statement->bindParam(2, $puser, PDO::PARAM_STR); + $sql_statement->execute(); + $sql_result = $sql_statement->fetchAll(PDO::FETCH_ASSOC); // __DIR__ is the document_repository ajax directory // when this script is executing. Go up a level to the // document_repository module directory, and use a // user_uploads directory as a base for user uploads - $base_path = __DIR__ . "/../user_uploads/"; - $fileBase = $puser . "/" . $fileName; + $base_path = realpath(__DIR__ . '/..') . '/user_uploads/'; + $fileBase = $puser . '/' + . $fileName + . '/' . $uuid + . '/' . $fileName; + // Create user directory /base_path/user if (!file_exists($base_path . $puser)) { - mkdir($base_path . $puser, 0777); + mkdir($base_path . $puser, 0770); } - - - $target_path = $base_path . $fileBase; + // Create filename directory /base_path/user/fileName + if (!file_exists($base_path . $puser . '/' . $fileName)) { + mkdir($base_path . $puser . '/' . $fileName, 0770); + } + // Create uuid directory /base_path/user/fileName/uuid + if (!file_exists( + $base_path . $puser . '/' . $fileName . '/' . $uuid + ) + ) { + mkdir( + $base_path . $puser . '/' . $fileName . '/' . $uuid, + 0770 + ); + } + $target_path = $base_path . $fileBase; if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_path)) { $success = $DB->insert( @@ -95,10 +132,10 @@ 'PSCID' => $pscid, 'visitLabel' => $visit, 'File_type' => $fileType, + 'UUID' => $uuid, ) ); - $msg_data['newDocument'] - = $baseURL . "/document_repository/"; + $msg_data['newDocument'] = $baseURL . "/document_repository/"; $msg_data['document'] = $fileName; $uploadNotifier->notify($msg_data); @@ -110,7 +147,7 @@ } else { echo "There was an error uploading the file"; } - } elseif ($action == 'edit') { + } else if ($action == 'edit') { $id = $_POST['idEdit']; $category = $_POST['categoryEdit']; $instrument = $_POST['instrumentEdit']; @@ -147,4 +184,21 @@ } } +/** + * Create a UUID v4 string. + * + * Source from comments: + * http://php.net/manual/en/function.com-create-guid.php + * Maybe move to Utilities class. + * + * @return String $version + */ +function uuid4() +{ + $data = openssl_random_pseudo_bytes(16); + $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100 + $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10 + return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); +} + ?> diff --git a/modules/document_repository/test/document_repositoryTest.php b/modules/document_repository/test/document_repositoryTest.php index fd21ed3af29..abed4cf0192 100644 --- a/modules/document_repository/test/document_repositoryTest.php +++ b/modules/document_repository/test/document_repositoryTest.php @@ -65,6 +65,7 @@ public function setUp() 'EARLI' => '0', 'hide_video' => '0', 'File_category' => '9999999', + 'UUID' => 'dc987d5c-6e5d-11e8-adc0-fa7ae01bbebc', ) ); diff --git a/test/RBdata.sql b/test/RBdata.sql index d9a8d7f6f01..0992206b23b 100644 --- a/test/RBdata.sql +++ b/test/RBdata.sql @@ -1673,7 +1673,7 @@ UNLOCK TABLES; LOCK TABLES `document_repository` WRITE; DELETE FROM `document_repository`; /*!40000 ALTER TABLE `document_repository` DISABLE KEYS */; -INSERT INTO `document_repository` VALUES (1,'','','',NULL,'2016-07-27 18:00:10','admin/bread-breakfast-knife.jpg','bread-breakfast-knife.jpg','jpg','1',439412,'admin',2,' ',NULL,0,0,2),(2,'','','',NULL,'2016-07-27 18:00:47','admin/bread-food-healthy-breakfast.jpg','bread-food-healthy-breakfast.jpg','jpg','2',1336341,'admin',2,' ',NULL,0,0,2),(3,'','','',NULL,'2016-07-27 18:02:58','admin/bread-food-healthy-breakfast.jpg','bread-food-healthy-breakfast.jpg','jpg','1',1336341,'admin',2,' ',NULL,0,0,2),(4,'','','',NULL,'2016-07-27 18:04:00','admin/test.txt','test.txt','txt','',29,'admin',3,' ',NULL,0,0,1),(5,'','','',NULL,'2016-07-27 18:04:53','admin/test.pdf','test.pdf','pdf','',7581,'admin',4,' ',NULL,0,0,3),(6,'','radiology_review','',NULL,'2016-07-27 18:05:36','admin/test.pdf','test.pdf','pdf','',7581,'admin',2,' ',NULL,0,0,3),(7,'','medical_history','',NULL,'2016-07-27 18:05:59','admin/test.pdf','test.pdf','pdf','',7581,'admin',3,' ',NULL,0,0,3),(8,'MTL003','bmi','V1',NULL,'2016-07-27 18:06:43','admin/test.txt','test.txt','txt','',29,'admin',2,' ',NULL,0,0,1); +INSERT INTO `document_repository` VALUES (1,'','','',NULL,'2016-07-27 18:00:10','admin/bread-breakfast-knife.jpg','bread-breakfast-knife.jpg','jpg','1',439412,'admin',2,' ',NULL,0,0,2,'531cc990-6e6f-11e8-adc0-fa7ae01bbebc'),(2,'','','',NULL,'2016-07-27 18:00:47','admin/bread-food-healthy-breakfast.jpg','bread-food-healthy-breakfast.jpg','jpg','2',1336341,'admin',2,' ',NULL,0,0,2,'736c6782-6e6f-11e8-adc0-fa7ae01bbebc'),(3,'','','',NULL,'2016-07-27 18:02:58','admin/bread-food-healthy-breakfast.jpg','bread-food-healthy-breakfast.jpg','jpg','1',1336341,'admin',2,' ',NULL,0,0,2,'876707d8-6e6f-11e8-adc0-fa7ae01bbebc'),(4,'','','',NULL,'2016-07-27 18:04:00','admin/test.txt','test.txt','txt','',29,'admin',3,' ',NULL,0,0,1,'917846f6-6e6f-11e8-adc0-fa7ae01bbebc'),(5,'','','',NULL,'2016-07-27 18:04:53','admin/test.pdf','test.pdf','pdf','',7581,'admin',4,' ',NULL,0,0,3,'99e04686-6e6f-11e8-adc0-fa7ae01bbebc'),(6,'','radiology_review','',NULL,'2016-07-27 18:05:36','admin/test.pdf','test.pdf','pdf','',7581,'admin',2,' ',NULL,0,0,3,'a256481a-6e6f-11e8-adc0-fa7ae01bbebc'),(7,'','medical_history','',NULL,'2016-07-27 18:05:59','admin/test.pdf','test.pdf','pdf','',7581,'admin',3,' ',NULL,0,0,3,'ab3e9a9a-6e6f-11e8-adc0-fa7ae01bbebc'),(8,'MTL003','bmi','V1',NULL,'2016-07-27 18:06:43','admin/test.txt','test.txt','txt','',29,'admin',2,' ',NULL,0,0,1,'b2b48a28-6e6f-11e8-adc0-fa7ae01bbebc'); /*!40000 ALTER TABLE `document_repository` ENABLE KEYS */; UNLOCK TABLES;