From 60056e697fb1666e9b491b6f9f5654b694e3b8c9 Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Sun, 17 Feb 2013 14:56:12 +0000 Subject: ensure diff formatters escape their output --- inc/DifferenceEngine.php | 58 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 16 deletions(-) (limited to 'inc') diff --git a/inc/DifferenceEngine.php b/inc/DifferenceEngine.php index 1b68cf6d3..42975b208 100644 --- a/inc/DifferenceEngine.php +++ b/inc/DifferenceEngine.php @@ -797,7 +797,7 @@ class DiffFormatter { function _lines($lines, $prefix = ' ') { foreach ($lines as $line) - echo "$prefix $line\n"; + echo "$prefix ".$this->_escape($line)."\n"; } function _context($lines) { @@ -816,6 +816,10 @@ class DiffFormatter { echo "---\n"; $this->_added($closing); } + + function _escape($str){ + return $str; + } } /** @@ -871,13 +875,13 @@ class _HWLDF_WordAccumulator { function _flushGroup($new_tag) { if ($this->_group !== '') { if ($this->_tag == 'mark') - $this->_line .= ''.$this->_group.''; + $this->_line .= ''.$this->_escape($this->_group).''; elseif ($this->_tag == 'add') - $this->_line .= ''.$this->_group.''; + $this->_line .= ''.$this->_escape($this->_group).''; elseif ($this->_tag == 'del') - $this->_line .= ''.$this->_group.''; + $this->_line .= ''.$this->_escape($this->_group).''; else - $this->_line .= $this->_group; + $this->_line .= $this->_escape($this->_group); } $this->_group = ''; $this->_tag = $new_tag; @@ -912,6 +916,10 @@ class _HWLDF_WordAccumulator { $this->_flushLine('~done'); return $this->_lines; } + + function _escape($str){ + return hsc($str); + } } class WordLevelDiff extends MappedDiff { @@ -1069,11 +1077,17 @@ class TableDiffFormatter extends DiffFormatter { function _lines($lines, $prefix=' ', $color="white") { } - function addedLine($line) { + function addedLine($line,$escaped=false) { + if (!$escaped){ + $line = $this->_escape($line); + } return '+' . $line.''; } - function deletedLine($line) { + function deletedLine($line,$escaped=false) { + if (!$escaped){ + $line = $this->_escape($line); + } return '-' . $line.''; } @@ -1082,12 +1096,16 @@ class TableDiffFormatter extends DiffFormatter { } function contextLine($line) { - return ' '.$line.''; + return ' '.$this->_escape($line).''; } function _added($lines) { + $this->_addedLines($lines,false); + } + + function _addedLines($lines,$escaped=false){ foreach ($lines as $line) { - print('' . $this->emptyLine() . $this->addedLine($line) . "\n"); + print('' . $this->emptyLine() . $this->addedLine($line,$escaped) . "\n"); } } @@ -1104,15 +1122,19 @@ class TableDiffFormatter extends DiffFormatter { } function _changed($orig, $closing) { - $diff = new WordLevelDiff($orig, $closing); + $diff = new WordLevelDiff($orig, $closing); // this escapes the diff data $del = $diff->orig(); $add = $diff->closing(); while ($line = array_shift($del)) { $aline = array_shift($add); - print('' . $this->deletedLine($line) . $this->addedLine($aline) . "\n"); + print('' . $this->deletedLine($line,true) . $this->addedLine($aline,true) . "\n"); } - $this->_added($add); # If any leftovers + $this->_addedLines($add,true); # If any leftovers + } + + function _escape($str) { + return hsc($str); } } @@ -1167,29 +1189,33 @@ class InlineDiffFormatter extends DiffFormatter { function _added($lines) { foreach ($lines as $line) { - print(''. $line . "\n"); + print(''. $this->_escape($line) . "\n"); } } function _deleted($lines) { foreach ($lines as $line) { - print('' . $line . "\n"); + print('' . $this->_escape($line) . "\n"); } } function _context($lines) { foreach ($lines as $line) { - print(''.$line."\n"); + print(''.$this->_escape($line)."\n"); } } function _changed($orig, $closing) { - $diff = new InlineWordLevelDiff($orig, $closing); + $diff = new InlineWordLevelDiff($orig, $closing); // this escapes the diff data $add = $diff->inline(); foreach ($add as $line) print(''.$line."\n"); } + + function _escape($str) { + return hsc($str); + } } -- cgit v1.2.3