8eaaad87baad1106b0ca54f5d770e0f683035fe7
[Pman.Base] / Pman / Images.php
1 <?php
2 /**
3  * view permission should be required on the underlying object...
4  * 
5  * 
6  * Use Cases:
7  * 
8  * args: ontable request
9  *      ontable (req) tablename.
10  *      filename
11  *      (other table args)
12  *      as (serve as a type) = eg. mimetype.
13  * 
14  * args: generic
15  *     as :(serve as a type) = eg. mimetype.
16  * 
17  * Images/{ID}/fullname.xxxx
18  * 
19  * (valid thumbs 200, 400)...?
20  * Images/Thumb/200/{ID}/fullname.xxxx
21  * Images/Download/{ID}/fullname.xxxx
22  * 
23  */
24 require_once  'Pman.php';
25 class Pman_Images extends Pman
26 {
27     function getAuth()
28     {
29         parent::getAuth(); // load company!
30         //return true;
31         $au = $this->getAuthUser();
32         //if (!$au) {
33         //    die("Access denied");
34        // }
35         $this->authUser = $au;
36         
37         return true;
38     }
39     
40     function get($s) // determin what to serve!!!!
41     {
42         $this->as_mimetype = empty($_REQUEST['as']) ? '' : $_REQUEST['as'];
43         
44         $bits= explode('/', $s);
45         $id = 0;
46         // without id as first part...
47         if (!empty($bits[0]) && $bits[0] == 'Thumb') {
48             $this->thumb = true;
49             $this->as_mimetype = 'image/jpeg';
50             $this->size = empty($bits[1]) ? '0x0' : $bits[1];
51             $id = empty($bits[2]) ? 0 :   $bits[2];
52             
53         } else if (!empty($bits[0]) && $bits[0] == 'Download') {
54             $this->method = 'attachment';
55             $id = empty($bits[1]) ? 0 :   $bits[1];
56             
57         } else  if (!empty($bits[1]) && $bits[1] == 'Thumb') { // with id as first part.
58             $this->thumb = true;
59             $this->as_mimetype = 'image/jpeg';
60             $this->size = empty($bits[2]) ? '0x0' : $bits[2];
61             $id = empty($bits[3]) ? 0 :   $bits[3];
62             
63         } else {
64             $id = empty($bits[0]) ? 0 :  $bits[0];
65         }
66         
67         if (strpos($id,':') > 0) {  // id format  tablename:id:-imgtype
68             $onbits = explode(':', $id);
69             if ((count($onbits) < 2)   || empty($onbits[1]) || !is_numeric($onbits[1]) || !strlen($onbits[0])) {
70                 die("Bad url");
71             }
72             //DB_DataObject::debugLevel(1);
73             $img = DB_DataObjecT::factory('Images');
74             $img->ontable = $onbits[0];
75             $img->onid = $onbits[1];
76             if (empty($_REQUEST['anytype'])) {
77                 $img->whereAdd("mimetype like 'image/%'");
78             }
79             
80             if (isset($onbits[2])) {
81                 $img->imgtype = $onbits[2];
82             }
83             $img->limit(1);
84             if (!$img->find(true)) {
85                 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
86                 urlencode("no images for that item: " . htmlspecialchars($id)));
87             }
88             
89             $id = $img->id;
90             
91             
92         }
93         $id = (int) $id;
94         
95         // depreciated - should use ontable:onid:type here...
96         if (!empty($_REQUEST['ontable'])) {
97
98             //DB_DataObjecT::debugLevel(1);
99             $img = DB_DataObjecT::factory('Images');
100             $img->setFrom($_REQUEST);
101             // use imgtype now...
102            // if (!empty($_REQUEST['query']['filename'])){
103            //     $img->whereAdd("filename LIKE '". $img->escape($_REQUEST['query']['filename']).".%'");
104            // }
105             
106             
107             $img->limit(1);
108             if (!$img->find(true)) {
109                 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason='. 
110                     urlencode("No file exists"));
111             } 
112             $id = $img->id;
113             
114         }
115         
116         
117        
118         $img = DB_DataObjecT::factory('Images');
119         if (!$id || !$img->get($id)) {
120             print_r($id);exit;
121             header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
122                 urlencode("image has been removed or deleted."));
123         }
124         $this->serve($img);
125         exit;
126     }
127     var $thumb = false;
128     var $asmimetype = false;
129     var $method = 'inline';
130     function serve($img)
131     {
132         require_once 'File/Convert.php';
133         if (!file_exists($img->getStoreName())) {
134             print_r($img);exit;
135             header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
136                 urlencode("Original file was missing : " . $img->getStoreName()));
137     
138         }
139         
140         $x = new File_Convert($img->getStoreName(), $img->mimetype);
141         if (empty($this->as_mimetype)) {
142             $this->as_mimetype  = $img->mimetype;
143         }
144         if (!$this->thumb) {
145             $x->convert( $this->as_mimetype);
146             $x->serve($this->method);
147             exit;
148         }
149         
150         $this->validateSize();
151         $x->convert( $this->as_mimetype, $this->size);
152         $x->serve();
153         exit;
154         
155         
156         
157         
158     }
159     function validateSize()
160     {
161         if (!in_array($this->size, array(
162                 '100', 
163                 '100x100', 
164                 '150', 
165                 '150x150', 
166                 '200', 
167                 '200x0',
168                 '200x200',  
169                 '400x0'
170             ))) {
171             die("invalid scale - ".$this->size);
172         }
173     }
174 }