MTrackWeb/Image.php
[web.mtrack] / MTrackWeb / Image.php
1 <?php
2
3 /**
4  * So why is this called image? and not attachment
5  *
6  * The original database design for handling attachments was only done for images initially
7  *
8  * It's role expanded to attachments over time, but still kept the image name..
9  *
10  * Crazy but true..
11  *
12  */
13
14 /**
15  * view permission should be required on the underlying object...
16  * 
17  * 
18  * Use Cases:
19  * 
20  * Images/{ID}/fullname.xxxx
21  * 
22  * (valid thumbs 200, 400)...?
23  * Images/Thumb/200/{ID}/fullname.xxxx
24  * == force a download..
25  * Images/Download/{ID}/fullname.xxxx
26  * 
27  */
28 require_once  'MTrackWeb.php';
29
30 class MTrackWeb_Image extends MTrackWeb
31 {
32     // relies on parent getAuth.
33     
34     function get($s) // determin what to serve!!!!
35     {
36         $this->as_mimetype = '' ;
37         
38         $bits= explode('/', $s);
39         $id = 0;
40         // without id as first part...
41         if (!empty($bits[0]) && $bits[0] == 'Thumb') {
42             $this->thumb = true;
43             $this->as_mimetype = 'image/jpeg';
44             $this->size = empty($bits[1]) ? '0x0' : $bits[1];
45             $id = empty($bits[2]) ? 0 :   $bits[2];
46             
47         } else if (!empty($bits[0]) && $bits[0] == 'Download') {
48             $this->method = 'attachment';
49             $id = empty($bits[1]) ? 0 :   $bits[1];
50             
51         } else  if (!empty($bits[1]) && $bits[1] == 'Thumb') { // with id as first part.
52             $this->thumb = true;
53             $this->as_mimetype = 'image/jpeg';
54             $this->size = empty($bits[2]) ? '0x0' : $bits[2];
55             $id = empty($bits[3]) ? 0 :   $bits[3];
56             
57         } else {
58             $id = empty($bits[0]) ? 0 :  $bits[0];
59         }
60         
61         $id = (int) $id;
62          
63        
64         $img = DB_DataObjecT::factory('Images');
65         if (!$id || !$img->get($id)) {
66              
67             header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
68                 urlencode("image with id does not exist, or is invalid"));
69             exit;
70         }
71         
72         
73         
74         
75         //----------- Authentication...
76         
77         
78         
79         $obj = $img->object();
80         if ($obj->tableName() == 'Projects') {
81             if ($this->currentProject() != $obj->id) {
82                 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
83                     urlencode("image is not the same project (1)"));
84                 exit;
85             }
86             
87         } else {
88             if (empty($obj->project_id) || $obj->project_id != $this->currentProject()) {
89                 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
90                     urlencode("image is not the same project (2)"));
91                 exit;
92             }
93         }
94         
95         // -- spamming us with resize..
96          
97         $this->serve($img);
98         exit;
99     }
100     var $thumb = false;
101     var $asmimetype = false;
102     var $method = 'inline';
103     function serve($img)
104     {
105         require_once 'File/Convert.php';
106         if (!file_exists($img->getStoreName())) {
107             //print_r($img);exit;
108             header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
109                 urlencode("Original file was missing : " . $img->getStoreName()));
110     
111         }
112         
113         $x = new File_Convert($img->getStoreName(), $img->mimetype);
114         if (empty($this->as_mimetype)) {
115             $this->as_mimetype  = $img->mimetype;
116         }
117         if (!$this->thumb) {
118             $x->convert( $this->as_mimetype);
119             $x->serve($this->method);
120             exit;
121         }
122         
123         $this->validateSize();
124         $x->convert( $this->as_mimetype, $this->size);
125         $x->serve();
126         exit;
127         
128     }
129     function validateSize()
130     {
131         if (!in_array($this->size, array(
132                
133                 '200', 
134                 /*'100x100', 
135                 '150', 
136                 '150x150', 
137                 '200', 
138                 '200x0',
139                 '200x200',  
140                 '400x0'
141                 */
142             ))) {
143             header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
144                     urlencode("invalid size"));
145                 exit;
146         }
147     }
148 }