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');
82 $ev = DB_DAtaObject::Factory('events');
83 if (!$ev->get($bits[1])) {
84 die("could not find event id");
86 // technically same user only.. -- normally www-data..
87 if (function_exists('posix_getpwuid')) {
88 $uinfo = posix_getpwuid( posix_getuid () );
90 $user = $uinfo['name'];
92 $user = getenv('USERNAME'); // windows.
94 $file = $ff->Pman['event_log_dir']. '/'. $user. date('/Y/m/d/'). $this->id . ".json";
95 $filesJ = json_decode(file_get_contents($file));
99 foreach($filesJ->FILES as $k=>$f){
100 if ($f->tmp_name == $bits[2]) {
104 $src = $ff->Pman['event_log_dir']. '/'. $user. date('/Y/m/d/'). $f->tmp_name ;
105 if (!file_exists($src)) {
106 die("file was not saved");
108 header ('Content-Type: ' . $f->mimetype);
110 header("Content-Disposition: attachment; filename=\"".basename($f->filename)."\";" );
116 die ("unknown file?");
119 $id = empty($bits[0]) ? 0 : $bits[0];
122 if (strpos($id,':') > 0) { // id format tablename:id:-imgtype
123 $onbits = explode(':', $id);
124 if ((count($onbits) < 2) || empty($onbits[1]) || !is_numeric($onbits[1]) || !strlen($onbits[0])) {
127 //DB_DataObject::debugLevel(1);
128 $img = DB_DataObject::factory('Images');
129 $img->ontable = $onbits[0];
130 $img->onid = $onbits[1];
131 if (empty($_REQUEST['anytype'])) {
132 $img->whereAdd("mimetype like 'image/%'");
135 if (isset($onbits[2])) {
136 $img->imgtype = $onbits[2];
139 if (!$img->find(true)) {
140 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
141 urlencode("no images for that item: " . htmlspecialchars($id)));
150 // depreciated - should use ontable:onid:type here...
151 if (!empty($_REQUEST['ontable'])) {
153 //DB_DataObjecT::debugLevel(1);
154 $img = DB_DataObjecT::factory('Images');
155 $img->setFrom($_REQUEST);
156 // use imgtype now...
157 // if (!empty($_REQUEST['query']['filename'])){
158 // $img->whereAdd("filename LIKE '". $img->escape($_REQUEST['query']['filename']).".%'");
163 if (!$img->find(true)) {
164 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason='.
165 urlencode("No file exists"));
173 $img = DB_DataObjecT::factory('Images');
174 if (!$id || !$img->get($id)) {
176 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
177 urlencode("image has been removed or deleted."));
187 if (!$this->authUser) {
188 $this->jerr("image conversion only allowed by registered users");
190 // converts a posted string (eg.svg)
191 // into another type..
192 if (empty($_REQUEST['as'])) {
193 $this->jerr("missing target type");
195 if (empty($_REQUEST['mimetype'])) {
196 $this->jerr("missing mimetype");
198 if (empty($_REQUEST['data'])) {
199 $this->jerr("missing data");
203 $this->as_mimetype = $_REQUEST['as'];
204 $this->mimetype = $_REQUEST['mimetype'];
205 require_once 'File/MimeType.php';
206 $y = new File_MimeType();
207 $src_ext = $y->toExt( $this->mimetype );
210 $tmp = $this->tempName($src_ext);
211 file_put_contents($tmp, $_REQUEST['data']);
213 require_once 'File/Convert.php';
214 $cv = new File_Convert($tmp, $this->mimetype);
218 empty($_REQUEST['width']) ? 0 : $_REQUEST['width'],
219 empty($_REQUEST['height']) ? 0 : $_REQUEST['height']
221 if (!empty($_REQUEST['as_data'])) {
222 $this->jok(base64_encode(file_get_contents($fn)));
225 $cv->serve('attachment');
236 require_once 'File/Convert.php';
237 if (!file_exists($img->getStoreName())) {
238 //print_r($img);exit;
239 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
240 urlencode("Original file was missing : " . $img->getStoreName()));
244 $x = $img->toFileConvert();
245 if (empty($this->as_mimetype)) {
246 $this->as_mimetype = $img->mimetype;
249 $x->convert( $this->as_mimetype);
250 $x->serve($this->method);
253 //echo "SKALING? $this->size";
254 // acutally if we generated the image, then we do not need to validate the size..
258 // if the mimetype is not converted..
259 // then the filename should be original.{size}.jpeg
260 $fn = $img->getStoreName() . '.'. $this->size . '.jpeg'; // thumbs are currenly all jpeg.!???
262 if (!file_exists($fn)) {
263 $fn = $img->getStoreName() . '.'. $this->size . '.'. $img->fileExt();
264 // if it's an image, convert into the same type for thumbnail..
265 if (preg_match('#^image/#', $img->mimetype)) {
266 $this->as_mimetype = $img->mimetype;
270 if (!file_exists($fn)) {
272 $this->validateSize();
275 $x->convert( $this->as_mimetype, $this->size);
283 function validateSize()
286 // DEFAULT allowed - override with $cfg['sizes'];
297 '300x100', // logo on login.
301 // this should be configurable...
302 $ff = HTML_FlexyFramework::get();
303 $cfg = isset($ff->Pman_Images) ? $ff->Pman_Images :
304 (isset($ff->Pman_Core_Images) ? $ff->Pman_Core_Images : array());
308 if (!empty($cfg['sizes'])) {
309 $sizes = array_merge($sizes , $cfg['sizes']);
313 if (!in_array($this->size, $sizes)) {
314 die("invalid scale - ".$this->size);
320 * The idea of this code was to replace urls for images when you have an admin
321 * and a distribution page. with different urls.
323 * it may be usefull later if things like embedded images in emails. but
324 * I think it's proably better not to use this.
326 * The key problem being how to determine if we are replacing 'our' images or some external one..
332 static function replaceImageURLS($html)
335 $ff = HTML_FlexyFramework::get();
336 if (!isset($ff->Pman_Images['public_baseURL'])) {
339 //var_dump($ff->Pman_Images['public_baseURL']);
340 $baseURL = $ff->Pman_Images['public_baseURL'];
342 preg_match_all('/<img\s+[^>]+>/i',$html, $result);
344 $matches = array_unique($result[0]);
345 foreach($matches as $img) {
347 preg_match_all('/(width|height|src)="([^"]*)"/i',$img, $imatch);
351 foreach($imatch[1] as $i=>$key) {
352 $attr[$key] = $imatch[2][$i];
354 if (!isset($attr['src']) || 0 !== strpos($attr['src'], $baseURL)) {
357 $html = self::replaceImgUrl($html, $baseURL, $img, $attr, 'src' );
361 preg_match_all('/<a\s+[^>]+>/i',$html, $result);
363 $matches = array_unique($result[0]);
364 foreach($matches as $img) {
366 preg_match_all('/(href)="([^"]*)"/i',$img, $imatch);
370 foreach($imatch[1] as $i=>$key) {
371 $attr[$key] = $imatch[2][$i];
373 if (!isset($attr['href']) || 0 !== strpos($attr['href'], $baseURL)) {
376 $html = self::replaceImgUrl($html, $baseURL, $img, $attr, 'href' );
381 static function replaceImgUrl($html, $baseURL, $tag, $attr, $attr_name)
385 // see if it's an image url..
386 // Images/{ID}/fullname.xxxx
387 // Images/Thumb/200/{ID}/fullname.xxxx
388 // Images/Download/{ID}/fullname.xxxx
390 $attr_url = $attr[$attr_name];
392 if(!preg_match('#/(Images|Images/Thumb/[a-z0-9]+|Images/Download)/([0-9]+)/(.*)$#', $attr_url, $umatch)) {
396 $img = DB_DataObject::factory('Images');
397 if (!$img->get($id)) {
400 $type = explode('/', $umatch[1]);
403 if (count($type) > 2 && $type[1] == 'Thumb') {
404 $thumbsize = $type[2];
405 $provider = '/Images/Thumb';
407 $provider = '/'.$umatch[1];
410 if (!empty($attr['width']) || !empty($attr['height']) )
412 // no support for %...
414 (empty($attr['width']) ? '0' : $attr['width'] * 1) .
416 (empty($attr['height']) ? '0' : $attr['height'] * 1);
417 $provider = '/Images/Thumb';
421 if ($thumbsize !== -1) {
423 // need to regenerate it..
425 $type = array('Images', 'Thumb', $thumbsize);
427 $fc = $img->toFileConvert();
428 // make sure it's available..
429 $fc->convert($img->mimetype, $thumbsize);
433 $provider = $provider == 'Images/Thumb' ? 'Images' : $provider;
437 // finally replace the original TAG with the new version..
439 $new_tag = str_replace(
440 $attr_name. '="'. $attr_url . '"',
441 $attr_name .'="'. htmlspecialchars($img->URL($thumbsize, $provider, $baseURL)) . '"',
446 return str_replace($tag, $new_tag, $html);