Skip to content

Commit

Permalink
Merge pull request #344 from barbushin/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Sebbo94BY committed Jun 4, 2019
2 parents 07ffd1e + 213c861 commit 3a8b766
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 32 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ Initially released in December 2012, the PHP IMAP Mailbox is a powerful and open
### Requirements

* PHP 5.6, 7.0, 7.1, 7.2 or 7.3
* IMAP extension must be present; so make sure this line is active in your php.ini: `extension=php_imap.dll`
* PHP `imap` extension must be present; so make sure this line is active in your php.ini: `extension=php_imap.dll`
* PHP `mbstring` extension must be present; so make sure this line is active in your php.ini: `extension=php_mbstring.dll`
* PHP `iconv` extension must be present, if `mbstring` is not available; so make sure this line is active in your php.ini: `extension=php_iconv.dll`

### Installation by Composer

Expand Down Expand Up @@ -81,6 +83,14 @@ if(!$mailsIds) {
// save all attachments to the specified directory
$mail = $mailbox->getMail($mailsIds[0]);

// Show, if $mail has one or more attachments
echo "\nMail has attachments? ";
if($mail->hasAttachments()) {
echo "Yes\n";
} else {
echo "No\n";
}

// Print all information of $mail
print_r($mail);

Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
],
"require": {
"php": ">=5.6",
"ext-imap": "*"
"ext-imap": "*",
"ext-mbstring": "*",
"ext-iconv": "*"
},
"require-dev": {
"phpunit/phpunit": "^5.7"
Expand Down
20 changes: 19 additions & 1 deletion src/PhpImap/IncomingMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class IncomingMail extends IncomingMailHeader {

/** @var IncomingMailAttachment[] */
protected $attachments = array();
protected $hasAttachments = false;
protected $dataInfo = array([],[]);

public function setHeader(IncomingMailHeader $header) {
Expand Down Expand Up @@ -44,10 +45,27 @@ public function __get ($name) {
return $this->$name;
}

public function addAttachment(IncomingMailAttachment $attachment) {
public function addAttachment(IncomingMailAttachment $attachment) {
$this->attachments[$attachment->id] = $attachment;
}

/**
* Sets property $hasAttachments
* @param boolean $hasAttachments True, if IncomingMail[] has one or more attachments
* @return void
*/
public function setHasAttachments($hasAttachments) {
$this->hasAttachments = $hasAttachments;
}

/**
* Returns, if the mail has attachments or not
* @return boolean true or false
*/
public function hasAttachments() {
return $this->hasAttachments;
}

/**
* @return IncomingMailAttachment[]
*/
Expand Down
55 changes: 32 additions & 23 deletions src/PhpImap/Mailbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
use stdClass;
use Exception;
use DateTime;
use PhpImap\IncomingMail;
use PhpImap\IncomingMailHeader;
use PhpImap\IncomingMailAttachment;
use PhpImap\Exceptions\ConnectionException;
use PhpImap\Exceptions\InvalidParameterException;

Expand Down Expand Up @@ -105,7 +102,11 @@ public function getServerEncoding() {
public function setServerEncoding($serverEncoding) {
$serverEncoding = strtoupper(trim($serverEncoding));

$supported_encodings = mb_list_encodings();
// https://github.com/barbushin/php-imap/issues/336
$supported_encodings = array();
if(extension_loaded('mbstring')) {
$supported_encodings = mb_list_encodings();
}

if(!in_array($serverEncoding, $supported_encodings) && $serverEncoding != "US-ASCII") {
throw new InvalidParameterException('"'.$serverEncoding.'" is not supported by setServerEncoding(). Your system only supports these encodings: US-ASCII, ' . implode(", ", $supported_encodings));
Expand All @@ -124,7 +125,7 @@ public function getImapSearchOption() {

/**
* Sets / Changes the IMAP search option
* @return string IMAP search option (eg. 'SE_UID')
* @param string IMAP search option (eg. 'SE_UID')
* @return void
* @throws InvalidParameterException
*/
Expand Down Expand Up @@ -479,7 +480,7 @@ public function moveMail($mailId, $mailBox) {
}

/**
* Copys mails listed in mailId into new mailbox
* Copies mails listed in mailId into new mailbox
* @param $mailId
* @param $mailBox
*/
Expand Down Expand Up @@ -594,10 +595,10 @@ public function getMailsInfo(array $mailsIds) {
if(isset($mail->subject)) {
$mail->subject = $this->decodeMimeStr($mail->subject, $this->getServerEncoding());
}
if(isset($mail->from) AND !empty($head->from)) {
if(isset($mail->from) AND !empty($mail->from)) {
$mail->from = $this->decodeMimeStr($mail->from, $this->getServerEncoding());
}
if(isset($mail->sender) AND !empty($head->sender)) {
if(isset($mail->sender) AND !empty($mail->sender)) {
$mail->sender = $this->decodeMimeStr($mail->sender, $this->getServerEncoding());
}
if(isset($mail->to)) {
Expand Down Expand Up @@ -832,13 +833,6 @@ public function getMail($mailId, $markAsSeen = true) {
}

protected function initMailPart(IncomingMail $mail, $partStructure, $partNum, $markAsSeen = true) {
// skip all but plain and html when attachments are not required
if ($this->getAttachmentsIgnore() &&
($partStructure->type !== TYPEMULTIPART &&
($partStructure->type !== TYPETEXT || !in_array(strtolower($partStructure->subtype), ['plain','html'])))) {
return false;
}

$options = ($this->imapSearchOption == SE_UID) ? FT_UID : 0;

if(!$markAsSeen) {
Expand Down Expand Up @@ -872,17 +866,28 @@ protected function initMailPart(IncomingMail $mail, $partStructure, $partNum, $m
}

if($isAttachment) {
$attachmentId = mt_rand() . mt_rand();
$mail->setHasAttachments(true);
}

// Do NOT parse attachments, when getAttachmentsIgnore() is true
if($this->getAttachmentsIgnore() &&
($partStructure->type !== TYPEMULTIPART &&
($partStructure->type !== TYPETEXT || !in_array(strtolower($partStructure->subtype), ['plain','html'])))) {
return false;
}

if($isAttachment) {
if(empty($params['filename']) && empty($params['name'])) {
$fileName = $attachmentId . '.' . strtolower($partStructure->subtype);
$fileName = strtolower($partStructure->subtype);
}
else {
$fileName = !empty($params['filename']) ? $params['filename'] : $params['name'];
$fileName = $this->decodeMimeStr($fileName, $this->getServerEncoding());
$fileName = $this->decodeRFC2231($fileName, $this->getServerEncoding());
}

$attachmentId = sha1($fileName);

$attachment = new IncomingMailAttachment();
$attachment->id = $attachmentId;
$attachment->contentId = $partStructure->ifid ? trim($partStructure->id, " <>") : null;
Expand Down Expand Up @@ -981,7 +986,7 @@ protected function decodeRFC2231($string, $charset = 'utf-8') {
/**
* Converts the datetime to a normalized datetime
* @param string Header datetime
* @return datetime Normalized datetime
* @return string Normalized datetime
*/
public function parseDateTime($dateHeader) {
if(empty($dateHeader)) {
Expand Down Expand Up @@ -1016,7 +1021,7 @@ public function convertStringEncoding($string, $fromEncoding, $toEncoding) {
} elseif(function_exists('iconv')) {
$convertedString = iconv($fromEncoding, $toEncoding . '//IGNORE', $string);
}
if(!$convertedString) {
if(!isset($convertedString)) {
throw new Exception('Mime string encoding conversion failed');
}
return $convertedString;
Expand Down Expand Up @@ -1053,11 +1058,13 @@ public function getMailboxes($search = "*") {
$arr = [];
if($t = imap_getmailboxes($this->getImapStream(), $this->imapPath, $search)) {
foreach($t as $item) {
// https://github.com/barbushin/php-imap/issues/339
$name = $this->decodeStringFromUtf7ImapToUtf8($item->name);
$arr[] = [
"fullpath" => $item->name,
"fullpath" => $name,
"attributes" => $item->attributes,
"delimiter" => $item->delimiter,
"shortpath" => substr($item->name, strpos($item->name, '}') + 1),
"shortpath" => substr($name, strpos($name, '}') + 1),
];
}
}
Expand All @@ -1072,11 +1079,13 @@ public function getSubscribedMailboxes($search = "*") {
$arr = [];
if($t = imap_getsubscribed($this->getImapStream(), $this->imapPath, $search)) {
foreach($t as $item) {
// https://github.com/barbushin/php-imap/issues/339
$name = $this->decodeStringFromUtf7ImapToUtf8($item->name);
$arr[] = [
"fullpath" => $item->name,
"fullpath" => $name,
"attributes" => $item->attributes,
"delimiter" => $item->delimiter,
"shortpath" => substr($item->name, strpos($item->name, '}') + 1),
"shortpath" => substr($name, strpos($name, '}') + 1),
];
}
}
Expand Down
18 changes: 14 additions & 4 deletions tests/unit/MailboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public function testSetAttachmentsIgnore($assertTest, $paramValue)
/**
* Provides test data for testing encoding
*/
public function encodingProvider() {
public function encodingTestStringsProvider() {
return [
'Avañe’ẽ' => ['Avañe’ẽ'], // Guaraní
'azərbaycanca' => ['azərbaycanca'], // Azerbaijani (Latin)
Expand Down Expand Up @@ -369,21 +369,31 @@ public function encodingProvider() {
'简体中文' => ['简体中文'], // Chinese (Simplified)
'繁體中文' => ['繁體中文'], // Chinese (Traditional)
'한국어' => ['한국어'], // Korean
'ąčęėįšųūžĄČĘĖĮŠŲŪŽ' => ['ąčęėįšųūžĄČĘĖĮŠŲŪŽ'], // Lithuanian letters
];
}

/**
* Test, that values are identical before and after encoding
* @dataProvider encodingProvider
* Test, that strings encoded to UTF-7 can be decoded back to UTF-8
* @dataProvider encodingTestStringsProvider
*/
public function testEncodingReturnsCorrectValues($str)
public function testEncodingToUtf7DecodeBackToUtf8($str)
{
$utf7_encoded_str = $this->mailbox->encodeStringToUtf7Imap($str);
$utf8_decoded_str = $this->mailbox->decodeStringFromUtf7ImapToUtf8($utf7_encoded_str);

$this->assertEquals($utf8_decoded_str, $str);
}

/**
* Test, that strings encoded to UTF-7 can be decoded back to UTF-8
* @dataProvider encodingTestStringsProvider
*/
public function testMimeDecodingReturnsCorrectValues($str)
{
$this->assertEquals($this->mailbox->decodeMimeStr($str, 'utf-8'), $str);
}

/**
* Provides test data for testing parsing datetimes
*/
Expand Down
19 changes: 17 additions & 2 deletions tests/unit/RequirementsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,22 @@

final class RequirementsTest extends TestCase
{
function testPhpImapExtensionIsEnabled() {
$this->assertTrue(extension_loaded('imap'));
/**
* Provides list of extensions, which are required by this library
*/
public function extensionProvider() {
return [
'imap' => ['imap'],
'mbstring' => ['mbstring'],
'iconv' => ['iconv'],
];
}

/**
* Test, that required modules are enabled
* @dataProvider extensionProvider
*/
function testRequiredExtensionsAreEnabled($extension) {
$this->assertTrue(extension_loaded($extension));
}
}

0 comments on commit 3a8b766

Please sign in to comment.