1: <?php
2: /**
3: * The IMP_Mime_Viewer_Zip class renders out the contents of ZIP files
4: * in HTML format and allows downloading of extractable files.
5: *
6: * Copyright 2002-2012 Horde LLC (http://www.horde.org/)
7: *
8: * See the enclosed file COPYING for license information (GPL). If you
9: * did not receive this file, see http://www.horde.org/licenses/gpl.
10: *
11: * @author Mike Cochrane <mike@graftonhall.co.nz>
12: * @author Michael Slusarz <slusarz@horde.org>
13: * @category Horde
14: * @license http://www.horde.org/licenses/gpl GPL
15: * @package IMP
16: */
17: class IMP_Mime_Viewer_Zip extends Horde_Mime_Viewer_Zip
18: {
19: /**
20: * Return the full rendered version of the Horde_Mime_Part object.
21: *
22: * URL parameters used by this function:
23: * <pre>
24: * 'zip_attachment' - (integer) The ZIP attachment to download.
25: * </pre>
26: *
27: * @return array See parent::render().
28: * @throws Horde_Exception
29: */
30: protected function _render()
31: {
32: if (!($zip_atc = Horde_Util::getFormData('zip_attachment'))) {
33: return array();
34: }
35:
36: /* Send the requested file. Its position in the zip archive is located
37: * in 'zip_attachment'. */
38: $data = $this->_mimepart->getContents();
39: $fileKey = $zip_atc - 1;
40:
41: if (!($zip = $this->getConfigParam('zip'))) {
42: $zip = Horde_Compress::factory('Zip');
43: $this->setConfigParam('zip', $zip);
44: }
45: $zipInfo = $zip->decompress($data, array(
46: 'action' => Horde_Compress_Zip::ZIP_LIST
47: ));
48:
49: /* Verify that the requested file exists. */
50: if (isset($zipInfo[$fileKey])) {
51: $text = $zip->decompress($data, array(
52: 'action' => Horde_Compress_Zip::ZIP_DATA,
53: 'info' => $zipInfo,
54: 'key' => $fileKey
55: ));
56: if (!empty($text)) {
57: return array(
58: $this->_mimepart->getMimeId() => array(
59: 'data' => $text,
60: 'name' => basename($zipInfo[$fileKey]['name']),
61: 'type' => 'application/octet-stream'
62: )
63: );
64: }
65: }
66:
67: // TODO: Error reporting
68: return array();
69: }
70:
71: /**
72: * Return the rendered information about the Horde_Mime_Part object.
73: *
74: * @return array See Horde_Mime_Viewer_Driver::render().
75: */
76: protected function _renderInfo()
77: {
78: $this->_callback = array($this, '_IMPcallback');
79: return parent::_renderInfo();
80: }
81:
82: /**
83: * The function to use as a callback to _renderInfo().
84: *
85: * @param integer $key The position of the file in the zip archive.
86: * @param array $val The information array for the archived file.
87: *
88: * @return string The content-type of the output.
89: */
90: protected function _IMPcallback($key, $val)
91: {
92: $name = preg_replace('/( )+$/', '', $val['name']);
93:
94: if (!empty($val['size']) && (strstr($val['attr'], 'D') === false) &&
95: ((($val['method'] == 0x8) && Horde_Util::extensionExists('zlib')) ||
96: ($val['method'] == 0x0))) {
97: $mime_part = clone $this->_mimepart;
98: $mime_part->setName(basename($name));
99: $val['name'] = str_replace($name, $this->getConfigParam('imp_contents')->linkView($mime_part, 'download_render', $name, array('jstext' => sprintf(_("View %s"), str_replace(' ', ' ', $name)), 'class' => 'fixed', 'params' => array('zip_attachment' => urlencode($key) + 1))), $val['name']);
100: }
101:
102: return $val;
103: }
104:
105: }
106: