From ce9ff10cb0ba2016a6130558e374cbd9dcdb51bf Mon Sep 17 00:00:00 2001 From: yuri Date: Thu, 29 Jan 2015 12:03:43 +0200 Subject: [PATCH] fix imap --- application/Espo/Core/Mail/Importer.php | 5 +- application/Espo/Core/Mail/Storage/Imap.php | 12 +-- .../Espo/Core/Mail/Storage/Message.php | 40 ++++++++++ application/Espo/Services/EmailAccount.php | 76 +++++++++---------- 4 files changed, 87 insertions(+), 46 deletions(-) create mode 100644 application/Espo/Core/Mail/Storage/Message.php diff --git a/application/Espo/Core/Mail/Importer.php b/application/Espo/Core/Mail/Importer.php index cb59b0164c..feabe57f8e 100644 --- a/application/Espo/Core/Mail/Importer.php +++ b/application/Espo/Core/Mail/Importer.php @@ -107,9 +107,8 @@ class Importer $inlineIds = array(); if ($message->isMultipart()) { - $count = $message->countParts(); - for ($i = 0; $i < $count; $i++) { - $part = $message->getPart($i + 1); + foreach (new \RecursiveIteratorIterator($message) as $part) { + echo "-"; $this->importPartDataToEmail($email, $part, $inlineIds); } } else { diff --git a/application/Espo/Core/Mail/Storage/Imap.php b/application/Espo/Core/Mail/Storage/Imap.php index 224bf3ce68..5f4398955e 100644 --- a/application/Espo/Core/Mail/Storage/Imap.php +++ b/application/Espo/Core/Mail/Storage/Imap.php @@ -1,4 +1,4 @@ -protocol->search(array('UID ' . $uid . ':*')); } - + public function getIdsFromDate($date) - { + { return $this->protocol->search(array('SINCE "' . $date . '"')); } - + } diff --git a/application/Espo/Core/Mail/Storage/Message.php b/application/Espo/Core/Mail/Storage/Message.php new file mode 100644 index 0000000000..5ac07f0ce9 --- /dev/null +++ b/application/Espo/Core/Mail/Storage/Message.php @@ -0,0 +1,40 @@ +contentType)) { + return false; + } + + try { + return stripos($this->contentType, 'multipart/') === 0; + } catch (Exception\ExceptionInterface $e) { + return false; + } + } +} + diff --git a/application/Espo/Services/EmailAccount.php b/application/Espo/Services/EmailAccount.php index 23584e3fbd..785f025e97 100644 --- a/application/Espo/Services/EmailAccount.php +++ b/application/Espo/Services/EmailAccount.php @@ -28,65 +28,65 @@ use \Espo\Core\Exceptions\Error; use \Espo\Core\Exceptions\Forbidden; class EmailAccount extends Record -{ +{ protected $internalFields = array('password'); - + protected $readOnlyFields = array('fetchData'); - + const PORTION_LIMIT = 10; - + protected function init() { $this->dependencies[] = 'fileManager'; $this->dependencies[] = 'crypt'; } - + protected function getFileManager() { return $this->injections['fileManager']; } - + protected function getCrypt() { return $this->injections['crypt']; } - + protected function handleInput(&$data) - { - parent::handleInput($data); + { + parent::handleInput($data); if (array_key_exists('password', $data)) { $data['password'] = $this->getCrypt()->encrypt($data['password']); } } - + public function getFolders($params) - { + { $password = $params['password']; - + if (!empty($params['id'])) { $entity = $this->getEntityManager()->getEntity('EmailAccount', $params['id']); if ($entity) { $password = $this->getCrypt()->decrypt($entity->get('password')); } - } - + } + $imapParams = array( 'host' => $params['host'], 'port' => $params['port'], 'user' => $params['username'], 'password' => $password, ); - + if (!empty($params['ssl'])) { $imapParams['ssl'] = 'SSL'; - } - - $foldersArr = array(); - - $storage = new \Zend\Mail\Storage\Imap($imapParams); - + } + + $foldersArr = array(); + + $storage = new \Zend\Mail\Storage\Imap($imapParams); + $folders = new \RecursiveIteratorIterator($storage->getFolders(), \RecursiveIteratorIterator::SELF_FIRST); - foreach ($folders as $name => $folder) { + foreach ($folders as $name => $folder) { $foldersArr[] = mb_convert_encoding($folder->getGlobalName(), 'UTF-8', 'UTF7-IMAP'); } return $foldersArr; @@ -177,32 +177,32 @@ class EmailAccount extends Record return false; } } - + if ((count($ids) == 1) && !empty($lastUID)) { if ($storage->getUniqueId($ids[0]) == $lastUID) { continue; } } - - $k = 0; - foreach ($ids as $i => $id) { + + $k = 0; + foreach ($ids as $i => $id) { if ($k == count($ids) - 1) { $lastUID = $storage->getUniqueId($id); } - + if ($maxSize) { if ($storage->getSize($id) > $maxSize * 1024 * 1024) { continue; } } - + $message = $storage->getMessage($id); - + $email = $importer->importMessage($message, $userId, array($teamId)); - + if ($k == count($ids) - 1) { $lastUID = $storage->getUniqueId($id); - + if ($message) { $dt = new \DateTime($message->date); if ($dt) { @@ -211,22 +211,22 @@ class EmailAccount extends Record } } } - + if ($k == self::PORTION_LIMIT - 1) { $lastUID = $storage->getUniqueId($id); break; } $k++; - } - + } + $fetchData['lastUID'][$folder] = $lastUID; $fetchData['lastDate'][$folder] = $lastDate; - + $emailAccount->set('fetchData', json_encode($fetchData)); - $this->getEntityManager()->saveEntity($emailAccount); + $this->getEntityManager()->saveEntity($emailAccount); } - - return true; + + return true; } }