php8
[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         //DB_DataObject::debugLevel(1);
39         
40         $bits= explode('/', $s);
41         $id = 0;
42         // without id as first part...
43         if (!empty($bits[0]) && $bits[0] == 'Thumb') {
44             $this->thumb = true;
45             $this->as_mimetype = 'image/jpeg';
46             $this->size = empty($bits[1]) ? '0x0' : $bits[1];
47             $id = empty($bits[2]) ? 0 :   $bits[2];
48             
49         } else if (!empty($bits[0]) && $bits[0] == 'Download') {
50             $this->method = 'attachment';
51             $id = empty($bits[1]) ? 0 :   $bits[1];
52             
53         } else  if (!empty($bits[1]) && $bits[1] == 'Thumb') { // with id as first part.
54             $this->thumb = true;
55             $this->as_mimetype = 'image/jpeg';
56             $this->size = empty($bits[2]) ? '0x0' : $bits[2];
57             $id = empty($bits[3]) ? 0 :   $bits[3];
58             
59         } else {
60             $id = empty($bits[0]) ? 0 :  $bits[0];
61         }
62         
63         $id = (int) $id;
64          
65        
66         $img = DB_DataObjecT::factory('Images');
67         if (!$id || !$img->get($id)) {
68              
69             header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
70                 urlencode("image with id does not exist, or is invalid"));
71             exit;
72         }
73         
74         
75         
76         
77         //----------- Authentication...
78         
79         
80         
81         $obj = $img->object();
82         if ($obj->tableName() == 'Projects') {
83             if ($this->currentProject() != $obj->id) {
84                 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
85                     urlencode("image is not the same project (1)"));
86                 exit;
87             }
88             
89         } else {
90             if (empty($obj->project_id) || $obj->project_id != $this->currentProject()) {
91                 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
92                     urlencode("image is not the same project (2)"));
93                 exit;
94             }
95         }
96         
97         // -- spamming us with resize..
98          
99         $this->serve($img);
100         exit;
101     }
102     var $thumb = false;
103     var $asmimetype = false;
104     var $method = 'inline';
105     function serve($img)
106     {
107         require_once 'File/Convert.php';
108         if (!file_exists($img->getStoreName())) {
109             //print_r($img);exit;
110             header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
111                 urlencode("Original file was missing : " . $img->getStoreName()));
112     
113         }
114         
115         $x = new File_Convert($img->getStoreName(), $img->mimetype);
116         if (empty($this->as_mimetype)) {
117             $this->as_mimetype  = $img->mimetype;
118         }
119         if (!$this->thumb) {
120             $x->convert( $this->as_mimetype);
121             $x->serve($this->method);
122             exit;
123         }
124         
125         $this->validateSize();
126         $x->convert( $this->as_mimetype, $this->size);
127         $x->serve();
128         exit;
129         
130     }
131     function validateSize()
132     {
133         if (!in_array($this->size, array(
134                
135                 '100',
136                 
137                 '150', 
138                 /*'100x100', 
139                 '150', 
140                 '150x150', 
141                 '200', 
142                 '200x0',
143                 '200x200',  
144                 '400x0'
145                 */
146             ))) {
147             header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
148                     urlencode("invalid size"));
149                 exit;
150         }
151     }
152 }