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') {
82 $popts = PEAR::getStaticProperty('Pman','options');
83 $ev = DB_DAtaObject::Factory('events');
84 if (!$ev->get($bits[1])) {
85 die("could not find event id");
87 // technically same user only.. -- normally www-data..
88 if (function_exists('posix_getpwuid')) {
89 $uinfo = posix_getpwuid( posix_getuid () );
90 $user = $uinfo['name'];
92 $user = getenv('USERNAME'); // windows.
94 $ff = HTML_FlexyFramework::get();
95 $file = $ff->Pman['event_log_dir']. '/'. $user. date('/Y/m/d/',strtotime($ev->event_when)). $ev->id . ".json";
96 $filesJ = json_decode(file_get_contents($file));
100 foreach($filesJ->FILES as $k=>$f){
101 if ($f->tmp_name != $bits[2]) {
105 $src = $ff->Pman['event_log_dir']. '/'. $user. date('/Y/m/d/', strtotime($ev->event_when)). $f->tmp_name ;
106 if (!file_exists($src)) {
107 die("file was not saved");
109 header ('Content-Type: ' . $f->type);
111 header("Content-Disposition: attachment; filename=\"".basename($f->name)."\";" );
117 die ("unknown file?");
120 $id = empty($bits[0]) ? 0 : $bits[0];
123 if (strpos($id,':') > 0) { // id format tablename:id:-imgtype
124 $onbits = explode(':', $id);
125 if ((count($onbits) < 2) || empty($onbits[1]) || !is_numeric($onbits[1]) || !strlen($onbits[0])) {
128 //DB_DataObject::debugLevel(1);
129 $img = DB_DataObject::factory('Images');
130 $img->ontable = $onbits[0];
131 $img->onid = $onbits[1];
132 if (empty($_REQUEST['anytype'])) {
133 $img->whereAdd("mimetype like 'image/%'");
136 if (isset($onbits[2])) {
137 $img->imgtype = $onbits[2];
140 if (!$img->find(true)) {
141 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
142 urlencode("no images for that item: " . htmlspecialchars($id)));
151 // depreciated - should use ontable:onid:type here...
152 if (!empty($_REQUEST['ontable'])) {
154 //DB_DataObjecT::debugLevel(1);
155 $img = DB_DataObjecT::factory('Images');
156 $img->setFrom($_REQUEST);
157 // use imgtype now...
158 // if (!empty($_REQUEST['query']['filename'])){
159 // $img->whereAdd("filename LIKE '". $img->escape($_REQUEST['query']['filename']).".%'");
164 if (!$img->find(true)) {
165 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason='.
166 urlencode("No file exists"));
174 $img = DB_DataObjecT::factory('Images');
175 if (!$id || !$img->get($id)) {
177 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
178 urlencode("image has been removed or deleted."));
188 if (!$this->authUser) {
189 $this->jerr("image conversion only allowed by registered users");
191 // converts a posted string (eg.svg)
192 // into another type..
193 if (empty($_REQUEST['as'])) {
194 $this->jerr("missing target type");
196 if (empty($_REQUEST['mimetype'])) {
197 $this->jerr("missing mimetype");
199 if (empty($_REQUEST['data'])) {
200 $this->jerr("missing data");
205 $this->as_mimetype = $_REQUEST['as'];
206 $this->mimetype = $_REQUEST['mimetype'];
207 require_once 'File/MimeType.php';
208 $y = new File_MimeType();
209 $src_ext = $y->toExt( $this->mimetype );
210 $test_temp = '/var/lib/php5/ClippingTMPh3cFeo.svg';
212 $xml = new SimpleXMLElement($_REQUEST['data']);
214 print_r($xml->asXML());exit;
216 $tmp = $this->tempName($src_ext);
217 file_put_contents($tmp, $_REQUEST['data']);
221 require_once 'File/Convert.php';
222 $cv = new File_Convert($test_temp, $this->mimetype);
226 empty($_REQUEST['width']) ? 0 : $_REQUEST['width'],
227 empty($_REQUEST['height']) ? 0 : $_REQUEST['height']
229 // print_r($tmp);exit;
230 if (!empty($_REQUEST['as_data'])) {
231 $this->jok(base64_encode(file_get_contents($fn)));
234 $cv->serve('attachment');
245 $this->sessionState(0); // turn off session... - locking...
247 require_once 'File/Convert.php';
248 if (!file_exists($img->getStoreName())) {
249 //print_r($img);exit;
250 header('Location: ' . $this->rootURL . '/Pman/templates/images/file-broken.png?reason=' .
251 urlencode("Original file was missing : " . $img->getStoreName()));
255 $x = $img->toFileConvert();
256 if (empty($this->as_mimetype)) {
257 $this->as_mimetype = $img->mimetype;
260 $x->convert( $this->as_mimetype);
261 $x->serve($this->method);
264 //echo "SKALING? $this->size";
265 // acutally if we generated the image, then we do not need to validate the size..
269 // if the mimetype is not converted..
270 // then the filename should be original.{size}.jpeg
271 $fn = $img->getStoreName() . '.'. $this->size . '.jpeg'; // thumbs are currenly all jpeg.!???
273 if (!file_exists($fn)) {
274 $fn = $img->getStoreName() . '.'. $this->size . '.'. $img->fileExt();
275 // if it's an image, convert into the same type for thumbnail..
276 if (preg_match('#^image/#', $img->mimetype)) {
277 $this->as_mimetype = $img->mimetype;
281 if (!file_exists($fn)) {
283 $this->validateSize();
286 $x->convert( $this->as_mimetype, $this->size);
294 function validateSize()
297 if ($this->authUser && $this->authUser->company_id && $this->authUser->company()->comptype=='OWNER') {
301 // DEFAULT allowed - override with $cfg['sizes'];
312 '300x100', // logo on login.
316 // this should be configurable...
317 $ff = HTML_FlexyFramework::get();
318 $cfg = isset($ff->Pman_Images) ? $ff->Pman_Images :
319 (isset($ff->Pman_Core_Images) ? $ff->Pman_Core_Images : array());
323 if (!empty($cfg['sizes'])) {
324 $sizes = array_merge($sizes , $cfg['sizes']);
328 if (!in_array($this->size, $sizes)) {
329 die("invalid scale - ".$this->size);
335 * The idea of this code was to replace urls for images when you have an admin
336 * and a distribution page. with different urls.
338 * it may be usefull later if things like embedded images in emails. but
339 * I think it's proably better not to use this.
341 * The key problem being how to determine if we are replacing 'our' images or some external one..
347 static function replaceImageURLS($html)
350 $ff = HTML_FlexyFramework::get();
351 if (!isset($ff->Pman_Images['public_baseURL'])) {
354 //var_dump($ff->Pman_Images['public_baseURL']);
355 $baseURL = $ff->Pman_Images['public_baseURL'];
357 preg_match_all('/<img\s+[^>]+>/i',$html, $result);
359 $matches = array_unique($result[0]);
360 foreach($matches as $img) {
362 preg_match_all('/(width|height|src)="([^"]*)"/i',$img, $imatch);
366 foreach($imatch[1] as $i=>$key) {
367 $attr[$key] = $imatch[2][$i];
369 if (!isset($attr['src']) || 0 !== strpos($attr['src'], $baseURL)) {
372 $html = self::replaceImgUrl($html, $baseURL, $img, $attr, 'src' );
376 preg_match_all('/<a\s+[^>]+>/i',$html, $result);
378 $matches = array_unique($result[0]);
379 foreach($matches as $img) {
381 preg_match_all('/(href)="([^"]*)"/i',$img, $imatch);
385 foreach($imatch[1] as $i=>$key) {
386 $attr[$key] = $imatch[2][$i];
388 if (!isset($attr['href']) || 0 !== strpos($attr['href'], $baseURL)) {
391 $html = self::replaceImgUrl($html, $baseURL, $img, $attr, 'href' );
396 static function replaceImgUrl($html, $baseURL, $tag, $attr, $attr_name)
400 // see if it's an image url..
401 // Images/{ID}/fullname.xxxx
402 // Images/Thumb/200/{ID}/fullname.xxxx
403 // Images/Download/{ID}/fullname.xxxx
405 $attr_url = $attr[$attr_name];
407 if(!preg_match('#/(Images|Images/Thumb/[a-z0-9]+|Images/Download)/([0-9]+)/(.*)$#', $attr_url, $umatch)) {
411 $img = DB_DataObject::factory('Images');
412 if (!$img->get($id)) {
415 $type = explode('/', $umatch[1]);
418 if (count($type) > 2 && $type[1] == 'Thumb') {
419 $thumbsize = $type[2];
420 $provider = '/Images/Thumb';
422 $provider = '/'.$umatch[1];
425 if (!empty($attr['width']) || !empty($attr['height']) )
427 // no support for %...
429 (empty($attr['width']) ? '0' : $attr['width'] * 1) .
431 (empty($attr['height']) ? '0' : $attr['height'] * 1);
432 $provider = '/Images/Thumb';
436 if ($thumbsize !== -1) {
438 // need to regenerate it..
440 $type = array('Images', 'Thumb', $thumbsize);
442 $fc = $img->toFileConvert();
443 // make sure it's available..
444 $fc->convert($img->mimetype, $thumbsize);
448 $provider = $provider == 'Images/Thumb' ? 'Images' : $provider;
452 // finally replace the original TAG with the new version..
454 $new_tag = str_replace(
455 $attr_name. '="'. $attr_url . '"',
456 $attr_name .'="'. htmlspecialchars($img->URL($thumbsize, $provider, $baseURL)) . '"',
461 return str_replace($tag, $new_tag, $html);