IMP_Contents::getBodyPart PHP Method

getBodyPart() public method

Gets the raw text for one section of the message.
public getBodyPart ( integer $id, array $options = [] ) : object
$id integer The ID of the MIME part.
$options array Additional options: - decode: (boolean) Attempt to decode the bodypart on the remote server. DEFAULT: No - length: (integer) If set, only download this many bytes of the bodypart from the server. DEFAULT: All data is retrieved. - mimeheaders: (boolean) Include the MIME headers also? DEFAULT: No - stream: (boolean) If true, return a stream. DEFAULT: No
return object Object with the following properties: - data: (mixed) The text of the part or a stream resource if 'stream' option is true. - decode: (string) If 'decode' option is true, and bodypart decoded on server, the content-type of the decoded data.
    public function getBodyPart($id, $options = array())
    {
        $ret = new stdClass();
        $ret->data = '';
        $ret->decode = null;
        if (empty($id)) {
            return $ret;
        }
        if (!$this->_indices || $this->isEmbedded($id)) {
            if (empty($options['mimeheaders']) || in_array($id, $this->_embedded)) {
                $ob = $this->getMimePart($id, array('nocontents' => true));
                if (empty($options['stream'])) {
                    if (!is_null($ob)) {
                        $ret->data = $ob->getContents();
                    }
                } else {
                    $ret->data = is_null($ob) ? fopen('php://temp', 'r+') : $ob->getContents(array('stream' => true));
                }
                return $ret;
            }
            $base_id = new Horde_Mime_Id($id);
            while (!in_array($base_id->id, $this->_embedded, true)) {
                $base_id->id = $base_id->idArithmetic($base_id::ID_UP);
                if (is_null($base_id->id)) {
                    return $ret;
                }
            }
            $body = '';
            $part = $this->getMimePart($base_id->id, array('nocontents' => true));
            if ($part) {
                $txt = $part->addMimeHeaders()->toString() . "\n" . $part->getContents();
                try {
                    $body = Horde_Mime_Part::getRawPartText($txt, 'header', '1') . "\n\n" . Horde_Mime_Part::getRawPartText($txt, 'body', '1');
                } catch (Horde_Mime_Exception $e) {
                }
            }
            if (empty($options['stream'])) {
                $ret->data = $body;
                return $ret;
            }
            $ret->data = fopen('php://temp', 'r+');
            if (strlen($body)) {
                fwrite($ret->data, $body);
                fseek($ret->data, 0);
            }
            return $ret;
        }
        $query = new Horde_Imap_Client_Fetch_Query();
        if (substr($id, -2) === '.0') {
            $rfc822 = true;
            $id = substr($id, 0, -2);
            $options['mimeheaders'] = true;
        } else {
            $rfc822 = false;
        }
        if (!isset($options['length']) || !empty($options['length'])) {
            $bodypart_params = array('decode' => !empty($options['decode']), 'peek' => true);
            if (isset($options['length'])) {
                $bodypart_params['start'] = 0;
                $bodypart_params['length'] = $options['length'];
            }
            if ($rfc822) {
                $bodypart_params['id'] = $id;
                $query->bodyText($bodypart_params);
            } else {
                $query->bodyPart($id, $bodypart_params);
            }
        }
        if (!empty($options['mimeheaders'])) {
            if ($rfc822) {
                $query->headerText(array('id' => $id, 'peek' => true));
            } else {
                $query->mimeHeader($id, array('peek' => true));
            }
        }
        if ($res = $this->_fetchData($query)) {
            try {
                if (empty($options['mimeheaders'])) {
                    $ret->decode = $res->getBodyPartDecode($id);
                    $ret->data = $rfc822 ? $res->getBodyText($id, !empty($options['stream'])) : $res->getBodyPart($id, !empty($options['stream']));
                    return $ret;
                } elseif (empty($options['stream'])) {
                    $ret->data = $rfc822 ? $res->getHeaderText($id) . $res->getBodyText($id) : $res->getMimeHeader($id) . $res->getBodyPart($id);
                    return $ret;
                }
                if ($rfc822) {
                    $data = array($res->getHeaderText($id, Horde_Imap_Client_Data_Fetch::HEADER_STREAM), $res->getBodyText($id, true));
                } else {
                    $data = array($res->getMimeHeader($id, Horde_Imap_Client_Data_Fetch::HEADER_STREAM), $res->getBodyPart($id, true));
                }
                $ret->data = Horde_Stream_Wrapper_Combine::getStream($data);
                return $ret;
            } catch (Horde_Exception $e) {
            }
        }
        if (!empty($options['stream'])) {
            $ret->data = fopen('php://temp', 'r+');
        }
        return $ret;
    }