Update tgz Viewer.
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 12 Nov 2008 05:38:35 +0000 (22:38 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 12 Nov 2008 05:38:35 +0000 (22:38 -0700)
framework/Mime/lib/Horde/Mime/Viewer/tgz.php

index a49595b..3fc6c94 100644 (file)
@@ -1,8 +1,4 @@
 <?php
-
-require_once 'Horde/Compress.php';
-require_once 'Horde/Text.php';
-
 /**
  * The Horde_Mime_Viewer_tgz class renders out plain or gzipped tarballs in
  * HTML.
@@ -17,88 +13,97 @@ require_once 'Horde/Text.php';
 class Horde_Mime_Viewer_tgz extends Horde_Mime_Viewer_Driver
 {
     /**
-     * Render out the currently set tar file contents.
+     * Can this driver render various views?
+     *
+     * @var boolean
+     */
+    protected $_capability = array(
+        'embedded' => false,
+        'full' => true,
+        'info' => false,
+        'inline' => true
+    );
+
+    /**
+     * The list of compressed subtypes.
+     *
+     * @var array
+     */
+    protected $_gzipSubtypes = array(
+        'x-compressed-tar', 'tgz', 'x-tgz', 'gzip', 'x-gzip',
+        'x-gzip-compressed', 'x-gtar'
+    );
+
+    /**
+     * Return the full rendered version of the Horde_Mime_Part object.
      *
-     * @param array $params  Any parameters the Viewer may need.
+     * @return array  See Horde_Mime_Viewer_Driver::render().
+     */
+    protected function _render()
+    {
+        $ret = $this->_renderInline();
+        if (!empty($ret)) {
+            $ret['data'] = '<html><body>' . $ret['data'] . '</body></html>';
+        }
+        return $ret;
+    }
+
+    /**
+     * Return the rendered inline 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 _renderInline()
     {
-        $contents = $this->mime_part->getContents();
+        $contents = $this->_mimepart->getContents();
 
         /* Only decompress gzipped files. */
-        $subtype = $this->mime_part->getSubType();
-        if (($subtype == 'x-compressed-tar') ||
-            ($subtype == 'tgz') ||
-            ($subtype == 'x-tgz') ||
-            ($subtype == 'gzip') ||
-            ($subtype == 'x-gzip') ||
-            ($subtype == 'x-gzip-compressed') ||
-            ($subtype == 'x-gtar')) {
+        $subtype = $this->_mimepart->getSubType();
+        if (in_array($subtype, $this->_gzipSubtypes)) {
             $gzip = &Horde_Compress::singleton('gzip');
             $contents = $gzip->decompress($contents);
-            if (empty($contents)) {
-                return _("Unable to open compressed archive.");
-            } elseif (is_a($contents, 'PEAR_Error')) {
-                return $contents->getMessage();
+            if (is_a($contents, 'PEAR_Error') ||
+                empty($contents)) {
+                return array();
             }
         }
 
-        if ($subtype == 'gzip' ||
-            $subtype == 'x-gzip' ||
-            $subtype == 'x-gzip-compressed') {
-            global $conf;
-            require_once 'Horde/MIME/Magic.php';
-            $mime_type = MIME_Magic::analyzeData($contents, isset($conf['mime']['magic_db']) ? $conf['mime']['magic_db'] : null);
-            if (!$mime_type) {
-                $mime_type = _("Unknown");
-            }
-            return sprintf(_("Content type of compressed file: %s"), $mime_type);
-        }
-
         /* Obtain the list of files/data in the tar file. */
-        $tar = Horde_Compress::factory('tar');
+        $tar = &Horde_Compress::singleton('tar');
         $tarData = $tar->decompress($contents);
         if (is_a($tarData, 'PEAR_Error')) {
-            return $tarData->getMessage();
+            return array();
         }
-
         $fileCount = count($tarData);
-        $text = '<strong>' . htmlspecialchars(sprintf(_("Contents of \"%s\""), $this->mime_part->getName())) . ':</strong>' . "\n" .
+
+        require_once 'Horde/Text.php';
+
+        $text = '<strong>' . htmlspecialchars(sprintf(_("Contents of \"%s\""), $this->_mimepart->getName(true))) . ':</strong>' . "\n" .
             '<table><tr><td align="left"><tt><span class="fixed">' .
-            Text::htmlAllSpaces(_("Archive Name") . ':  ' . $this->mime_part->getName()) . "\n" .
+            Text::htmlAllSpaces(_("Archive Name") . ':  ' . $this->_mimepart->getName(true)) . "\n" .
             Text::htmlAllSpaces(_("Archive File Size") . ': ' . strlen($contents) . ' bytes') . "\n" .
             Text::htmlAllSpaces(sprintf(ngettext("File Count: %d file", "File Count: %d files", $fileCount), $fileCount)) .
             "\n\n" .
             Text::htmlAllSpaces(
-                str_pad(_("File Name"),     62, ' ', STR_PAD_RIGHT) .
-                str_pad(_("Attributes"),    15, ' ', STR_PAD_LEFT) .
-                str_pad(_("Size"),          10, ' ', STR_PAD_LEFT) .
+                str_pad(_("File Name"), 62, ' ', STR_PAD_RIGHT) .
+                str_pad(_("Attributes"), 15, ' ', STR_PAD_LEFT) .
+                str_pad(_("Size"), 10, ' ', STR_PAD_LEFT) .
                 str_pad(_("Modified Date"), 19, ' ', STR_PAD_LEFT)
             ) . "\n" .
             str_repeat('-', 106) . "\n";
 
         foreach ($tarData as $val) {
             $text .= Text::htmlAllSpaces(
-                         str_pad($val['name'], 62, ' ', STR_PAD_RIGHT) .
-                         str_pad($val['attr'], 15, ' ', STR_PAD_LEFT) .
-                         str_pad($val['size'], 10, ' ', STR_PAD_LEFT) .
-                         str_pad(strftime("%d-%b-%Y %H:%M", $val['date']), 19, ' ', STR_PAD_LEFT)
-                     ) . "\n";
+                str_pad($val['name'], 62, ' ', STR_PAD_RIGHT) .
+                str_pad($val['attr'], 15, ' ', STR_PAD_LEFT) .
+                str_pad($val['size'], 10, ' ', STR_PAD_LEFT) .
+                str_pad(strftime("%d-%b-%Y %H:%M", $val['date']), 19, ' ', STR_PAD_LEFT)
+            ) . "\n";
         }
 
-        return nl2br($text . str_repeat('-', 106) . "\n" .
-                     '</span></tt></td></tr></table>');
-    }
-
-    /**
-     * 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('-', 106) . "\n" . '</span></tt></td></tr></table>'),
+            'type' => 'text/html; charset=' . NLS::getCharset()
+        );
     }
 }