Skip to content

Commit baf728e

Browse files
committed
simplify/removed duplicated code, have mailParser class do decoding, additional database fields
1 parent cd46556 commit baf728e

File tree

6 files changed

+128
-196
lines changed

6 files changed

+128
-196
lines changed

decode/MailParser.php

Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ public function decode($attachment_directory = null, $saving = true)
8989
$this->attachment_directory .= \DIRECTORY_SEPARATOR;
9090
}
9191

92-
9392
// http://pear.php.net/manual/en/package.mail.mail-mimedecode.decode.php
9493
$decoder = new Mail_mimeDecode($this->raw);
9594

@@ -228,6 +227,19 @@ private function extractHeadersAndBody()
228227
}
229228
}
230229

230+
/**
231+
* Add additional allowed mime types to the list.
232+
*
233+
* @param string
234+
*/
235+
public function addMimeType($mime_types = '')
236+
{
237+
if (\strpos($mime_types, '/') !== false)
238+
\array_push($this->allowedMimeTypes, $mime_types);
239+
240+
return $this;
241+
}
242+
231243
/**
232244
* @return string - UTF8 encoded
233245
*
@@ -465,14 +477,19 @@ public function getFrom()
465477
}
466478

467479
/**
468-
* Returns the sender name
480+
* Returns the name of To or From
469481
*
470482
* @return string
483+
* @throws \Exception if To or From is not found
471484
*/
472-
public function getFromName()
485+
private function getToFromName($toFrom = null)
473486
{
487+
if (empty($toFrom)) {
488+
throw new \Exception("Couldn't find To or From of the email");
489+
}
490+
474491
// the returned string is like "John Smith <john.smith@example.com>"
475-
$strName = $this->getHeader("from");
492+
$strName = $this->getHeader($toFrom);
476493

477494
//if returned string == "<john.smith@example.com>" there's not a name. So < and > chars are removed the result is checked
478495
$trans = array("<" => "", ">" => "");
@@ -487,15 +504,29 @@ public function getFromName()
487504
return \substr($strName, 0, \strpos($strName, '<') - 1);
488505
}
489506

507+
public function getFromName()
508+
{
509+
return $this->getToFromName("from");
510+
}
511+
512+
public function getToName()
513+
{
514+
return $this->getToFromName("to");
515+
}
516+
490517
/**
491-
* Returns the sender email
518+
* Returns the email of To or From
492519
*
493-
* @return string $str
520+
* @return string
494521
*/
495-
public function getFromEmail()
522+
private function getToFromEmail($toFrom = null)
496523
{
524+
if (empty($toFrom)) {
525+
throw new \Exception("Couldn't find To or From of the email");
526+
}
527+
497528
// the returned string is like "John Smith <john.smith@example.com>"
498-
$strEmail = $this->getHeader("from");
529+
$strEmail = $this->getHeader($toFrom);
499530

500531
//if returned string == "<john.smith@example.com>" there's not a name. So < and > chars are removed the result is checked
501532
$trans = array("<" => "", ">" => "");
@@ -510,6 +541,26 @@ public function getFromEmail()
510541
return \substr($strEmail, \strpos($strEmail, '<') + 1, \strlen($strEmail) - \strpos($strEmail, '<') - 2);
511542
}
512543

544+
public function getFromEmail()
545+
{
546+
return $this->getToFromEmail('from');
547+
}
548+
549+
public function getToEmail()
550+
{
551+
return $this->getToFromEmail('to');
552+
}
553+
554+
/**
555+
* Decode a single body part of an email message,
556+
* the body part of the email message, as parsed by Mail_mimeDecode.
557+
*
558+
* Recursive if nested body parts are found
559+
*
560+
* This is the meat of the script.
561+
*
562+
* @param mixed $body_part (required)
563+
*/
513564
private function decodePart($body_part, $saving = true)
514565
{
515566
if ( isset($body_part->ctype_parameters) && \is_array($body_part->ctype_parameters) ) {
@@ -562,14 +613,21 @@ private function decodePart($body_part, $saving = true)
562613

563614
private function isValidAttachment($mime_type)
564615
{
565-
if ( empty($this->allowedMimeTypes) || \in_array($mime_type, $this->allowedMimeTypes) ) {
566-
if ( empty($this->disallowedMimeTypes) || !\in_array($mime_type, $this->disallowedMimeTypes) ) {
567-
return true;
568-
}
616+
if (\in_array($mime_type, $this->allowedMimeTypes) && !\in_array($mime_type, $this->disallowedMimeTypes)) {
617+
return true;
569618
}
619+
570620
return false;
571621
}
572622

623+
/**
624+
* Save off a single file
625+
*
626+
* @param string $filename (required) The filename to use for this file
627+
* @param mixed $contents (required) The contents of the file we will save
628+
* @param string $mimeType (required) The mime-type of the file
629+
* @param bool $saving should we actual save to file system
630+
*/
573631
private function saveAttachment($filename, $contents, $mime_type = 'unknown', $saving = true)
574632
{
575633
$filename = \mb_convert_encoding($filename, $this->charset, $this->charset);
@@ -614,6 +672,12 @@ private function saveAttachment($filename, $contents, $mime_type = 'unknown', $s
614672
}
615673
}
616674

675+
/**
676+
* Format Bytes into human-friendly sizes
677+
* with the number of bytes in the largest applicable unit (eg. KB, MB, GB, TB)
678+
*
679+
* @return string
680+
*/
617681
private function formatBytes($bytes, $precision = 2)
618682
{
619683
$units = [ 'B', 'KB', 'MB', 'GB', 'TB' ];

0 commit comments

Comments
 (0)