From 77aac94570ec4160695201193ed80bc6bdb35e52 Mon Sep 17 00:00:00 2001 From: Alan Date: Wed, 12 Jan 2022 10:29:31 +0800 Subject: [PATCH] fix data uploads in html --- DataObjects/Images.php | 20 +++++- Images.php | 151 +++++++++++++++++++++++++++++++---------- 2 files changed, 133 insertions(+), 38 deletions(-) diff --git a/DataObjects/Images.php b/DataObjects/Images.php index 10089ae6..3b07351b 100644 --- a/DataObjects/Images.php +++ b/DataObjects/Images.php @@ -841,13 +841,27 @@ class Pman_Core_DataObjects_Images extends DB_DataObject function createFromData($data) { - $this->mimetype= strtolower($this->mimetype); + if (0 === strpos($data, "data:")) { + // data:image/png;base64, + $data = substr($data,5); + $bits = explode(";", $data); + $this->mimetype = $bits[0]; + } + static $imgid = 1; + if (empty($this->filename)) { + require_once 'File/MimeType.php'; + $y = new File_MimeType(); + $this->filename = 'image-'.$imgid++.'.'.$y->toExt($this->mimetype); + } + + + $this->mimetype = strtolower($this->mimetype); $explode_mimetype = explode('/', $this->mimetype); if (array_shift($explode_mimetype) == 'image') { - $imgs = @getimagesize($data); + $imgs = @getimagesize('data://'. $data); if (!empty($imgs) && !empty($imgs[0]) && !empty($imgs[1])) { list($this->width , $this->height) = $imgs; @@ -871,7 +885,7 @@ class Pman_Core_DataObjects_Images extends DB_DataObject } file_put_contents($f, file_get_contents("data://" . $data)); - + //var_dump($f);exit; $o = clone($this); $this->filesize = filesize($f); diff --git a/Images.php b/Images.php index 884d88ab..327fa8c5 100644 --- a/Images.php +++ b/Images.php @@ -398,7 +398,7 @@ class Pman_Core_Images extends Pman */ - static function replaceImageURLS($html) + static function replaceImageURLS($html, $obj = false) { $ff = HTML_FlexyFramework::get(); @@ -408,61 +408,142 @@ class Pman_Core_Images extends Pman //var_dump($ff->Pman_Images['public_baseURL']); $baseURL = $ff->Pman_Images['public_baseURL']; - preg_match_all('/]+>/i',$html, $result); - //print_r($result); - $matches = array_unique($result[0]); - foreach($matches as $img) { - $imatch = array(); - preg_match_all('/(width|height|src)="([^"]*)"/i',$img, $imatch); - // build a keymap - $attr = array(); - - foreach($imatch[1] as $i=>$key) { - $attr[$key] = $imatch[2][$i]; - } - // does it contain baseURL??? --- well what about relative paths... - //print_R($attr); - - if (empty($attr['src'])) { + libxml_use_internal_errors(true); + $dom = new DOMDocument(); + $dom->loadHTML("
{$html}
"); + $imgs = $dom->getElementsByTagName('img'); + + + foreach($imgs as $img) { + $src = $img->getAttribute('src'); + if (!$src|| !strlen(trim($src))) { continue; } - if (0 !== strpos($attr['src'], $baseURL)) { - // it starts with our 'new' baseURL? - $html = self::replaceImgUrl($html, $baseURL, $img, $attr, 'src' ); + + if (0 === strpos($src, 'data:')) { + if (!$obj) { + HTML_FlexyFramework::get()->page->jerr("no object to attach data url"); + } + + self::replaceDataUrl($baseURL, $img, $obj); continue; } - if (false !== strpos($attr['src'], '//') && false === strpos($attr['src'], $baseURL)) { - // contains an absolute path.. that is probably not us... + + + if (false !== strpos($src, '//') && false === strpos($src, $baseURL)) { + // contains an absolute path.. and not our baseURL. continue; } + + $img->setAttribute('src', self::domImgUrl($baseURL, $img)); + // what about mailto or data... - just ignore?? for images... - $html = self::replaceImgUrl($html, $baseURL, $img, $attr, 'src' ); } - + $anchors = $dom->getElementsByTagName('a'); $result = array(); preg_match_all('/]+>/i',$html, $result); $matches = array_unique($result[0]); - foreach($matches as $img) { - $imatch = array(); - preg_match_all('/(href)="([^"]*)"/i',$img, $imatch); - // build a keymap - $attr = array(); - - foreach($imatch[1] as $i=>$key) { - $attr[$key] = $imatch[2][$i]; - } - if (!isset($attr['href']) || 0 !== strpos($attr['href'], $baseURL)) { + foreach($anchors as $anc) { + $href = $anc->getAttribute('href'); + if (!empty($href) || 0 !== strpos($href, $baseURL)) { continue; } - $html = self::replaceImgUrl($html, $baseURL, $img, $attr, 'href' ); + $anc->setAttribute('href', self::domImgUrl($baseURL, $href)); } + + $inner = $dom->getElementById("tmp_dom_wrapper"); + $html = ''; + foreach ($inner->childNodes as $child) { + $html .= ($dom->saveHTML($child)); + } return $html; } + + static function domImgUrl($baseURL, $dom) + { + $url = $dom; + if (!is_string($url)) { + $url = $dom->getAttribute($src); + } + $umatch = false; + if(!preg_match('#/(Images|Images/Thumb/[a-z0-9]+|Images/Download)/([0-9]+)/(.*)$#', $attr_url, $umatch)) { + return $url; + } + $id = $umatch[2]; + $hash = ''; + + if (!empty($umatch[3]) && strpos($umatch[3],'#')) { + $hh = explode('#',$umatch[3]); + $hash = '#'. array_pop($hh); + } + + + $img = DB_DataObject::factory('Images'); + if (!$img->get($id)) { + return $url; + } + $type = explode('/', $umatch[1]); + $thumbsize = -1; + + if (count($type) > 2 && $type[1] == 'Thumb') { + $thumbsize = $type[2]; + $provider = '/Images/Thumb'; + } else { + $provider = '/'.$umatch[1]; + } + + $w = is_string($dom) ? false : $dom->getAttribute('width'); + $h = is_string($dom) ? false : $dom->getAttribute('width'); + + if (!is_string($dom) && (!empty($w) || !empty($h)) ) + { + // no support for %... + $thumbsize = + (empty($w) ? '0' : $w * 1) . + 'x' . + (empty($h) ? '0' : $h * 1); + $provider = '/Images/Thumb'; + + } + + if ($thumbsize !== -1) { + // change in size.. + // need to regenerate it.. + + $type = array('Images', 'Thumb', $thumbsize); + + $fc = $img->toFileConvert(); + // make sure it's available.. + $fc->convert($img->mimetype, $thumbsize); + + + } else { + $provider = $provider == 'Images/Thumb' ? 'Images' : $provider; + } + + + // finally replace the original TAG with the new version.. + + return $img->URL($thumbsize, $provider, $baseURL) . $hash ; + + + } + + static function replaceDataUrl($baseURL, $img, $obj) + { + $d = DB_DataObject::Factory('Images'); + $d->object($obj); + + + $d->createFromData($img->getAttribute('src')); + $img->setAttribute('src', $d->URL(-1, '/Images' , $baseURL)); + } + static function replaceImgUrl($html, $baseURL, $tag, $attr, $attr_name) { -- 2.39.2