Skip to content

Commit 72f99d7

Browse files
johnsaiglesamirdas
authored andcommitted
[Doc Repo] Ensure file exists in DB before serving (Redmine #12220) (aces#2742)
* [Doc Repo] Ensure file exists in DB before serving * Output file in downloadable format
1 parent 4f26e0b commit 72f99d7

1 file changed

Lines changed: 20 additions & 15 deletions

File tree

modules/document_repository/ajax/GetFile.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
* @author Dave MacFarlane <driusan@bic.mni.mcgill.ca>
1212
* @license Loris license
1313
* @link https://github.com/aces/Loris-Trunk
14-
*
1514
*/
1615

1716
$user =& User::singleton();
@@ -35,27 +34,33 @@
3534
// Checks that config settings are set
3635
$config =& NDB_Config::singleton();
3736

38-
$File = $_GET['File'];
37+
$file = $_GET['File'];
38+
39+
// Ensure file exists in the document_repository table before serving
40+
$db =& Database::singleton();
41+
$record = $db->pselectOne(
42+
"SELECT record_id FROM document_repository WHERE "
43+
. "Data_dir=:dd",
44+
array('dd' => $file)
45+
);
3946

40-
// Make sure that the user isn't trying to break out of the $path by
41-
// using a relative filename.
42-
// No need to check for '/' since all downloads are relative to $basePath
43-
if (strpos("..", $File) !== false) {
47+
if (empty($record)) {
4448
error_log("ERROR: Invalid filename");
4549
header("HTTP/1.1 400 Bad Request");
4650
exit(4);
47-
}
48-
51+
}
4952

50-
$FullPath = __DIR__ . "/../user_uploads/$File";
53+
$path = __DIR__ . "/../user_uploads/$file";
5154

52-
if (!file_exists($FullPath)) {
53-
error_log("ERROR: File $FullPath does not exist");
55+
if (!file_exists($path)) {
56+
error_log("ERROR: File $path does not exist");
5457
header("HTTP/1.1 404 Not Found");
5558
exit(5);
5659
}
5760

58-
$fp = fopen($FullPath, 'r');
59-
fpassthru($fp);
60-
fclose($fp);
61-
?>
61+
// Output file in downloadable format
62+
header('Content-Description: File Transfer');
63+
header('Content-Type: application/force-download');
64+
header("Content-Transfer-Encoding: Binary");
65+
header("Content-disposition: attachment; filename=\"" . basename($path) . "\"");
66+
readfile($path);

0 commit comments

Comments
 (0)