3 * Deal with image delivery and HTML replacement of image links in body text.
5 * $str = Pman_Core_Images::replaceImg($str); // < use with HTML
9 * Deliver image /file etc..
13 * args: ontable request
14 * ontable (req) tablename.
17 * as (serve as a type) = eg. mimetype.
20 * as :(serve as a type) = eg. mimetype.
22 * Images/{ID}/fullname.xxxx
24 * (valid thumbs 200, 400)...?
25 * Images/Thumb/200/{ID}/fullname.xxxx
26 * Images/Download/{ID}/fullname.xxxx
31 * Used to be in Base... now in core..
34 * view permission should be required on the underlying object...
37 require_once 'Pman.php';
38 class Pman_Core_Images extends Pman
42 parent::getAuth(); // load company!
44 $au = $this->getAuthUser();
46 // die("Access denied");
48 $this->authUser = $au;
53 var $as_mimetype = false;
54 var $method = 'inline';
56 function get($s) // determin what to serve!!!!
58 $this->as_mimetype = empty($_REQUEST['as']) ? '' : $_REQUEST['as'];
60 $bits= explode('/', $s);
62 // without id as first part...
63 if (!empty($bits[0]) && $bits[0] == 'Thumb') {
65 $this->as_mimetype = 'image/jpeg';
66 $this->size = empty($bits[1]) ? '0x0' : $bits[1];
67 $id = empty($bits[2]) ? 0 : $bits[2];
69 } else if (!empty($bits[0]) && $bits[0] == 'Download') {
70 $this->method = 'attachment';
71 $id = empty($bits[1]) ? 0 : $bits[1];
73 } else if (!empty($bits[1]) && $bits[1] == 'Thumb') { // with id as first part.
75 $this->as_mimetype = 'image/jpeg';
76 $this->size = empty($bits[2]) ? '0x0' : $bits[2];
77 $id = empty($bits[3]) ? 0 : $bits[3];
80 $id = empty($bits[0]) ? 0 : $bits[0];
83 if (strpos($id,':') > 0) { // id format tablename:id:-imgtype
84 $onbits = explode(':', $id);
85 if ((count($onbits) < 2) || empty($onbits[1]) || !is_numeric($onbits[1]) || !strlen($onbits[0])) {
88 //DB_DataObject::debugLevel(1);
89 $img = DB_DataObject::factory('Images');
90 $img->ontable = $onbits[0];
91 $img->onid = $onbits[1];
92 if (empty($_REQUEST['anytype'])) {
93 $img->whereAdd("mimetype like 'image/%'");
96 if (isset($onbits[2])) {
97 $img->imgtype = $onbits[2];
100 if (!$img->find(true)) {
101 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
102 urlencode("no images for that item: " . htmlspecialchars($id)));
111 // depreciated - should use ontable:onid:type here...
112 if (!empty($_REQUEST['ontable'])) {
114 //DB_DataObjecT::debugLevel(1);
115 $img = DB_DataObjecT::factory('Images');
116 $img->setFrom($_REQUEST);
117 // use imgtype now...
118 // if (!empty($_REQUEST['query']['filename'])){
119 // $img->whereAdd("filename LIKE '". $img->escape($_REQUEST['query']['filename']).".%'");
124 if (!$img->find(true)) {
125 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason='.
126 urlencode("No file exists"));
134 $img = DB_DataObjecT::factory('Images');
135 if (!$id || !$img->get($id)) {
137 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
138 urlencode("image has been removed or deleted."));
146 require_once 'File/Convert.php';
147 if (!file_exists($img->getStoreName())) {
148 //print_r($img);exit;
149 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
150 urlencode("Original file was missing : " . $img->getStoreName()));
154 $x = $img->toFileConvert();
155 if (empty($this->as_mimetype)) {
156 $this->as_mimetype = $img->mimetype;
159 $x->convert( $this->as_mimetype);
160 $x->serve($this->method);
163 //echo "SKALING? $this->size";
164 // acutally if we generated the image, then we do not need to validate the size..
165 $this->validateSize();
167 $x->convert( $this->as_mimetype, $this->size);
175 function validateSize()
178 // DEFAULT allowed - override with $cfg['sizes'];
189 '300x100', // logo on login.
193 // this should be configurable...
194 $ff = HTML_FlexyFramework::get();
195 $cfg = isset($ff->Pman_Images) ? $ff->Pman_Images :
196 (isset($ff->Pman_Core_Images) ? $ff->Pman_Core_Images : array());
200 if (!empty($cfg['sizes'])) {
201 $sizes = array_merge($sizes , $cfg['sizes']);
205 if (!in_array($this->size, $sizes)) {
206 die("invalid scale - ".$this->size);
212 * The idea of this code was to replace urls for images when you have an admin
213 * and a distribution page. with different urls.
215 * it may be usefull later if things like embedded images in emails. but
216 * I think it's proably better not to use this.
218 * The key problem being how to determine if we are replacing 'our' images or some external one..
224 static function replaceImageURLS($html)
227 $ff = HTML_FlexyFramework::get();
228 if (!isset($ff->Pman_Images['public_baseURL'])) {
231 //var_dump($ff->Pman_Images['public_baseURL']);
232 $baseURL = $ff->Pman_Images['public_baseURL'];
234 preg_match_all('/<img\s+[^>]+>/i',$html, $result);
236 $matches = array_unique($result[0]);
237 foreach($matches as $img) {
239 preg_match_all('/(width|height|src)="([^"]*)"/i',$img, $imatch);
243 foreach($imatch[1] as $i=>$key) {
244 $attr[$key] = $imatch[2][$i];
246 if (!isset($attr['src']) || 0 !== strpos($attr['src'], $baseURL)) {
249 $html = self::replaceImgUrl($html, $baseURL, $img, $attr, 'src' );
253 preg_match_all('/<a\s+[^>]+>/i',$html, $result);
255 $matches = array_unique($result[0]);
256 foreach($matches as $img) {
258 preg_match_all('/(href)="([^"]*)"/i',$img, $imatch);
262 foreach($imatch[1] as $i=>$key) {
263 $attr[$key] = $imatch[2][$i];
265 if (!isset($attr['href']) || 0 !== strpos($attr['href'], $baseURL)) {
268 $html = self::replaceImgUrl($html, $baseURL, $img, $attr, 'href' );
273 static function replaceImgUrl($html, $baseURL, $tag, $attr, $attr_name)
277 // see if it's an image url..
278 // Images/{ID}/fullname.xxxx
279 // Images/Thumb/200/{ID}/fullname.xxxx
280 // Images/Download/{ID}/fullname.xxxx
282 $attr_url = $attr[$attr_name];
284 if(!preg_match('#/(Images|Images/Thumb/[a-z0-9]+|Images/Download)/([0-9]+)/(.*)$#', $attr_url, $umatch)) {
288 $img = DB_DataObject::factory('Images');
289 if (!$img->get($id)) {
292 $type = explode('/', $umatch[1]);
295 if (count($type) > 2 && $type[1] == 'Thumb') {
296 $thumbsize = $type[2];
297 $provider = '/Images/Thumb';
299 $provider = '/'.$umatch[1];
302 if (!empty($attr['width']) || !empty($attr['height']) )
304 // no support for %...
306 (empty($attr['width']) ? '0' : $attr['width'] * 1) .
308 (empty($attr['height']) ? '0' : $attr['height'] * 1);
309 $provider = '/Images/Thumb';
313 if ($thumbsize !== -1) {
315 // need to regenerate it..
317 $type = array('Images', 'Thumb', $thumbsize);
319 $fc = $img->toFileConvert();
320 // make sure it's available..
321 $fc->convert($img->mimetype, $thumbsize);
325 $provider = $provider == 'Images/Thumb' ? 'Images' : $provider;
329 // finally replace the original TAG with the new version..
331 $new_tag = str_replace(
332 $attr_name. '="'. $attr_url . '"',
333 $attr_name .'="'. htmlspecialchars($img->URL($thumbsize, $provider, $baseURL)) . '"',
338 return str_replace($tag, $new_tag, $html);