[ Index ]
 

Code source de Horde 3.1.3

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/Horde/MIME/Viewer/ -> rar.php (source)

   1  <?php
   2  /**
   3   * The MIME_Viewer_rar class renders out the contents of .rar archives in HTML
   4   * format.
   5   *
   6   * $Horde: framework/MIME/MIME/Viewer/rar.php,v 1.18.10.9 2006/01/01 21:28:25 jan Exp $
   7   *
   8   * Copyright 1999-2006 Anil Madhavapeddy <anil@recoil.org>
   9   * Copyright 2002-2006 Michael Cochrane <mike@graftonhall.co.nz>
  10   *
  11   * See the enclosed file COPYING for license information (LGPL). If you
  12   * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  13   *
  14   * @author  Anil Madhavapeddy <anil@recoil.org>
  15   * @author  Michael Cochrane <mike@graftonhall.co.nz>
  16   * @since   Horde 1.3
  17   * @package Horde_MIME_Viewer
  18   */
  19  class MIME_Viewer_rar extends MIME_Viewer {
  20  
  21      /**
  22       * Rar compression methods.
  23       *
  24       * @var array
  25       */
  26      var $_methods = array(
  27          0x30  =>  'Store',
  28          0x31  =>  'Fastest',
  29          0x32  =>  'Fast',
  30          0x33  =>  'Normal',
  31          0x34  =>  'Good',
  32          0x35  =>  'Best'
  33      );
  34  
  35      /**
  36       * Render out the currently set contents using rar.
  37       *
  38       * @param array $params  Any parameters the Viewer may need.
  39       *
  40       * @return string  The rendered contents.
  41       */
  42      function render($params = array())
  43      {
  44          $contents = $this->mime_part->getContents();
  45  
  46          /* Make sure this is a valid rar file. */
  47          if ($this->checkRarData($contents) === false) {
  48              return '<pre>' . _("This does not appear to be a valid rar archive.") . '</pre>';
  49          }
  50  
  51          require_once 'Horde/Text.php';
  52  
  53          $rarData = $this->getRarData($contents);
  54          $fileCount = count($rarData);
  55  
  56          $text  = '<strong>' . htmlspecialchars(sprintf(_("Contents of \"%s\""), $this->mime_part->getName())) . ':</strong>' . "\n";
  57          $text .= '<table><tr><td align="left"><tt><span class="fixed">';
  58          $text .= Text::htmlAllSpaces(_("Archive Name") . ':  ' . $this->mime_part->getName()) . "\n";
  59          $text .= Text::htmlAllSpaces(_("Archive File Size") . ': ' . strlen($contents) . ' bytes') . "\n";
  60          $text .= Text::htmlAllSpaces(($fileCount != 1) ? sprintf(_("File Count: %s files"), $fileCount) : sprintf(_("File Count: %s file"), $fileCount));
  61          $text .= "\n\n";
  62          $text .= Text::htmlAllSpaces(
  63                       str_pad(_("File Name"),     50, ' ', STR_PAD_RIGHT) .
  64                       str_pad(_("Attributes"),    10, ' ', STR_PAD_LEFT) .
  65                       str_pad(_("Size"),          10, ' ', STR_PAD_LEFT) .
  66                       str_pad(_("Modified Date"), 19, ' ', STR_PAD_LEFT) .
  67                       str_pad(_("Method"),        10, ' ', STR_PAD_LEFT) .
  68                       str_pad(_("Ratio"),          7, ' ', STR_PAD_LEFT)
  69                   ) . "\n";
  70  
  71          $text .= str_repeat('-', 106) . "\n";
  72  
  73          foreach ($rarData as $val) {
  74              $ratio = (empty($val['size'])) ? 0 : 100 * ($val['csize'] / $val['size']);
  75              $text .= Text::htmlAllSpaces(
  76                           str_pad($val['name'], 50, ' ', STR_PAD_RIGHT) .
  77                           str_pad($val['attr'], 10, ' ', STR_PAD_LEFT) .
  78                           str_pad($val['size'], 10, ' ', STR_PAD_LEFT) .
  79                           str_pad(strftime("%d-%b-%Y %H:%M", $val['date']), 19, ' ', STR_PAD_LEFT) .
  80                           str_pad($val['method'], 10, ' ', STR_PAD_LEFT) .
  81                           str_pad(sprintf("%1.1f%%", $ratio), 7, ' ', STR_PAD_LEFT)
  82                       ) . "\n";
  83          }
  84  
  85          $text .= str_repeat('-', 106) . "\n";
  86          $text .= '</span></tt></td></tr></table>';
  87  
  88          return nl2br($text);
  89      }
  90  
  91      /**
  92       * Returns the MIME type of this part.
  93       *
  94       * @return string  The MIME type of this part.
  95       */
  96      function getType()
  97      {
  98          return 'text/html; charset=' . NLS::getCharset();
  99      }
 100  
 101      /**
 102       * Checks to see if the data is a valid Rar archive.
 103       *
 104       * @param string &$data  The rar archive data.
 105       *
 106       * @return boolean  True if valid, false if invalid.
 107       */
 108      function checkRarData(&$data)
 109      {
 110          $fileHeader = "\x52\x61\x72\x21\x1a\x07\x00";
 111          if (strpos($data, $fileHeader) === false) {
 112              return false;
 113          } else {
 114              return true;
 115          }
 116      }
 117  
 118      /**
 119       * Get the list of files/data from the rar archive.
 120       *
 121       * @param string &$data  The rar archive data.
 122       *
 123       * @return array  KEY: Position in RAR archive
 124       *                VALUES: 'attr'    --  File attributes
 125       *                        'date'    --  File modification time
 126       *                        'csize'   --  Compressed file size
 127       *                        'method'  --  Compression method
 128       *                        'name'    --  Filename
 129       *                        'size'    --  Original file size
 130       */
 131      function getRarData(&$data)
 132      {
 133          $return_array = array();
 134  
 135          $blockStart = strpos($data, "\x52\x61\x72\x21\x1a\x07\x00");
 136          $position = $blockStart + 7;
 137  
 138          while ($position < strlen($data)) {
 139              $head_crc   = substr($data, $position + 0, 2);
 140              $head_type  = ord(substr($data, $position + 2, 1));
 141              $head_flags = unpack('vFlags', substr($data, $position + 3, 2));
 142              $head_flags = $head_flags['Flags'];
 143              $head_size  = unpack('vSize', substr($data, $position + 5, 2));
 144              $head_size  = $head_size['Size'];
 145  
 146              $position += 7;
 147              $head_size -= 7;
 148  
 149              switch ($head_type) {
 150  
 151              case 0x73:
 152                  /* Archive header */
 153                  $position += $head_size;
 154  
 155                  break;
 156  
 157              case 0x74:
 158                  $file = array();
 159  
 160                  /* File Header */
 161                  $info = unpack('VPacked/VUnpacked/COS/VCRC32/VTime/CVersion/CMethod/vLength/vAttrib', substr($data, $position));
 162  
 163                  $file['name'] = substr($data, $position + 25, $info['Length']);
 164                  $file['size'] = $info['Unpacked'];
 165                  $file['csize'] = $info['Packed'];
 166  
 167                  $file['date'] = mktime((($info['Time'] >> 11) & 0x1f),
 168                                         (($info['Time'] >> 5) & 0x3f),
 169                                         (($info['Time'] << 1) & 0x3e),
 170                                         (($info['Time'] >> 21) & 0x07),
 171                                         (($info['Time'] >> 16) & 0x1f),
 172                                         ((($info['Time'] >> 25) & 0x7f) + 80));
 173  
 174                  $file['method'] = $this->_methods[$info['Method']];
 175  
 176                  $file['attr']  = '';
 177                  $file['attr'] .= ($info['Attrib'] & 0x10) ? 'D' : '-';
 178                  $file['attr'] .= ($info['Attrib'] & 0x20) ? 'A' : '-';
 179                  $file['attr'] .= ($info['Attrib'] & 0x03) ? 'S' : '-';
 180                  $file['attr'] .= ($info['Attrib'] & 0x02) ? 'H' : '-';
 181                  $file['attr'] .= ($info['Attrib'] & 0x01) ? 'R' : '-';
 182  
 183                  $return_array[] = $file;
 184  
 185                  $position += $head_size;
 186                  $position += $info['Packed'];
 187                  break;
 188  
 189              default:
 190                  $position += $head_size;
 191                  if (isset($add_size)) {
 192                      $position += $add_size;
 193                  }
 194                  break;
 195  
 196              }
 197          }
 198  
 199          return $return_array;
 200      }
 201  
 202  }


Généré le : Sun Feb 25 18:01:28 2007 par Balluche grâce à PHPXref 0.7