public static function storeExportFile($original_filename, $file_content, $archiveFile = false, $dateShiftDates = false) { global $edoc_storage_option; ## Create the stored name of the file as it wll be stored in the file system $stored_name = date('YmdHis') . "_pid" . PROJECT_ID . "_" . generateRandomHash(6) . getFileExt($original_filename, true); $file_extension = getFileExt($original_filename); $mime_type = strtolower($file_extension) == 'csv' ? 'application/csv' : 'application/octet-stream'; // If file is UTF-8 encoded, then add BOM // Do NOT use addBOMtoUTF8() on Stata syntax file (.do) because BOM causes issues in syntax file if (strtolower($file_extension) != 'do') { $file_content = addBOMtoUTF8($file_content); } // If Gzip enabled, then gzip the file and append filename with .gz extension list($file_content, $stored_name, $gzipped) = gzip_encode_file($file_content, $stored_name); // Get file size in bytes $docs_size = strlen($file_content); // Add file to file system if ($edoc_storage_option == '0') { // Store locally $fp = fopen(EDOC_PATH . $stored_name, 'w'); if ($fp !== false && fwrite($fp, $file_content) !== false) { // Close connection fclose($fp); } else { // Send error response return false; } // Add file to S3 } elseif ($edoc_storage_option == '2') { global $amazon_s3_key, $amazon_s3_secret, $amazon_s3_bucket; $s3 = new S3($amazon_s3_key, $amazon_s3_secret, SSL); if (!$s3->putObject($file_content, $amazon_s3_bucket, $stored_name, S3::ACL_PUBLIC_READ_WRITE)) { // Send error response return false; } } else { // Store using WebDAV require_once APP_PATH_CLASSES . "WebdavClient.php"; require APP_PATH_WEBTOOLS . 'webdav/webdav_connection.php'; $wdc = new WebdavClient(); $wdc->set_server($webdav_hostname); $wdc->set_port($webdav_port); $wdc->set_ssl($webdav_ssl); $wdc->set_user($webdav_username); $wdc->set_pass($webdav_password); $wdc->set_protocol(1); // use HTTP/1.1 $wdc->set_debug(false); // enable debugging? if (!$wdc->open()) { // Send error response return false; } if (substr($webdav_path, -1) != '/') { $webdav_path .= '/'; } $http_status = $wdc->put($webdav_path . $stored_name, $file_content); $wdc->close(); } ## Add file info to edocs_metadata table // If not archiving file in File Repository, then set to be deleted in 1 hour $delete_time = $archiveFile ? "" : NOW; // Add to table $sql = "insert into redcap_edocs_metadata (stored_name, mime_type, doc_name, doc_size, file_extension, project_id, \n\t\t\t\tstored_date, delete_date, gzipped) values ('" . prep($stored_name) . "', '{$mime_type}', '" . prep($original_filename) . "', \n\t\t\t\t'" . prep($docs_size) . "', '" . prep($file_extension) . "', " . PROJECT_ID . ", '" . NOW . "', " . checkNull($delete_time) . ", {$gzipped})"; if (!db_query($sql)) { // Send error response return false; } // Get edoc_id $edoc_id = db_insert_id(); ## Add to doc_to_edoc table // Set flag if data is date shifted $dateShiftFlag = $dateShiftDates ? "DATE_SHIFT" : ""; // Set "comment" in docs table if (strtolower($file_extension) == 'csv') { $docs_comment = "Data export file created by " . USERID . " on " . date("Y-m-d-H-i-s"); } else { if ($file_extension == 'sps') { $stats_package_name = 'Spss'; } elseif ($file_extension == 'do') { $stats_package_name = 'Stata'; } else { $stats_package_name = camelCase($file_extension); } $docs_comment = "{$stats_package_name} syntax file created by " . USERID . " on " . date("Y-m-d-H-i-s"); } // Archive in redcap_docs table $sql = "INSERT INTO redcap_docs (project_id, docs_name, docs_file, docs_date, docs_size, docs_comment, docs_type, \n\t\t\t\tdocs_rights, export_file, temp) VALUES (" . PROJECT_ID . ", '" . prep($original_filename) . "', NULL, '" . TODAY . "', \n\t\t\t\t'{$docs_size}', '" . prep($docs_comment) . "', '{$mime_type}', " . checkNull($dateShiftFlag) . ", 1, \n\t\t\t\t" . checkNull($archiveFile ? "0" : "1") . ")"; if (db_query($sql)) { $docs_id = db_insert_id(); // Add to redcap_docs_to_edocs also $sql = "insert into redcap_docs_to_edocs (docs_id, doc_id) values ({$docs_id}, {$edoc_id})"; db_query($sql); } else { // Could not store in table, so remove from edocs_metadata also db_query("delete from redcap_edocs_metadata where doc_id = {$edoc_id}"); return false; } // Return successful response of docs_id from redcap_docs table return $docs_id; }
{ $dummy_tmp_name = $dummy['tmp_name']; $dummy_file_size = $dummy['size']; $dummy_file_type = $dummy['type']; $dummy_file_name = $dummy['name']; $dummy_file_name = preg_replace("/[^a-zA-Z-._0-9]/","_",$dummy_file_name); $dummy_file_name = str_replace("__","_",$dummy_file_name); $dummy_file_name = str_replace("__","_",$dummy_file_name); $file_extension = getFileExt($dummy_file_name); $stored_name = date('YmdHis') . "_pid" . $project_id . "_" . generateRandomHash(6) . getFileExt($dummy_file_name, true); if ($edoc_storage_option == '1') { // Webdav $dummy_file_content = file_get_contents($dummy['tmp_name']); $upload_success = ($wdc->put($webdav_path . $stored_name, $dummy_file_content) == '201'); } elseif ($edoc_storage_option == '2') { // S3 $s3 = new S3($amazon_s3_key, $amazon_s3_secret, SSL); $upload_success = ($s3->putObjectFile($dummy['tmp_name'], $amazon_s3_bucket, $stored_name, S3::ACL_PUBLIC_READ_WRITE)); } else { // Local $upload_success = move_uploaded_file($dummy_tmp_name, EDOC_PATH . $stored_name); } if ($upload_success === TRUE) {