import
[web.mtrack] / web / attachment.php
1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
3
4 include '../inc/common.php';
5
6 $pi = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
7 $vars = explode('/', $pi);
8 array_shift($vars);
9 $filename = array_pop($vars);
10 $cid = array_pop($vars);
11 $object = join('/', $vars);
12
13 MTrackACL::requireAllRights($object, 'read');
14
15 foreach (MTrackDB::q('select hash, size from attachments where
16     object = ? and cid = ? and filename = ?', $object, $cid, $filename)
17     ->fetchAll() as $row)
18 {
19   $filename = basename($filename);
20   header("Pragma: public");
21   header('Expires: 0');
22   header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
23   header('Cache-Control: private', false);
24   $path = MTrackAttachment::local_path($row['hash']);
25   $mimetype = mtrack_mime_detect($path, $filename);
26   header("Content-Type: $mimetype");
27
28   list($major) = explode('/', $mimetype, 2);
29   if ($major == 'image' || $major == 'text') {
30     $disp = 'inline';
31   } else {
32     $disp = 'attachment';
33   }
34   header("Content-Disposition: $disp; filename=\"$filename\"");
35   header('Content-Transfer-Encoding: binary');
36   header("Content-Length: $row[size]");
37   readfile($path);
38   exit;
39 }
40
41 mtrack_header('Not found');
42 mtrack_foot();