~ubuntu-branches/ubuntu/saucy/horde3/saucy

« back to all changes in this revision

Viewing changes to lib/Horde/MIME/Viewer/rar.php

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2005-05-04 23:08:08 UTC
  • Revision ID: james.westby@ubuntu.com-20050504230808-p4hf3hk28o3v7wir
Tags: upstream-3.0.4
ImportĀ upstreamĀ versionĀ 3.0.4

Show diffs side-by-side

added added

removed removed

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