From 3228ad47233c087b94d823cb12981df602105313 Mon Sep 17 00:00:00 2001 From: Clivern Date: Tue, 15 Aug 2017 14:55:01 +0200 Subject: [PATCH] attachement class finished --- src/Core/Message.php | 36 ++++-- src/Core/Message/Attachment.php | 190 +++++++++++++++++++++++++++++++ src/Core/Message/Attachments.php | 58 ---------- src/Core/MessageIterator.php | 3 +- 4 files changed, 219 insertions(+), 68 deletions(-) create mode 100644 src/Core/Message/Attachment.php delete mode 100644 src/Core/Message/Attachments.php diff --git a/src/Core/Message.php b/src/Core/Message.php index c2c2466..c04435f 100644 --- a/src/Core/Message.php +++ b/src/Core/Message.php @@ -8,7 +8,7 @@ namespace Clivern\Imap\Core; use Clivern\Imap\Core\Connection; use Clivern\Imap\Core\Message\Header; use Clivern\Imap\Core\Message\Actions; -use Clivern\Imap\Core\Message\Attachments; +use Clivern\Imap\Core\Message\Attachment; use Clivern\Imap\Core\Message\Body; /** @@ -34,9 +34,9 @@ class Message protected $actions; /** - * @var Attachments + * @var array */ - protected $attachments; + protected $attachments = null; /** * @var Body @@ -58,12 +58,11 @@ class Message * * @param Connection $connection */ - public function __construct(Connection $connection, Header $header, Actions $actions, Attachments $attachments, Body $body) + public function __construct(Connection $connection, Header $header, Actions $actions, Body $body) { $this->connection = $connection; $this->header = $header; $this->actions = $actions; - $this->attachments = $attachments; $this->body = $body; } @@ -162,13 +161,34 @@ class Message } /** - * Get Message Attachments Object + * Get Message Attachments * - * @return Attachments + * @return array */ public function attachments() { - return $this->attachments->config($this->msg_number, $this->uid); + if( !is_null($this->attachments) ){ + return $this->attachments; + } + + $structure = imap_fetchstructure($this->connection->getStream(), $this->getMsgNo()); + + $this->attachments = []; + if (!isset($structure->parts)) { + return $this->attachments; + } + + $i = 0; + foreach ($structure->parts as $index => $part) { + if (!$part->ifdisposition){ + continue; + } + $this->attachments[$i] = new Attachment($this->connection); + $this->attachments[$i]->config($this->getMsgNo(), $this->getUid(), $index + 1, $part); + $i += 1; + } + + return $this->attachments; } /** diff --git a/src/Core/Message/Attachment.php b/src/Core/Message/Attachment.php new file mode 100644 index 0000000..6331a35 --- /dev/null +++ b/src/Core/Message/Attachment.php @@ -0,0 +1,190 @@ + + */ + +namespace Clivern\Imap\Core\Message; + +use Clivern\Imap\Core\Connection; + +/** + * Attachment Class + * + * @package Clivern\Imap\Core\Message + */ +class Attachment +{ + + /** + * @var Connection + */ + protected $connection; + + /** + * @var integer + */ + protected $message_number; + + /** + * @var integer + */ + protected $message_uid; + + /** + * @var array + */ + protected $attachment; + + /** + * @var Object + */ + protected $part; + + + /** + * Class Constructor + * + * @param Connection $connection + */ + public function __construct(Connection $connection) + { + $this->connection = $connection; + } + + /** + * Config Attachments + * + * @param integer $message_number + * @param integer $message_uid + * @return Attachments + */ + public function config($message_number, $message_uid, $attachment_id, $part) + { + $this->message_number = $message_number; + $this->message_uid = $message_uid; + $this->attachment['index'] = $attachment_id; + $this->part = $part; + + $this->parseParts(); + + return $this; + } + + public function get($key, $default = false) + { + return (isset($this->attachment[$key])) ? $this->attachment[$key] : $default; + } + + public function getFilename() + { + return $this->get('filename', false); + } + + public function getExtension() + { + return $this->get('extension', false); + } + + public function getSize() + { + return $this->get('size', false); + } + + public function getEncoding() + { + return $this->get('encoding', false); + } + + public function getBytes() + { + return $this->get('bytes', false); + } + + public function getPlainBody() + { + if( $this->get('plain_body') ){ + return $this->get('plain_body'); + } + + $this->attachment['plain_body'] = imap_fetchbody($this->connection->getStream(), $this->message_number, $this->attachment['index']); + + var_dump($this->attachment['plain_body']); + return $this->get('plain_body'); + } + + public function getBody() + { + + if( $this->get('body') ){ + return $this->get('body'); + } + + switch ($this->getEncoding()) { + case 0: // 7BIT + + case 1: // 8BIT + + case 2: // BINARY + $this->attachment['body'] = $this->getPlainBody(); + return $this->get('body'); + + case 3: // BASE-64 + $this->attachment['body'] = base64_decode($this->getPlainBody()); + return $this->get('body'); + + case 4: // QUOTED-PRINTABLE + $this->attachment['body'] = imap_qprint($this->getPlainBody()); + return $this->get('body'); + } + + throw new \Exception(sprintf('Encoding failed: Unknown encoding %s.', $this->getEncoding())); + } + + public function store($path, $file_name = false) + { + $file_name = ($file_name) ? $file_name : "{$this->getFilename()}.{$this->getExtension()}"; + $path = rtrim($path, '/') . "/"; + return (boolean) file_put_contents($path . $file_name, $this->getBody()); + } + + protected function parseParts() + { + if( (count($this->attachment) > 2) ){ + return true; + } + + $this->attachment['type'] = $this->part->type; + $this->attachment['encoding'] = $this->part->encoding; + $this->attachment['ifsubtype'] = $this->part->ifsubtype; + $this->attachment['subtype'] = $this->part->subtype; + $this->attachment['ifdescription'] = $this->part->ifdescription; + $this->attachment['ifid'] = $this->part->ifid; + $this->attachment['id'] = $this->part->id; + $this->attachment['bytes'] = $this->part->bytes; + $this->attachment['size'] = $this->part->bytes; + $this->attachment['ifdisposition'] = $this->part->ifdisposition; + $this->attachment['disposition'] = $this->part->disposition; + $this->attachment['ifdparameters'] = $this->part->ifdparameters; + $this->attachment['ifparameters'] = $this->part->ifparameters; + + if( is_array($this->part->dparameters) ){ + foreach ($this->part->dparameters as $obj) { + if( in_array(strtolower($obj->attribute), ['filename', 'name']) ){ + $this->attachment[strtolower($obj->attribute)] = pathinfo($obj->value, PATHINFO_FILENAME); + $this->attachment['extension'] = pathinfo($obj->value, PATHINFO_EXTENSION); + } + } + } + + if( is_array($this->part->parameters) ){ + foreach ($this->part->parameters as $obj) { + if( in_array(strtolower($obj->attribute), ['filename', 'name']) ){ + $this->attachment[strtolower($obj->attribute)] = pathinfo($obj->value, PATHINFO_FILENAME); + $this->attachment['extension'] = pathinfo($obj->value, PATHINFO_EXTENSION); + } + } + } + + return true; + } +} \ No newline at end of file diff --git a/src/Core/Message/Attachments.php b/src/Core/Message/Attachments.php deleted file mode 100644 index da2344e..0000000 --- a/src/Core/Message/Attachments.php +++ /dev/null @@ -1,58 +0,0 @@ - - */ - -namespace Clivern\Imap\Core\Message; - -use Clivern\Imap\Core\Connection; - -/** - * Attachments Class - * - * @package Clivern\Imap\Core\Message - */ -class Attachments -{ - - /** - * @var Connection - */ - protected $connection; - - /** - * @var integer - */ - protected $message_number; - - /** - * @var integer - */ - protected $message_uid; - - - /** - * Class Constructor - * - * @param Connection $connection - */ - public function __construct(Connection $connection) - { - $this->connection = $connection; - } - - /** - * Config Attachments - * - * @param integer $message_number - * @param integer $message_uid - * @return Attachments - */ - public function config($message_number, $message_uid) - { - $this->message_number = $message_number; - $this->message_uid = $message_uid; - - return $this; - } -} \ No newline at end of file diff --git a/src/Core/MessageIterator.php b/src/Core/MessageIterator.php index 6765681..b8effa1 100644 --- a/src/Core/MessageIterator.php +++ b/src/Core/MessageIterator.php @@ -9,7 +9,6 @@ use Clivern\Imap\Core\Message; use Clivern\Imap\Core\Connection; use Clivern\Imap\Core\Message\Header; use Clivern\Imap\Core\Message\Actions; -use Clivern\Imap\Core\Message\Attachments; use Clivern\Imap\Core\Message\Body; /** @@ -44,7 +43,7 @@ class MessageIterator extends \ArrayIterator */ public function current() { - $message = new Message($this->connection, new Header($this->connection), new Actions($this->connection), new Attachments($this->connection), new Body($this->connection)); + $message = new Message($this->connection, new Header($this->connection), new Actions($this->connection), new Body($this->connection)); return $message->setUid(parent::current())->config(); } } \ No newline at end of file