root/releases/0.2/_files/download.php

Revision 9, 2.2 kB (checked in by sven, 3 years ago)

snapshot of elgg 0.2

Line 
1 <?php
2
3     // Download script
4     // Usage: http://URL/{username}/files/{folder_id}/{file_id}/{filename}
5     
6     // Run includes
7         require("../includes.php");
8         
9     // Initialise functions for user details, icon management and profile management
10         run("userdetails:init");
11         run("profile:init");
12         run("files:init");
13         
14     // If an ID number for the file has been specified ...
15     
16         if (isset($_REQUEST['id'])) {
17             $id = (int) $_REQUEST['id'];
18             
19     // ... and the file exists ...
20             
21             $file = db_query("select * from files where ident = $id");
22             if (sizeof($file) > 0) {
23                 
24                 $file = $file[0];
25                 
26     // ... and the owner of the file in the URL line hasn't been spoofed ...
27                 
28                 if (run("users:name_to_id",$_REQUEST['files_name']) == $file->owner
29                     || run("users:name_to_id",$_REQUEST['files_name']) == $file->files_owner) {
30     
31     // ... and the current user is allowed to access it ...
32                 
33                     if (run("users:access_level_check",$file->access) == true) {
34
35     // Send 304s where possible, rather than spitting out the file each time
36                         $if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']);
37                         
38                         $tstamp = filemtime($file->location);
39                         $lm = gmdate("D, d M Y H:i:s", $tstamp) . " GMT";
40                         
41                         if ($if_modified_since == $lm) {
42                             header("{$_SERVER['SERVER_PROTOCOL']} 304 Not Modified");
43                             exit;
44                         }
45
46     // Send last-modified header to enable if-modified-since requests
47                         if ($tstamp < time()) {
48                             header("Last-Modified: " . $lm);
49                         }
50                         
51     // Then output some appropriate headers and send the file data!
52     
53                         $mimetype = run("files:mimetype:determine",$file->location);
54                         if ($mimetype == false) {
55                             $mimetype = "application/octet-stream";
56                         }
57
58     // "Cache-Control: private" to allow a user's browser to cache the file, but not a shared proxy
59     // Also to override PHP's default "DON'T EVER CACHE THIS EVER" header
60                         header("Cache-Control: private");
61                         
62                         header("Content-type: $mimetype");
63                         if ($mimetype == "application/octet-stream") {
64                             header('Content-Disposition: attachment');
65                         }
66                         readfile($file->location);
67                         
68                     }
69                     
70                 }
71             }
72         }
73
74 ?>
Note: See TracBrowser for help on using the browser.