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