Update zip Viewers.
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 12 Nov 2008 06:14:56 +0000 (23:14 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 12 Nov 2008 06:14:56 +0000 (23:14 -0700)
framework/Mime/lib/Horde/Mime/Viewer/zip.php
imp/lib/Mime/Viewer/zip.php

index 3dd3685..f7be978 100644 (file)
 class Horde_Mime_Viewer_zip extends Horde_Mime_Viewer_Driver
 {
     /**
-     * Render out the current zip contents.
+     * Can this driver render various views?
      *
-     * @param array $params  Any parameters the Viewer may need.
+     * @var boolean
+     */
+    protected $_capability = array(
+        'embedded' => false,
+        'full' => true,
+        'info' => false,
+        'inline' => true
+    );
+
+    /**
+     * A callback function to use in _toHTML().
+     *
+     * @var callback
+     */
+    protected $_callback = null;
+
+    /**
+     * Return the full rendered version of the Horde_Mime_Part object.
      *
-     * @return string  The rendered contents.
+     * @return array  See Horde_Mime_Viewer_Driver::render().
      */
-    public function render($params = array())
+    protected function _render()
     {
-        return $this->_render($this->mime_part->getContents());
+        $ret = $this->_toHTML();
+        if (!empty($ret)) {
+            $ret['data'] = '<html><body>' . $ret['data'] . '</body></html>';
+        }
+        return $ret;
     }
 
     /**
-     * Output the file list.
+     * Return the rendered inline version of the Horde_Mime_Part object.
      *
-     * @param string $contents  The contents of the zip archive.
-     * @param mixed $callback   The callback function to use on the zipfile
-     *                          information.
+     * @return array  See Horde_Mime_Viewer_Driver::render().
+     */
+    protected function _renderInline()
+    {
+        return $this->_toHTML();
+    }
+
+    /**
+     * Converts the ZIP file to an HTML display.
      *
-     * @return string  The file list.
+     * @return array  See Horde_Mime_Viewer_Driver::render().
      */
-    protected function _render($contents, $callback = null)
+    protected function _toHTML()
     {
-        require_once 'Horde/Compress.php';
+        $contents = $this->_mimepart->getContents();
 
+        require_once 'Horde/Compress.php';
         $zip = &Horde_Compress::singleton('zip');
 
         /* Make sure this is a valid zip file. */
         if ($zip->checkZipData($contents) === false) {
-            return '<pre>' . _("This does not appear to be a valid zip file.")
-                . '</pre>';
+            return array();
         }
 
-        $zipInfo = $zip->decompress(
-            $contents,
-            array('action' => HORDE_COMPRESS_ZIP_LIST));
+        $zipInfo = $zip->decompress($contents, array('action' => HORDE_COMPRESS_ZIP_LIST));
         if (is_a($zipInfo, 'PEAR_Error')) {
-            return $zipInfo->getMessage();
+            return array();
         }
         $fileCount = count($zipInfo);
 
         /* Determine maximum file name length. */
-        $maxlen = 0;
+        $max_array = array();
         foreach ($zipInfo as $val) {
-            $maxlen = max($maxlen, strlen($val['name']));
+            $max_array[] = strlen($val['name']);
         }
+        $maxlen = empty($max_array) ? 0 : max($max_array);
 
         require_once 'Horde/Text.php';
 
-        $text = '<strong>'
-            . htmlspecialchars(sprintf(_("Contents of \"%s\""),
-                                       $this->mime_part->getName()))
-            . ':</strong>' . "\n"
-            . '<table><tr><td align="left"><tt><span class="fixed">'
-            . Text::htmlAllSpaces(
-                _("Archive Name") . ': ' . $this->mime_part->getName() . "\n"
-                . _("Archive File Size") . ': ' . strlen($contents)
-                . ' bytes' . "\n"
-                . sprintf(
-                    ngettext("File Count: %d file", "File Count: %d files",
-                             $fileCount),
-                    $fileCount)
-                . "\n\n"
-                . String::pad(_("File Name"),     $maxlen, ' ', STR_PAD_RIGHT)
-                . String::pad(_("Attributes"),    10,      ' ', STR_PAD_LEFT)
-                . String::pad(_("Size"),          10,      ' ', STR_PAD_LEFT)
-                . String::pad(_("Modified Date"), 19,      ' ', STR_PAD_LEFT)
-                . String::pad(_("Method"),        10,      ' ', STR_PAD_LEFT)
-                . String::pad(_("CRC"),           10,      ' ', STR_PAD_LEFT)
-                . String::pad(_("Ratio"),         10,      ' ', STR_PAD_LEFT)
-                . "\n")
-            . str_repeat('-', 69 + $maxlen) . "\n";
+        $name = $this->_mimepart->getName(true);
+        if (empty($name)) {
+            $name = _("unnamed");
+        }
+
+        $text = '<strong>' . htmlspecialchars(sprintf(_("Contents of \"%s\""), $name)) . ':</strong>' . "\n" .
+            '<table><tr><td align="left"><tt><span class="fixed">' .
+            Text::htmlAllSpaces(
+                _("Archive Name") . ': ' . $name . "\n" .
+                _("Archive File Size") . ': ' . strlen($contents) .
+                ' bytes' . "\n" .
+                sprintf(ngettext("File Count: %d file", "File Count: %d files", $fileCount), $fileCount) .
+                "\n\n" .
+                String::pad(_("File Name"), $maxlen, ' ', STR_PAD_RIGHT) .
+                String::pad(_("Attributes"), 10, ' ', STR_PAD_LEFT) .
+                String::pad(_("Size"), 10, ' ', STR_PAD_LEFT) .
+                String::pad(_("Modified Date"), 19, ' ', STR_PAD_LEFT) .
+                String::pad(_("Method"), 10, ' ', STR_PAD_LEFT) .
+                String::pad(_("CRC"), 10, ' ', STR_PAD_LEFT) .
+                String::pad(_("Ratio"), 10, ' ', STR_PAD_LEFT) .
+                "\n"
+            ) . str_repeat('-', 69 + $maxlen) . "\n";
 
         foreach ($zipInfo as $key => $val) {
             $ratio = (empty($val['size']))
                 ? 0
                 : 100 * ($val['csize'] / $val['size']);
 
-            $val['name']   = String::pad($val['name'],
-                                         $maxlen, ' ', STR_PAD_RIGHT);
-            $val['attr']   = String::pad($val['attr'],
-                                         10,      ' ', STR_PAD_LEFT);
-            $val['size']   = String::pad($val['size'],
-                                         10,      ' ', STR_PAD_LEFT);
-            $val['date']   = String::pad(strftime("%d-%b-%Y %H:%M",
-                                                  $val['date']),
-                                         19,      ' ', STR_PAD_LEFT);
-            $val['method'] = String::pad($val['method'],
-                                         10,      ' ', STR_PAD_LEFT);
-            $val['crc']    = String::pad($val['crc'],
-                                         10,      ' ', STR_PAD_LEFT);
-            $val['ratio']  = String::pad(sprintf("%1.1f%%", $ratio),
-                                         10,      ' ', STR_PAD_LEFT);
+            $val['name']   = String::pad($val['name'], $maxlen, ' ', STR_PAD_RIGHT);
+            $val['attr']   = String::pad($val['attr'], 10,' ', STR_PAD_LEFT);
+            $val['size']   = String::pad($val['size'], 10, ' ', STR_PAD_LEFT);
+            $val['date']   = String::pad(strftime("%d-%b-%Y %H:%M", $val['date']), 19, ' ', STR_PAD_LEFT);
+            $val['method'] = String::pad($val['method'], 10, ' ', STR_PAD_LEFT);
+            $val['crc']    = String::pad($val['crc'], 10, ' ', STR_PAD_LEFT);
+            $val['ratio']  = String::pad(sprintf("%1.1f%%", $ratio), 10, ' ', STR_PAD_LEFT);
 
             $val = array_map(array('Text', 'htmlAllSpaces'), $val);
-            if (!is_null($callback)) {
-                $val = call_user_func($callback, $key, $val);
+            if (!is_null($this->_callback)) {
+                $val = call_user_func($this->_callback, $key, $val);
             }
 
-            $text .= $val['name'] . $val['attr'] . $val['size'] . $val['date']
-                . $val['method'] . $val['crc'] . $val['ratio'] . "\n";
+            $text .= $val['name'] . $val['attr'] . $val['size'] .
+                $val['date'] . $val['method'] . $val['crc'] . $val['ratio'] .
+                "\n";
         }
 
-        $text .= str_repeat('-', 69 + $maxlen) . "\n"
-            . '</span></tt></td></tr></table>';
-
-        return nl2br($text);
-    }
-
-    /**
-     * Return the content-type
-     *
-     * @return string  The content-type of the output.
-     */
-    public function getType()
-    {
-        return 'text/html; charset=' . NLS::getCharset();
+        return array(
+            'data' => nl2br($text . str_repeat('-', 69 + $maxlen) . "\n" . '</span></tt></td></tr></table>'),
+            'type' => 'text/html; charset=' . NLS::getCharset()
+        );
     }
 }
index a2faf01..b0287ed 100644 (file)
 class IMP_Horde_Mime_Viewer_zip extends Horde_Mime_Viewer_zip
 {
     /**
-     * The IMP_Contents object, needed for the _callback() function.
+     * Return the full rendered version of the Horde_Mime_Part object.
      *
-     * @var IMP_Contents
-     */
-    protected $_contents;
-
-    /**
-     * Render out the currently set contents.
-     *
-     * @param array $params  An array with a reference to a  MIME_Contents
-     *                       object.
+     * URL parameters used by this function:
+     * <pre>
+     * 'zip_attachment' - (integer) The ZIP attachment to download.
+     * </pre>
      *
-     * @return string  Either the list of zip files or the data of an
-     *                 individual zip file.
+     * @return array  See Horde_Mime_Viewer_Driver::render().
      */
-    public function render($params)
+    protected function _render()
     {
-        $contents = &$params[0];
-
-        $data = $this->mime_part->getContents();
-        $text = '';
+        if (!Util::getFormData('zip_attachment')) {
+            $this->_callback = array(&$this, '_IMPcallback');
+            return parent::_render();
+        }
 
         /* Send the requested file. Its position in the zip archive is located
          * in 'zip_attachment'. */
-        if (Util::getFormData('zip_attachment')) {
-            $zip = &Horde_Compress::singleton('zip');
-            $fileKey = Util::getFormData('zip_attachment') - 1;
-            $zipInfo = $zip->decompress(
-                $data, array('action' => HORDE_COMPRESS_ZIP_LIST));
-            /* Verify that the requested file exists. */
-            if (isset($zipInfo[$fileKey])) {
-                $text = $zip->decompress(
-                    $data,
-                    array('action' => HORDE_COMPRESS_ZIP_DATA,
-                          'info' => &$zipInfo,
-                          'key' => $fileKey));
-                if (empty($text)) {
-                    $text = '<pre>' . _("Could not extract the requested file from the Zip archive.") . '</pre>';
-                } else {
-                    $this->mime_part->setType('application/octet-stream');
-                    $this->mime_part->setName(basename($zipInfo[$fileKey]['name']));
-                }
-            } else {
-                $text = '<pre>' . _("The requested file does not exist in the Zip attachment.") . '</pre>';
+        $data = $this->_mimepart->getContents();
+        $zip = &Horde_Compress::singleton('zip');
+        $fileKey = Util::getFormData('zip_attachment') - 1;
+        $zipInfo = $zip->decompress($data, array('action' => HORDE_COMPRESS_ZIP_LIST));
+
+        /* Verify that the requested file exists. */
+        if (isset($zipInfo[$fileKey])) {
+            $text = $zip->decompress($data, array('action' => HORDE_COMPRESS_ZIP_DATA, 'info' => &$zipInfo, 'key' => $fileKey));
+            if (!empty($text)) {
+                return array(
+                    'data' => $text,
+                    'name' => basename($zipInfo[$fileKey]['name']),
+                    'type' => 'application/octet-stream'
+                );
             }
-        } else {
-            $this->_contents = $contents;
-            $text = parent::_render($data, array($this, '_callback'));
         }
 
-        return $text;
+        // TODO: Error reporting
+        return array();
     }
 
     /**
-     * The function to use as a callback to parent::_render().
+     * Return the rendered inline version of the Horde_Mime_Part object.
+     *
+     * @return array  See Horde_Mime_Viewer_Driver::render().
+     */
+    protected function _renderInline()
+    {
+        $this->_callback = array(&$this, '_IMPcallback');
+        return parent::_renderInline();
+    }
+
+    /**
+     * The function to use as a callback to _toHTML().
      *
      * @param integer $key  The position of the file in the zip archive.
      * @param array $val    The information array for the archived file.
      *
      * @return string  The content-type of the output.
      */
-    protected function _callback($key, $val)
+    protected function _IMPcallback($key, $val)
     {
         $name = preg_replace('/(&nbsp;)+$/', '', $val['name']);
+
         if (!empty($val['size']) && (strstr($val['attr'], 'D') === false) &&
             ((($val['method'] == 0x8) && Util::extensionExists('zlib')) ||
              ($val['method'] == 0x0))) {
-            $old_name = $this->mime_part->getName();
-            $this->mime_part->setName(basename($name));
-            $val['name'] = str_replace(
-                $name,
-                $this->_contents->linkView(
-                    $this->mime_part, 'download_render', $name,
-                    array('jstext' => sprintf(_("View %s"),
-                                              str_replace('&nbsp;', ' ', $name)),
-                          'class' => 'fixed',
-                          'viewparams' => array(
-                              'ctype' => 'application/zip',
-                              'zip_attachment' => (urlencode($key) + 1)))),
-                $val['name']);
-            $this->mime_part->setName($old_name);
+            $mime_part = $this->_mimepart;
+            $mime_part->setName(basename($name));
+            $val['name'] = str_replace($name, $this->_params['contents']->linkView($mime_part, 'download_render', $name, array('jstext' => sprintf(_("View %s"), str_replace('&nbsp;', ' ', $name)), 'class' => 'fixed', 'params' => array('zip_attachment' => urlencode($key) + 1))), $val['name']);
         }
 
         return $val;