Skip to content
Snippets Groups Projects
Commit 6edd2334 authored by Jared Hancock's avatar Jared Hancock
Browse files

Use strongly typed ThreadBody instances for output

parent 67ae49cf
Branches
Tags
No related merge requests found
...@@ -140,7 +140,7 @@ class Thread { ...@@ -140,7 +140,7 @@ class Thread {
$entries = array(); $entries = array();
if(($res=db_query($sql)) && db_num_rows($res)) { if(($res=db_query($sql)) && db_num_rows($res)) {
while($rec=db_fetch_array($res)) { while($rec=db_fetch_array($res)) {
$rec['body'] = new ThreadBody($rec['body'], $rec['format']); $rec['body'] = ThreadBody::fromFormattedText($rec['body'], $rec['format']);
$entries[] = $rec; $entries[] = $rec;
} }
} }
...@@ -308,7 +308,7 @@ Class ThreadEntry { ...@@ -308,7 +308,7 @@ Class ThreadEntry {
} }
function getBody() { function getBody() {
return $this->ht['body']; return ThreadBody::fromFormattedText($this->ht['body'], $this->ht['format']);
} }
function setBody($body) { function setBody($body) {
...@@ -770,7 +770,7 @@ Class ThreadEntry { ...@@ -770,7 +770,7 @@ Class ThreadEntry {
/* variables */ /* variables */
function __toString() { function __toString() {
return $this->getBody(); return (string) $this->getBody();
} }
function asVar() { function asVar() {
...@@ -1002,7 +1002,7 @@ Class ThreadEntry { ...@@ -1002,7 +1002,7 @@ Class ThreadEntry {
} }
} }
if (!($body = (string) $vars['body'])) if (!($body = $vars['body']->getClean()))
$body = '-'; //Special tag used to signify empty message as stored. $body = '-'; //Special tag used to signify empty message as stored.
$poster = $vars['poster']; $poster = $vars['poster'];
...@@ -1259,36 +1259,23 @@ class ThreadBody /* extends SplString */ { ...@@ -1259,36 +1259,23 @@ class ThreadBody /* extends SplString */ {
var $type; var $type;
var $stripped_images = array(); var $stripped_images = array();
var $embedded_images = array(); var $embedded_images = array();
var $options = array(
'strip-embedded' => true
);
function __construct($body, $type='text') { function __construct($body, $type='text', $options=array()) {
$type = strtolower($type); $type = strtolower($type);
if (!in_array($type, static::$types)) if (!in_array($type, static::$types))
throw new Exception($type.': Unsupported ThreadBody type'); throw new Exception($type.': Unsupported ThreadBody type');
$this->body = (string) $body; $this->body = (string) $body;
$this->type = $type; $this->type = $type;
$this->options = array_merge($this->options, $options);
} }
function isEmpty() { function isEmpty() {
return !$this->body || $this->body == '-'; return !$this->body || $this->body == '-';
} }
function display($output=false) {
if ($this->isEmpty())
return '(empty)';
switch (strtolower($this->type)) {
case 'text':
return '<div style="white-space:pre-wrap">'.$this->body.'</div>';
case 'html':
switch ($output) {
case 'pdf':
return Format::clickableurls($this->body, false);
default:
return Format::display($this->body);
}
}
}
function convertTo($type) { function convertTo($type) {
if ($type === $this->type) if ($type === $this->type)
return $this; return $this;
...@@ -1340,21 +1327,58 @@ class ThreadBody /* extends SplString */ { ...@@ -1340,21 +1327,58 @@ class ThreadBody /* extends SplString */ {
return $this->type; return $this->type;
} }
function getClean() {
return trim($this->body);
}
function __toString() { function __toString() {
return $this->body; return $this->display();
}
function toHtml() {
return $this->display('html');
}
static function fromFormattedText($text, $format=false) {
switch ($format) {
case 'text':
return new TextThreadBody($text);
case 'html':
return new HtmlThreadBody($text, array('strip-embedded'=>false));
default:
return new ThreadBody($text);
}
} }
} }
class TextThreadBody extends ThreadBody { class TextThreadBody extends ThreadBody {
function __construct($body) { function __construct($body, $options=array()) {
parent::__construct(Format::stripEmptyLines($body), 'text'); parent::__construct($body, 'text', $options);
}
function getClean() {
return Format::stripEmptyLines($this->body);
}
function display($output=false) {
if ($this->isEmpty())
return '(empty)';
switch ($output) {
case 'html':
return '<div style="white-space:pre-wrap">'.$this->body.'</div>';
case 'pdf':
return nl2br($this->body);
default:
return '<pre>'.$this->body.'</pre>';
}
} }
} }
class HtmlThreadBody extends ThreadBody { class HtmlThreadBody extends ThreadBody {
function __construct($body) { function __construct($body, $options=array()) {
$body = $this->extractEmbeddedHtmlImages($body); parent::__construct($body, 'html', $options);
$body = trim($body, " <>br/\t\n\r") ? Format::safe_html($body) : ''; if ($this->options['strip-embedded'])
parent::__construct($body, 'html'); $this->body = $this->extractEmbeddedHtmlImages($this->body);
} }
function extractEmbeddedHtmlImages($body) { function extractEmbeddedHtmlImages($body) {
...@@ -1370,8 +1394,20 @@ class HtmlThreadBody extends ThreadBody { ...@@ -1370,8 +1394,20 @@ class HtmlThreadBody extends ThreadBody {
}, $body); }, $body);
} }
function __toString() { function getClean() {
return Format::sanitize($this->body); return trim($body, " <>br/\t\n\r") ? Format::sanitize($body) : '';
}
function display($output=false) {
if ($this->isEmpty())
return '(empty)';
switch ($output) {
case 'pdf':
return Format::clickableurls($this->body, false);
default:
return Format::display($this->body);
}
} }
} }
?> ?>
...@@ -110,7 +110,7 @@ if($ticket->getThreadCount() && ($thread=$ticket->getClientThread())) { ...@@ -110,7 +110,7 @@ if($ticket->getThreadCount() && ($thread=$ticket->getClientThread())) {
?> ?>
<table class="thread-entry <?php echo $threadType[$entry['thread_type']]; ?>" cellspacing="0" cellpadding="1" width="800" border="0"> <table class="thread-entry <?php echo $threadType[$entry['thread_type']]; ?>" cellspacing="0" cellpadding="1" width="800" border="0">
<tr><th><?php echo Format::db_datetime($entry['created']); ?> &nbsp;&nbsp;<span class="textra"></span><span><?php echo $poster; ?></span></th></tr> <tr><th><?php echo Format::db_datetime($entry['created']); ?> &nbsp;&nbsp;<span class="textra"></span><span><?php echo $poster; ?></span></th></tr>
<tr><td class="thread-body"><div><?php echo $entry['body']->display(); ?></div></td></tr> <tr><td class="thread-body"><div><?php echo $entry['body']->toHtml(); ?></div></td></tr>
<?php <?php
if($entry['attachments'] if($entry['attachments']
&& ($tentry=$ticket->getThreadEntry($entry['id'])) && ($tentry=$ticket->getThreadEntry($entry['id']))
......
...@@ -362,7 +362,7 @@ $tcount+= $ticket->getNumNotes(); ...@@ -362,7 +362,7 @@ $tcount+= $ticket->getNumNotes();
</tr> </tr>
<tr><td colspan="4" class="thread-body" id="thread-id-<?php <tr><td colspan="4" class="thread-body" id="thread-id-<?php
echo $entry['id']; ?>"><div><?php echo $entry['id']; ?>"><div><?php
echo $entry['body']->display(); ?></div></td></tr> echo $entry['body']->toHtml(); ?></div></td></tr>
<?php <?php
if($entry['attachments'] if($entry['attachments']
&& ($tentry = $ticket->getThreadEntry($entry['id'])) && ($tentry = $ticket->getThreadEntry($entry['id']))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment