From: x2003bruneau Date: Fri, 12 Jan 2007 13:21:42 +0000 (+0000) Subject: Better support of multipart emails for rendering and quoting X-Git-Url: http://git.polytechnique.org/?a=commitdiff_plain;ds=sidebyside;h=bf791d6951a98d91d5db965f7f9c05abb1c910a4;p=banana.git Better support of multipart emails for rendering and quoting git-svn-id: svn+ssh://murphy/home/svn/banana/trunk@156 9869982d-c50d-0410-be91-f2a2ec7c7c7b --- diff --git a/banana/banana.inc.php.in b/banana/banana.inc.php.in index 7245b02..82bb114 100644 --- a/banana/banana.inc.php.in +++ b/banana/banana.inc.php.in @@ -40,12 +40,12 @@ class Banana ### Message display ### static public $msgshow_headers = array('from', 'newsgroups', 'followup-to', 'to', 'cc', 'reply-to', 'organization', 'date', 'references', 'in-reply-to'); - static public $msgshow_mimeparts = array('text/html', 'text/plain', 'text/enriched', 'text', 'message'); + static public $msgshow_mimeparts = array('multipart/report', 'multipart/mixed', 'text/html', 'text/plain', 'text/enriched', 'text', 'message'); static public $msgshow_xface = true; static public $msgshow_wrap = 78; /** Match an url - * Should be included in a regexp delimited using /, !, , or @ (eg: "/$url_regexp/i") + * Should be included in a regexp delimited using /, !, , or @ (eg: "/$url_regexp/ui") * If it matches, return 3 main parts : * \\1 and \\3 are delimiters * \\2 is the url @@ -63,7 +63,10 @@ class Banana /** Global headers to use for messages */ static public $msgedit_headers = array('Mime-Version' => '1.0', 'User-Agent' => 'Banana @VERSION@'); - + /** Mime type order for quoting + */ + static public $msgedit_mimeparts = array('multipart/report', 'multipart/mixed', 'text/plain', 'text/enriched', 'text/html', 'text', 'message'); + ### Protocole ### /** News serveur to use */ diff --git a/banana/mbox.inc.php b/banana/mbox.inc.php index 1ddb5d5..c5bb9e2 100644 --- a/banana/mbox.inc.php +++ b/banana/mbox.inc.php @@ -111,7 +111,8 @@ class BananaMBox implements BananaProtocoleInterface $message = null; return $message; } - return new BananaMessage($message[$id]['message']); + $message = new BananaMessage($message[$id]['message']); + return $message; } /** Return the sources of the given message diff --git a/banana/message.func.inc.php b/banana/message.func.inc.php index db01d2f..8164172 100644 --- a/banana/message.func.inc.php +++ b/banana/message.func.inc.php @@ -413,6 +413,14 @@ function banana_formatRichText(BananaMimePart &$part) return banana_cleanHtml($text); } +function banana_quoteRichtText(BananaMimePart &$part) +{ + $text = $part->getText(); + $text = banana_richtextToHtml($text); + $text = banana_htmlToPlainText($text); + return banana_wrap($text, 1); +} + // }}} // vim:set et sw=4 sts=4 ts=4 enc=utf-8: diff --git a/banana/message.inc.php b/banana/message.inc.php index db4e968..295af5b 100644 --- a/banana/message.inc.php +++ b/banana/message.inc.php @@ -243,25 +243,25 @@ final class BananaMessage extends BananaMimePart if (empty($parts)) { continue; } - foreach ($parts as &$part) { - list($type, $subtype) = $part->getType(); - switch ($subtype) { - case 'html': return banana_formatHtml($part); - case 'enriched': case 'richtext': return banana_formatRichText($part); - default: return banana_formatPlainText($part); - } - } + return $parts[0]->toHtml(); } return null; } public function quote() { - $part = $this->toPlainText(); - if (is_null($part)) { - return banana_quoteHtml($this->toHtml()); + foreach (Banana::$msgedit_mimeparts as $type) { + @list($type, $subtype) = explode('/', $type); + $parts = $this->getParts($type, $subtype); + if (empty($parts)) { + continue; + } + if ($parts[0] === $this) { + return parent::quote(); + } + return $parts[0]->quote(); } - return banana_quotePlainText($part); + return null; } public function canCancel() diff --git a/banana/mimepart.inc.php b/banana/mimepart.inc.php index a2d8220..9f039eb 100644 --- a/banana/mimepart.inc.php +++ b/banana/mimepart.inc.php @@ -406,13 +406,76 @@ class BananaMimePart public function getText() { - if (!$this->isType('text')) { - return null; - } $this->decodeContent(); return $this->body; } + public function toHtml() + { + list($type, $subtype) = $this->getType(); + if ($type == 'image') { + $part = $this->id ? $this->id : $this->filename; + return '' . banana_htmlentities($this->filename) . ''; + } elseif (!in_array($type, Banana::$msgshow_mimeparts) + && !in_array($this->content_type, Banana::$msgshow_mimeparts)) { + $part = $this->id ? $this->id : $this->filename; + if (!$part) { + $part = $this->content_type; + } + return '[' . Banana::$page->makeImgLink(array('group' => Banana::$group, + 'artid' => Banana::$artid, + 'part' => $part, + 'text' => $this->filename ? $this->filename : $this->content_type, + 'img' => 'save')) . ']'; + } else { + if ($type == 'multipart' && ($subtype == 'mixed' || $subtype == 'report')) { + $text = ''; + foreach ($this->multipart as &$part) { + $text .= $part->toHtml(); + } + return $text; + } + switch ($subtype) { + case 'html': return banana_formatHtml($this); + case 'enriched': case 'richtext': return banana_formatRichText($this); + default: + if ($type == 'message') { + return '
' . banana_formatPlainText($this); + } + return banana_formatPlainText($this); + } + } + return null; + } + + public function quote() + { + list($type, $subtype) = $this->getType(); + if (in_array($type, Banana::$msgedit_mimeparts) || in_array($this->content_type, Banana::$msgedit_mimeparts)) { + if ($type == 'multipart' && ($subtype == 'mixed' || $subtype == 'report')) { + $text = ''; + foreach ($this->multipart as &$part) { + $qt = $part->quote(); + $qt = rtrim($qt); + if (!empty($text)) { + $text .= "\n" . banana_quote("", 1) . "\n"; + } + $text .= $qt; + } + return $text; + } + switch ($subtype) { + case 'html': return banana_quoteHtml($this); + case 'enriched': case 'richtext': return banana_quoteRichText($this); + default: return banana_quotePlainText($this); + } + } + } + protected function getType() { return explode('/', $this->content_type); @@ -447,24 +510,6 @@ class BananaMimePart return $parts; } - public function toPlainText() - { - $parts = $this->getParts('text', 'plain'); - return (count($parts) ? $parts[0] : null); - } - - public function toHtml() - { - $parts = $this->getParts('text', 'html'); - return (count($parts) ? $parts[0] : null); - } - - public function toRichText() - { - $parts = $this->getParts('text', 'enriched'); - return (count($parts) ? $parts[0] : null); - } - public function getFile($filename) { if ($this->filename == $filename) { diff --git a/banana/templates/banana-message.inc.tpl b/banana/templates/banana-message.inc.tpl index 678ce01..1c14cd2 100644 --- a/banana/templates/banana-message.inc.tpl +++ b/banana/templates/banana-message.inc.tpl @@ -46,8 +46,7 @@ Fichiers joints {foreach from=$files item=file name=attachs} - {$file->getFilename()|htmlentities} - {imglink img=save alt="Enregistrer" group=$group artid=$artid part=$file->getFilename()}{if !$smarty.foreach.attachs.last}, {/if} + {imglink img=save alt="Enregistrer" group=$group artid=$artid part=$file->getFilename() text=$file->getFilename()}{if !$smarty.foreach.attachs.last}, {/if} {/foreach}