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 $this->downloadEvent($bits);
85 die ("unknown file?");
89 $id = empty($bits[0]) ? 0 : $bits[0];
92 if (strpos($id,':') > 0) { // id format tablename:id:-imgtype
93 $onbits = explode(':', $id);
94 if ((count($onbits) < 2) || empty($onbits[1]) || !is_numeric($onbits[1]) || !strlen($onbits[0])) {
97 //DB_DataObject::debugLevel(1);
98 $img = DB_DataObject::factory('Images');
99 $img->ontable = $onbits[0];
100 $img->onid = $onbits[1];
101 if (empty($_REQUEST['anytype'])) {
102 $img->whereAdd("mimetype like 'image/%'");
105 if (isset($onbits[2])) {
106 $img->imgtype = $onbits[2];
109 if (!$img->find(true)) {
110 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
111 urlencode("no images for that item: " . htmlspecialchars($id)));
120 // depreciated - should use ontable:onid:type here...
121 if (!empty($_REQUEST['ontable'])) {
123 //DB_DataObjecT::debugLevel(1);
124 $img = DB_DataObjecT::factory('Images');
125 $img->setFrom($_REQUEST);
126 // use imgtype now...
127 // if (!empty($_REQUEST['query']['filename'])){
128 // $img->whereAdd("filename LIKE '". $img->escape($_REQUEST['query']['filename']).".%'");
133 if (!$img->find(true)) {
134 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason='.
135 urlencode("No file exists"));
143 $img = DB_DataObjecT::factory('Images');
145 if (!$id || !$img->get($id)) {
147 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
148 urlencode("image has been removed or deleted."));
159 if (!$this->authUser) {
160 $this->jerr("image conversion only allowed by registered users");
162 // converts a posted string (eg.svg)
163 // into another type..
164 if (empty($_REQUEST['as'])) {
165 $this->jerr("missing target type");
167 if (empty($_REQUEST['mimetype'])) {
168 $this->jerr("missing mimetype");
170 if (empty($_REQUEST['data'])) {
171 $this->jerr("missing data");
175 $this->as_mimetype = $_REQUEST['as'];
176 $this->mimetype = $_REQUEST['mimetype'];
177 require_once 'File/MimeType.php';
178 $y = new File_MimeType();
179 $src_ext = $y->toExt( $this->mimetype );
182 $tmp = $this->tempName($src_ext);
183 file_put_contents($tmp, $_REQUEST['data']);
185 require_once 'File/Convert.php';
186 $cv = new File_Convert($tmp, $this->mimetype);
190 empty($_REQUEST['width']) ? 0 : $_REQUEST['width'],
191 empty($_REQUEST['height']) ? 0 : $_REQUEST['height']
193 if (!empty($_REQUEST['as_data'])) {
194 $this->jok(base64_encode(file_get_contents($fn)));
197 $cv->serve('attachment');
208 $this->sessionState(0); // turn off session... - locking...
210 require_once 'File/Convert.php';
211 if (!file_exists($img->getStoreName())) {
212 // print_r($img);exit;
213 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
214 urlencode("Original file was missing : " . $img->getStoreName()));
217 // print_r($img);exit;
218 $x = $img->toFileConvert();
219 if (empty($this->as_mimetype)) {
220 $this->as_mimetype = $img->mimetype;
223 $x->convert( $this->as_mimetype);
224 $x->serve($this->method);
227 //echo "SKALING? $this->size";
228 // acutally if we generated the image, then we do not need to validate the size..
232 // if the mimetype is not converted..
233 // then the filename should be original.{size}.jpeg
234 $fn = $img->getStoreName() . '.'. $this->size . '.jpeg'; // thumbs are currenly all jpeg.!???
236 if (!file_exists($fn)) {
237 $fn = $img->getStoreName() . '.'. $this->size . '.'. $img->fileExt();
238 // if it's an image, convert into the same type for thumbnail..
239 if (preg_match('#^image/#', $img->mimetype)) {
240 $this->as_mimetype = $img->mimetype;
244 if (!file_exists($fn)) {
246 $this->validateSize();
249 $x->convert( $this->as_mimetype, $this->size);
257 function validateSize()
260 if ($this->authUser && $this->authUser->company_id && $this->authUser->company()->comptype=='OWNER') {
264 // DEFAULT allowed - override with $cfg['sizes'];
275 '300x100', // logo on login.
279 // this should be configurable...
280 $ff = HTML_FlexyFramework::get();
281 $cfg = isset($ff->Pman_Images) ? $ff->Pman_Images :
282 (isset($ff->Pman_Core_Images) ? $ff->Pman_Core_Images : array());
286 if (!empty($cfg['sizes'])) {
287 $sizes = array_merge($sizes , $cfg['sizes']);
291 if (!in_array($this->size, $sizes)) {
292 die("invalid scale - ".$this->size);
298 * The idea of this code was to replace urls for images when you have an admin
299 * and a distribution page. with different urls.
301 * it may be usefull later if things like embedded images in emails. but
302 * I think it's proably better not to use this.
304 * The key problem being how to determine if we are replacing 'our' images or some external one..
310 static function replaceImageURLS($html)
313 $ff = HTML_FlexyFramework::get();
314 if (!isset($ff->Pman_Images['public_baseURL'])) {
317 //var_dump($ff->Pman_Images['public_baseURL']);
318 $baseURL = $ff->Pman_Images['public_baseURL'];
320 preg_match_all('/<img\s+[^>]+>/i',$html, $result);
322 $matches = array_unique($result[0]);
323 foreach($matches as $img) {
325 preg_match_all('/(width|height|src)="([^"]*)"/i',$img, $imatch);
329 foreach($imatch[1] as $i=>$key) {
330 $attr[$key] = $imatch[2][$i];
332 if (!isset($attr['src']) || 0 !== strpos($attr['src'], $baseURL)) {
335 $html = self::replaceImgUrl($html, $baseURL, $img, $attr, 'src' );
339 preg_match_all('/<a\s+[^>]+>/i',$html, $result);
341 $matches = array_unique($result[0]);
342 foreach($matches as $img) {
344 preg_match_all('/(href)="([^"]*)"/i',$img, $imatch);
348 foreach($imatch[1] as $i=>$key) {
349 $attr[$key] = $imatch[2][$i];
351 if (!isset($attr['href']) || 0 !== strpos($attr['href'], $baseURL)) {
354 $html = self::replaceImgUrl($html, $baseURL, $img, $attr, 'href' );
359 static function replaceImgUrl($html, $baseURL, $tag, $attr, $attr_name)
363 // see if it's an image url..
364 // Images/{ID}/fullname.xxxx
365 // Images/Thumb/200/{ID}/fullname.xxxx
366 // Images/Download/{ID}/fullname.xxxx
368 $attr_url = $attr[$attr_name];
370 if(!preg_match('#/(Images|Images/Thumb/[a-z0-9]+|Images/Download)/([0-9]+)/(.*)$#', $attr_url, $umatch)) {
374 $img = DB_DataObject::factory('Images');
375 if (!$img->get($id)) {
378 $type = explode('/', $umatch[1]);
381 if (count($type) > 2 && $type[1] == 'Thumb') {
382 $thumbsize = $type[2];
383 $provider = '/Images/Thumb';
385 $provider = '/'.$umatch[1];
388 if (!empty($attr['width']) || !empty($attr['height']) )
390 // no support for %...
392 (empty($attr['width']) ? '0' : $attr['width'] * 1) .
394 (empty($attr['height']) ? '0' : $attr['height'] * 1);
395 $provider = '/Images/Thumb';
399 if ($thumbsize !== -1) {
401 // need to regenerate it..
403 $type = array('Images', 'Thumb', $thumbsize);
405 $fc = $img->toFileConvert();
406 // make sure it's available..
407 $fc->convert($img->mimetype, $thumbsize);
411 $provider = $provider == 'Images/Thumb' ? 'Images' : $provider;
415 // finally replace the original TAG with the new version..
417 $new_tag = str_replace(
418 $attr_name. '="'. $attr_url . '"',
419 $attr_name .'="'. htmlspecialchars($img->URL($thumbsize, $provider, $baseURL)) . '"',
424 return str_replace($tag, $new_tag, $html);
428 function downloadEvent($bits)
430 $popts = PEAR::getStaticProperty('Pman','options');
431 $ev = DB_DAtaObject::Factory('events');
432 if (!$ev->get($bits[1])) {
433 die("could not find event id");
435 // technically same user only.. -- normally www-data..
436 if (function_exists('posix_getpwuid')) {
437 $uinfo = posix_getpwuid( posix_getuid () );
438 $user = $uinfo['name'];
440 $user = getenv('USERNAME'); // windows.
442 $ff = HTML_FlexyFramework::get();
443 $file = $ff->Pman['event_log_dir']. '/'. $user. date('/Y/m/d/',strtotime($ev->event_when)). $ev->id . ".json";
444 $filesJ = json_decode(file_get_contents($file));
448 foreach($filesJ->FILES as $k=>$f){
449 if ($f->tmp_name != $bits[2]) {
453 $src = $ff->Pman['event_log_dir']. '/'. $user. date('/Y/m/d/', strtotime($ev->event_when)). $f->tmp_name ;
454 if (!file_exists($src)) {
455 die("file was not saved");
457 header ('Content-Type: ' . $f->type);
459 header("Content-Disposition: attachment; filename=\"".basename($f->name)."\";" );