/** * @dataProvider isChildProvider */ public function testIsChild($base, $id, $expected) { $id_ob = new Horde_Mime_Id($base); if ($expected) { $this->assertTrue($id_ob->isChild($id)); } else { $this->assertFalse($id_ob->isChild($id)); } }
/** * Determines if a given MIME part ID is a part of embedded data. * * @param string $mime_id The MIME ID. * * @return boolean True if the MIME ID is part of embedded data. */ public function isEmbedded($mime_id) { foreach ($this->_embedded as $val) { if ($mime_id == $val || ($id_ob = new Horde_Mime_Id($val)) && $id_ob->isChild($mime_id)) { return true; } } return false; }
/** * Render out the currently set contents. * * @param boolean $inline Are we viewing inline? * * @return array See self::render(). */ protected function _IMPrender($inline) { $related_id = $this->_mimepart->getMimeId(); $used = array($related_id); if (!($id = $this->_init($inline))) { return array(); } $render = $this->getConfigParam('imp_contents')->renderMIMEPart($id, $inline ? IMP_Contents::RENDER_INLINE : IMP_Contents::RENDER_FULL); if (!$inline) { foreach (array_keys($render) as $key) { if (!is_null($render[$key])) { return array($related_id => $render[$key]); } } return null; } $data_id = null; $ret = array(); foreach ($this->_mimepart->partIterator(false) as $val) { $ret[$val->getMimeId()] = null; } foreach (array_keys($render) as $val) { $ret[$val] = $render[$val]; if ($ret[$val]) { $data_id = $val; } } if (!is_null($data_id)) { $this->_mimepart->setMetadata('viewable_part', $data_id); /* We want the inline display to show multipart/related vs. the * viewable MIME part. This is because a multipart/related part * is not downloadable and clicking on the MIME part may not * produce the desired result in the full display (i.e. HTML parts * with related images). */ if (strcmp($data_id, $related_id) !== 0) { $ret[$related_id] = $ret[$data_id]; $ret[$data_id] = null; } } /* Fix for broken messages that don't refer to a related CID part * within the base part. */ if ($cids_used = $this->_mimepart->getMetadata('related_cids_used')) { $used = array_merge($used, $cids_used); } $id_ob = new Horde_Mime_Id($id); foreach (array_diff(array_keys($ret), $used) as $val) { if (strcmp($val, $id) !== 0 && !$id_ob->isChild($val)) { $summary = $this->getConfigParam('imp_contents')->getSummary($val, IMP_Contents::SUMMARY_SIZE | IMP_Contents::SUMMARY_ICON | IMP_Contents::SUMMARY_DESCRIP_LINK | IMP_Contents::SUMMARY_DOWNLOAD); $status = new IMP_Mime_Status_RenderIssue($this->_mimepart, array(_("This part contains an attachment that can not be displayed within this part:"), implode(' ', array($summary['icon'], $summary['description'], $summary['size'], $summary['download'])))); $status->action($status::WARNING); if (isset($ret[$related_id]['status'])) { if (!is_array($ret[$related_id]['status'])) { $ret[$related_id]['status'] = array($ret[$related_id]['status']); } } else { $ret[$related_id]['status'] = array(); } $ret[$related_id]['status'][] = $status; } } return $ret; }
/** * Generate inline message display. * * @param array $options Options: * - mask: (integer) The mask needed for a getSummary() call. * - mimeid: (string) Restrict output to this MIME ID (and children). * * @return array See getInlineOutput(). */ protected function _getInlineOutput(array $options) { global $prefs, $registry; $atc_parts = $display_ids = $i = $metadata = $msgtext = $wrap_ids = array(); $text_out = ''; $view = $registry->getView(); $contents_mask = isset($options['mask']) ? $options['mask'] : 0; $mimeid_filter = isset($options['mimeid']) ? new Horde_Mime_Id($options['mimeid']) : null; $show_parts = $prefs->getValue('parts_display'); foreach ($this->contents->getMIMEMessage()->partIterator() as $part) { $mime_id = $part->getMimeId(); $i[] = $mime_id; if (isset($display_ids[$mime_id]) || isset($atc_parts[$mime_id])) { continue; } if ($mimeid_filter && (strval($mimeid_filter) != $mime_id && !$mimeid_filter->isChild($mime_id))) { continue; } if (!($render_mode = $this->contents->canDisplay($mime_id, IMP_Contents::RENDER_INLINE_AUTO))) { if (IMP_Mime_Attachment::isAttachment($part)) { if ($show_parts == 'atc') { $atc_parts[$mime_id] = 1; } if ($contents_mask) { $msgtext[$mime_id] = array('text' => $this->_formatSummary($this->contents->getSummary($mime_id, $contents_mask), true)); } } continue; } $render_part = $this->contents->renderMIMEPart($mime_id, $render_mode); if ($show_parts == 'atc' && IMP_Mime_Attachment::isAttachment($part) && (empty($render_part) || !($render_mode & IMP_Contents::RENDER_INLINE))) { $atc_parts[$mime_id] = 1; } if (empty($render_part)) { if ($contents_mask && IMP_Mime_Attachment::isAttachment($part)) { $msgtext[$mime_id] = array('text' => $this->_formatSummary($this->contents->getSummary($mime_id, $contents_mask), true)); } continue; } reset($render_part); while (list($id, $info) = each($render_part)) { $display_ids[$id] = 1; if (empty($info)) { continue; } $part_text = $contents_mask && empty($info['nosummary']) ? $this->_formatSummary($this->contents->getSummary($id, $contents_mask), !empty($info['attach'])) : ''; if (empty($info['attach'])) { if (isset($info['status'])) { if (!is_array($info['status'])) { $info['status'] = array($info['status']); } $render_issues = array(); foreach ($info['status'] as $val) { if (in_array($view, $val->views)) { if ($val instanceof IMP_Mime_Status_RenderIssue) { $render_issues[] = $val; } else { $part_text .= strval($val); } } } if (!empty($render_issues)) { $render_issues_ob = new IMP_Mime_Status_RenderIssue_Display(); $render_issues_ob->addIssues($render_issues); $part_text .= strval($render_issues_ob); } } $part_text .= '<div class="mimePartData">' . $info['data'] . '</div>'; } elseif ($show_parts == 'atc') { $atc_parts[$id] = 1; } $msgtext[$id] = array('text' => $part_text, 'wrap' => empty($info['wrap']) ? null : $info['wrap']); if (isset($info['metadata'])) { /* Format: array(identifier, ...[data]...) */ $metadata = array_merge($metadata, $info['metadata']); } } } if (!empty($msgtext)) { uksort($msgtext, 'strnatcmp'); } reset($msgtext); while (list($id, $part) = each($msgtext)) { while (!empty($wrap_ids)) { $id_ob = new Horde_Mime_Id(end($wrap_ids)); if ($id_ob->isChild($id)) { break; } array_pop($wrap_ids); $text_out .= '</div>'; } if (!empty($part['wrap'])) { $text_out .= '<div class="' . $part['wrap'] . '" impcontentsmimeid="' . $id . '">'; $wrap_ids[] = $id; } $text_out .= '<div class="mimePartBase"' . (empty($part['wrap']) ? ' impcontentsmimeid="' . $id . '"' : '') . '>' . $part['text'] . '</div>'; } $text_out .= str_repeat('</div>', count($wrap_ids)); if (!strlen($text_out)) { $text_out = strval(new IMP_Mime_Status(null, _("There are no parts that can be shown inline."))); } $atc_parts = $show_parts == 'all' ? $i : array_keys($atc_parts); return array('atc_parts' => $atc_parts, 'display_ids' => array_keys($display_ids), 'metadata' => $metadata, 'msgtext' => $text_out, 'one_part' => count($i) === 1); }
/** * @deprecated Use Horde_Mime_Id instead. */ public static function isChild($base, $id) { $id_ob = new Horde_Mime_Id($base); return $id_ob->isChild($id); }
/** * Regenerates body text for use in the compose screen from IMAP data. * * @param IMP_Contents $contents An IMP_Contents object. * @param array $options Additional options: * <ul> * <li>html: (boolean) Return text/html part, if available.</li> * <li>imp_msg: (integer) If non-empty, the message data was created by * IMP. Either: * <ul> * <li>self::COMPOSE</li> * <li>self::FORWARD</li> * <li>self::REPLY</li> * </ul> * </li> * <li>replylimit: (boolean) Enforce length limits?</li> * <li>toflowed: (boolean) Do flowed conversion?</li> * </ul> * * @return mixed Null if bodypart not found, or array with the following * keys: * - charset: (string) The guessed charset to use. * - flowed: (Horde_Text_Flowed) A flowed object, if the text is flowed. * Otherwise, null. * - id: (string) The MIME ID of the bodypart. * - mode: (string) Either 'text' or 'html'. * - text: (string) The body text. */ protected function _getMessageText($contents, array $options = array()) { global $conf, $injector, $notification, $prefs; $body_id = null; $mode = 'text'; $options = array_merge(array('imp_msg' => self::COMPOSE), $options); if (!empty($options['html']) && self::canHtmlCompose() && ($body_id = $contents->findBody('html')) !== null) { $mime_message = $contents->getMIMEMessage(); switch ($mime_message->getPrimaryType()) { case 'multipart': if ($body_id != '1' && $mime_message->getSubType() == 'mixed' && ($id_ob = new Horde_Mime_Id('1')) && !$id_ob->isChild($body_id)) { $body_id = null; } else { $mode = 'html'; } break; default: if (strval($body_id) != '1') { $body_id = null; } else { $mode = 'html'; } break; } } if (is_null($body_id)) { $body_id = $contents->findBody(); if (is_null($body_id)) { return null; } } if (!($part = $contents->getMimePart($body_id))) { return null; } $type = $part->getType(); $part_charset = $part->getCharset(); $msg = Horde_String::convertCharset($part->getContents(), $part_charset, 'UTF-8'); /* Enforce reply limits. */ if (!empty($options['replylimit']) && !empty($conf['compose']['reply_limit'])) { $limit = $conf['compose']['reply_limit']; if (Horde_String::length($msg) > $limit) { $msg = Horde_String::substr($msg, 0, $limit) . "\n" . _("[Truncated Text]"); } } if ($mode == 'html') { $dom = $injector->getInstance('Horde_Core_Factory_TextFilter')->filter($msg, 'Xss', array('charset' => $this->charset, 'return_dom' => true, 'strip_style_attributes' => false)); /* If we are replying to a related part, and this part refers * to local message parts, we need to move those parts into this * message (since the original message may disappear during the * compose process). */ if ($related_part = $contents->findMimeType($body_id, 'multipart/related')) { $this->_setMetadata('related_contents', $contents); $related_ob = new Horde_Mime_Related($related_part); $related_ob->cidReplace($dom, array($this, '_getMessageTextCallback'), $part_charset); $this->_setMetadata('related_contents', null); } /* Convert any Data URLs to attachments. */ $xpath = new DOMXPath($dom->dom); foreach ($xpath->query('//*[@src]') as $val) { $data_url = new Horde_Url_Data($val->getAttribute('src')); if (strlen($data_url->data)) { $data_part = new Horde_Mime_Part(); $data_part->setContents($data_url->data); $data_part->setType($data_url->type); try { $atc = $this->addAttachmentFromPart($data_part); $val->setAttribute('src', $atc->viewUrl()); $this->addRelatedAttachment($atc, $val, 'src'); } catch (IMP_Compose_Exception $e) { $notification->push($e, 'horde.warning'); } } } $msg = $dom->returnBody(); } elseif ($type == 'text/html') { $msg = $injector->getInstance('Horde_Core_Factory_TextFilter')->filter($msg, 'Html2text'); $type = 'text/plain'; } /* Always remove leading/trailing whitespace. The data in the * message body is not intended to be the exact representation of the * original message (use forward as message/rfc822 part for that). */ $msg = trim($msg); if ($type == 'text/plain') { if ($prefs->getValue('reply_strip_sig') && ($pos = strrpos($msg, "\n-- ")) !== false) { $msg = rtrim(substr($msg, 0, $pos)); } /* Remove PGP armored text. */ $pgp = $injector->getInstance('Horde_Crypt_Pgp_Parse')->parseToPart($msg); if (!is_null($pgp)) { $msg = ''; $pgp->buildMimeIds(); foreach ($pgp->partIterator() as $val) { if ($val->getPrimaryType() === 'text') { $msg .= $val->getContents(); } } } if ($part->getContentTypeParameter('format') == 'flowed') { $flowed = new Horde_Text_Flowed($msg, 'UTF-8'); if (Horde_String::lower($part->getContentTypeParameter('delsp')) == 'yes') { $flowed->setDelSp(true); } $flowed->setMaxLength(0); $msg = $flowed->toFixed(false); } else { /* If the input is *not* in flowed format, make sure there is * no padding at the end of lines. */ $msg = preg_replace("/\\s*\n/U", "\n", $msg); } if (isset($options['toflowed'])) { $flowed = new Horde_Text_Flowed($msg, 'UTF-8'); $msg = $options['toflowed'] ? $flowed->toFlowed(true) : $flowed->toFlowed(false, array('nowrap' => true)); } } if (strcasecmp($part->getCharset(), 'windows-1252') === 0) { $part_charset = 'ISO-8859-1'; } return array('charset' => $part_charset, 'flowed' => isset($flowed) ? $flowed : null, 'id' => $body_id, 'mode' => $mode, 'text' => $msg); }