* @license PHP License * @package wb * @subpackage Markup */ WBClass::load('WBMarkup_Converter'); /** * Tag converter * * Convert node to node * * @version 0.3.0 * @package wb * @subpackage Markup */ class WBMarkup_Converter_Wb_Vfsfile extends WBMarkup_Converter { /** * associative array to configure converter * @param array */ protected $config = array( 'defaultwidth' => 200, 'defaultheight' => 200, 'maxwidth' => 400, 'maxheight' => 400, 'previewwidth' => 600, 'previewheight' => 600 ); /** * transform Xinha Tags to XML * * @param array $node to be modified document node * @return bool true on success */ public function toXml(&$node) { $style = $this->explodeStyle($node); $xml = array( 'ns' => 'wb', 'tag' => 'vfsfile', 'attributes' => array(), 'isEmpty' => true, 'cData' => array() ); $size = array( intval($this->config['defaultwidth']), intval($this->config['defaultheight']) ); if (isset($style['width'])) { $width = intval($style['width']); $width = min($width, intval($this->config['maxwidth'])); $size[0] = $width; } if (isset($style['height'])) { $height = intval($style['height']); $height = min($height, intval($this->config['maxheight'])); $size[1] = $height; } $atts = array( 'obscureid' => $node['attributes']['wb:value'], 'size' => implode('x',$size), 'position' => 'block' ); if (isset($node['attributes']['position'])) { $atts['position'] = $node['attributes']['position']; } $xml['attributes'] = $atts; $node = $xml; return true; } /** * transform Xml to Xinha (html) tags * * @param array $node to be modified document node * @return bool true on success */ public function toXinha(&$node) { $xinha = array( 'ns' => '', 'tag' => 'img', 'attributes' => array(), 'isEmpty' => true, 'cData' => array() ); $size = intval($this->config['defaultwidth']) . 'x' . intval($this->config['defaultheight']); if (isset($node['attributes']['size'])) { $size = $node['attributes']['size']; } $src = '[[SERVICE_FILE]]' . $node['attributes']['obscureid'] . '/image' . '?size=' . $size; $size = explode('x', $size); $style = array( 'display' => 'inline-block', 'width' => $size[0] . 'px', 'height' => $size[1] . 'px' ); switch ($node['attributes']['position']) { case 'left': $style['float'] = 'left'; break; case 'right': $style['float'] = 'right'; break; default: case 'block': break; } $atts = array( 'wb:dialog' => 'Vfsfile', 'wb:value' => $node['attributes']['obscureid'], 'position' => $node['attributes']['position'], 'style' => $this->implodeStyle($style), 'src' => $src, 'alt' => $node['attributes']['obscureid'], 'title' => 'double click to change' ); if (isset($node['attributes']['class'])) { $atts['class'] = $node['attributes']['class']; } $xinha['attributes'] = $atts; $node = $xinha; return true; } /** * transform XML to Html * * @param array $node to be modified document node * @return bool true on success */ public function toHtml(&$node) { $oid = $node['attributes']['obscureid']; // load file $loader = WBClass::create('WBVFS_File'); /** @var $loader WBVFS_File */ $file = $loader->loadByObscureId($oid); if (!$file->isOk()) { $this->toHtmlBrokenFile($file, $node); return true; } switch ($file->getMime(WBVFS_File::MIME_MAJOR)) { case 'video': $this->toHtmlVideo($file, $node); break; case 'image': $this->toHtmlImage($file, $node); break; default: $this->toHtmlDownload($file, $node); break; } return true; } /** * display broken file * * In case file is bloken, display something at least * * @param WBVFS_File $file * @param array $node */ protected function toHtmlBrokenFile($file, &$node) { $this->toHtmlDownload($file, $node); } /** * file to download link * * Download file * * @param WBVFS_File $file * @param array $node */ protected function toHtmlDownload($file, &$node) { if (!$this->loadTemplates('download')) { return $this->toHtmlDownloadString($file, $node); } $data = $this->prefillPlaceholder($file, $node, 'download'); $data['url'] = $file->getUrl('application', true); $data['img_url'] = $file->getUrl('image', true); $size = intval($this->config['defaultwidth']) . 'x' . intval($this->config['defaultheight']); if (isset($node['attributes']['size'])) { $size = $node['attributes']['size']; } $get = array('size' => $size); $data['img_src'] = $this->getImageSrc($file, $get); //$data['img_src'] = $data['img_url'] . '?size=' . $size; $this->template2HtmlNode($node, $data); } /** * display image * * Show image and use lightbox to display "big" picture * * @param WBVFS_File $file * @param array $node * @return unknown_type */ protected function toHtmlImage($file, &$node) { if (!$this->loadTemplates('image')) { return $this->toHtmlImageString($file, $node); } $data = $this->prefillPlaceholder($file, $node, 'image'); $url = $file->getUrl('image', true); $size = intval($this->config['defaultwidth']) . 'x' . intval($this->config['defaultheight']); if (isset($node['attributes']['size'])) { $size = $node['attributes']['size']; } $get = array('size' => $size); $data['src'] = $this->getImageSrc($file, $get); //$data['src'] = $url . '?size=' . $size; $data['url'] = $url; $size = explode('x', $size); $data['size_width'] = $size[0]; $data['size_height'] = $size[1]; $this->template2HtmlNode($node, $data); } /** * display video * * Start Flash video player * * @param WBVFS_File $file * @param array $node */ protected function toHtmlVideo($file, &$node) { if (!$this->loadTemplates('video')) { return $this->toHtmlVideoString($file, $node); } $data = $this->prefillPlaceholder($file, $node, 'video'); $data['url'] = $file->getUrl('image', true); $this->template2HtmlNode($node, $data); } /** * transform position to style attribute * * Play with display and float * * @param array $node * @return string $style */ protected function position2Style($node, $inline = false) { if (!isset($node['attributes']['position'])) { return ''; } $style = 'display:block;'; if ($inline) { $style = 'display:inline-block'; } switch ($node['attributes']['position']) { case 'left': $style = 'float:left;'; break; case 'right': $style = 'float:right;'; break; default: case 'block': break; } return $style; } /** * prefill placeholders with file information * * Convenience method to prepare template data * * @param WBVFS_File $file * @param array $node * @param string $class additional class name * @return array $data */ private function prefillPlaceholder($file, &$node, $class) { // default position if (!isset($node['attributes']['position'])) { $node['attributes']['position'] = ''; } $data = array( 'checksum' => $this->md5, 'class' => 'vfsfile ' . $class, 'position' => $node['attributes']['position'], 'obscureid' => $file->getObscureId(), 'name' => $file->getName(), 'description' => $file->getDescription(), 'title' => $file->getDescription(), 'server' => $file->getServer() ); if (empty($data['title'])) { $data['title'] = $data['name']; } return $data; } /** * file to download link * * Download file * Fallback method without templates * * @param WBVFS_File $file * @param array $node */ protected function toHtmlDownloadString($file, &$node) { $html = array( 'ns' => null, 'tag' => 'a', 'attributes' => array(), 'isEmpty' => false, 'cData' => array() ); $atts = array( 'class' => sprintf('vfsfile download %s %s', $file->getMime(WBVFS_File::MIME_MAJOR), $file->getMime(WBVFS_File::MIME_MINOR)), 'href' => $file->getUrl('application', true), 'style' => $this->position2Style($node), 'title' => $file->getName() ); $html['attributes'] = $atts; $html['cData'][] = $file->getName(); $node = $html; } /** * display image * * Show image and use lightbox to display "big" picture. * Fallback method without templates * * @param WBVFS_File $file * @param array $node * @return unknown_type */ protected function toHtmlImageString($file, &$node) { $html = array( 'ns' => null, 'tag' => 'a', 'attributes' => array(), 'isEmpty' => false, 'cData' => array() ); $size = intval($this->config['defaultwidth']) . 'x' . intval($this->config['defaultheight']); if (isset($node['attributes']['size'])) { $size = $node['attributes']['size']; } $src = $file->getUrl('image', true) . '?size='; $atts = array( 'class' => 'vfsfile image', 'href' => $src . intval($this->config['previewwidth']) . 'x' . intval($this->config['previewheight']), 'style' => $this->position2Style($node, true), 'rel' => sprintf('lightbox[%s]', $this->md5) ); $alt = $file->getName(); $atts['title'] = $file->getDescription(); if (empty($atts['title'])) { $atts['title'] = $alt; } // pass reference to just created html node $get = array('size' => $size); $html['cData'][] = sprintf('%s', $this->getImageSrc($file, $get), $alt); $html['attributes'] = $atts; $node = $html; } /** * display video * * Start Flash video player * Fallback method without templates * * @param WBVFS_File $file * @param array $node */ protected function toHtmlVideoString($file, &$node) { $oid = $file->getObscureId(); $html = array( 'ns' => null, 'tag' => 'span', 'attributes' => array(), 'isEmpty' => false, 'cData' => array() ); $atts = array( 'class' => 'vfsfile video', 'style' => $this->position2Style($node) ); $urlDl = $file->getUrl('application', true); $server = $file->getServer(); $innerHtml = << WBVFSFile-playVideo-%1\$s-%2\$s-WBVFSFile %4\$s EOT; $html['cData'][] = sprintf($innerHtml , $this->md5 , $file->getObscureid() , urlencode($file->getName()) , addslashes($file->getName()) , addslashes($urlDl) , addslashes($server) ); $html['attributes'] = $atts; $node = $html; } /** * Get image source * * Either get image source as URL or as data URI. * * @param WBVFS_File $file * @param array $get * @todo refactor this method and merge with WBVFS_Mime_Image */ private function getImageSrc($file, &$get) { $src = $file->getUrl('image', true); if (!empty($get)) { $src .= '?' . http_build_query($get); } // well, just don't do anything... if (!WBParam::get('wb/html/datauri/use', false)) { return $src; } // image size if (!isset($get['size'])) { return $src; } // only for small images $size = explode('x', $get['size']); if (201 < max($size)) { return $src; } $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))); } }