File: Part.php

package info (click to toggle)
horde2 2.2.8-1sarge3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,832 kB
  • ctags: 2,897
  • sloc: php: 12,784; sh: 954; sql: 149; makefile: 104; perl: 97; xml: 24; pascal: 6
file content (233 lines) | stat: -rw-r--r-- 6,051 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/*
 * $Horde: horde/lib/MIME/Part.php,v 1.11.2.9 2003/01/03 12:48:24 jan Exp $
 *
 * Copyright 1999-2003 Chuck Hagenbuch <chuck@horde.org>
 *
 * See the enclosed file COPYING for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 */

/**
 * The MIME_Part:: class provides a wrapper around MIME parts and methods
 * for dealing with them.
 *
 * @author  Chuck Hagenbuch <chuck@horde.org>
 * @version $Revision: 1.11.2.9 $
 * @since   Horde 1.3
 * @package horde.mime
 */
class MIME_Part {

    /** The type (ex.: text) of this part. */
    var $type = 'application';

    /** The subtype (ex.: plain) of this part. */
    var $subtype = 'octet-stream';

    /** The body of the part. */
    var $contents = '';

    /** The transfer encoding of this part. */
    var $transferEncoding = '7bit';

    /** The description of this part. */
    var $description = '';

    /** The name of this part. */
    var $name = '';

    /** The character set of this part. */
    var $charset = '';

    /** The disposition of this part (inline or attachment). */
    var $disposition = 'attachment';


    /**
     * MIME_Part constructor.
     *
     * @param string $mimetype  The mimetype (ex.: 'text/plain') of the part.
     * @param string $contents  The body of the part.
     *
     * @return object MIME_Part The new MIME_Part object.
     */
    function MIME_Part($mimetype = null, $contents = null, $charset = null, $disposition = null)
    {
        if (!is_null($mimetype)) {
            $this->setType($mimetype);
        }
        if (!is_null($contents)) {
            $this->contents = $contents;
        }
        if (!is_null($charset)) {
            $this->charset = $charset;
        }
        if (!is_null($disposition)) {
            $this->disposition = $disposition;
        }
    }


    /**
     * Set the mimetype of this part.
     *
     * @param string $mimetype The mimetype to set (ex.: text/plain).
     */
    function setType($mimetype)
    {
        list($this->type, $this->subtype) = explode('/', strtolower($mimetype));
    }

    /*
     * @since Horde 2.1
     */
    function setDisposition($disposition)
    {
        $this->disposition = $disposition;
    }

    /**
     * Set the name of this part.
     *
     * @param string $name The name to set.
     */
    function setName($name)
    {
        $this->name = $name;
    }

    /**
     * Set the body of this part.
     *
     * @param string $contents The part body.
     */
    function setContents($contents)
    {
        $this->contents = $contents;
    }

    /**
     * Add to the body of this part.
     *
     * @param string $contents The content to append to the part body.
     */
    function appendContents($contents)
    {
        $this->contents .= $contents;
    }

    /**
     * Return the body of the part.
     *
     * @return string The body of the part.
     */
    function getContents()
    {
        return $this->contents;
    }

    /**
     * Get the full mimetype of this part
     *
     * @return string The mimetype of this part (ex.: text/plain)
     */
    function getType()
    {
        if (!isset($this->type) || !isset($this->subtype)) {
            return false;
        } else {
            return $this->type . '/' . $this->subtype;
        }
    }

    /**
     * Set the transfer encoding of the part.
     *
     * @param string $encoding The transfer-encoding to use.
     */
    function setTransferEncoding($encoding)
    {
        $this->transferEncoding = $encoding;
    }

    /**
     * Add the appropriate MIME headers for this part to an existing array.
     *
     * @param array $headers  An array of any other headers for the part.
     * @return array          The headers, with the MIME headers added.
     */
    function header($headers)
    {
        $headers['Content-Type'] = $this->getType();
        if (!empty($this->name))
            $headers['Content-Type'] .= '; name="' . $this->name . '"';

        $this->setEncoding();

        if (($this->type == 'text') && (!empty($this->charset))) {
            $headers['Content-Type'] .= '; charset=' . $this->charset;
        }
        $headers['Content-Transfer-Encoding'] = $this->transferEncoding;

        if ($this->name != '') {
            $headers['Content-Disposition'] = $this->disposition . '; filename="' . $this->name . '"';
        }
        if ($this->description != '') {
            $headers['Content-Description'] = $this->description;
        }

        return $headers;
    }

    /**
     * Return the entire part, including headers.
     *
     * @return string The entire MIME part.
     */
    function toString()
    {
        $part = '';
        $headers = $this->header(array());
        while (list($key, $val) = each($headers)) {
            $part .= $key . ': ' . $val . "\n";
        }

        return $part . "\n" . $this->getContents();
    }

    /**
     * Set the encoding of the message and the charset based on the
     * current language and browser capabilities.
     */
    function setEncoding()
    {
        $text = str_replace("\n", ' ', $this->contents);

        // FIXME: dependancy on IMAP module
        if ($this->type == 'text'
            && $this->transferEncoding != 'base64'
            && imap_8bit($text) != $text) {
            $this->transferEncoding = '8bit';

            global $language, $nls;

            if (empty($this->charset)) {
                if (!empty($nls['charsets'][$language])) {
                    $this->charset = $nls['charsets'][$language];
                } else if (!empty($_SERVER['HTTP_ACCEPT_CHARSET'])) {
                    $charsets = explode(',', $_SERVER['HTTP_ACCEPT_CHARSET']);
                    if (!empty($charsets[0])) {
                        $this->charset = trim($charsets[0]);
                    }
                }

                if (empty($this->charset)) {
                    $this->charset = Lang::getCharset();
                }
            }
        }
    }

}
?>