Commit 5cc71129 authored by Sebastian Kummer's avatar Sebastian Kummer

ZP-1240 Add HTML safe truncation options to stream wrappers.

Released under the Affero GNU General Public License (AGPL) version 3.
parent 9ec28777
...@@ -31,6 +31,7 @@ class MAPIStreamWrapper { ...@@ -31,6 +31,7 @@ class MAPIStreamWrapper {
private $position; private $position;
private $streamlength; private $streamlength;
private $toTruncate; private $toTruncate;
private $truncateHtmlSafe;
/** /**
* Opens the stream * Opens the stream
...@@ -52,6 +53,7 @@ class MAPIStreamWrapper { ...@@ -52,6 +53,7 @@ class MAPIStreamWrapper {
$this->position = 0; $this->position = 0;
$this->toTruncate = false; $this->toTruncate = false;
$this->truncateHtmlSafe = (isset($contextOptions[self::PROTOCOL]['truncatehtmlsafe'])) ? $contextOptions[self::PROTOCOL]['truncatehtmlsafe'] : false;
// this is our stream! // this is our stream!
$this->mapistream = $contextOptions[self::PROTOCOL]['stream']; $this->mapistream = $contextOptions[self::PROTOCOL]['stream'];
...@@ -65,7 +67,7 @@ class MAPIStreamWrapper { ...@@ -65,7 +67,7 @@ class MAPIStreamWrapper {
$this->streamlength = 0; $this->streamlength = 0;
} }
ZLog::Write(LOGLEVEL_DEBUG, sprintf("MAPIStreamWrapper::stream_open(): initialized mapistream: %s streamlength: %d", $this->mapistream, $this->streamlength)); ZLog::Write(LOGLEVEL_DEBUG, sprintf("MAPIStreamWrapper::stream_open(): initialized mapistream: %s - streamlength: %d - HTML-safe-truncate: %s", $this->mapistream, $this->streamlength, Utils::PrintAsString($this->truncateHtmlSafe)));
return true; return true;
} }
...@@ -95,7 +97,7 @@ class MAPIStreamWrapper { ...@@ -95,7 +97,7 @@ class MAPIStreamWrapper {
// we need to truncate UTF8 compatible if ftruncate() was called // we need to truncate UTF8 compatible if ftruncate() was called
if ($this->toTruncate && $this->position >= $this->streamlength) { if ($this->toTruncate && $this->position >= $this->streamlength) {
$data = Utils::Utf8_truncate($data, $this->streamlength); $data = Utils::Utf8_truncate($data, $this->streamlength, $this->truncateHtmlSafe);
} }
return $data; return $data;
...@@ -176,12 +178,13 @@ class MAPIStreamWrapper { ...@@ -176,12 +178,13 @@ class MAPIStreamWrapper {
* Instantiates a MAPIStreamWrapper * Instantiates a MAPIStreamWrapper
* *
* @param mapistream $mapistream The stream to be wrapped * @param mapistream $mapistream The stream to be wrapped
* @param boolean $truncatehtmlsafe Indicates if a truncation should be done html-safe - default: false
* *
* @access public * @access public
* @return MAPIStreamWrapper * @return MAPIStreamWrapper
*/ */
static public function Open($mapistream) { static public function Open($mapistream, $truncatehtmlsafe = false) {
$context = stream_context_create(array(self::PROTOCOL => array('stream' => &$mapistream))); $context = stream_context_create(array(self::PROTOCOL => array('stream' => &$mapistream, 'truncatehtmlsafe' => $truncatehtmlsafe)));
return fopen(self::PROTOCOL . "://",'r', false, $context); return fopen(self::PROTOCOL . "://",'r', false, $context);
} }
} }
......
...@@ -30,6 +30,7 @@ class StringStreamWrapper { ...@@ -30,6 +30,7 @@ class StringStreamWrapper {
private $stringstream; private $stringstream;
private $position; private $position;
private $stringlength; private $stringlength;
private $truncateHtmlSafe;
/** /**
* Opens the stream * Opens the stream
...@@ -53,9 +54,10 @@ class StringStreamWrapper { ...@@ -53,9 +54,10 @@ class StringStreamWrapper {
// this is our stream! // this is our stream!
$this->stringstream = $contextOptions[self::PROTOCOL]['string']; $this->stringstream = $contextOptions[self::PROTOCOL]['string'];
$this->truncateHtmlSafe = (isset($contextOptions[self::PROTOCOL]['truncatehtmlsafe'])) ? $contextOptions[self::PROTOCOL]['truncatehtmlsafe'] : false;
$this->stringlength = strlen($this->stringstream); $this->stringlength = strlen($this->stringstream);
ZLog::Write(LOGLEVEL_DEBUG, sprintf("StringStreamWrapper::stream_open(): initialized stream length: %d", $this->stringlength)); ZLog::Write(LOGLEVEL_DEBUG, sprintf("StringStreamWrapper::stream_open(): initialized stream length: %d - HTML-safe-truncate: %s", $this->stringlength, Utils::PrintAsString($this->truncateHtmlSafe)));
return true; return true;
} }
...@@ -135,7 +137,7 @@ class StringStreamWrapper { ...@@ -135,7 +137,7 @@ class StringStreamWrapper {
*/ */
public function stream_truncate ($new_size) { public function stream_truncate ($new_size) {
// cut the string! // cut the string!
$this->stringstream = Utils::Utf8_truncate($this->stringstream, $new_size); $this->stringstream = Utils::Utf8_truncate($this->stringstream, $new_size, $this->truncateHtmlSafe);
$this->stringlength = strlen($this->stringstream); $this->stringlength = strlen($this->stringstream);
if ($this->position > $this->stringlength) { if ($this->position > $this->stringlength) {
...@@ -162,12 +164,13 @@ class StringStreamWrapper { ...@@ -162,12 +164,13 @@ class StringStreamWrapper {
* Instantiates a StringStreamWrapper * Instantiates a StringStreamWrapper
* *
* @param string $string The string to be wrapped * @param string $string The string to be wrapped
* @param boolean $truncatehtmlsafe Indicates if a truncation should be done html-safe - default: false
* *
* @access public * @access public
* @return StringStreamWrapper * @return StringStreamWrapper
*/ */
static public function Open($string) { static public function Open($string, $truncatehtmlsafe = false) {
$context = stream_context_create(array(self::PROTOCOL => array('string' => &$string))); $context = stream_context_create(array(self::PROTOCOL => array('string' => &$string, 'truncatehtmlsafe' => $truncatehtmlsafe)));
return fopen(self::PROTOCOL . "://",'r', false, $context); return fopen(self::PROTOCOL . "://",'r', false, $context);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment