+ /**
+ * Returns BODYSTRUCTURE for the specified message.
+ *
+ * @param string $mailbox Folder name
+ * @param int $id Message sequence number or UID
+ * @param bool $is_uid True if $id is an UID
+ *
+ * @return array/bool Body structure array or False on error.
+ * @since 0.6
+ */
+ function getStructure($mailbox, $id, $is_uid = false)
+ {
+ $result = $this->fetch($mailbox, $id, $is_uid, array('BODYSTRUCTURE'));
+ if (is_array($result)) {
+ $result = array_shift($result);
+ return $result->bodystructure;
+ }
+ return false;
+ }
+
+ /**
+ * Returns data of a message part according to specified structure.
+ *
+ * @param array $structure Message structure (getStructure() result)
+ * @param string $part Message part identifier
+ *
+ * @return array Part data as hash array (type, encoding, charset, size)
+ */
+ static function getStructurePartData($structure, $part)
+ {
+ $part_a = self::getStructurePartArray($structure, $part);
+ $data = array();
+
+ if (empty($part_a)) {
+ return $data;
+ }
+
+ // content-type
+ if (is_array($part_a[0])) {
+ $data['type'] = 'multipart';
+ }
+ else {
+ $data['type'] = strtolower($part_a[0]);
+
+ // encoding
+ $data['encoding'] = strtolower($part_a[5]);
+
+ // charset
+ if (is_array($part_a[2])) {
+ while (list($key, $val) = each($part_a[2])) {
+ if (strcasecmp($val, 'charset') == 0) {
+ $data['charset'] = $part_a[2][$key+1];
+ break;
+ }
+ }
+ }
+ }
+
+ // size
+ $data['size'] = intval($part_a[6]);
+
+ return $data;
+ }
+
+ static function getStructurePartArray($a, $part)
+ {
+ if (!is_array($a)) {
+ return false;
+ }
+ if (strpos($part, '.') > 0) {
+ $original_part = $part;
+ $pos = strpos($part, '.');
+ $rest = substr($original_part, $pos+1);
+ $part = substr($original_part, 0, $pos);
+ if ((strcasecmp($a[0], 'message') == 0) && (strcasecmp($a[1], 'rfc822') == 0)) {
+ $a = $a[8];
+ }
+ return self::getStructurePartArray($a[$part-1], $rest);
+ }
+ else if ($part>0) {
+ if (!is_array($a[0]) && (strcasecmp($a[0], 'message') == 0)
+ && (strcasecmp($a[1], 'rfc822') == 0)) {
+ $a = $a[8];
+ }
+ if (is_array($a[$part-1]))
+ return $a[$part-1];
+ else
+ return $a;
+ }
+ else if (($part == 0) || (empty($part))) {
+ return $a;
+ }
+ }
+