File: view.php

package info (click to toggle)
imp4 4.1.3-4
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 14,988 kB
  • ctags: 3,720
  • sloc: xml: 17,038; php: 16,350; makefile: 64
file content (178 lines) | stat: -rw-r--r-- 6,452 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
 * This script displays a rendered MIME_Part object.
 * The following are potential URL parameters that we should honor:
 *   'actionID' -- The action ID to perform
 *     -> 'compose_attach_preview'
 *     -> 'download_all'
 *     -> 'download_attach'
 *     -> 'download_render'
 *     -> 'save_message'
 *     -> 'view_attach'
 *     -> 'view_source'
 *   'ctype'    -- The content-type to use instead of the content-type
 *                 found in the original MIME_Part object
 *   'id'       -- The MIME part to display
 *   'index'    -- The index of the message; only used for IMP_Contents
 *                 objects
 *   'zip'      -- Download in .zip format?
 *
 * $Horde: imp/view.php,v 2.199.4.10 2006/03/21 01:01:01 jan Exp $
 *
 * Copyright 1999-2006 Charles J. Hagenbuch <chuck@horde.org>
 * Copyright 1999-2006 Jon Parise <jon@horde.org>
 * Copyright 2002-2006 Michael Slusarz <slusarz@bigworm.colorado.edu>
 *
 * See the enclosed file COPYING for license information (GPL).  If you
 * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
 */

/* We can't register this session as 'readonly' if we are doing a
 * 'view_attach' since some MIME_Drivers may need to create temporary cache
 * files to correctly generate links. */
if (isset($_GET['actionID']) && $_GET['actionID'] != 'view_attach') {
    $session_control = 'readonly';
}

$no_compress = true;
@define('IMP_BASE', dirname(__FILE__));
require_once IMP_BASE . '/lib/base.php';
require_once IMP_BASE . '/lib/MIME/Contents.php';

$index = Util::getFormData('index');
$id = Util::getFormData('id');
$actionID = Util::getFormData('actionID');

/* 'compose_attach_preview' doesn't use IMP_Contents since there is no
 * IMAP message data - rather, we must use the IMP_Compose object to
 * get the necessary MIME_Part. */
if ($actionID == 'compose_attach_preview') {
    /* Initialize the IMP_Compose:: object. */
    require_once IMP_BASE . '/lib/Compose.php';
    $imp_compose = &new IMP_Compose(array('cacheID' => Util::getFormData('messageCache')));
    $mime = $imp_compose->buildAttachment($id);

    /* Create a dummy MIME_Contents() object so we can use the view
     * code below.  Then use the 'view_attach' handler to output to
     * the user. */
    $contents = &new IMP_Contents(new MIME_Message());
    $actionID = 'view_attach';
} else {
    /* Prevent blind fetching of attachments without knowing the MIME ID.
     * Index *can* be empty (think embedded MIME parts - there is no
     * corresponding message index) - but see below; id can be part 0 (whole
     * message) so just make sure that it's specified. */
    if (!in_array($actionID, array('save_message', 'download_all'))
        && is_null($id)) {
        exit;
    }

    /* Get cached item, if available. */
    if (!($contents = &IMP_Contents::getCache())) {
        /* If we make it to here without an index, then something is broken
         * since there is nothing in the cache and we have no way to create
         * a viewable object. */
        if (empty($index)) {
            exit;
        }
        $contents = &IMP_Contents::singleton($index . IMP_IDX_SEP . $_SESSION['imp']['thismailbox']);
    }

    if (!in_array($actionID, array('download_attach', 'download_all', 'save_message', 'view_source'))) {
        $mime = $contents->getDecodedMIMEPart($id);
        if (($ctype = Util::getFormData('ctype'))) {
            $mime->setType($ctype);
        }
    }
}

/* Run through action handlers */
switch ($actionID) {
case 'download_all':
    $tosave = array();
    $headers = &$contents->getHeaderOb();
    $headers->buildHeaders();
    $zipfile = trim(preg_replace('/[^\w-+_\. ]/', '_', $headers->getValue('subject')), ' _');
    if (empty($zipfile)) {
        $zipfile = _("attachments.zip");
    } else {
        $zipfile .= '.zip';
    }
    foreach ($contents->getDownloadAllList() as $val) {
        $mime = $contents->getDecodedMIMEPart($val);
        $tosave[] = array('data' => $mime->getContents(), 'name' => $mime->getName(true, true));
    }
    if (is_a($contents, 'PEAR_Error')) {
        Horde::fatal($contents, __FILE__, __LINE__);
    }

    require_once 'Horde/Compress.php';
    $horde_compress = &Horde_Compress::singleton('zip');
    $body = $horde_compress->compress($tosave);
    $browser->downloadHeaders($zipfile, 'application/zip', false, strlen($body));
    echo $body;
    exit;

case 'download_attach':
case 'download_render':
    switch ($actionID) {
    case 'download_attach':
        /* Make sure we get the entire contents of the part. */
        $mime = $contents->getDecodedMIMEPart($id, true);
        $body = $mime->getContents();
        $type = $mime->getType(true);
        break;

    case 'download_render':
        $body = $contents->renderMIMEPart($mime);
        $type = $contents->getMIMEViewerType($mime);
        break;
    }

    $name = $mime->getName(true, true);

    /* Compress output? */
    if (($actionID == 'download_attach') && Util::getFormData('zip')) {
        require_once 'Horde/Compress.php';
        $horde_compress = &Horde_Compress::singleton('zip');
        $body = $horde_compress->compress(array(array('data' => $body, 'name' => $name)));
        $name .= '.zip';
        $type = 'application/zip';
    }
    $browser->downloadHeaders($name, $type, false, strlen($body));
    echo $body;
    exit;

case 'view_attach':
    $body = $contents->renderMIMEPart($mime);
    $type = $contents->getMIMEViewerType($mime);
    $browser->downloadHeaders($mime->getName(true, true), $type, true, strlen($body));
    echo $body;
    exit;

case 'view_source':
    $msg = $contents->fullMessageText();
    $browser->downloadHeaders('Message Source', 'text/plain', true, strlen($msg));
    echo $msg;
    exit;

case 'save_message':
    require_once IMP_BASE . '/lib/MIME/Headers.php';
    $imp_headers = &new IMP_Headers($index);

    $name = 'saved_message';
    if (($subject = $imp_headers->getOb('subject', true))) {
        $name = trim(preg_replace('/[^\w-+_\. ]/', '_', $subject), ' _');
    }

    if (!($from = $imp_headers->getFromAddress())) {
        $from = '<>';
    }
    $date = strftime('%a %b %d %H:%M:%S %Y', $imp_headers->getOb('udate'));
    $body = 'From ' . $from . ' ' . $date . "\n" . $contents->fullMessageText();
    $body = str_replace("\r\n", "\n", $body);

    $browser->downloadHeaders($name . '.eml', 'message/rfc822', false, strlen($body));
    echo $body;
    exit;
}