mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Striping tags in the message.
This commit is contained in:
parent
2795d2b771
commit
3a1ebf159a
2 changed files with 130 additions and 3 deletions
|
@ -843,7 +843,6 @@ class CakeEmail {
|
||||||
* @param string $message Message to wrap
|
* @param string $message Message to wrap
|
||||||
* @return array Wrapped message
|
* @return array Wrapped message
|
||||||
* @access protected
|
* @access protected
|
||||||
* @TODO Do not wrap tags
|
|
||||||
*/
|
*/
|
||||||
function _wrap($message) {
|
function _wrap($message) {
|
||||||
$message = str_replace(array("\r\n", "\r"), "\n", $message);
|
$message = str_replace(array("\r\n", "\r"), "\n", $message);
|
||||||
|
@ -851,10 +850,84 @@ class CakeEmail {
|
||||||
$formatted = array();
|
$formatted = array();
|
||||||
|
|
||||||
foreach ($lines as $line) {
|
foreach ($lines as $line) {
|
||||||
|
if (empty($line)) {
|
||||||
|
$formatted[] = '';
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if ($line[0] === '.') {
|
if ($line[0] === '.') {
|
||||||
$line = '.' . $line;
|
$line = '.' . $line;
|
||||||
}
|
}
|
||||||
$formatted = array_merge($formatted, explode("\n", wordwrap($line, self::LINE_LENGTH_SHOULD, "\n", true)));
|
if (!preg_match('/\<[a-z]/i', $line)) {
|
||||||
|
$formatted = array_merge($formatted, explode("\n", wordwrap($line, self::LINE_LENGTH_SHOULD, "\n", true)));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$tagOpen = false;
|
||||||
|
$tmpLine = $tag = '';
|
||||||
|
$tmpLineLength = 0;
|
||||||
|
for ($i = 0, $count = strlen($line); $i < $count; $i++) {
|
||||||
|
$char = $line[$i];
|
||||||
|
if ($tagOpen) {
|
||||||
|
$tag .= $char;
|
||||||
|
if ($char === '>') {
|
||||||
|
$tagLength = strlen($tag);
|
||||||
|
if ($tagLength + $tmpLineLength < self::LINE_LENGTH_SHOULD) {
|
||||||
|
$tmpLine .= $tag;
|
||||||
|
$tmpLineLength += $tagLength;
|
||||||
|
} else {
|
||||||
|
if ($tmpLineLength > 0) {
|
||||||
|
$formatted[] = trim($tmpLine);
|
||||||
|
$tmpLine = '';
|
||||||
|
$tmpLineLength = 0;
|
||||||
|
}
|
||||||
|
if ($tagLength > self::LINE_LENGTH_SHOULD) {
|
||||||
|
$formatted[] = $tag;
|
||||||
|
} else {
|
||||||
|
$tmpLine = $tag;
|
||||||
|
$tmpLineLength = $tagLength;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$tag = '';
|
||||||
|
$tagOpen = false;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ($char === '<') {
|
||||||
|
$tagOpen = true;
|
||||||
|
$tag = '<';
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ($char === ' ' && $tmpLineLength >= self::LINE_LENGTH_SHOULD) {
|
||||||
|
pr(1);
|
||||||
|
$formatted[] = $tmpLine;
|
||||||
|
$tmpLineLength = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$tmpLine .= $char;
|
||||||
|
$tmpLineLength++;
|
||||||
|
if ($tmpLineLength === self::LINE_LENGTH_SHOULD) {
|
||||||
|
$nextChar = $line[$i + 1];
|
||||||
|
if ($nextChar === ' ' || $nextChar === '<') {
|
||||||
|
$formatted[] = trim($tmpLine);
|
||||||
|
$tmpLine = '';
|
||||||
|
$tmpLineLength = 0;
|
||||||
|
if ($nextChar === ' ') {
|
||||||
|
$i++;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$lastSpace = strrpos($tmpLine, ' ');
|
||||||
|
if ($lastSpace === false) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$formatted[] = trim(substr($tmpLine, 0, $lastSpace));
|
||||||
|
$tmpLine = substr($tmpLine, $lastSpace + 1);
|
||||||
|
$tmpLineLength = strlen($tmpLine);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!empty($tmpLine)) {
|
||||||
|
$formatted[] = $tmpLine;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$formatted[] = '';
|
$formatted[] = '';
|
||||||
return $formatted;
|
return $formatted;
|
||||||
|
|
|
@ -29,7 +29,15 @@ class TestCakeEmail extends CakeEmail {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function formatAddress($address) {
|
public function formatAddress($address) {
|
||||||
return $this->_formatAddress($address);
|
return parent::_formatAddress($address);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrap to protected method
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function wrap($text) {
|
||||||
|
return parent::_wrap($text);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -306,4 +314,50 @@ class CakeEmailTest extends CakeTestCase {
|
||||||
$this->assertIdentical($this->CakeEmail->getTo(), array());
|
$this->assertIdentical($this->CakeEmail->getTo(), array());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* testWrap method
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testWrap() {
|
||||||
|
$text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac turpis orci, non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.';
|
||||||
|
$result = $this->CakeEmail->wrap($text);
|
||||||
|
$expected = array(
|
||||||
|
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac turpis orci,',
|
||||||
|
'non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.',
|
||||||
|
''
|
||||||
|
);
|
||||||
|
$this->assertIdentical($result, $expected);
|
||||||
|
|
||||||
|
$text = 'Lorem ipsum dolor sit amet, consectetur < adipiscing elit. Donec ac turpis orci, non commodo odio. Morbi nibh nisi, vehicula > pellentesque accumsan amet.';
|
||||||
|
$result = $this->CakeEmail->wrap($text);
|
||||||
|
$expected = array(
|
||||||
|
'Lorem ipsum dolor sit amet, consectetur < adipiscing elit. Donec ac turpis',
|
||||||
|
'orci, non commodo odio. Morbi nibh nisi, vehicula > pellentesque accumsan',
|
||||||
|
'amet.',
|
||||||
|
''
|
||||||
|
);
|
||||||
|
$this->assertIdentical($result, $expected);
|
||||||
|
|
||||||
|
$text = '<p>Lorem ipsum dolor sit amet,<br> consectetur adipiscing elit.<br> Donec ac turpis orci, non <b>commodo</b> odio. <br /> Morbi nibh nisi, vehicula pellentesque accumsan amet.<hr></p>';
|
||||||
|
$result = $this->CakeEmail->wrap($text);
|
||||||
|
$expected = array(
|
||||||
|
'<p>Lorem ipsum dolor sit amet,<br> consectetur adipiscing elit.<br> Donec ac',
|
||||||
|
'turpis orci, non <b>commodo</b> odio. <br /> Morbi nibh nisi, vehicula',
|
||||||
|
'pellentesque accumsan amet.<hr></p>',
|
||||||
|
''
|
||||||
|
);
|
||||||
|
$this->assertIdentical($result, $expected);
|
||||||
|
|
||||||
|
$text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac <a href="http://cakephp.org">turpis</a> orci, non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.';
|
||||||
|
$result = $this->CakeEmail->wrap($text);
|
||||||
|
$expected = array(
|
||||||
|
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac',
|
||||||
|
'<a href="http://cakephp.org">turpis</a> orci, non commodo odio. Morbi nibh',
|
||||||
|
'nisi, vehicula pellentesque accumsan amet.',
|
||||||
|
''
|
||||||
|
);
|
||||||
|
$this->assertIdentical($result, $expected);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue