clivern-imap/src/Core/Message/Body.php

111 lines
2.2 KiB
PHP
Raw Normal View History

2017-08-13 15:01:56 +02:00
<?php
/*
* This file is part of the Imap PHP package.
* (c) Clivern <hello@clivern.com>
2017-08-13 15:01:56 +02:00
*/
namespace Clivern\Imap\Core\Message;
2017-08-13 15:04:11 +02:00
use Clivern\Imap\Core\Connection;
2017-08-13 15:01:56 +02:00
/**
* Body Class.
2017-08-13 15:01:56 +02:00
*/
class Body
{
/**
* @var Connection
*/
protected $connection;
/**
* @var int
2017-08-13 15:01:56 +02:00
*/
protected $message_number;
/**
* @var int
2017-08-13 15:01:56 +02:00
*/
protected $message_uid;
2017-08-14 13:24:33 +02:00
/**
* @var int
2017-08-14 13:24:33 +02:00
*/
protected $encoding;
/**
* @var string
*/
protected $message = '';
2017-08-13 15:01:56 +02:00
/**
* Class Constructor.
2017-08-13 15:01:56 +02:00
*/
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
/**
* Config Body.
*
* @param int $message_number
* @param int $message_uid
2017-08-13 15:01:56 +02:00
*
* @return Body
*/
public function config($message_number, $message_uid)
{
$this->message_number = $message_number;
$this->message_uid = $message_uid;
return $this;
}
2017-08-14 13:24:33 +02:00
/**
* Get Message.
*
* @param int $option
2017-08-14 13:24:33 +02:00
*
* @return string
*/
public function getMessage($option = 2)
{
if (!empty($this->message)) {
2017-08-14 13:24:33 +02:00
return $this->message;
}
$structure = imap_fetchstructure($this->connection->getStream(), $this->message_number);
if (isset($structure->parts) && \is_array($structure->parts) && isset($structure->parts[1])) {
2017-08-14 13:24:33 +02:00
$part = $structure->parts[1];
$this->message = imap_fetchbody($this->connection->getStream(), $this->message_number, $option);
2017-08-14 13:24:33 +02:00
$this->encoding = $part->encoding;
if (3 === $part->encoding) {
2017-08-14 13:24:33 +02:00
$this->message = imap_base64($this->message);
} elseif (1 === $part->encoding) {
2017-08-14 13:24:33 +02:00
$this->message = imap_8bit($this->message);
} else {
$this->message = imap_qprint($this->message);
}
2019-02-25 14:47:07 +01:00
} else {
$this->message = imap_body($this->connection->getStream(), $this->message_number, $option);
}
2017-08-14 13:24:33 +02:00
return $this->message;
}
/**
* Get Encoding.
2017-08-14 13:24:33 +02:00
*
* @return int
2017-08-14 13:24:33 +02:00
*/
public function getEncoding()
{
return $this->encoding;
}
}