Skip to content

Commit

Permalink
Error/sanity checking when calling IMP_Contents#getMIMEPart()
Browse files Browse the repository at this point in the history
Return is allowed to be null (e.g. failure to connect to IMAP backend),
so need to check for that everywhere we use.
  • Loading branch information
slusarz committed Nov 19, 2014
1 parent e3a820e commit 6e1e050
Show file tree
Hide file tree
Showing 15 changed files with 116 additions and 64 deletions.
7 changes: 4 additions & 3 deletions imp/lib/Ajax/Imple/ImportEncryptKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ protected function _handle(Horde_Variables $vars)
/* Retrieve the key from the message. */
try {
$contents = $injector->getInstance('IMP_Factory_Contents')->create(new IMP_Indices_Mailbox($vars));
$mime_part = $contents->getMIMEPart($vars->mime_id);
if (empty($mime_part)) {
throw new IMP_Exception(_("Cannot retrieve public key from message."));
if (!($mime_part = $contents->getMIMEPart($vars->mime_id))) {
throw new IMP_Exception(
_("Cannot retrieve public key from message.")
);
}

/* Add the public key to the storage system. */
Expand Down
7 changes: 4 additions & 3 deletions imp/lib/Ajax/Imple/ItipRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ protected function _handle(Horde_Variables $vars)
/* Retrieve the calendar data from the message. */
try {
$contents = $injector->getInstance('IMP_Factory_Contents')->create(new IMP_Indices_Mailbox($vars));
$mime_part = $contents->getMIMEPart($vars->mime_id);
if (empty($mime_part)) {
throw new IMP_Exception(_("Cannot retrieve calendar data from message."));
if (!($mime_part = $contents->getMIMEPart($vars->mime_id))) {
throw new IMP_Exception(
_("Cannot retrieve calendar data from message.")
);
} elseif (!$vCal->parsevCalendar($mime_part->getContents(), 'VCALENDAR', $mime_part->getCharset())) {
throw new IMP_Exception(_("The calendar data is invalid"));
}
Expand Down
7 changes: 4 additions & 3 deletions imp/lib/Ajax/Imple/VcardImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ protected function _handle(Horde_Variables $vars)
try {
$contents = $injector->getInstance('IMP_Factory_Contents')
->create(new IMP_Indices_Mailbox($vars));
$mime_part = $contents->getMIMEPart($vars->mime_id);
if (empty($mime_part)) {
throw new IMP_Exception(_("Cannot retrieve vCard data from message."));
if (!($mime_part = $contents->getMIMEPart($vars->mime_id))) {
throw new IMP_Exception(
_("Cannot retrieve vCard data from message.")
);
} elseif (!$iCal->parsevCalendar($mime_part->getContents(), 'VCALENDAR', $mime_part->getCharset())) {
throw new IMP_Exception(_("Error reading the contact data."));
}
Expand Down
5 changes: 4 additions & 1 deletion imp/lib/Basic/Saveimage.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ protected function _init()
switch ($this->vars->actionID) {
case 'save_image':
$contents = $injector->getInstance('IMP_Factory_Contents')->create($this->indices);
$mime_part = $contents->getMIMEPart($this->vars->id);
if (!($mime_part = $contents->getMIMEPart($this->vars->id))) {
$notification->push(_("Could not load message."));
break;
}
$image_data = array(
'data' => $mime_part->getContents(),
'description' => $mime_part->getDescription(true),
Expand Down
15 changes: 10 additions & 5 deletions imp/lib/Compose.php
Original file line number Diff line number Diff line change
Expand Up @@ -3033,7 +3033,9 @@ protected function _getMessageText($contents, array $options = array())
}
}

$part = $contents->getMIMEPart($body_id);
if (!($part = $contents->getMIMEPart($body_id))) {
return null;
}
$type = $part->getType();
$part_charset = $part->getCharset();

Expand Down Expand Up @@ -3155,14 +3157,17 @@ protected function _getMessageText($contents, array $options = array())
/**
* Callback used in _getMessageText().
*
* @return Horde_Url
* @return string Replacement text.
*/
public function _getMessageTextCallback($id, $attribute, $node)
{
$atc = $this->addAttachmentFromPart($this->getMetadata('related_contents')->getMIMEPart($id));
$this->addRelatedAttachment($atc, $node, $attribute);
if ($part = $this->getMetadata('related_contents')->getMIMEPart($id)) {
$atc = $this->addAttachmentFromPart($part);
$this->addRelatedAttachment($atc, $node, $attribute);
return $atc->viewUrl();
}

return $atc->viewUrl();
return $node->getAttribute($attribute);
}

/**
Expand Down
78 changes: 51 additions & 27 deletions imp/lib/Contents.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,17 +249,22 @@ public function getBodyPart($id, $options = array())
}
}

$part = $this->getMIMEPart($base_id->id, array('nocontents' => true));
$txt = $part->addMimeHeaders()->toString() .
"\n" .
$part->getContents();
$body = '';
$part = $this->getMIMEPart(
$base_id->id,
array('nocontents' => true)
);

try {
$body = Horde_Mime_Part::getRawPartText($txt, 'header', '1') .
"\n\n" .
Horde_Mime_Part::getRawPartText($txt, 'body', '1');
} catch (Horde_Mime_Exception $e) {
$body = '';
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'])) {
Expand Down Expand Up @@ -506,28 +511,29 @@ public function getMIMEMessage()
* - nocontents: (boolean) If true, don't add the contents to the part
* DEFAULT: Contents are added to the part
*
* @return Horde_Mime_Part The raw MIME part asked for (reference).
* @return Horde_Mime_Part The raw MIME part asked for. If not found,
* returns null.
*/
public function getMIMEPart($id, $options = array())
{
$this->_buildMessage();

$part = $this->_message->getPart($id);
if (!$part = $this->_message->getPart($id)) {
return null;
}

/* Ticket #9201: Treat 'ISO-8859-1' as 'windows-1252'. 1252 has some
* characters (e.g. euro sign, back quote) not in 8859-1. There
* shouldn't be any issue doing this since the additional code points
* in 1252 don't map to anything in 8859-1. */
if ($part &&
(strcasecmp($part->getCharset(), 'ISO-8859-1') === 0)) {
if (strcasecmp($part->getCharset(), 'ISO-8859-1') === 0) {
$part->setCharset('windows-1252');
}

/* Don't download contents of entire body if ID == 0 (indicating the
* body of the main multipart message). I'm pretty sure we never
* want to download the body of that part here. */
if (!empty($id) &&
!is_null($part) &&
(substr($id, -2) != '.0') &&
empty($options['nocontents']) &&
$this->_indices &&
Expand Down Expand Up @@ -717,10 +723,18 @@ public function generatePreview()
// account for any content-encoding & HTML tags.
$pmime = $this->getMIMEPart($mimeid, array('length' => $maxlen * 3));

$ptext = Horde_String::convertCharset($pmime->getContents(), $pmime->getCharset(), 'UTF-8');
if ($pmime) {
$ptext = Horde_String::convertCharset(
$pmime->getContents(),
$pmime->getCharset(),
'UTF-8'
);

if ($pmime->getType() == 'text/html') {
$ptext = $GLOBALS['injector']->getInstance('Horde_Core_Factory_TextFilter')->filter($ptext, 'Html2text');
if ($pmime->getType() == 'text/html') {
$ptext = $GLOBALS['injector']->getInstance('Horde_Core_Factory_TextFilter')->filter($ptext, 'Html2text');
}
} else {
$ptext = '';
}

$this->_build = $oldbuild;
Expand Down Expand Up @@ -797,6 +811,10 @@ public function getSummary($id, $mask = 0)
);

$mime_part = $this->getMIMEPart($id, array('nocontents' => true));
if (!$mime_part) {
return $part;
}

$mime_type = $mime_part->getType();

/* If this is an attachment that has no specific MIME type info, see
Expand Down Expand Up @@ -1090,9 +1108,17 @@ protected function _buildMessage($parts = null)
$last_id = null;

$mime_part = $this->getMIMEPart($id, array('nocontents' => true));
$viewer = $mv_factory->create($mime_part, array('contents' => $this));
if ($viewer->embeddedMimeParts()) {
$mime_part = $this->getMIMEPart($id);
if (!$mime_part) {
continue;
}

$viewer = $mv_factory->create(
$mime_part,
array('contents' => $this)
);

if ($viewer->embeddedMimeParts() &&
($mime_part = $this->getMIMEPart($id))) {
$viewer->setMIMEPart($mime_part);
$new_part = $viewer->getEmbeddedMimeParts();
if (!is_null($new_part)) {
Expand Down Expand Up @@ -1122,10 +1148,8 @@ protected function _buildMessage($parts = null)
*/
public function canDisplay($part, $mask, $type = null)
{
if (!is_object($part)) {
$part = $this->getMIMEPart($part, array('nocontents' => true));
}
if (!$part) {
if (!is_object($part) &&
!($part = $this->getMIMEPart($part, array('nocontents' => true)))) {
return 0;
}
$viewer = $GLOBALS['injector']->getInstance('IMP_Factory_MimeViewer')->create($part, array('contents' => $this, 'type' => $type));
Expand Down Expand Up @@ -1266,8 +1290,8 @@ public function buildMessageContents($ignore = array())
$curr_ignore = null;
if (($key != 0) &&
($val != 'message/rfc822') &&
(strpos($val, 'multipart/') === false)) {
$part = $this->getMIMEPart($key);
(strpos($val, 'multipart/') === false) &&
($part = $this->getMIMEPart($key))) {
$message->alterPart($key, $part);
}
}
Expand Down
14 changes: 10 additions & 4 deletions imp/lib/Contents/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public function downloadAll()

$tosave = array();
foreach ($this->_contents->downloadAllList() as $val) {
$mime = $this->_getRawDownloadPart($val);
if (!($mime = $this->_getRawDownloadPart($val))) {
continue;
}
if (!($name = $mime->getName(true))) {
$name = sprintf(_("part %s"), $val);
}
Expand Down Expand Up @@ -91,7 +93,9 @@ public function downloadAttach($id)

$session->close();

$mime = $this->_getRawDownloadPart($id);
if (!($mime = $this->_getRawDownloadPart($id))) {
return array();
}

return array(
'data' => $mime->getContents(array('stream' => true)),
Expand Down Expand Up @@ -385,11 +389,13 @@ public static function addToken(array $params = array())
*
* @param string $id MIME ID.
*
* @return Horde_Mime_Part MIME part.
* @return Horde_Mime_Part MIME part, or null on error.
*/
protected function _getRawDownloadPart($id)
{
$mime = $this->_contents->getMIMEPart($id);
if (!($mime = $this->_contents->getMIMEPart($id))) {
return null;
}

if ($this->_contents->canDisplay($id, IMP_Contents::RENDER_RAW)) {
$render = $this->_contents->renderMIMEPart($id, IMP_Contents::RENDER_RAW);
Expand Down
4 changes: 4 additions & 0 deletions imp/lib/Indices/Copy.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public function copy($mbox, IMP_Indices $indices, $move)
$body_part = $imp_contents->getMIMEPart(
$imp_contents->findBody()
);
if (!$body_part) {
$success = false;
continue;
}
$flowed = new Horde_Text_Flowed($body_part->getContents());
if ($body_part->getContentTypeParameter('delsp') == 'yes') {
$flowed->setDelSp(true);
Expand Down
5 changes: 3 additions & 2 deletions imp/lib/Mime/Viewer/Externalbody.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ protected function _getEmbeddedMimeParts()
foreach (array_keys($base_part->contentTypeMap(true)) as $key) {
if (($part = $base_part->getPart($key)) &&
($part->getContentId() == $cid) &&
($part->getType() != 'message/external-body')) {
$full_part = clone $imp_contents->getMIMEPart($key);
($part->getType() != 'message/external-body') &&
($full_part = $imp_contents->getMIMEPart($key))) {
$full_part = clone $full_part;
$full_part->setMimeId($this->_mimepart->getMimeId());
// TODO: Add headers from referring body part.
return $full_part;
Expand Down
5 changes: 3 additions & 2 deletions imp/lib/Mime/Viewer/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,9 @@ protected function _node($doc, $node)
if ($node->hasAttribute('href')) {
$tmp = $node->getAttribute('href');

if ($id = $this->_cidSearch($tmp, false)) {
$this->_imptmp['style'][] = $this->getConfigParam('imp_contents')->getMIMEPart($id)->getContents();
if (($id = $this->_cidSearch($tmp, false)) &&
($mime_part = $this->getConfigParam('imp_contents')->getMIMEPart($id))) {
$this->_imptmp['style'][] = $mime_part->getContents();
} elseif ($this->_imgBlock()) {
$node->setAttribute(self::CSSBLOCK, $node->getAttribute('href'));
$node->removeAttribute('href');
Expand Down
7 changes: 4 additions & 3 deletions imp/lib/Mime/Viewer/Mdn.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,12 @@ protected function _renderInfo()
$ret[$id_ob->id] = null;

/* Display a link to the sent message. */
$imp_contents = $this->getConfigParam('imp_contents');
$part3_id = $id_ob->idArithmetic($id_ob::ID_NEXT);
$part = $this->getConfigParam('imp_contents')->getMIMEPart($part3_id);
if ($part) {

if ($part = $imp_contents->getMIMEPart($part3_id)) {
$status->addText(
$this->getConfigParam('imp_contents')->linkViewJS(
$imp_contents->linkViewJS(
$part,
'view_attach',
_("View the text of the sent message."),
Expand Down
9 changes: 6 additions & 3 deletions imp/lib/Mime/Viewer/Pgp.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ protected function _getEmbeddedMimeParts()
return null;
}

$imp_contents = $this->getConfigParam('imp_contents');
$partlist = array_keys($this->_mimepart->contentTypeMap());
$base_id = reset($partlist);
$version_id = next($partlist);
Expand All @@ -187,7 +188,7 @@ protected function _getEmbeddedMimeParts()
$status = new IMP_Mime_Status($this->_mimepart);
$status->icon('mime/encryption.png', 'PGP');

$cache = $this->getConfigParam('imp_contents')->getViewCache();
$cache = $imp_contents->getViewCache();
$cache->pgp[$base_id] = array(
'status' => array($status),
'other' => array(
Expand All @@ -207,9 +208,11 @@ protected function _getEmbeddedMimeParts()
/* PGP version information appears in the first MIME subpart. We
* don't currently need to do anything with this information. The
* encrypted data appears in the second MIME subpart. */
$encrypted_part = $this->getConfigParam('imp_contents')->getMIMEPart($data_id);
$encrypted_data = $encrypted_part->getContents();
if (!($encrypted_part = $imp_contents->getMIMEPart($data_id))) {
return null;
}

$encrypted_data = $encrypted_part->getContents();
$symmetric_pass = $personal_pass = null;

/* Check if this a symmetrically encrypted message. */
Expand Down
6 changes: 4 additions & 2 deletions imp/lib/Mime/Viewer/Smil.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ protected function _startElement($parser, $name, $attrs)
*/
protected function _getRelatedLink($cid)
{
return (($related_part = $this->getConfigParam('imp_contents')->findMimeType($this->_mimepart->getMimeId(), 'multipart/related')) &&
$imp_contents = $this->getConfigParam('imp_contents');

return (($related_part = $imp_contents->findMimeType($this->_mimepart->getMimeId(), 'multipart/related')) &&
(($key = $related_part->getMetadata('related_ob')->cidSearch($cid)) !== false))
? $this->getConfigParam('imp_contents')->getMIMEPart($key)
? $imp_contents->getMIMEPart($key)
: false;
}

Expand Down
3 changes: 1 addition & 2 deletions imp/lib/Mime/Viewer/Smime.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,7 @@ protected function _parseSignedData($sig_only = false)
return;
}

$subpart = $imp_contents->getMIMEPart($sig_id);
if (empty($subpart)) {
if (!($subpart = $imp_contents->getMIMEPart($sig_id))) {
try {
$msg_data = $this->_impsmime->extractSignedContents($raw_text);
$subpart = Horde_Mime_Part::parseMessage($msg_data, array('forcemime' => true));
Expand Down
8 changes: 4 additions & 4 deletions imp/lib/Mime/Viewer/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ protected function _renderInfo()

/* Get the action first - it appears in the second part. */
$action = null;
$part2 = $this->getConfigParam('imp_contents')->getMIMEPart($part2_id);
$imp_contents = $this->getConfigParam('imp_contents');
$part2 = $imp_contents->getMIMEPart($part2_id);

foreach (explode("\n", $part2->getContents()) as $line) {
if (stristr($line, 'Action:') !== false) {
Expand Down Expand Up @@ -132,10 +133,9 @@ protected function _renderInfo()
}

/* Display a link to the returned message, if it exists. */
$part3 = $this->getConfigParam('imp_contents')->getMIMEPart($part3_id);
if ($part3) {
if ($part3 = $imp_contents->getMIMEPart($part3_id)) {
$status->addText(
$this->getConfigParam('imp_contents')->linkViewJS(
$imp_contents->linkViewJS(
$part3,
'view_attach',
$msg_link,
Expand Down

0 comments on commit 6e1e050

Please sign in to comment.