attachement class finished
This commit is contained in:
parent
5fcb1a0068
commit
3228ad4723
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
190
src/Core/Message/Attachment.php
Normal file
190
src/Core/Message/Attachment.php
Normal file
|
@ -0,0 +1,190 @@
|
|||
<?php
|
||||
/**
|
||||
* @author clivern <hello@clivern.com>
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -1,58 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* @author clivern <hello@clivern.com>
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue