Fixed CRLF handling in the gui

Signed-off-by: Janos SUTO <sj@acts.hu>
This commit is contained in:
Janos SUTO 2023-06-04 18:24:18 +02:00
parent 5cacb212e9
commit 30a89d856f
4 changed files with 29 additions and 39 deletions

View File

@ -434,7 +434,7 @@ define('TABLE_PRIVATE', 'private');
define('TABLE_DELETED', 'deleted'); define('TABLE_DELETED', 'deleted');
define('VIEW_MESSAGES', 'v_messages'); define('VIEW_MESSAGES', 'v_messages');
define('EOL', "\n"); define('EOL', "\r\n");
define('DIR_SYSTEM', DIR_BASE . 'system/'); define('DIR_SYSTEM', DIR_BASE . 'system/');
define('DIR_MODEL', DIR_BASE . 'model/'); define('DIR_MODEL', DIR_BASE . 'model/');

View File

@ -8,6 +8,9 @@ class ModelMailMail extends Model {
require_once 'Zend/Mail/Protocol/Smtp.php'; require_once 'Zend/Mail/Protocol/Smtp.php';
require_once 'Zend/Mail/Protocol/Smtp/Auth/Login.php'; require_once 'Zend/Mail/Protocol/Smtp/Auth/Login.php';
// Workaround for the zend framework
$msg = str_replace("\r", "", $msg);
$ok = 0; $ok = 0;
if($to == "" || strlen($msg) < 30){ return $ok; } if($to == "" || strlen($msg) < 30){ return $ok; }

View File

@ -224,6 +224,7 @@ class ModelSearchMessage extends Model {
if($html == 0) { if($html == 0) {
foreach($terms as $k => $v) { foreach($terms as $k => $v) {
$s = preg_replace("/THE_BREAK_HTML_TAG/", "<br />", $s);
$s = preg_replace("/$v/i", "<span class=\"mssghglght\">$v</span>", $s); $s = preg_replace("/$v/i", "<span class=\"mssghglght\">$v</span>", $s);
} }

View File

@ -5,6 +5,12 @@ class Piler_Mime_Decode {
const HEADER_FIELDS = ['from', 'to', 'cc', 'subject', 'date']; const HEADER_FIELDS = ['from', 'to', 'cc', 'subject', 'date'];
public static function normalize_message($message) {
$a = preg_split("/\r?\n/", $message);
return implode(EOL, $a);
}
public static function parseMessage($message, &$result) { public static function parseMessage($message, &$result) {
self::splitMessage($message, $headers, $body); self::splitMessage($message, $headers, $body);
@ -53,11 +59,9 @@ class Piler_Mime_Decode {
$start = 0; $start = 0;
$res = array(); $res = array();
$body = self::remove_LF($body);
// Extract the mime parts excluding the boundary itself // Extract the mime parts excluding the boundary itself
$p = strpos($body, '--' . $boundary . "\n", $start); $p = strpos($body, '--' . $boundary . EOL, $start);
if($p === false) { if($p === false) {
// no parts found! // no parts found!
return array(); return array();
@ -67,7 +71,7 @@ class Piler_Mime_Decode {
$start = $p + 3 + strlen($boundary); $start = $p + 3 + strlen($boundary);
while(($p = strpos($body, '--' . $boundary . "\n", $start)) !== false) { while(($p = strpos($body, '--' . $boundary . EOL, $start)) !== false) {
$res[] = substr($body, $start, $p-$start); $res[] = substr($body, $start, $p-$start);
$start = $p + 3 + strlen($boundary); $start = $p + 3 + strlen($boundary);
} }
@ -86,22 +90,20 @@ class Piler_Mime_Decode {
} }
public static function splitMessage($message, &$headers, &$body, $EOL = "\n") { public static function splitMessage($message, &$headers, &$body) {
self::splitMessageRaw($message, $headers, $journal, $body); self::splitMessageRaw($message, $headers, $journal, $body);
$headers = self::splitHeaders($headers); $headers = self::splitHeaders($headers);
} }
public static function splitMessageRaw($message, &$headers, &$journal, &$body, $EOL = "\n") { public static function splitMessageRaw($message, &$headers, &$journal, &$body) {
$headers = []; $headers = [];
$body = ''; $body = '';
$message = self::remove_LF($message);
// Find an empty line between headers and body, otherwise we got a header-only message // Find an empty line between headers and body, otherwise we got a header-only message
if(strpos($message, $EOL . $EOL)) { if(strpos($message, EOL . EOL)) {
list($headers, $body) = explode($EOL . $EOL, $message, 2); list($headers, $body) = explode(EOL . EOL, $message, 2);
// Check if the header is actually a journal header // Check if the header is actually a journal header
$headers_array = self::splitHeaders($headers); $headers_array = self::splitHeaders($headers);
@ -113,7 +115,7 @@ class Piler_Mime_Decode {
if(count($parts) >= 2) { if(count($parts) >= 2) {
self::splitMessageRaw($parts[0], $s, $j, $journal); self::splitMessageRaw($parts[0], $s, $j, $journal);
$i = strpos($parts[1], $EOL . $EOL); $i = strpos($parts[1], EOL . EOL);
$msg = substr($parts[1], $i); $msg = substr($parts[1], $i);
$i = 0; $i = 0;
@ -141,26 +143,16 @@ class Piler_Mime_Decode {
} }
public static function removeJournal(&$message, $EOL = "\n") { public static function removeJournal(&$message) {
$has_journal = 0; $has_journal = 0;
$crlfs = substr_count($message, "\r\n");
self::splitMessageRaw($message, $headers, $journal, $body); self::splitMessageRaw($message, $headers, $journal, $body);
if($journal) { if($journal) {
$has_journal = 1; $has_journal = 1;
} }
// If the message has >10 CRLF sequences, then we assume $message = $headers . EOL . EOL . $body;
// that we need to restore the removed LF characters
if($crlfs > 10) {
$headers = str_replace("\n", "\r\n", $headers);
$body = str_replace("\n", "\r\n", $body);
$EOL = "\r\n";
}
$message = $headers . $EOL . $EOL . $body;
return $has_journal; return $has_journal;
} }
@ -222,7 +214,7 @@ class Piler_Mime_Decode {
$last_token = ''; $last_token = '';
$result = array(); $result = array();
$headers = explode("\n", $headers); $headers = explode(EOL, $headers);
foreach($headers as $h) { foreach($headers as $h) {
@ -236,7 +228,7 @@ class Piler_Mime_Decode {
// Skip line if it doesn't have a colon (:) and the 1st character is not a whitespace // Skip line if it doesn't have a colon (:) and the 1st character is not a whitespace
if(!ctype_space($h[0]) && !strchr($h, ':')) { continue; } if($h && !ctype_space($h[0]) && !strchr($h, ':')) { continue; }
if($line) { if($line) {
if(substr($line[0], -1) == ':') { if(substr($line[0], -1) == ':') {
@ -256,7 +248,7 @@ class Piler_Mime_Decode {
} }
else { else {
if($token) { if($token) {
$result[$last_token] .= "\n"; $result[$last_token] .= EOL;
} }
$result[$last_token] .= ' ' . $line_str; $result[$last_token] .= ' ' . $line_str;
@ -267,8 +259,8 @@ class Piler_Mime_Decode {
foreach($result as $k => $v) { foreach($result as $k => $v) {
if(strchr($v, "\n")) { if(strchr($v, EOL)) {
$result[$k] = explode("\n", $v); $result[$k] = explode(EOL, $v);
} }
} }
@ -299,12 +291,6 @@ class Piler_Mime_Decode {
} }
public static function remove_LF($message = '') {
return str_replace("\r", "", $message);
//return preg_replace("/\r/", "", $message);
}
public static function getBoundary($headers = array()) { public static function getBoundary($headers = array()) {
if(isset($headers['content-type']['boundary'])) { if(isset($headers['content-type']['boundary'])) {
return $headers['content-type']['boundary']; return $headers['content-type']['boundary'];
@ -335,8 +321,8 @@ class Piler_Mime_Decode {
if(strtolower($headers['content-type']['type']) == 'text/plain') { if(strtolower($headers['content-type']['type']) == 'text/plain') {
$body = self::escape_lt_gt_symbols($body); $body = self::escape_lt_gt_symbols($body);
$body = preg_replace("/\n/", "<br />\n", $body); $body = preg_replace("/\n/", "THE_BREAK_HTML_TAG\n", $body);
$body = "\n" . self::printNicely($body); $body = EOL . self::printNicely($body);
} }
return $body; return $body;
@ -361,9 +347,9 @@ class Piler_Mime_Decode {
$nice .= $x[$i] . " "; $nice .= $x[$i] . " ";
$k += strlen($x[$i]); $k += strlen($x[$i]);
if(strstr($x[$i], "\n")){ $k = 0; } if(strstr($x[$i], EOL)){ $k = 0; }
if($k > 70){ $nice .= "\n"; $k = 0; } if($k > 70){ $nice .= EOL; $k = 0; }
} }
return $nice; return $nice;