c4ea1b3f1232313b6973ddbf6ee30dff847412af
[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     var $thumb = false;
40     var $as_mimetype = false;
41     var $method = 'inline';
42     
43     function get($s) // determin what to serve!!!!
44     {
45         $this->as_mimetype = empty($_REQUEST['as']) ? '' : $_REQUEST['as'];
46         
47         $bits= explode('/', $s);
48         $id = 0;
49         // without id as first part...
50         if (!empty($bits[0]) && $bits[0] == 'Thumb') {
51             $this->thumb = true;
52             $this->as_mimetype = 'image/jpeg';
53             $this->size = empty($bits[1]) ? '0x0' : $bits[1];
54             $id = empty($bits[2]) ? 0 :   $bits[2];
55             
56         } else if (!empty($bits[0]) && $bits[0] == 'Download') {
57             $this->method = 'attachment';
58             $id = empty($bits[1]) ? 0 :   $bits[1];
59             
60         } else  if (!empty($bits[1]) && $bits[1] == 'Thumb') { // with id as first part.
61             $this->thumb = true;
62             $this->as_mimetype = 'image/jpeg';
63             $this->size = empty($bits[2]) ? '0x0' : $bits[2];
64             $id = empty($bits[3]) ? 0 :   $bits[3];
65             
66         } else {
67             $id = empty($bits[0]) ? 0 :  $bits[0];
68         }
69         
70         if (strpos($id,':') > 0) {  // id format  tablename:id:-imgtype
71             $onbits = explode(':', $id);
72             if ((count($onbits) < 2)   || empty($onbits[1]) || !is_numeric($onbits[1]) || !strlen($onbits[0])) {
73                 die("Bad url");
74             }
75             //DB_DataObject::debugLevel(1);
76             $img = DB_DataObjecT::factory('Images');
77             $img->ontable = $onbits[0];
78             $img->onid = $onbits[1];
79             if (empty($_REQUEST['anytype'])) {
80                 $img->whereAdd("mimetype like 'image/%'");
81             }
82             
83             if (isset($onbits[2])) {
84                 $img->imgtype = $onbits[2];
85             }
86             $img->limit(1);
87             if (!$img->find(true)) {
88                 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
89                 urlencode("no images for that item: " . htmlspecialchars($id)));
90             }
91             
92             $id = $img->id;
93             
94             
95         }
96         $id = (int) $id;
97         
98         // depreciated - should use ontable:onid:type here...
99         if (!empty($_REQUEST['ontable'])) {
100
101             //DB_DataObjecT::debugLevel(1);
102             $img = DB_DataObjecT::factory('Images');
103             $img->setFrom($_REQUEST);
104             // use imgtype now...
105            // if (!empty($_REQUEST['query']['filename'])){
106            //     $img->whereAdd("filename LIKE '". $img->escape($_REQUEST['query']['filename']).".%'");
107            // }
108             
109             
110             $img->limit(1);
111             if (!$img->find(true)) {
112                 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason='. 
113                     urlencode("No file exists"));
114             } 
115             $id = $img->id;
116             
117         }
118         
119         
120        
121         $img = DB_DataObjecT::factory('Images');
122         if (!$id || !$img->get($id)) {
123              
124             header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
125                 urlencode("image has been removed or deleted."));
126         }
127         $this->serve($img);
128         exit;
129     }
130  
131     function serve($img)
132     {
133         require_once 'File/Convert.php';
134         if (!file_exists($img->getStoreName())) {
135             //print_r($img);exit;
136             header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
137                 urlencode("Original file was missing : " . $img->getStoreName()));
138     
139         }
140         
141         $x = new File_Convert($img->getStoreName(), $img->mimetype);
142         if (empty($this->as_mimetype)) {
143             $this->as_mimetype  = $img->mimetype;
144         }
145         if (!$this->thumb) {
146             $x->convert( $this->as_mimetype);
147             $x->serve($this->method);
148             exit;
149         }
150         //echo "SKALING?  $this->size";
151         $this->validateSize();
152         $x->convert( $this->as_mimetype, $this->size);
153         $x->serve();
154         exit;
155         
156         
157         
158         
159     }
160     function validateSize()
161     {
162         $sizes = array(
163                 '100', 
164                 '100x100', 
165                 '150', 
166                 '150x150', 
167                 '200', 
168                 '200x0',
169                 '200x200',  
170                 '400x0',
171                 '300x100', // logo on login.
172                 '500'
173             );
174         
175         // this should be configurable...
176         $ff = HTML_FlexyFramework::get();
177         if (!empty($ff->Pman_Images['sizes'])) {
178             $sizes = array_merge($sizes , $ff->Pman_Images['sizes']);
179         }
180         
181         
182         if (!in_array($this->size, $sizes)) {
183             die("invalid scale - ".$this->size);
184         }
185     }
186 }