php8
[web.mtrack] / MTrackWeb / Attachment.php
1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
3
4 require_once 'MTrackWeb.php';
5
6 class MTrackWeb_Attachment extends MTrackWeb
7 {
8     function getAuth()
9     {
10         return parent::getAuth();
11     }
12     
13     function get($pi)
14     {
15         die("not yet");
16         require_once 'MTrack/Attachment.php';
17
18
19         $vars = explode('/', $pi);
20         //array_shift($vars); // as our version does not add '/'
21         $filename = array_pop($vars);
22         $cid = array_pop($vars);
23         $object = join('/', $vars);
24
25         MTrackACL::requireAllRights($object, 'read');
26         $q = MTrackDB::q('
27             select 
28                 hash, size 
29             from 
30                 attachments 
31             where
32                 object = ? and cid = ? and filename = ?',
33                 $object, $cid, $filename);
34         $ar = $q->fetchAll();
35         if (empty($ar)) {
36             throw new Exception("Image not found");
37            }
38         $row = array_shift($q->fetchAll());
39         $filename = basename($filename);
40         header("Pragma: public");
41         header('Expires: 0');
42         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
43         header('Cache-Control: private', false);
44         $path = MTrackAttachment::local_path($row['hash']);
45         require_once 'File/MimeType.php';
46         $y = new File_MimeType();
47         $mimetype = $y->fromFilename($filename);
48
49         header("Content-Type: $mimetype");
50
51         list($major) = explode('/', $mimetype, 2);
52         if ($major == 'image' || $major == 'text') {
53             $disp = 'inline';
54         } else {
55             $disp = 'attachment';
56         }
57         header("Content-Disposition: $disp; filename=\"$filename\"");
58         header('Content-Transfer-Encoding: binary');
59         header("Content-Length: $row[size]");
60         readfile($path);
61         exit;
62     }
63     
64 }
65