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