Pman/Images.php
[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                 die("no images for that item: " . htmlspecialchars($id));
86             }
87             
88             $id = $img->id;
89             
90             
91         }
92         $id = (int) $id;
93         
94         // depreciated - should use ontable:onid:type here...
95         if (!empty($_REQUEST['ontable'])) {
96
97             //DB_DataObjecT::debugLevel(1);
98             $img = DB_DataObjecT::factory('Images');
99             $img->setFrom($_REQUEST);
100             // use imgtype now...
101            // if (!empty($_REQUEST['query']['filename'])){
102            //     $img->whereAdd("filename LIKE '". $img->escape($_REQUEST['query']['filename']).".%'");
103            // }
104             
105             
106             $img->limit(1);
107             if (!$img->find(true)) {
108                 die("No file exists");
109             } 
110             $id = $img->id;
111             
112         }
113         
114         
115        
116         $img = DB_DataObjecT::factory('Images');
117         if (!$id || !$img->get($id)) {
118             die("image has been removed or deleted.");
119         }
120         $this->serve($img);
121         exit;
122     }
123     var $thumb = false;
124     var $asmimetype = false;
125     var $method = 'inline';
126     function serve($img)
127     {
128         require_once 'File/Convert.php';
129         if (!file_exists($img->getStoreName())) {
130             die("Original file was missing : " . $img->getStoreName());
131     
132         }
133         
134         $x = new File_Convert($img->getStoreName(), $img->mimetype);
135         if (empty($this->as_mimetype)) {
136             $this->as_mimetype  = $img->mimetype;
137         }
138         if (!$this->thumb) {
139             $x->convert( $this->as_mimetype);
140             $x->serve($this->method);
141             exit;
142         }
143         
144         $this->validateSize();
145         $x->convert( $this->as_mimetype, $this->size);
146         $x->serve();
147         exit;
148         
149         
150         
151         
152     }
153     function validateSize()
154     {
155         if (!in_array($this->size, array(
156                 '100', 
157                 '100x100', 
158                 '150', 
159                 '150x150', 
160                 '200', 
161                 '200x0',
162                 '200x200',  
163                 '400x0'
164             ))) {
165             die("invalid scale - ".$this->size);
166         }
167     }
168 }