Commit 48a596d2 authored by Roman Mátyus's avatar Roman Mátyus
Browse files

Refactoring and fix plaintext mails

parent ebbe8dd0
......@@ -47,8 +47,7 @@ class FileMailer extends Object implements IMailer
{
$this->checkRequirements();
$content = $message->generateMessage();
preg_match("/----------[a-z0-9]{10}--/", $content, $match);
$message_id = substr($match[0], 10, -2);
$message_id = self::mailParser($content)->message_id;
$path = $this->tempDir."/".$this->prefix.$message_id;
if ($bytes = file_put_contents($path, $content))
return $bytes;
......@@ -76,5 +75,65 @@ class FileMailer extends Object implements IMailer
{
return $this->prefix;
}
/**
* Parser of stored files.
* @param string $content
* @return StdClass
*/
public static function mailParser($content)
{
preg_match("/Message-ID: <[a-zA-Z0-9-]*@[a-zA-Z0-9-]*>/", $content, $match);
$message_id = (isset($match[0])) ? substr($match[0], 13, -2) : NULL;
$message_id = explode("@",$message_id);
$message_id = $message_id[0];
}
\ No newline at end of file
$mess = explode("\r\n\r\n", $content);
preg_match_all("/[a-zA-Z-]*: .*/", $mess[0], $matches);
$header = array();
foreach ($matches[0] as $line) {
$temp = explode(": ",$line);
$header[strtolower($temp[0])] = iconv_mime_decode(str_replace(array("\r","\n","\r\n"),"",$temp[1]));
}
if (isset($header["date"]))
$header["date"] = new DateTime($header["date"]);
if (preg_match("/\r\n\r\n----------/", $content)) { // html mail
$mess = explode("\r\n\r\n----------", $content);
$mess = substr($mess[1], 10, -22);
$mess = explode("----------", $mess);
$temp_mess = array();
foreach ($mess as $part) {
if (preg_match("/text\/html/", $part))
$temp_mess["html"] = $part;
elseif (preg_match("/text\/plain/", $part))
$temp_mess["plain"] = $part;
}
$mess = $temp_mess;
$temp_mess = array();
foreach ($mess as $type => $part) {
$temp_mess[$type] = explode("\r\n",$part);
for($i=0;$i<=3;$i++)
unset($temp_mess[$type][$i]);
$temp_mess[$type] = implode("\r\n",$temp_mess[$type]);
}
$mess = $temp_mess;
} else {
$mess = array(
"plain" => $mess[1],
"html" => NULL,
);
}
return (object) array_merge(
array(
"message_id" => $message_id,
"header" => $header,
"plain" => $mess['plain'],
"html" => $mess['html'],
"raw" => $content,
),
$header
);
}
}
......@@ -40,12 +40,11 @@ img#mailpanel-icon {
border:1px solid silver;
}
</style>
<h1>MailPanel: {if $countAll==0}no{else}{$countAll}{/if} mail{if $countAll>1}s{/if}, {$countNew} new</h1>
<div n:if="$countAll>0"><a href="/?mail-panel=delete-all">Delete All</a></div>
<div n:if="$countAll>0" class="nette-inner">
<div n:foreach="$messages as $message">
<a class="nette-toggler" rel="#nette-mailPanel-message-{$message->message_id}" href="#">{$message->date|date:"d.m.Y H:i:s"} {$message->subject} ►</a>
<a class="nette-toggler" rel="#nette-mailPanel-message-{$message->message_id}" href="#">{$message->date} {$message->subject} ►</a>
<div id="nette-mailPanel-message-{$message->message_id}" class="nette-collapsed">
<table n:if="!empty($show)" n:inner-foreach="$show as $item">
<tr n:if="$message->$item">
......@@ -70,8 +69,8 @@ img#mailpanel-icon {
<pre>{$message->plain}</pre>
</div>
</span>
<a class="nette-toggler" rel="#nette-mailPanel-message-html-{$message->message_id}" href="#">html ►</a>
<span id="nette-mailPanel-message-html-{$message->message_id}" class="nette-collapsed">
<a n:if="$message->html" class="nette-toggler" rel="#nette-mailPanel-message-html-{$message->message_id}" href="#">html ►</a>
<span n:if="$message->html" id="nette-mailPanel-message-html-{$message->message_id}" class="nette-collapsed">
<div class="nette-mailPanel-box">
{!$message->html}
</div>
......@@ -84,4 +83,4 @@ img#mailpanel-icon {
</span>
</div>
</div>
</div>
\ No newline at end of file
</div>
......@@ -164,7 +164,7 @@ class MailPanel extends Control implements IBarPanel {
foreach (Finder::findFiles('*')->in($this->fileMailer->tempDir) as $file) {
$message = $this->cache->load($file->getFilename());
if ($message === NULL) {
$message = self::mailParser(file_get_contents($file));
$message = FileMailer::mailParser(file_get_contents($file));
$this->cache->save($file->getFilename(),$message);
}
$time = new DateTime;
......@@ -176,58 +176,6 @@ class MailPanel extends Control implements IBarPanel {
return TRUE;
}
/**
* Parser of stored files.
* @param string $content
* @return StdClass
*/
public static function mailParser($content)
{
preg_match("/----------[a-z0-9]{10}--/", $content, $match);
$message_id = (isset($match[0])) ? substr($match[0], 10, -2) : NULL;
$mess = explode("\r\n\r\n----------", $content);
preg_match_all("/[a-zA-Z-]*: .*/", $mess[0], $matches);
$header = array();
foreach ($matches[0] as $line) {
$temp = explode(": ",$line);
$header[strtolower($temp[0])] = iconv_mime_decode(substr($temp[1],0,-1));
}
if (isset($header["date"]))
$header["date"] = new DateTime($header["date"]);
$mess = explode("\r\n\r\n----------", $content);
$mess = substr($mess[1], 10, -22);
$mess = explode("----------", $mess);
$temp_mess = array();
foreach ($mess as $part) {
if (preg_match("/text\/html/", $part))
$temp_mess["html"] = $part;
elseif (preg_match("/text\/plain/", $part))
$temp_mess["plain"] = $part;
}
$mess = $temp_mess;
$temp_mess = array();
foreach ($mess as $type => $part) {
$temp_mess[$type] = explode("\r\n",$part);
for($i=0;$i<=3;$i++)
unset($temp_mess[$type][$i]);
$temp_mess[$type] = implode("\r\n",$temp_mess[$type]);
}
$mess = $temp_mess;
return (object) array_merge(
array(
"message_id" => $message_id,
"header" => $header,
"plain" => $mess['plain'],
"html" => $mess['html'],
"raw" => $content,
),
$header
);
}
/**
* Autoremove mails from filesystem an cache by argument 'autoremove'.
*/
......@@ -247,4 +195,4 @@ class MailPanel extends Control implements IBarPanel {
}
}
}
\ No newline at end of file
}
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