Skip to content

Commit

Permalink
Рефакторинг кода
Browse files Browse the repository at this point in the history
  • Loading branch information
Afinogen committed Jun 9, 2016
1 parent c4c7972 commit 4b24f23
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 79 deletions.
6 changes: 3 additions & 3 deletions example/test-file.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@
//Перебор всех писем
//echo $storage->countMessage().PHP_EOL;

for($i=0; $i< $storage->countMessage(); $i++) {
for ($i = 0; $i < $storage->countMessage(); $i++) {
echo PHP_EOL.'---------------------------------------- '.$i.' ------------------------------------------------'.PHP_EOL;

$msg = $storage->getMessage($i);

echo $msg->getHeaders()->getFrom().PHP_EOL;
echo $msg->getHeaders()->getFromName().PHP_EOL;
echo $msg->getHeaders()->getTo().PHP_EOL;
echo $msg->getHeaders()->getCC().PHP_EOL;

echo $msg->getHeaders()->getSubject().PHP_EOL;

foreach ($msg->getParts() as $part) {
Expand Down
9 changes: 8 additions & 1 deletion example/test-pop3.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
// Composer
require('../vendor/autoload.php');

$storage = new \afinogen89\getmail\storage\Pop3(['host' => 'pop.gmail.com', 'user' => 'test@gmail.com', 'password' => '123456', 'ssl' => 'SSL']);
$storage = new \afinogen89\getmail\storage\Pop3(
[
'host' => 'pop.gmail.com',
'user' => 'test@gmail.com',
'password' => '123456',
'ssl' => 'SSL'
]
);

echo $storage->countMessages().PHP_EOL;

Expand Down
1 change: 1 addition & 0 deletions src/message/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

/**
* Class Attachment
*
* @package storage
*/
class Attachment
Expand Down
3 changes: 2 additions & 1 deletion src/message/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

/**
* Class Content
*
* @package storage
*/
class Content
Expand Down Expand Up @@ -47,7 +48,7 @@ public function getContentDecode()
$content = '';
if ($this->transferEncoding == 'base64') {
$content = base64_decode($this->content);
} else if ($this->transferEncoding == 'quoted-printable'){
} elseif ($this->transferEncoding == 'quoted-printable') {
$content = quoted_printable_decode($this->content);
} else {
$content = $this->content;
Expand Down
36 changes: 19 additions & 17 deletions src/message/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

/**
* Class Headers
*
* @package storage
*/
class Headers
Expand Down Expand Up @@ -36,7 +37,7 @@ class Headers
/** @var string */
private $_transferEncoding;
/** @var bool */
private $_isAutoReply;
private $_isAutoReply;

/**
* @param string $headers
Expand All @@ -60,38 +61,37 @@ protected function parserHeaders()
$this->_subject = isset($headers['subject']) ? self::decodeMimeString(current($headers['subject'])) : '';

preg_match(self::EMAIL_PATTERN, $this->_from, $email);
if(!empty($email)){
$email= $email[0];
}else{
if (!empty($email)) {
$email = $email[0];
} else {
$email = null;
new \Exception('Email not found in "'.$this->_from.'"');
}

$this->_fromName = str_replace($email, '', $this->_from);
$this->_fromName = trim($this->_fromName, '<>,');
$this->_fromName = trim($this->_fromName,' "');
$this->_fromName = trim($this->_fromName, ' "');

$this->_from = $email;

//TODO может быть несколько получателей
preg_match(self::EMAIL_PATTERN, $this->_to, $email);
if(!empty($email)){
if (!empty($email)) {
$this->_to = $email[0];
}

$part = current($headers['content-type']);
$this->_messageContentType = trim(explode(';', $part)[0]);

if (preg_match_all('/(boundary|charset)\s*\=\s*["\']?([\w\-\/\=]+)/i', $part, $result))
{
foreach($result[1] as $key=>$val) {
if (preg_match_all('/(boundary|charset)\s*\=\s*["\']?([\w\-\/\=]+)/i', $part, $result)) {
foreach ($result[1] as $key => $val) {
$val = '_'.strtolower($val);
$this->{$val} = $result[2][$key];
}
}

$this->parseAutoReply();

if (isset($headers['content-transfer-encoding'])) {
$this->_transferEncoding = trim(current($headers['content-transfer-encoding']));
}
Expand All @@ -101,7 +101,7 @@ public function parseAutoReply()
{
//TODO проверить другие варианты
$replays = ['auto-replied'];

$headers = $this->asArray();
if (isset($headers['auto-submitted']) && is_array($headers['auto-submitted'])) {
$headers['auto-submitted'] = current($headers['auto-submitted']);
Expand All @@ -118,13 +118,14 @@ public function parseAutoReply()

/**
* @param string $strMime
*
* @return string
*/
public static function decodeMimeString($strMime)
{
$items = preg_split('/[\r\n]{2,}/si', $strMime);
$result = '';
foreach($items as $item) {
foreach ($items as $item) {
$data = explode('?', $item);
$str = '';
if (!empty($data)) {
Expand All @@ -133,11 +134,11 @@ public static function decodeMimeString($strMime)
$type = strtoupper(array_shift($data));
if ($type == 'B') {
$str = base64_decode(array_shift($data));
$str = $str . ltrim($data[0], '=');
$str = $str.ltrim($data[0], '=');
} elseif ($type == 'Q') {
$str = quoted_printable_decode(array_shift($data));
}
if (!empty($encode)){
if (!empty($encode)) {
$str = mb_convert_encoding($str, 'UTF-8', $encode);
}
}
Expand Down Expand Up @@ -253,16 +254,17 @@ public function asArray()

/**
* @param string $headers
*
* @return array
*/
public static function toArray($headers)
{
$headers .= "\r\n\r\n";
preg_match_all('#[\r\n]*([\w-]+\:)(.+?)(?=([\r\n]+[\w-]+\:|[\r\n]{3,}|\n{2,}))#si',$headers,$result);
preg_match_all('#[\r\n]*([\w-]+\:)(.+?)(?=([\r\n]+[\w-]+\:|[\r\n]{3,}|\n{2,}))#si', $headers, $result);

$headers = [];
foreach ($result[1] as $k => $header) {
$header = strtolower(rtrim($header , ':'));
$header = strtolower(rtrim($header, ':'));
$headers[$header][] = trim($result[2][$k]);
}

Expand Down
40 changes: 28 additions & 12 deletions src/message/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

/**
* Class Message
*
* @package storage
*/
class Message
Expand Down Expand Up @@ -48,6 +49,7 @@ public function __construct($header, $message, $id = null)

/**
* @param string $path
*
* @return int
*/
public function saveToFile($path)
Expand Down Expand Up @@ -89,6 +91,7 @@ public function getAttachments()

/**
* Текст письма
*
* @return Content|null
*/
public function getMsgBody()
Expand All @@ -114,6 +117,7 @@ public function getMsgBody()

/**
* Альтернативный текст письма
*
* @return Content|null
*/
public function getMsgAlternativeBody()
Expand All @@ -130,6 +134,7 @@ public function getMsgAlternativeBody()

/**
* Разбор тела сообщения
*
* @param string $boundary
* @param string $content
*/
Expand All @@ -155,7 +160,9 @@ protected function parserContent($boundary, $content)
$type = trim($data[0]);

//get body message
if ($type == Content::CT_MULTIPART_ALTERNATIVE || $type == Content::CT_TEXT_HTML || $type == Content::CT_TEXT_PLAIN || $type == Content::CT_MESSAGE_DELIVERY) {
if ($type == Content::CT_MULTIPART_ALTERNATIVE || $type == Content::CT_TEXT_HTML || $type == Content::CT_TEXT_PLAIN
|| $type == Content::CT_MESSAGE_DELIVERY
) {
$this->parserBodyMessage($part);
} elseif ($type == Content::CT_MESSAGE_RFC822) {
$this->parserBodyMessage($part);
Expand Down Expand Up @@ -246,7 +253,7 @@ protected function parserAttachment($part)
$data = explode(';', current($headers['content-type']));

$attachment->contentType = trim($data[0]);

array_shift($data);
if (count($data) == 1) {
$name = preg_replace('#.*name\s*\=\s*[\'"]([^\'"]+).*#si', '$1', $data[0]);
Expand All @@ -261,7 +268,12 @@ protected function parserAttachment($part)
}
$name = Headers::decodeMimeString($name);
$encode = mb_detect_encoding($name, ['UTF-8', 'Windows-1251']);
$encode = mb_detect_encoding(
$name, [
'UTF-8',
'Windows-1251'
]
);
if ($encode && $encode !== 'UTF-8') {
$name = mb_convert_encoding($name, 'UTF-8', $encode);
}
Expand All @@ -272,23 +284,23 @@ protected function parserAttachment($part)
if (isset($headers['content-disposition'])) {
$data = explode(';', current($headers['content-disposition']));
$attachment->contentDisposition = trim($data[0]);

$tmpName = $data;

unset($tmpName[0]);
foreach($tmpName as $key=>$val) {
if (preg_match('/[\w\-]{3,}\=/i', trim($val), $result) && stripos($result[0], 'name') ===false) {
foreach ($tmpName as $key => $val) {
if (preg_match('/[\w\-]{3,}\=/i', trim($val), $result) && stripos($result[0], 'name') === false) {
unset($tmpName[$key]);
} else {
$tmpName[$key] = preg_replace('/(file)?name\s*(\*\d+\*)?\s*\=/i', '', $val);
}
}

$tmpName = implode($tmpName);
$tmpName=preg_replace('#\s+#s',"\n\n",$tmpName);
if(preg_match_all($pattern, $tmpName, $result)){
$tmpName = preg_replace('#\s+#s', "\n\n", $tmpName);
if (preg_match_all($pattern, $tmpName, $result)) {
$name = [];
foreach($result[3] as $v){
foreach ($result[3] as $v) {
$name[] = $v;
}

Expand All @@ -299,9 +311,9 @@ protected function parserAttachment($part)
if (mb_detect_encoding($name) != 'UTF-8') {
$name = mb_convert_encoding($name, 'UTF-8');
}

$attachment->filename = $name;
}else{
} else {
$name = trim(preg_replace('/(file)?name\s*(\*\d+\*)?\s*\=/i', '', $name));
$attachment->filename = $name;//Headers::decodeMimeString($name);
}
Expand All @@ -322,12 +334,16 @@ protected function parserAttachment($part)

/**
* @param string $str
*
* @return array
*/
public static function splitContent($str)
{
$data = preg_split('/[\r\n]{3,}/si', $str);

return ['header' => array_shift($data), 'content' => implode("\r\n", $data)];
return [
'header' => array_shift($data),
'content' => implode("\r\n", $data)
];
}
}
Loading

0 comments on commit 4b24f23

Please sign in to comment.