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
|
<?php
/*
* $Horde: horde/lib/MIME/Structure.php,v 1.15.2.7 2003/01/03 12:48:25 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.
*/
/** @constant MIME_DEFAULT_CHARSET The character set to use if none is specified. */
define('MIME_DEFAULT_CHARSET', 'us-ascii');
/** @constant MIME_DEFAULT_ENCODING The encoding to use if none is specified. */
define('MIME_DEFAULT_ENCODING', ENC7BIT);
/** @constant MIME_DEFAULT_DESCRIPTION The description to use for parts with none set. */
define('MIME_DEFAULT_DESCRIPTION', 'unnamed');
/**
* The MIME_Structure:: class provides methods for dealing with MIME mail.
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @version $Revision: 1.15.2.7 $
* @since Horde 1.3
* @package horde.mime
*/
class MIME_Structure {
/**
* Given the results of imap_fetchstructure(), parse the structure
* of the message, figuring out correct bodypart numbers, etc.
*
* @access public
* @author justin <justinc@andrew.cmu.edu>
*
* @param object stdClass $body The result of imap_fetchstructure().
* @param int $index The IMAP UID of the message being parsed.
* @param array &$MimeID An array of Mime IDs to write any CIDs found to.
*
* @return array An array containing all parts of the message.
*/
function parse($body, $index, &$MimeID)
{
if (!is_array($MimeID)) $MimeID = array();
$attachments = array();
return MIME_Structure::_parse($body, $index, $MimeID, $attachments);
}
/**
* Given the results of imap_fetchstructure(), parse the structure
* of the message, figuring out correct bodypart numbers, etc.
* @access private
*
* @param object stdClass $body The result of imap_fetchstructure().
* @param int $index The IMAP UID of the message being parsed.
* @param array &$MimeID An array of Mime IDs to write any CIDs found to.
* @param array &$attachments The array of attachments that is being built up.
* @param string $ref The current bodypart.
* @param string $alternative Whether or not the part is one of several alternatives.
*
* @return array An array containing all parts of the message
* that have been parsed so far.
*/
function _parse($body, $index, &$MimeID, &$attachments, $ref = '', $alternative = '')
{
if (!strlen($ref)) { // top multiparts don't get their own line
$ref = (isset($body->type) && $body->type == TYPEMULTIPART) ? '' : 1;
}
if (isset($body->subtype) && $body->subtype == 'RFC822') {
$href = "$ref.0";
$attachments[$href] = new stdClass;
$attachments[$href]->part = true;
$attachments[$href]->alternative = $alternative;
$attachments[$href]->header = true;
$attachments[$href]->header_imap_id = $href;
$attachments[$href]->index = $index;
}
if (!empty($body->bytes) &&
($body->subtype != 'MIXED') &&
($body->subtype != 'ALTERNATIVE')) {
$attachments[$ref] = MIME_Structure::setInfo($body, $MimeID, $ref);
$attachments[$ref]->alternative = $alternative;
$attachments[$ref]->header = false;
$attachments[$ref]->imap_id = $ref;
$attachments[$ref]->index = $index;
}
if (isset($body->parts)) {
$alternative_id = (isset($body->subtype) && strtolower($body->subtype) == 'alternative') ? (strlen($ref) ? $ref : '-') : '';
$parts = $body->parts;
$i = 0;
foreach ($parts as $sub_part) {
if (isset($body->type) && ($body->type == TYPEMESSAGE) && isset($sub_part->type) && ($sub_part->type == TYPEMULTIPART)) {
$sub_ref = $ref;
} else {
$sub_ref = (!strlen($ref)) ? '' . ($i + 1) : $ref . '.' . ($i + 1);
}
MIME_Structure::_parse($sub_part, $index, $MimeID, $attachments, $sub_ref, $alternative_id);
$i++;
}
}
return $attachments;
}
/**
* Given a mime part from imap_fetchstructure(), munge it into a
* useful form and make sure that any parameters which are missing
* are given default values.
* @access private
*
* @param object stdClass $part The original part info.
* @param array &$MimeID An array of Mime IDs to write this part's CID to.
* @param string $ref The number of this part.
*
* @return object stdClass The populated object.
*/
function setInfo($part, &$MimeID, $ref)
{
global $mime_types, $mime_actions;
$object = new stdClass;
$object->ref = $ref;
$object->type = isset($part->type) ? $part->type : TYPETEXT;
$object->subtype = ($part->ifsubtype) ? strtolower($part->subtype) : 'x-unknown';
$object->encoding = isset($part->encoding) ? $part->encoding : MIME_DEFAULT_ENCODING;
$object->bytes = isset($part->bytes) ? $part->bytes : '?';
$localeinfo = localeconv();
$object->size = ($object->bytes != '?') ? number_format($object->bytes/1024, 2, $localeinfo['decimal_point'], '') : '?';
$object->disposition = $part->ifdisposition ? strtolower($part->disposition) : 'inline';
if ($part->ifid) {
$object->id = $part->id;
$MimeID[$ref] = $object->id;
} else {
$object->id = false;
}
// Set default value of charset
$object->charset = MIME_DEFAULT_CHARSET;
// go through the parameters, if any
if ($part->ifparameters) {
while (list(,$param) = each($part->parameters)) {
$field = strtolower($param->attribute);
if ($field == 'type') {
if (isset($mime_types[strtolower($param->value)])) {
$object->type = $mime_types[strtolower($param->value)];
}
} else {
$object->$field = $param->value;
}
}
}
// go through the disposition parameters, if any
if ($part->ifdparameters) {
while (list(,$param) = each($part->dparameters)) {
$field = strtolower($param->attribute);
$object->$field = $param->value;
}
}
// make sure a name is set
if (empty($object->name) && !empty($object->filename)) {
$object->name = $object->filename;
}
// make sure there's a description
if (!empty($part->description)) {
$object->description = $part->description;
} else {
if (!empty($object->name)) {
$object->description = $object->name;
} else {
$object->description = MIME_DEFAULT_DESCRIPTION;
}
}
if (empty($object->name)) $object->name = preg_replace('|\W|', '_', $object->description);
$object->TYPE = isset($mime_types[$object->type]) ? $mime_types[$object->type] : $mime_types[TYPETEXT];
return $object;
}
}
?>
|