5 +-----------------------------------------------------------------------+
6 | program/include/rcube_mime_struct.php |
8 | This file is part of the Roundcube Webmail client |
9 | Copyright (C) 2005-2010, Roundcube Dev. - Switzerland |
10 | Licensed under the GNU GPL |
13 | Provide functions for handling mime messages structure |
15 | Based on Iloha MIME Library. See http://ilohamail.org/ for details |
17 +-----------------------------------------------------------------------+
18 | Author: Aleksander Machniak <alec@alec.pl> |
19 | Author: Ryo Chijiiwa <Ryo@IlohaMail.org> |
20 +-----------------------------------------------------------------------+
27 * Helper class to process IMAP's BODYSTRUCTURE string
30 * @author Aleksander Machniak <alec@alec.pl>
32 class rcube_mime_struct
37 function __construct($str=null)
40 $this->structure = $this->parseStructure($str);
44 * Parses IMAP's BODYSTRUCTURE string into array
46 function parseStructure($str)
48 $line = substr($str, 1, strlen($str) - 2);
49 $line = str_replace(')(', ') (', $line);
51 $struct = self::parseBSString($line);
52 if (!is_array($struct[0]) && (strcasecmp($struct[0], 'message') == 0)
53 && (strcasecmp($struct[1], 'rfc822') == 0)) {
54 $struct = array($struct);
61 * Parses IMAP's BODYSTRUCTURE string into array and loads it into class internal variable
63 function loadStructure($str)
68 $this->structure = $this->parseStructure($str);
69 return (!empty($this->structure));
72 function getPartType($part)
74 $part_a = $this->getPartArray($this->structure, $part);
75 if (!empty($part_a)) {
76 if (is_array($part_a[0]))
85 function getPartEncoding($part)
87 $part_a = $this->getPartArray($this->structure, $part);
89 if (!is_array($part_a[0]))
96 function getPartCharset($part)
98 $part_a = $this->getPartArray($this->structure, $part);
100 if (is_array($part_a[0]))
103 if (is_array($part_a[2])) {
105 while (list($key, $val) = each($part_a[2]))
106 if (strcasecmp($val, 'charset') == 0)
107 return $part_a[2][$key+1];
115 function getPartArray($a, $part)
120 if (strpos($part, '.') > 0) {
121 $original_part = $part;
122 $pos = strpos($part, '.');
123 $rest = substr($original_part, $pos+1);
124 $part = substr($original_part, 0, $pos);
125 if ((strcasecmp($a[0], 'message') == 0) && (strcasecmp($a[1], 'rfc822') == 0)) {
128 return self::getPartArray($a[$part-1], $rest);
131 if (!is_array($a[0]) && (strcasecmp($a[0], 'message') == 0)
132 && (strcasecmp($a[1], 'rfc822') == 0)) {
135 if (is_array($a[$part-1]))
140 else if (($part==0) || (empty($part))) {
145 private function closingParenPos($str, $start)
151 for ($i=$start; $i<$len; $i++) {
152 if ($str[$i] == '"' && $str[$i-1] != "\\") {
153 $in_quote = ($in_quote + 1) % 2;
158 else if (($level > 0) && ($str[$i] == ')'))
160 else if (($level == 0) && ($str[$i] == ')'))
167 * Parses IMAP's BODYSTRUCTURE string into array
169 private function parseBSString($str)
176 for ($i=0; $i<$len; $i++) {
177 if ($str[$i] == '"') {
178 $in_quote = ($in_quote + 1) % 2;
179 } else if (!$in_quote) {
180 // space means new element
181 if ($str[$i] == ' ') {
183 // skip additional spaces
184 while ($str[$i+1] == ' ')
187 } else if ($str[$i] == '(') {
189 $endPos = self::closingParenPos($str, $i);
190 $partLen = $endPos - $i;
193 $part = substr($str, $i, $partLen);
194 $a[$id] = self::parseBSString($part); // send part string
197 $a[$id] .= $str[$i]; //add to current element in array
198 } else if ($in_quote) {
199 if ($str[$i] == "\\") {
200 $i++; // escape backslashes
201 if ($str[$i] == '"' || $str[$i] == "\\")
205 $a[$id] .= $str[$i]; //add to current element in array