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 // var_dump($bits);die('in');
64 // without id as first part...
65 if (!empty($bits[0]) && $bits[0] == 'Thumb') {
67 $this->as_mimetype = 'image/jpeg';
68 $this->size = empty($bits[1]) ? '0x0' : $bits[1];
69 $id = empty($bits[2]) ? 0 : $bits[2];
71 } else if (!empty($bits[0]) && $bits[0] == 'Download') {
72 $this->method = 'attachment';
73 $id = empty($bits[1]) ? 0 : $bits[1];
75 } else if (!empty($bits[1]) && $bits[1] == 'Thumb') { // with id as first part.
77 $this->as_mimetype = 'image/jpeg';
78 $this->size = empty($bits[2]) ? '0x0' : $bits[2];
79 $id = empty($bits[3]) ? 0 : $bits[3];
81 } else if (!empty($bits[0]) && $bits[0] == 'events') {
83 $popts = PEAR::getStaticProperty('Pman','options');
84 $ev = DB_DAtaObject::Factory('events');
85 if (!$ev->get($bits[1])) {
86 die("could not find event id");
88 // technically same user only.. -- normally www-data..
89 if (function_exists('posix_getpwuid')) {
90 $uinfo = posix_getpwuid( posix_getuid () );
91 $user = $uinfo['name'];
93 $user = getenv('USERNAME'); // windows.
95 $ff = HTML_FlexyFramework::get();
96 $file = $ff->Pman['event_log_dir']. '/'. $user. date('/Y/m/d/',strtotime($ev->event_when)). $ev->id . ".json";
97 $filesJ = json_decode(file_get_contents($file));
101 foreach($filesJ->FILES as $k=>$f){
102 if ($f->tmp_name != $bits[2]) {
106 $src = $ff->Pman['event_log_dir']. '/'. $user. date('/Y/m/d/', strtotime($ev->event_when)). $f->tmp_name ;
107 if (!file_exists($src)) {
108 die("file was not saved");
110 header ('Content-Type: ' . $f->type);
112 header("Content-Disposition: attachment; filename=\"".basename($f->name)."\";" );
118 die ("unknown file?");
121 $id = empty($bits[0]) ? 0 : $bits[0];
124 if (strpos($id,':') > 0) { // id format tablename:id:-imgtype
125 $onbits = explode(':', $id);
126 if ((count($onbits) < 2) || empty($onbits[1]) || !is_numeric($onbits[1]) || !strlen($onbits[0])) {
129 //DB_DataObject::debugLevel(1);
130 $img = DB_DataObject::factory('Images');
131 $img->ontable = $onbits[0];
132 $img->onid = $onbits[1];
133 if (empty($_REQUEST['anytype'])) {
134 $img->whereAdd("mimetype like 'image/%'");
137 if (isset($onbits[2])) {
138 $img->imgtype = $onbits[2];
141 if (!$img->find(true)) {
142 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
143 urlencode("no images for that item: " . htmlspecialchars($id)));
152 // depreciated - should use ontable:onid:type here...
153 if (!empty($_REQUEST['ontable'])) {
155 //DB_DataObjecT::debugLevel(1);
156 $img = DB_DataObjecT::factory('Images');
157 $img->setFrom($_REQUEST);
158 // use imgtype now...
159 // if (!empty($_REQUEST['query']['filename'])){
160 // $img->whereAdd("filename LIKE '". $img->escape($_REQUEST['query']['filename']).".%'");
165 if (!$img->find(true)) {
166 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason='.
167 urlencode("No file exists"));
175 $img = DB_DataObjecT::factory('Images');
177 if (!$id || !$img->get($id)) {
179 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
180 urlencode("image has been removed or deleted."));
191 if (!$this->authUser) {
192 $this->jerr("image conversion only allowed by registered users");
194 // converts a posted string (eg.svg)
195 // into another type..
196 if (empty($_REQUEST['as'])) {
197 $this->jerr("missing target type");
199 if (empty($_REQUEST['mimetype'])) {
200 $this->jerr("missing mimetype");
202 if (empty($_REQUEST['data'])) {
203 $this->jerr("missing data");
207 $this->as_mimetype = $_REQUEST['as'];
208 $this->mimetype = $_REQUEST['mimetype'];
209 require_once 'File/MimeType.php';
210 $y = new File_MimeType();
211 $src_ext = $y->toExt( $this->mimetype );
214 $tmp = $this->tempName($src_ext);
215 file_put_contents($tmp, $_REQUEST['data']);
217 require_once 'File/Convert.php';
218 $cv = new File_Convert($tmp, $this->mimetype);
222 empty($_REQUEST['width']) ? 0 : $_REQUEST['width'],
223 empty($_REQUEST['height']) ? 0 : $_REQUEST['height']
225 if (!empty($_REQUEST['as_data'])) {
226 $this->jok(base64_encode(file_get_contents($fn)));
229 $cv->serve('attachment');
240 $this->sessionState(0); // turn off session... - locking...
242 require_once 'File/Convert.php';
243 if (!file_exists($img->getStoreName())) {
244 // print_r($img);exit;
245 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
246 urlencode("Original file was missing : " . $img->getStoreName()));
249 // print_r($img);exit;
250 $x = $img->toFileConvert();
251 if (empty($this->as_mimetype)) {
252 $this->as_mimetype = $img->mimetype;
255 $x->convert( $this->as_mimetype);
256 $x->serve($this->method);
259 //echo "SKALING? $this->size";
260 // acutally if we generated the image, then we do not need to validate the size..
264 // if the mimetype is not converted..
265 // then the filename should be original.{size}.jpeg
266 $fn = $img->getStoreName() . '.'. $this->size . '.jpeg'; // thumbs are currenly all jpeg.!???
268 if (!file_exists($fn)) {
269 $fn = $img->getStoreName() . '.'. $this->size . '.'. $img->fileExt();
270 // if it's an image, convert into the same type for thumbnail..
271 if (preg_match('#^image/#', $img->mimetype)) {
272 $this->as_mimetype = $img->mimetype;
276 if (!file_exists($fn)) {
278 $this->validateSize();
281 $x->convert( $this->as_mimetype, $this->size);
289 function validateSize()
292 if ($this->authUser && $this->authUser->company_id && $this->authUser->company()->comptype=='OWNER') {
296 // DEFAULT allowed - override with $cfg['sizes'];
307 '300x100', // logo on login.
311 // this should be configurable...
312 $ff = HTML_FlexyFramework::get();
313 $cfg = isset($ff->Pman_Images) ? $ff->Pman_Images :
314 (isset($ff->Pman_Core_Images) ? $ff->Pman_Core_Images : array());
318 if (!empty($cfg['sizes'])) {
319 $sizes = array_merge($sizes , $cfg['sizes']);
323 if (!in_array($this->size, $sizes)) {
324 die("invalid scale - ".$this->size);
330 * The idea of this code was to replace urls for images when you have an admin
331 * and a distribution page. with different urls.
333 * it may be usefull later if things like embedded images in emails. but
334 * I think it's proably better not to use this.
336 * The key problem being how to determine if we are replacing 'our' images or some external one..
342 static function replaceImageURLS($html)
345 $ff = HTML_FlexyFramework::get();
346 if (!isset($ff->Pman_Images['public_baseURL'])) {
349 //var_dump($ff->Pman_Images['public_baseURL']);
350 $baseURL = $ff->Pman_Images['public_baseURL'];
352 preg_match_all('/<img\s+[^>]+>/i',$html, $result);
354 $matches = array_unique($result[0]);
355 foreach($matches as $img) {
357 preg_match_all('/(width|height|src)="([^"]*)"/i',$img, $imatch);
361 foreach($imatch[1] as $i=>$key) {
362 $attr[$key] = $imatch[2][$i];
364 if (!isset($attr['src']) || 0 !== strpos($attr['src'], $baseURL)) {
367 $html = self::replaceImgUrl($html, $baseURL, $img, $attr, 'src' );
371 preg_match_all('/<a\s+[^>]+>/i',$html, $result);
373 $matches = array_unique($result[0]);
374 foreach($matches as $img) {
376 preg_match_all('/(href)="([^"]*)"/i',$img, $imatch);
380 foreach($imatch[1] as $i=>$key) {
381 $attr[$key] = $imatch[2][$i];
383 if (!isset($attr['href']) || 0 !== strpos($attr['href'], $baseURL)) {
386 $html = self::replaceImgUrl($html, $baseURL, $img, $attr, 'href' );
391 static function replaceImgUrl($html, $baseURL, $tag, $attr, $attr_name)
395 // see if it's an image url..
396 // Images/{ID}/fullname.xxxx
397 // Images/Thumb/200/{ID}/fullname.xxxx
398 // Images/Download/{ID}/fullname.xxxx
400 $attr_url = $attr[$attr_name];
402 if(!preg_match('#/(Images|Images/Thumb/[a-z0-9]+|Images/Download)/([0-9]+)/(.*)$#', $attr_url, $umatch)) {
406 $img = DB_DataObject::factory('Images');
407 if (!$img->get($id)) {
410 $type = explode('/', $umatch[1]);
413 if (count($type) > 2 && $type[1] == 'Thumb') {
414 $thumbsize = $type[2];
415 $provider = '/Images/Thumb';
417 $provider = '/'.$umatch[1];
420 if (!empty($attr['width']) || !empty($attr['height']) )
422 // no support for %...
424 (empty($attr['width']) ? '0' : $attr['width'] * 1) .
426 (empty($attr['height']) ? '0' : $attr['height'] * 1);
427 $provider = '/Images/Thumb';
431 if ($thumbsize !== -1) {
433 // need to regenerate it..
435 $type = array('Images', 'Thumb', $thumbsize);
437 $fc = $img->toFileConvert();
438 // make sure it's available..
439 $fc->convert($img->mimetype, $thumbsize);
443 $provider = $provider == 'Images/Thumb' ? 'Images' : $provider;
447 // finally replace the original TAG with the new version..
449 $new_tag = str_replace(
450 $attr_name. '="'. $attr_url . '"',
451 $attr_name .'="'. htmlspecialchars($img->URL($thumbsize, $provider, $baseURL)) . '"',
456 return str_replace($tag, $new_tag, $html);