update v1.0.7.1

This commit is contained in:
Sujit Prasad
2016-04-20 15:26:00 +05:30
parent f5c52a90fd
commit a97e78069c
19 changed files with 196 additions and 216 deletions

View File

@@ -1,13 +1,10 @@
<?php namespace PhpImap;
use stdClass;
/**
* @see https://github.com/barbushin/php-imap
* @author Barbushin Sergey http://linkedin.com/in/barbushin
*/
class Mailbox {
protected $imapPath;
protected $imapLogin;
protected $imapPassword;
@@ -16,7 +13,6 @@ class Mailbox {
protected $imapParams = array();
protected $serverEncoding;
protected $attachmentsDir;
public function __construct($imapPath, $login, $password, $attachmentsDir = null, $serverEncoding = 'UTF-8') {
$this->imapPath = $imapPath;
$this->imapLogin = $login;
@@ -29,7 +25,6 @@ class Mailbox {
$this->attachmentsDir = rtrim(realpath($attachmentsDir), '\\/');
}
}
/**
* Set custom connection arguments of imap_open method. See http://php.net/imap_open
* @param int $options
@@ -41,7 +36,6 @@ class Mailbox {
$this->imapRetriesNum = $retriesNum;
$this->imapParams = $params;
}
/**
* Get IMAP mailbox connection stream
* @param bool $forceConnection Initialize connection if it's not initialized
@@ -60,7 +54,6 @@ class Mailbox {
}
return $imapStream;
}
protected function initImapStream() {
$imapStream = @imap_open($this->imapPath, $this->imapLogin, $this->imapPassword, $this->imapOptions, $this->imapRetriesNum, $this->imapParams);
if(!$imapStream) {
@@ -68,14 +61,12 @@ class Mailbox {
}
return $imapStream;
}
protected function disconnect() {
$imapStream = $this->getImapStream(false);
if($imapStream && is_resource($imapStream)) {
imap_close($imapStream, CL_EXPUNGE);
}
}
/**
* Get information about the current mailbox.
*
@@ -91,17 +82,14 @@ class Mailbox {
public function checkMailbox() {
return imap_check($this->getImapStream());
}
/**
* Creates a new mailbox specified by mailbox.
*
* @return bool
*/
public function createMailbox() {
return imap_createmailbox($this->getImapStream(), imap_utf7_encode($this->imapPath));
}
/**
* Gets status information about the given mailbox.
*
@@ -110,12 +98,9 @@ class Mailbox {
*
* @return stdClass if the box doesn't exist
*/
public function statusMailbox() {
return imap_status($this->getImapStream(), $this->imapPath, SA_ALL);
}
/**
* Gets listing the folders
*
@@ -124,7 +109,6 @@ class Mailbox {
*
* @return array listing the folders
*/
public function getListingFolders() {
$folders = imap_list($this->getImapStream(), $this->imapPath, "*");
foreach ($folders as $key => $folder)
@@ -134,8 +118,6 @@ class Mailbox {
}
return $folders;
}
/**
* This function performs a search on the mailbox currently opened in the given IMAP stream.
* For example, to match all unanswered mails sent by Mom, you'd use: "UNANSWERED FROM mom".
@@ -174,7 +156,6 @@ class Mailbox {
$mailsIds = imap_search($this->getImapStream(), $criteria, SE_UID, $this->serverEncoding);
return $mailsIds ? $mailsIds : array();
}
/**
* Save mail body.
* @return bool
@@ -182,7 +163,6 @@ class Mailbox {
public function saveMail($mailId, $filename = 'email.eml') {
return imap_savebody($this->getImapStream(), $filename, $mailId, "", FT_UID);
}
/**
* Marks mails listed in mailId for deletion.
* @return bool
@@ -190,11 +170,9 @@ class Mailbox {
public function deleteMail($mailId) {
return imap_delete($this->getImapStream(), $mailId, FT_UID);
}
public function moveMail($mailId, $mailBox) {
return imap_mail_move($this->getImapStream(), $mailId, $mailBox, CP_UID) && $this->expungeDeletedMails();
}
/**
* Deletes all the mails marked for deletion by imap_delete(), imap_mail_move(), or imap_setflag_full().
* @return bool
@@ -202,7 +180,6 @@ class Mailbox {
public function expungeDeletedMails() {
return imap_expunge($this->getImapStream());
}
/**
* Add the flag \Seen to a mail.
* @return bool
@@ -210,7 +187,6 @@ class Mailbox {
public function markMailAsRead($mailId) {
return $this->setFlag(array($mailId), '\\Seen');
}
/**
* Remove the flag \Seen from a mail.
* @return bool
@@ -218,7 +194,6 @@ class Mailbox {
public function markMailAsUnread($mailId) {
return $this->clearFlag(array($mailId), '\\Seen');
}
/**
* Add the flag \Flagged to a mail.
* @return bool
@@ -226,7 +201,6 @@ class Mailbox {
public function markMailAsImportant($mailId) {
return $this->setFlag(array($mailId), '\\Flagged');
}
/**
* Add the flag \Seen to a mails.
* @return bool
@@ -234,7 +208,6 @@ class Mailbox {
public function markMailsAsRead(array $mailId) {
return $this->setFlag($mailId, '\\Seen');
}
/**
* Remove the flag \Seen from some mails.
* @return bool
@@ -242,7 +215,6 @@ class Mailbox {
public function markMailsAsUnread(array $mailId) {
return $this->clearFlag($mailId, '\\Seen');
}
/**
* Add the flag \Flagged to some mails.
* @return bool
@@ -250,7 +222,6 @@ class Mailbox {
public function markMailsAsImportant(array $mailId) {
return $this->setFlag($mailId, '\\Flagged');
}
/**
* Causes a store to add the specified flag to the flags set for the mails in the specified sequence.
*
@@ -261,7 +232,6 @@ class Mailbox {
public function setFlag(array $mailsIds, $flag) {
return imap_setflag_full($this->getImapStream(), implode(',', $mailsIds), $flag, ST_UID);
}
/**
* Cause a store to delete the specified flag to the flags set for the mails in the specified sequence.
*
@@ -272,7 +242,6 @@ class Mailbox {
public function clearFlag(array $mailsIds, $flag) {
return imap_clearflag_full($this->getImapStream(), implode(',', $mailsIds), $flag, ST_UID);
}
/**
* Fetch mail headers for listed mails ids
*
@@ -316,7 +285,6 @@ class Mailbox {
}
return $mails;
}
/**
* Get information about the current mailbox.
*
@@ -332,11 +300,9 @@ class Mailbox {
*
* @return object Object with info | FALSE on failure
*/
public function getMailboxInfo() {
return imap_mailboxmsginfo($this->getImapStream());
}
public function get_overview($mailId) {
$overview = imap_fetch_overview($this->getImapStream(), $mailId, FT_UID);
return $overview;
@@ -346,8 +312,6 @@ class Mailbox {
$body = imap_body($this->getImapStream(), $mailId, 1.1);
return $body;
}
/**
* Gets mails ids sorted by some criteria
*
@@ -367,7 +331,6 @@ class Mailbox {
public function sortMails($criteria = SORTARRIVAL, $reverse = true) {
return imap_sort($this->getImapStream(), $criteria, $reverse, SE_UID);
}
/**
* Get mails count in mail box
* @return int
@@ -375,7 +338,6 @@ class Mailbox {
public function countMails() {
return imap_num_msg($this->getImapStream());
}
/**
* Retrieve the quota settings per user
* @return array - FALSE in the case of call failure
@@ -383,7 +345,6 @@ class Mailbox {
protected function getQuota() {
return imap_get_quotaroot($this->getImapStream(), 'INBOX');
}
/**
* Return quota limit in KB
* @return int - FALSE in the case of call failure
@@ -395,7 +356,6 @@ class Mailbox {
}
return $quota;
}
/**
* Return quota usage in KB
* @return int - FALSE in the case of call failure
@@ -407,7 +367,6 @@ class Mailbox {
}
return $quota;
}
/**
* Get mail data
*
@@ -417,14 +376,12 @@ class Mailbox {
*/
public function getMail($mailId, $markAsSeen = true) {
$head = imap_rfc822_parse_headers(imap_fetchheader($this->getImapStream(), $mailId, FT_UID));
$mail = new IncomingMail();
$mail->id = $mailId;
$mail->date = date('Y-m-d H:i:s', isset($head->date) ? strtotime(preg_replace('/\(.*?\)/', '', $head->date)) : time());
$mail->subject = isset($head->subject) ? $this->decodeMimeStr($head->subject, $this->serverEncoding) : null;
$mail->fromName = isset($head->from[0]->personal) ? $this->decodeMimeStr($head->from[0]->personal, $this->serverEncoding) : null;
$mail->fromAddress = strtolower($head->from[0]->mailbox . '@' . $head->from[0]->host);
if(isset($head->to)) {
$toStrings = array();
// dd($mail);
@@ -438,21 +395,17 @@ class Mailbox {
}
$mail->toString = implode(', ', $toStrings);
}
if(isset($head->cc)) {
foreach($head->cc as $cc) {
$mail->cc[strtolower($cc->mailbox . '@' . $cc->host)] = isset($cc->personal) ? $this->decodeMimeStr($cc->personal, $this->serverEncoding) : null;
}
}
if(isset($head->reply_to)) {
foreach($head->reply_to as $replyTo) {
$mail->replyTo[strtolower($replyTo->mailbox . '@' . $replyTo->host)] = isset($replyTo->personal) ? $this->decodeMimeStr($replyTo->personal, $this->serverEncoding) : null;
}
}
$mailStructure = imap_fetchstructure($this->getImapStream(), $mailId, FT_UID);
if(empty($mailStructure->parts)) {
$this->initMailPart($mail, $mailStructure, 0, $markAsSeen);
}
@@ -461,17 +414,14 @@ class Mailbox {
$this->initMailPart($mail, $partStructure, $partNum + 1, $markAsSeen);
}
}
return $mail;
}
protected function initMailPart(IncomingMail $mail, $partStructure, $partNum, $markAsSeen = true) {
$options = FT_UID;
if(!$markAsSeen) {
$options |= FT_PEEK;
}
$data = $partNum ? imap_fetchbody($this->getImapStream(), $mail->id, $partNum, $options) : imap_body($this->getImapStream(), $mail->id, $options);
if($partStructure->encoding == 1) {
$data = imap_utf8($data);
}
@@ -484,7 +434,6 @@ class Mailbox {
elseif($partStructure->encoding == 4) {
$data = quoted_printable_decode($data);
}
$params = array();
if(!empty($partStructure->parameters)) {
foreach($partStructure->parameters as $param) {
@@ -502,12 +451,10 @@ class Mailbox {
}
}
}
// attachments
$attachmentId = $partStructure->ifid
? trim($partStructure->id, " <>")
: (isset($params['filename']) || isset($params['name']) ? mt_rand() . mt_rand() : null);
if($attachmentId) {
if(empty($params['filename']) && empty($params['name'])) {
$fileName = $attachmentId . '.' . strtolower($partStructure->subtype);
@@ -523,7 +470,7 @@ class Mailbox {
if($this->attachmentsDir) {
$replace = array(
'/\s/' => '_',
'/[^0-9a-zа-<2D>?іїє_\.]/iu' => '',
'/[^0-9a-z?-????_\.]/iu' => '',
'/_+/' => '_',
'/(^_)|(_$)/' => '',
);
@@ -560,7 +507,6 @@ class Mailbox {
}
}
}
protected function decodeMimeStr($string, $charset = 'utf-8') {
$newString = '';
$elements = imap_mime_header_decode($string);
@@ -572,13 +518,11 @@ class Mailbox {
}
return $newString;
}
function isUrlEncoded($string) {
$hasInvalidChars = preg_match( '#[^%a-zA-Z0-9\-_\.\+]#', $string );
$hasEscapedChars = preg_match( '#%[a-zA-Z0-9]{2}#', $string );
return !$hasInvalidChars && $hasEscapedChars;
}
protected function decodeRFC2231($string, $charset = 'utf-8') {
if(preg_match("/^(.*?)'.*?'(.*?)$/", $string, $matches)) {
$encoding = $matches[1];
@@ -589,7 +533,6 @@ class Mailbox {
}
return $string;
}
/**
* Converts a string from one encoding to another.
* @param string $string
@@ -607,12 +550,9 @@ class Mailbox {
}
return $convertedString ?: $string;
}
public function __destruct() {
$this->disconnect();
}
}
class Exception extends \Exception {
}
}