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!!!!
59 //if (!empty($_GET['_post'])) {
60 // return $this->post();
63 $this->as_mimetype = empty($_REQUEST['as']) ? '' : $_REQUEST['as'];
65 $bits= explode('/', $s);
67 // var_dump($bits);die('in');
68 // without id as first part...
69 if (!empty($bits[0]) && $bits[0] == 'Thumb') {
71 $this->as_mimetype = 'image/jpeg';
72 $this->size = empty($bits[1]) ? '0x0' : $bits[1];
73 $id = empty($bits[2]) ? 0 : $bits[2];
75 } else if (!empty($bits[0]) && $bits[0] == 'Download') {
76 $this->method = 'attachment';
77 $id = empty($bits[1]) ? 0 : $bits[1];
79 } else if (!empty($bits[1]) && $bits[1] == 'Thumb') { // with id as first part.
81 $this->as_mimetype = 'image/jpeg';
82 $this->size = empty($bits[2]) ? '0x0' : $bits[2];
83 $id = empty($bits[3]) ? 0 : $bits[3];
85 } else if (!empty($bits[0]) && $bits[0] == 'events') {
87 $this->downloadEvent($bits);
89 die ("unknown file?");
93 $id = empty($bits[0]) ? 0 : $bits[0];
96 if (strpos($id,':') > 0) { // id format tablename:id:-imgtype
97 $onbits = explode(':', $id);
98 if ((count($onbits) < 2) || empty($onbits[1]) || !is_numeric($onbits[1]) || !strlen($onbits[0])) {
101 //DB_DataObject::debugLevel(1);
102 $img = DB_DataObject::factory('Images');
103 $img->ontable = $onbits[0];
104 $img->onid = $onbits[1];
105 if (empty($_REQUEST['anytype'])) {
106 $img->whereAdd("mimetype like 'image/%'");
109 if (isset($onbits[2])) {
110 $img->imgtype = $onbits[2];
113 if (!$img->find(true)) {
114 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
115 urlencode("no images for that item: " . htmlspecialchars($id)));
124 // depreciated - should use ontable:onid:type here...
125 if (!empty($_REQUEST['ontable'])) {
127 //DB_DataObjecT::debugLevel(1);
128 $img = DB_DataObjecT::factory('Images');
129 $img->setFrom($_REQUEST);
130 // use imgtype now...
131 // if (!empty($_REQUEST['query']['filename'])){
132 // $img->whereAdd("filename LIKE '". $img->escape($_REQUEST['query']['filename']).".%'");
137 if (!$img->find(true)) {
138 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason='.
139 urlencode("No file exists"));
147 $img = DB_DataObjecT::factory('Images');
149 if (!$id || !$img->get($id)) {
151 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
152 urlencode("image has been removed or deleted."));
163 if (!$this->authUser) {
164 $this->jerr("image conversion only allowed by registered users");
166 // converts a posted string (eg.svg)
167 // into another type..
168 if (empty($_REQUEST['as'])) {
169 $this->jerr("missing target type");
171 if (empty($_REQUEST['mimetype'])) {
172 $this->jerr("missing mimetype");
174 if (empty($_REQUEST['data'])) {
175 $this->jerr("missing data");
179 $this->as_mimetype = $_REQUEST['as'];
180 $this->mimetype = $_REQUEST['mimetype'];
181 require_once 'File/MimeType.php';
182 $y = new File_MimeType();
183 $src_ext = $y->toExt( $this->mimetype );
186 $tmp = $this->tempName($src_ext);
187 file_put_contents($tmp, $_REQUEST['data']);
189 require_once 'File/Convert.php';
190 $cv = new File_Convert($tmp, $this->mimetype);
194 empty($_REQUEST['width']) ? 0 : $_REQUEST['width'],
195 empty($_REQUEST['height']) ? 0 : $_REQUEST['height']
197 if (!empty($_REQUEST['as_data'])) {
198 $this->jok(base64_encode(file_get_contents($fn)));
201 $cv->serve('attachment');
212 $this->sessionState(0); // turn off session... - locking...
214 require_once 'File/Convert.php';
215 if (!file_exists($img->getStoreName())) {
216 // print_r($img);exit;
217 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
218 urlencode("Original file was missing : " . $img->getStoreName()));
221 // print_r($img);exit;
222 $x = $img->toFileConvert();
223 if (empty($this->as_mimetype)) {
224 $this->as_mimetype = $img->mimetype;
227 $x->convert( $this->as_mimetype);
228 $x->serve($this->method);
231 //echo "SKALING? $this->size";
232 // acutally if we generated the image, then we do not need to validate the size..
236 // if the mimetype is not converted..
237 // then the filename should be original.{size}.jpeg
238 $fn = $img->getStoreName() . '.'. $this->size . '.jpeg'; // thumbs are currenly all jpeg.!???
240 if (!file_exists($fn)) {
241 $fn = $img->getStoreName() . '.'. $this->size . '.'. $img->fileExt();
242 // if it's an image, convert into the same type for thumbnail..
243 if (preg_match('#^image/#', $img->mimetype)) {
244 $this->as_mimetype = $img->mimetype;
248 if (!file_exists($fn)) {
250 $this->validateSize();
253 $x->convert( $this->as_mimetype, $this->size);
261 function validateSize()
263 if (($this->authUser && $this->authUser->company_id && $this->authUser->company()->comptype=='OWNER') || $_SERVER['SERVER_ADDR'] == $_SERVER['REMOTE_ADDR']) {
267 // DEFAULT allowed - override with $cfg['sizes'];
278 '300x100', // logo on login.
282 // this should be configurable...
283 $ff = HTML_FlexyFramework::get();
284 $cfg = isset($ff->Pman_Images) ? $ff->Pman_Images :
285 (isset($ff->Pman_Core_Images) ? $ff->Pman_Core_Images : array());
289 if (!empty($cfg['sizes'])) {
290 $sizes = array_merge($sizes , $cfg['sizes']);
294 if (!in_array($this->size, $sizes)) {
295 die("invalid scale - ".$this->size);
301 * The idea of this code was to replace urls for images when you have an admin
302 * and a distribution page. with different urls.
304 * it may be usefull later if things like embedded images in emails. but
305 * I think it's proably better not to use this.
307 * The key problem being how to determine if we are replacing 'our' images or some external one..
313 static function replaceImageURLS($html)
316 $ff = HTML_FlexyFramework::get();
317 if (!isset($ff->Pman_Images['public_baseURL'])) {
320 //var_dump($ff->Pman_Images['public_baseURL']);
321 $baseURL = $ff->Pman_Images['public_baseURL'];
323 preg_match_all('/<img\s+[^>]+>/i',$html, $result);
325 $matches = array_unique($result[0]);
326 foreach($matches as $img) {
328 preg_match_all('/(width|height|src)="([^"]*)"/i',$img, $imatch);
332 foreach($imatch[1] as $i=>$key) {
333 $attr[$key] = $imatch[2][$i];
335 if (!isset($attr['src']) || 0 !== strpos($attr['src'], $baseURL)) {
338 $html = self::replaceImgUrl($html, $baseURL, $img, $attr, 'src' );
342 preg_match_all('/<a\s+[^>]+>/i',$html, $result);
344 $matches = array_unique($result[0]);
345 foreach($matches as $img) {
347 preg_match_all('/(href)="([^"]*)"/i',$img, $imatch);
351 foreach($imatch[1] as $i=>$key) {
352 $attr[$key] = $imatch[2][$i];
354 if (!isset($attr['href']) || 0 !== strpos($attr['href'], $baseURL)) {
357 $html = self::replaceImgUrl($html, $baseURL, $img, $attr, 'href' );
362 static function replaceImgUrl($html, $baseURL, $tag, $attr, $attr_name)
366 // see if it's an image url..
367 // Images/{ID}/fullname.xxxx
368 // Images/Thumb/200/{ID}/fullname.xxxx
369 // Images/Download/{ID}/fullname.xxxx
371 $attr_url = $attr[$attr_name];
373 if(!preg_match('#/(Images|Images/Thumb/[a-z0-9]+|Images/Download)/([0-9]+)/(.*)$#', $attr_url, $umatch)) {
377 $img = DB_DataObject::factory('Images');
378 if (!$img->get($id)) {
381 $type = explode('/', $umatch[1]);
384 if (count($type) > 2 && $type[1] == 'Thumb') {
385 $thumbsize = $type[2];
386 $provider = '/Images/Thumb';
388 $provider = '/'.$umatch[1];
391 if (!empty($attr['width']) || !empty($attr['height']) )
393 // no support for %...
395 (empty($attr['width']) ? '0' : $attr['width'] * 1) .
397 (empty($attr['height']) ? '0' : $attr['height'] * 1);
398 $provider = '/Images/Thumb';
402 if ($thumbsize !== -1) {
404 // need to regenerate it..
406 $type = array('Images', 'Thumb', $thumbsize);
408 $fc = $img->toFileConvert();
409 // make sure it's available..
410 $fc->convert($img->mimetype, $thumbsize);
414 $provider = $provider == 'Images/Thumb' ? 'Images' : $provider;
418 // finally replace the original TAG with the new version..
420 $new_tag = str_replace(
421 $attr_name. '="'. $attr_url . '"',
422 $attr_name .'="'. htmlspecialchars($img->URL($thumbsize, $provider, $baseURL)) . '"',
427 return str_replace($tag, $new_tag, $html);
431 function downloadEvent($bits)
433 $popts = PEAR::getStaticProperty('Pman','options');
434 $ev = DB_DAtaObject::Factory('events');
435 if (!$ev->get($bits[1])) {
436 die("could not find event id");
438 // technically same user only.. -- normally www-data..
439 if (function_exists('posix_getpwuid')) {
440 $uinfo = posix_getpwuid( posix_getuid () );
441 $user = $uinfo['name'];
443 $user = getenv('USERNAME'); // windows.
445 $ff = HTML_FlexyFramework::get();
446 $file = $ff->Pman['event_log_dir']. '/'. $user. date('/Y/m/d/',strtotime($ev->event_when)). $ev->id . ".json";
447 $filesJ = json_decode(file_get_contents($file));
451 foreach($filesJ->FILES as $k=>$f){
452 if ($f->tmp_name != $bits[2]) {
456 $src = $ff->Pman['event_log_dir']. '/'. $user. date('/Y/m/d/', strtotime($ev->event_when)). $f->tmp_name ;
457 if (!file_exists($src)) {
458 die("file was not saved");
460 header ('Content-Type: ' . $f->type);
462 header("Content-Disposition: attachment; filename=\"".basename($f->name)."\";" );