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. ?as=audio/mpeg
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);
63 // without id as first part...
64 if (!empty($bits[0]) && $bits[0] == 'Thumb') {
66 $this->as_mimetype = 'image/jpeg';
67 $this->size = empty($bits[1]) ? '0x0' : $bits[1];
68 $id = empty($bits[2]) ? 0 : $bits[2];
70 } else if (!empty($bits[0]) && $bits[0] == 'Download') {
71 $this->method = 'attachment';
72 $id = empty($bits[1]) ? 0 : $bits[1];
74 } else if (!empty($bits[1]) && $bits[1] == 'Thumb') { // with id as first part.
76 $this->as_mimetype = 'image/jpeg';
77 $this->size = empty($bits[2]) ? '0x0' : $bits[2];
78 $id = empty($bits[3]) ? 0 : $bits[3];
80 } else if (!empty($bits[0]) && $bits[0] == 'events') {
81 $popts = PEAR::getStaticProperty('Pman','options');
83 header ('Content-Type: image/jpeg');
84 if(!empty($bits[2]) && $bits[2] == 'download'){
85 $file = "{$popts['event_log_dir']}/{$bits[1]}";
86 header("Content-Disposition: attachment; filename=\"".basename($file)."\";" );
91 $file = "{$popts['event_log_dir']}/{$bits[1]}.jpg";
92 $fh = fopen($file,'r');
93 echo fread($fh,filesize($file));
98 $id = empty($bits[0]) ? 0 : $bits[0];
101 if (strpos($id,':') > 0) { // id format tablename:id:-imgtype
102 $onbits = explode(':', $id);
103 if ((count($onbits) < 2) || empty($onbits[1]) || !is_numeric($onbits[1]) || !strlen($onbits[0])) {
106 //DB_DataObject::debugLevel(1);
107 $img = DB_DataObject::factory('Images');
108 $img->ontable = $onbits[0];
109 $img->onid = $onbits[1];
110 if (empty($_REQUEST['anytype'])) {
111 $img->whereAdd("mimetype like 'image/%'");
114 if (isset($onbits[2])) {
115 $img->imgtype = $onbits[2];
118 if (!$img->find(true)) {
119 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
120 urlencode("no images for that item: " . htmlspecialchars($id)));
129 // depreciated - should use ontable:onid:type here...
130 if (!empty($_REQUEST['ontable'])) {
132 //DB_DataObjecT::debugLevel(1);
133 $img = DB_DataObjecT::factory('Images');
134 $img->setFrom($_REQUEST);
135 // use imgtype now...
136 // if (!empty($_REQUEST['query']['filename'])){
137 // $img->whereAdd("filename LIKE '". $img->escape($_REQUEST['query']['filename']).".%'");
142 if (!$img->find(true)) {
143 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason='.
144 urlencode("No file exists"));
152 $img = DB_DataObjecT::factory('Images');
153 if (!$id || !$img->get($id)) {
155 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
156 urlencode("image has been removed or deleted."));
166 if (!$this->authUser) {
167 $this->jerr("image conversion only allowed by registered users");
169 // converts a posted string (eg.svg)
170 // into another type..
171 if (empty($_REQUEST['as'])) {
172 $this->jerr("missing target type");
174 if (empty($_REQUEST['mimetype'])) {
175 $this->jerr("missing mimetype");
177 if (empty($_REQUEST['data'])) {
178 $this->jerr("missing data");
182 $this->as_mimetype = $_REQUEST['as'];
183 $this->mimetype = $_REQUEST['mimetype'];
184 require_once 'File/MimeType.php';
185 $y = new File_MimeType();
186 $src_ext = $y->toExt( $this->mimetype );
189 $tmp = $this->tempName($src_ext);
190 file_put_contents($tmp, $_REQUEST['data']);
192 require_once 'File/Convert.php';
193 $cv = new File_Convert($tmp, $this->mimetype);
197 empty($_REQUEST['width']) ? 0 : $_REQUEST['width'],
198 empty($_REQUEST['height']) ? 0 : $_REQUEST['height']
200 if (!empty($_REQUEST['as_data'])) {
201 $this->jok(base64_encode(file_get_contents($fn)));
204 $cv->serve('attachment');
215 require_once 'File/Convert.php';
216 if (!file_exists($img->getStoreName())) {
217 //print_r($img);exit;
218 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
219 urlencode("Original file was missing : " . $img->getStoreName()));
223 $x = $img->toFileConvert();
224 if (empty($this->as_mimetype)) {
225 $this->as_mimetype = $img->mimetype;
228 $x->convert( $this->as_mimetype);
229 $x->serve($this->method);
232 //echo "SKALING? $this->size";
233 // acutally if we generated the image, then we do not need to validate the size..
237 // if the mimetype is not converted..
238 // then the filename should be original.{size}.jpeg
239 $fn = $img->getStoreName() . '.'. $this->size . '.jpeg'; // thumbs are currenly all jpeg.!???
241 if (!file_exists($fn)) {
242 $fn = $img->getStoreName() . '.'. $this->size . '.'. $img->fileExt();
243 // if it's an image, convert into the same type for thumbnail..
244 if (preg_match('#^image/#', $img->mimetype)) {
245 $this->as_mimetype = $img->mimetype;
249 if (!file_exists($fn)) {
251 $this->validateSize();
254 $x->convert( $this->as_mimetype, $this->size);
262 function validateSize()
265 // DEFAULT allowed - override with $cfg['sizes'];
276 '300x100', // logo on login.
280 // this should be configurable...
281 $ff = HTML_FlexyFramework::get();
282 $cfg = isset($ff->Pman_Images) ? $ff->Pman_Images :
283 (isset($ff->Pman_Core_Images) ? $ff->Pman_Core_Images : array());
287 if (!empty($cfg['sizes'])) {
288 $sizes = array_merge($sizes , $cfg['sizes']);
292 if (!in_array($this->size, $sizes)) {
293 die("invalid scale - ".$this->size);
299 * The idea of this code was to replace urls for images when you have an admin
300 * and a distribution page. with different urls.
302 * it may be usefull later if things like embedded images in emails. but
303 * I think it's proably better not to use this.
305 * The key problem being how to determine if we are replacing 'our' images or some external one..
311 static function replaceImageURLS($html)
314 $ff = HTML_FlexyFramework::get();
315 if (!isset($ff->Pman_Images['public_baseURL'])) {
318 //var_dump($ff->Pman_Images['public_baseURL']);
319 $baseURL = $ff->Pman_Images['public_baseURL'];
321 preg_match_all('/<img\s+[^>]+>/i',$html, $result);
323 $matches = array_unique($result[0]);
324 foreach($matches as $img) {
326 preg_match_all('/(width|height|src)="([^"]*)"/i',$img, $imatch);
330 foreach($imatch[1] as $i=>$key) {
331 $attr[$key] = $imatch[2][$i];
333 if (!isset($attr['src']) || 0 !== strpos($attr['src'], $baseURL)) {
336 $html = self::replaceImgUrl($html, $baseURL, $img, $attr, 'src' );
340 preg_match_all('/<a\s+[^>]+>/i',$html, $result);
342 $matches = array_unique($result[0]);
343 foreach($matches as $img) {
345 preg_match_all('/(href)="([^"]*)"/i',$img, $imatch);
349 foreach($imatch[1] as $i=>$key) {
350 $attr[$key] = $imatch[2][$i];
352 if (!isset($attr['href']) || 0 !== strpos($attr['href'], $baseURL)) {
355 $html = self::replaceImgUrl($html, $baseURL, $img, $attr, 'href' );
360 static function replaceImgUrl($html, $baseURL, $tag, $attr, $attr_name)
364 // see if it's an image url..
365 // Images/{ID}/fullname.xxxx
366 // Images/Thumb/200/{ID}/fullname.xxxx
367 // Images/Download/{ID}/fullname.xxxx
369 $attr_url = $attr[$attr_name];
371 if(!preg_match('#/(Images|Images/Thumb/[a-z0-9]+|Images/Download)/([0-9]+)/(.*)$#', $attr_url, $umatch)) {
375 $img = DB_DataObject::factory('Images');
376 if (!$img->get($id)) {
379 $type = explode('/', $umatch[1]);
382 if (count($type) > 2 && $type[1] == 'Thumb') {
383 $thumbsize = $type[2];
384 $provider = '/Images/Thumb';
386 $provider = '/'.$umatch[1];
389 if (!empty($attr['width']) || !empty($attr['height']) )
391 // no support for %...
393 (empty($attr['width']) ? '0' : $attr['width'] * 1) .
395 (empty($attr['height']) ? '0' : $attr['height'] * 1);
396 $provider = '/Images/Thumb';
400 if ($thumbsize !== -1) {
402 // need to regenerate it..
404 $type = array('Images', 'Thumb', $thumbsize);
406 $fc = $img->toFileConvert();
407 // make sure it's available..
408 $fc->convert($img->mimetype, $thumbsize);
412 $provider = $provider == 'Images/Thumb' ? 'Images' : $provider;
416 // finally replace the original TAG with the new version..
418 $new_tag = str_replace(
419 $attr_name. '="'. $attr_url . '"',
420 $attr_name .'="'. htmlspecialchars($img->URL($thumbsize, $provider, $baseURL)) . '"',
425 return str_replace($tag, $new_tag, $html);