|
11 | 11 | * @author Dave MacFarlane <driusan@bic.mni.mcgill.ca> |
12 | 12 | * @license Loris license |
13 | 13 | * @link https://github.com/aces/Loris-Trunk |
14 | | - * |
15 | 14 | */ |
16 | 15 |
|
17 | 16 | $user =& User::singleton(); |
|
35 | 34 | // Checks that config settings are set |
36 | 35 | $config =& NDB_Config::singleton(); |
37 | 36 |
|
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 | +); |
39 | 46 |
|
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)) { |
44 | 48 | error_log("ERROR: Invalid filename"); |
45 | 49 | header("HTTP/1.1 400 Bad Request"); |
46 | 50 | exit(4); |
47 | | -} |
48 | | - |
| 51 | +} |
49 | 52 |
|
50 | | -$FullPath = __DIR__ . "/../user_uploads/$File"; |
| 53 | +$path = __DIR__ . "/../user_uploads/$file"; |
51 | 54 |
|
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"); |
54 | 57 | header("HTTP/1.1 404 Not Found"); |
55 | 58 | exit(5); |
56 | 59 | } |
57 | 60 |
|
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