1 | <?php |
---|
2 | class imap_attachment |
---|
3 | { |
---|
4 | function flattenParts($messageParts, $flattenedParts = array(), $prefix = '', $index = 1, $fullPrefix = true) { |
---|
5 | |
---|
6 | foreach($messageParts as $part) { |
---|
7 | $flattenedParts[$prefix.$index] = $part; |
---|
8 | if(isset($part->parts)) { |
---|
9 | if($part->type == 2) { |
---|
10 | $flattenedParts = $this->flattenParts($part->parts, $flattenedParts, $prefix.$index.'.', 0, false); |
---|
11 | } |
---|
12 | elseif($fullPrefix) { |
---|
13 | $flattenedParts = $this->flattenParts($part->parts, $flattenedParts, $prefix.$index.'.'); |
---|
14 | } |
---|
15 | else { |
---|
16 | $flattenedParts = $this->flattenParts($part->parts, $flattenedParts, $prefix); |
---|
17 | } |
---|
18 | unset($flattenedParts[$prefix.$index]->parts); |
---|
19 | } |
---|
20 | $index++; |
---|
21 | } |
---|
22 | |
---|
23 | return $flattenedParts; |
---|
24 | |
---|
25 | } |
---|
26 | |
---|
27 | function get_attachment_info($mbox_stream, $msg_number) |
---|
28 | { |
---|
29 | $structure = imap_fetchstructure($mbox_stream,$msg_number,FT_UID); |
---|
30 | $aaa = $this->flattenParts($structure->parts); |
---|
31 | $contentParts = count($structure->parts); |
---|
32 | |
---|
33 | $msg_info['number_attachments'] = $contentParts -1; |
---|
34 | |
---|
35 | if($contentParts > 1) |
---|
36 | { |
---|
37 | for($i=1; $i<$contentParts; $i++) |
---|
38 | { |
---|
39 | $msg_info['attachment'][$i]['part_in_msg'] = ($i); |
---|
40 | $msg_info['attachment'][$i]['name'] = urlencode($structure->parts[$i]->dparameters[0]->value); |
---|
41 | $msg_info['attachment'][$i]['type'] = $structure->parts[$i]->subtype; |
---|
42 | $msg_info['attachment'][$i]['bytes'] = $structure->parts[$i]->bytes; |
---|
43 | } |
---|
44 | } |
---|
45 | return $msg_info; |
---|
46 | } |
---|
47 | |
---|
48 | function get_attachment_headerinfo($mbox, $msgno) |
---|
49 | { |
---|
50 | include_once("class.message_components.inc.php"); |
---|
51 | |
---|
52 | $msg =& new message_components($mbox); |
---|
53 | $msg->fetch_structure($msgno); |
---|
54 | |
---|
55 | $msg_info = array(); |
---|
56 | $msg_info['names'] = ''; |
---|
57 | |
---|
58 | $msg_info['number_attachments'] = count($msg->fname[$msgno]); |
---|
59 | |
---|
60 | if ($msg_info['number_attachments']) |
---|
61 | { |
---|
62 | foreach ($msg->fname[$msgno] as $fname) |
---|
63 | { |
---|
64 | $msg_info['names'] .= $this->flat_mime_decode($fname) . ', '; |
---|
65 | } |
---|
66 | } |
---|
67 | $msg_info['names'] = substr($msg_info['names'],0,(strlen($msg_info['names']) - 2)); |
---|
68 | |
---|
69 | return $msg_info; |
---|
70 | } |
---|
71 | |
---|
72 | function download_attachment($mbox, $msgno) |
---|
73 | { |
---|
74 | include_once("class.message_components.inc.php"); |
---|
75 | |
---|
76 | $msg =& new message_components($mbox); |
---|
77 | $msg->fetch_structure($msgno); |
---|
78 | $array_parts_attachments = array(); |
---|
79 | //$array_parts_attachments['names'] = ''; |
---|
80 | |
---|
81 | //print_r($msg->fname[$msgno]); |
---|
82 | |
---|
83 | if (count($msg->fname[$msgno]) > 0) |
---|
84 | { |
---|
85 | $i = 0; |
---|
86 | foreach ($msg->fname[$msgno] as $index=>$fname) |
---|
87 | { |
---|
88 | $array_parts_attachments[$i]['pid'] = $msg->pid[$msgno][$index]; |
---|
89 | $array_parts_attachments[$i]['name'] = $this->decode_mimeheader($fname); |
---|
90 | //$array_parts_attachments[$i]['name'] = $this->flat_mime_decode($fname); |
---|
91 | $array_parts_attachments[$i]['name'] = $array_parts_attachments[$i]['name'] ? $array_parts_attachments[$i]['name'] : "attachment.bin"; |
---|
92 | $array_parts_attachments[$i]['encoding'] = $msg->encoding[$msgno][$index]; |
---|
93 | //$array_parts_attachments['names'] .= $array_parts_attachments[$i]['name'] . ', '; |
---|
94 | $array_parts_attachments[$i]['fsize'] = $msg->fsize[$msgno][$index]; |
---|
95 | $i++; |
---|
96 | } |
---|
97 | } |
---|
98 | //$array_parts_attachments['names'] = substr($array_parts_attachments['names'],0,(strlen($array_parts_attachments['names']) - 2)); |
---|
99 | return $array_parts_attachments; |
---|
100 | } |
---|
101 | |
---|
102 | function decode_mimeheader($string) { |
---|
103 | return mb_decode_mimeheader($string); |
---|
104 | } |
---|
105 | function flat_mime_decode($string) { |
---|
106 | $array = imap_mime_header_decode($string); |
---|
107 | $str = ""; |
---|
108 | foreach ($array as $key => $part) |
---|
109 | $str .= @eregi_replace("\{", "[",@eregi_replace("\}", "]",$part->text)); |
---|
110 | |
---|
111 | return $str; |
---|
112 | } |
---|
113 | } |
---|