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  * 
22  */
23 require_once  'Pman.php';
24 class Pman_Images extends Pman
25 {
26     function getAuth()
27     {
28         parent::getAuth(); // load company!
29         //return true;
30         $au = $this->getAuthUser();
31         if (!$au) {
32             die("Access denied");
33         }
34         $this->authUser = $au;
35         
36         return true;
37     }
38     
39     function get($s) // determin what to serve!!!!
40     {
41         $this->as_mimetype = empty($_REQUEST['as']) ? '' : $_REQUEST['as'];
42         
43         $bits= explode('/', $s);
44         $id = 0;
45         // without id as first part...
46         if (!empty($bits[0]) && $bits[0] == 'Thumb') {
47             $this->thumb = true;
48             $this->as_mimetype = 'image/jpeg';
49             $this->size = empty($bits[1]) ? '0x0' : $bits[1];
50             $id = empty($bits[2]) ? 0 :   $bits[2];
51             
52         } else if (!empty($bits[0]) && $bits[0] == 'Download') {
53             $this->method = 'attachment';
54             $id = empty($bits[1]) ? 0 :   $bits[1];
55             
56         } else  if (!empty($bits[1]) && $bits[1] == 'Thumb') { // with id as first part.
57             $this->thumb = true;
58             $this->as_mimetype = 'image/jpeg';
59             $this->size = empty($bits[2]) ? '0x0' : $bits[2];
60             $id = empty($bits[3]) ? 0 :   $bits[3];
61             
62         } else {
63             $id = empty($bits[0]) ? 0 :  $bits[0];
64         }
65         
66         if (strpos($id,':') > 0) {  // id format  tablename:id:-imgtype
67             $onbits = explode(':', $id);
68             if ((count($onbits) < 2)   || empty($onbits[1]) || !is_numeric($onbits[1]) || !strlen($onbits[0])) {
69                 die("Bad url");
70             }
71             $img = DB_DataObjecT::factory('Images');
72             $img->ontable = $onbits[0];
73             $img->onid = $onbits[1];
74             if (isset($onbits[2])) {
75                 $img->imgtype = $onbits[2];
76             }
77             $img->limit(1);
78             if (!$img->find(true)) {
79                 die("no image");
80             }
81             
82             $id = $img->id;
83             
84             
85         }
86         $id = (int) $id;
87         
88         // depreciated - should use ontable:onid:type here...
89         if (!empty($_REQUEST['ontable'])) {
90
91             //DB_DataObjecT::debugLevel(1);
92             $img = DB_DataObjecT::factory('Images');
93             $img->setFrom($_REQUEST);
94             // use imgtype now...
95            // if (!empty($_REQUEST['query']['filename'])){
96            //     $img->whereAdd("filename LIKE '". $img->escape($_REQUEST['query']['filename']).".%'");
97            // }
98             
99             
100             $img->limit(1);
101             if (!$img->find(true)) {
102                 die("No file exists");
103             } 
104             $id = $img->id;
105             
106         }
107         
108         
109        
110         $img = DB_DataObjecT::factory('Images');
111         if (!$id || !$img->get($id)) {
112             die("image has been removed or deleted.");
113         }
114         $this->serve($img);
115         exit;
116     }
117     var $thumb = false;
118     var $asmimetype = false;
119     var $method = 'inline';
120     function serve($img)
121     {
122         require_once 'File/Convert.php';
123         $x = new File_Convert($img->getStoreName(), $img->mimetype);
124         if (empty($this->as_mimetype)) {
125             $this->as_mimetype  = $img->mimetype;
126         }
127         if (!$this->thumb) {
128             $x->convert( $this->as_mimetype);
129             $x->serve($this->method);
130             exit;
131         }
132         
133         $this->validateSize();
134         $x->convert( $this->as_mimetype, $this->size);
135         $x->serve();
136         exit;
137         
138         
139         
140         
141     }
142     function validateSize()
143     {
144         if (!in_array($this->size, array(
145                 '100', 
146                 '150', 
147                 '150x150', 
148                 '200', 
149                 '200x0',
150                 '200x200',  
151                 '400x0'
152             ))) {
153             die("invalid scale");
154         }
155     }
156 }