root/releases/0.1.2a/_files/download.php

Revision 7, 2.1 kB (checked in by sven, 3 years ago)

snapshot of elgg 0.1.2a

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     
30     // ... and the current user is allowed to access it ...
31                 
32                     if (run("users:access_level_check",$file->access) == true) {
33
34     // Send 304s where possible, rather than spitting out the file each time
35                         $if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']);
36                         
37                         $tstamp = filemtime($file->location);
38                         $lm = gmdate("D, d M Y H:i:s", $tstamp) . " GMT";
39                         
40                         if ($if_modified_since == $lm) {
41                             header("{$_SERVER['SERVER_PROTOCOL']} 304 Not Modified");
42                             exit;
43                         }
44
45     // Send last-modified header to enable if-modified-since requests
46                         if ($tstamp < time()) {
47                             header("Last-Modified: " . $lm);
48                         }
49                         
50     // Then output some appropriate headers and send the file data!
51     
52                         $mimetype = run("files:mimetype:determine",$file->location);
53                         if ($mimetype == false) {
54                             $mimetype = "application/octet-stream";
55                         }
56
57     // "Cache-Control: private" to allow a user's browser to cache the file, but not a shared proxy
58     // Also to override PHP's default "DON'T EVER CACHE THIS EVER" header
59                         header("Cache-Control: private");
60                         
61                         header("Content-type: $mimetype");
62                         if ($mimetype == "application/octet-stream") {
63                             header('Content-Disposition: attachment');
64                         }
65                         readfile($file->location);
66                         
67                     }
68                     
69                 }
70             }
71         }
72
73 ?>
Note: See TracBrowser for help on using the browser.