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