*/ /** * 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; } // copy size parameter $get = array(); if (isset($params['size'])) { $get['size'] = $params['size']; } switch ($params['target'] ) { case 'mime': return $file->getMime(); 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; } // create attribute string $attStr = array(); $atts = array( 'class' => null, 'title' => null, 'align' => null, 'style' => null, 'alt' => $file->getName(), ); $this->insertSrc($file, $atts, $get, $params); foreach ($atts as $a => $def) { if (isset($params[$a])) { $attStr[] = $a . '="' . $params[$a] . '"'; continue; } if (is_null($def)) { continue; } $attStr[] = $a . '="' . $def . '"'; } $attStr = implode(' ', $attStr); return sprintf('', $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) { $atts['src'] = $this->getUrl($file, $get, 'image'); // use data uri in all cases if (empty($params['forcedatauri'])) { // well, just don't do anything... if (!WBParam::get('wb/html/datauri/use', false)) { return; } // image size if (!isset($get['size'])) { return; } // only for small images $size = explode('x', $get['size']); if (201 < max($size)) { return; } } $atts['src'] = $this->getSrcData($file, $get); $size = explode('x', $get['size']); $atts['width'] = $size[0]; $atts['height'] = $size[1]; } /** * @todo refactor this method and merge with insertSrc() * @see insertSrc() */ private function getSrcData($file, &$get) { $req = WBClass::create('WBRequest', array('module' => 'Fake')); $req->import($get); $res = WBClass::create('WBResponse'); $mimeMinor = null; $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); } $mimeMinor = $mimeHdl->getMime(WBVFS_File::MIME_MINOR); $fmt = 'data:image/%s;base64,%s'; return 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; } } ?>