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 {
private $position;
private $streamlength;
private $toTruncate;
private $truncateHtmlSafe;
/**
* Opens the stream
......@@ -52,6 +53,7 @@ class MAPIStreamWrapper {
$this->position = 0;
$this->toTruncate = false;
$this->truncateHtmlSafe = (isset($contextOptions[self::PROTOCOL]['truncatehtmlsafe'])) ? $contextOptions[self::PROTOCOL]['truncatehtmlsafe'] : false;
// this is our stream!
$this->mapistream = $contextOptions[self::PROTOCOL]['stream'];
......@@ -65,7 +67,7 @@ class MAPIStreamWrapper {
$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;
}
......@@ -95,7 +97,7 @@ class MAPIStreamWrapper {
// we need to truncate UTF8 compatible if ftruncate() was called
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;
......@@ -175,13 +177,14 @@ class 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
* @return MAPIStreamWrapper
*/
static public function Open($mapistream) {
$context = stream_context_create(array(self::PROTOCOL => array('stream' => &$mapistream)));
static public function Open($mapistream, $truncatehtmlsafe = false) {
$context = stream_context_create(array(self::PROTOCOL => array('stream' => &$mapistream, 'truncatehtmlsafe' => $truncatehtmlsafe)));
return fopen(self::PROTOCOL . "://",'r', false, $context);
}
}
......
......@@ -30,6 +30,7 @@ class StringStreamWrapper {
private $stringstream;
private $position;
private $stringlength;
private $truncateHtmlSafe;
/**
* Opens the stream
......@@ -53,9 +54,10 @@ class StringStreamWrapper {
// this is our stream!
$this->stringstream = $contextOptions[self::PROTOCOL]['string'];
$this->truncateHtmlSafe = (isset($contextOptions[self::PROTOCOL]['truncatehtmlsafe'])) ? $contextOptions[self::PROTOCOL]['truncatehtmlsafe'] : false;
$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;
}
......@@ -135,7 +137,7 @@ class StringStreamWrapper {
*/
public function stream_truncate ($new_size) {
// 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);
if ($this->position > $this->stringlength) {
......@@ -161,13 +163,14 @@ class 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
* @return StringStreamWrapper
*/
static public function Open($string) {
$context = stream_context_create(array(self::PROTOCOL => array('string' => &$string)));
static public function Open($string, $truncatehtmlsafe = false) {
$context = stream_context_create(array(self::PROTOCOL => array('string' => &$string, 'truncatehtmlsafe' => $truncatehtmlsafe)));
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