From f797a10256969c0e3bf214967e5eafe8df886149 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Wed, 17 Mar 2010 13:58:25 -0700 Subject: [PATCH] Display scrubbed HTML attachments inline on attachment view page. --- lib/attachmentlist.php | 62 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/lib/attachmentlist.php b/lib/attachmentlist.php index dc6709d678..22ae8ba07a 100644 --- a/lib/attachmentlist.php +++ b/lib/attachmentlist.php @@ -330,6 +330,13 @@ class Attachment extends AttachmentListItem $this->out->element('param', array('name' => 'autoStart', 'value' => 1)); $this->out->elementEnd('object'); break; + + case 'text/html': + if ($this->attachment->filename) { + // Locally-uploaded HTML. Scrub and display inline. + $this->showHtmlFile($this->attachment); + } + break; } } } else { @@ -356,5 +363,60 @@ class Attachment extends AttachmentListItem } } } + + protected function showHtmlFile(File $attachment) + { + $body = $this->scrubHtmlFile($attachment); + if ($body) { + $this->out->elementStart('div', array('class' => 'inline-attachment')); + $this->out->raw($body); + $this->out->elementEnd('div'); + } + } + + /** + * @return mixed false on failure, HTML fragment string on success + */ + protected function scrubHtmlFile(File $attachment) + { + $path = File::path($attachment->filename); + if (!file_exists($path) || !is_readable($path)) { + common_log(LOG_ERR, "Missing local HTML attachment $path"); + return false; + } + $raw = file_get_contents($path); + + // Normalize... + $dom = new DOMDocument(); + if(!$dom->loadHTML($raw)) { + common_log(LOG_ERR, "Bad HTML in local HTML attachment $path"); + return false; + } + + // Remove