*/ /** * patTemplate modifier Vfsfile * * Display file * * @package wombat * @package Modifiers * @author gERD Schaufelberger */ class patTemplate_Modifier_Vfsfile extends patTemplate_Modifier { /** * shared file object * @var WBVFS_File */ static protected $file = null; /** * shared config * @var array */ static private $config; /** * modify the value * * @access public * @param string value * @return string modified value */ public function modify($value, $params = array()) { if (empty($value)) { return ''; } $file = $this->getFile($value); if(!$file->isOK()) { return ''; } if (!isset($params['forcedatauri'])) { $params['forcedatauri'] = 0; } if (!isset($params['target'])) { $params['target'] = 'img'; } if (!isset($params['mime'])) { $params['mime'] = null; } if (!isset($params['info'])) { $params['info'] = ''; } // copy size parameter $get = array(); if (isset($params['size'])) { $get['size'] = $params['size']; } switch ($params['target'] ) { case 'auto': if ('video' == $file->getMime()) { return $this->getTarget4Video($file, $get, $params); } break; case 'mime': return $file->getMime(); break; case 'info': if (empty($params['info'])) { return ''; } return $file->getInfo($params['info']); break; case 'obscureid': return $file->getObscureId(); break; case 'server': return $file->getServer(); break; case 'url': $url = $this->getUrl($file, $get, $params['mime']); return $url; break; case 'uri': return $file->getUri($params['mime']); break; case 'desc': case 'description': $desc = $file->getDescription(); if (empty($desc)) { $desc = $file->getName(); } return $desc; break; case 'name': return $file->getName(); break; default: case 'img': break; } if (0 < WBParam::get('wb/vfs/mime/image/usepicture', 0)) { return $this->getTarget4Picture($file, $get, $params); } return $this->getTarget4Image($file, $get, $params); } /** * Create Target String 4 Video * * Render Video tag * * @param WBVFS_File * @param array list of GET parameter for file url * @param array list of additional parameter * @return string */ private function getTarget4Video($file, $get, $params) { if (empty($params['size'])) { $params['size'] = '1280x720'; } $size = explode('x', $params['size']); $aria = str_replace("\n", ' ', trim(htmlentities($file->getDescription()))); $atts = array( 'controls' => 'controls', 'preload' => 'none', 'muted' => null, 'autoplay' => null, 'loop' => null, 'class' => null, 'style' => null, 'aria-label' => $aria, 'title' => $file->getName(), 'width' => $size[0], 'height' => $size[1] ); if ('auto' == $params['width']) { unset($atts['width']); } if ('auto' == $params['height']) { unset($atts['height']); } $tag = << EOT; $data = $file->get(); $data['server'] = $file->getServer(); $data['name_url'] = urlencode($file->getName()); $data['size'] = $params['size']; $data['att'] = $this->getAttrString($atts, $params); WBClass::load('WBString'); return WBString::populate($tag, $data); } /** * Create Target String 4 Image * * Render image tag * * @param WBVFS_File * @param array list of GET parameter for file url * @param array list of additional parameter * @return string */ private function getTarget4Picture($file, $get, $params) { $aria = str_replace("\n", ' ', trim(htmlentities($file->getDescription()))); $atts = array( 'align' => null, 'class' => null, 'style' => null, 'role' => 'img', //'aria-label' => $aria, 'title' => $file->getName(), 'alt' => $aria, ); $params['datauri'] = 0; $this->insertSrc($file, $atts, $get, $params); WBClass::load('WBString'); $data = $file->get(); $data['server'] = $file->getServer(); $data['name_url'] = urlencode($file->getName()); $data['att'] = $this->getAttrString($atts, $params); $data['get'] = http_build_query($get); // alternative $minor = array('webp', 'png', 'jpeg'); $tag = array(); $tag[] = ''; if (!empty($get['size'])) { $size = explode('x', $get['size']); $size[0] = intval($size[0]); if (800 < $size[0]) { foreach ($minor as $m) { $data['mimeminor'] = $m; $tag[] = WBString::populate('', $data); } $f = $size[0] / 800; $size[0] = 800; $size[1] = round($size[1] / $f); $get['size'] = implode('x', $size); $data['get'] = http_build_query($get); } } foreach ($minor as $m) { $data['mimeminor'] = $m; $tag[] = WBString::populate('', $data); } // alternative image tag $tag[] = WBString::populate('', $data); $tag[] = ''; $tag = implode("\n", $tag); return $tag; } /** * Create Target String 4 Image * * Render image tag * * @param WBVFS_File * @param array list of GET parameter for file url * @param array list of additional parameter * @return string */ private function getTarget4Image($file, $get, $params) { $aria = str_replace("\n", ' ', trim(htmlentities($file->getDescription()))); $atts = array( 'align' => null, 'class' => null, 'style' => null, 'role' => 'img', // 'aria-label' => $aria, 'title' => $file->getName(), 'alt' => $aria ); $this->insertSrc($file, $atts, $get, $params); $attStr = $this->getAttrString($atts, $params); return sprintf('', $attStr); } /** * Create Attribute String * * Build attribute string from default and mix with parameters * * @param array * @param array * @return string */ private function getAttrString($atts, $params) { if (isset($params['width']) && 'auto' == $params['width']) { unset($atts['width']); } if (isset($params['height']) && 'auto' == $params['height']) { unset($atts['height']); } foreach ($atts as $a => $def) { if (isset($params[$a])) { $attStr[] = $a . '="' . $params[$a] . '"'; continue; } if (is_null($def)) { continue; } $attStr[] = $a . '="' . $def . '"'; } return implode(' ', $attStr); } /** * insert source attribute * * Fill image tag's attribute list with image URL. * * @todo prepared to insert inline date * @param WBVFS_File $file * @param array $atts image tag's attributes * @param array $get get parameter * @param array $params modifier parameters */ private function insertSrc($file, &$atts, &$get, $params) { // avoid redirect $req = WBClass::create('WBRequest', array('module' => 'Fake')); $req->import($get); $res = WBClass::create('WBResponse'); $mimeMinor = $file->getMime(WBVFS_File::MIME_MINOR); switch ($mimeMinor) { case 'jpeg': case 'png': case 'webp': break; default: $mimeMinor = 'jpeg'; break; } $redirect = array(); $mimeHdl = WBClass::create('WBVFS_Mime_Image'); /** @var $mimeHdl VFS_Mime_Image */ $mimeHdl->setVirtualFile($file); $path = $mimeHdl->getRequestedFile($req, $res, $redirect, $mimeMinor); // do it again but with different GET parameters if (!empty($redirect)) { $get = $redirect['get']; $req->import($get); $redirect = array(); $path = $mimeHdl->getRequestedFile($req, $res, $redirect, $mimeMinor); } $atts['src'] = $this->getUrl($file, $get, 'image'); $size = explode('x', $get['size']); $atts['width'] = $size[0]; $atts['height'] = $size[1]; // avoid data uri if (isset($params['datauri']) && empty($params['datauri'])) { return; } // use data uri in all cases if (empty($params['forcedatauri'])) { // well, just don't do anything... if (1 > WBParam::get('wb/html/datauri/use', 0)) { return; } // image size if (!isset($get['size'])) { return; } // only for small images $size = explode('x', $get['size']); if (WBParam::get('wb/html/datauri/maxsize', 201) < max($size)) { return; } } $mimeMinor = $mimeHdl->getMime(WBVFS_File::MIME_MINOR); $fmt = 'data:image/%s;base64,%s'; $atts['src'] = sprintf($fmt, $mimeMinor, base64_encode(file_get_contents($path))); } /** * load file object with id * * @param $id * @return WBVFS_File */ protected function getFile($id) { if (!self::$file) { self::$file = WBClass::create('WBVFS_File'); } $file = self::$file->loadById($id); if (!$file->isOK()) { $file = self::$file->loadByObscureId($id); } return $file; } /** * Get URL for file * * Create URL for each file. * * @param WBVFS_File $file * @param array $get * @param string $mime * @return string */ private function getUrl($file, $get, $mime = null) { $url = $file->getUrl($mime, true); if (!empty($get)) { $url .= '?' . http_build_query($get); } return $url; } }