mirror of
https://github.com/retailcrm/Fetch.git
synced 2025-02-06 10:39:21 +03:00
Merge pull request #10 from sgrodzicki/master
Fixed PHPDoc & PSR Coding Standards
This commit is contained in:
commit
b7c53f620e
@ -21,180 +21,181 @@ namespace Fetch;
|
||||
class Attachment
|
||||
{
|
||||
|
||||
/**
|
||||
* This is the structure object for the piece of the message body that the attachment is located it.
|
||||
*
|
||||
* @var stdClass
|
||||
*/
|
||||
protected $structure;
|
||||
/**
|
||||
* This is the structure object for the piece of the message body that the attachment is located it.
|
||||
*
|
||||
* @var \stdClass
|
||||
*/
|
||||
protected $structure;
|
||||
|
||||
/**
|
||||
* This is the unique identifier for the message this attachment belongs to.
|
||||
*
|
||||
* @var unknown_type
|
||||
*/
|
||||
protected $messageId;
|
||||
/**
|
||||
* This is the unique identifier for the message this attachment belongs to.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $messageId;
|
||||
|
||||
/**
|
||||
* This is the ImapResource.
|
||||
*
|
||||
* @var resource
|
||||
*/
|
||||
protected $imapStream;
|
||||
/**
|
||||
* This is the ImapResource.
|
||||
*
|
||||
* @var resource
|
||||
*/
|
||||
protected $imapStream;
|
||||
|
||||
/**
|
||||
* This is the id pointing to the section of the message body that contains the attachment.
|
||||
*
|
||||
* @var unknown_type
|
||||
*/
|
||||
protected $partId;
|
||||
/**
|
||||
* This is the id pointing to the section of the message body that contains the attachment.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $partId;
|
||||
|
||||
/**
|
||||
* This is the attachments filename.
|
||||
*
|
||||
* @var unknown_type
|
||||
*/
|
||||
protected $filename;
|
||||
/**
|
||||
* This is the attachments filename.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $filename;
|
||||
|
||||
/**
|
||||
* This is the size of the attachment.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $size;
|
||||
/**
|
||||
* This is the size of the attachment.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $size;
|
||||
|
||||
/**
|
||||
* This stores the data of the attachment so it doesn't have to be retrieved from the server multiple times. It is
|
||||
* only populated if the getData() function is called and should not be directly used.
|
||||
*
|
||||
* @internal
|
||||
* @var unknown_type
|
||||
*/
|
||||
protected $data;
|
||||
/**
|
||||
* This stores the data of the attachment so it doesn't have to be retrieved from the server multiple times. It is
|
||||
* only populated if the getData() function is called and should not be directly used.
|
||||
*
|
||||
* @internal
|
||||
* @var array
|
||||
*/
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* This function takes in an ImapMessage, the structure object for the particular piece of the message body that the
|
||||
* attachment is located at, and the identifier for that body part. As a general rule you should not be creating
|
||||
* instances of this yourself, but rather should get them from an ImapMessage class.
|
||||
*
|
||||
* @param ImapMessage $message
|
||||
* @param stdClass $structure
|
||||
* @param string $partIdentifier
|
||||
*/
|
||||
public function __construct(Message $message, $structure, $partIdentifier = null)
|
||||
{
|
||||
$this->messageId = $message->getUid();
|
||||
$this->imapStream = $message->getImapBox()->getImapStream();
|
||||
$this->structure = $structure;
|
||||
/**
|
||||
* This function takes in an ImapMessage, the structure object for the particular piece of the message body that the
|
||||
* attachment is located at, and the identifier for that body part. As a general rule you should not be creating
|
||||
* instances of this yourself, but rather should get them from an ImapMessage class.
|
||||
*
|
||||
* @param Message $message
|
||||
* @param \stdClass $structure
|
||||
* @param string $partIdentifier
|
||||
*/
|
||||
public function __construct(Message $message, $structure, $partIdentifier = null)
|
||||
{
|
||||
$this->messageId = $message->getUid();
|
||||
$this->imapStream = $message->getImapBox()->getImapStream();
|
||||
$this->structure = $structure;
|
||||
|
||||
if(isset($partIdentifier))
|
||||
$this->partId = $partIdentifier;
|
||||
if (isset($partIdentifier))
|
||||
$this->partId = $partIdentifier;
|
||||
|
||||
$parameters = Message::getParametersFromStructure($structure);
|
||||
$parameters = Message::getParametersFromStructure($structure);
|
||||
|
||||
if(isset($parameters['filename']))
|
||||
{
|
||||
$this->filename = $parameters['filename'];
|
||||
}elseif(isset($parameters['name'])){
|
||||
$this->filename = $parameters['name'];
|
||||
}
|
||||
|
||||
$this->size = $structure->bytes;
|
||||
|
||||
$this->mimeType = Message::typeIdToString($structure->type);
|
||||
|
||||
if(isset($structure->subtype))
|
||||
$this->mimeType .= '/' . strtolower($structure->subtype);
|
||||
|
||||
$this->encoding = $structure->encoding;
|
||||
if (isset($parameters['filename'])) {
|
||||
$this->filename = $parameters['filename'];
|
||||
} elseif (isset($parameters['name'])) {
|
||||
$this->filename = $parameters['name'];
|
||||
}
|
||||
|
||||
/**
|
||||
* This function returns the data of the attachment. Combined with getMimeType() it can be used to directly output
|
||||
* data to a browser.
|
||||
*
|
||||
* @return binary
|
||||
*/
|
||||
public function getData()
|
||||
{
|
||||
if(!isset($this->data))
|
||||
{
|
||||
$messageBody = isset($this->partId) ?
|
||||
imap_fetchbody($this->imapStream, $this->messageId, $this->partId, FT_UID)
|
||||
: imap_body($this->imapStream, $this->messageId, FT_UID);
|
||||
$this->size = $structure->bytes;
|
||||
|
||||
$messageBody = Message::decode($messageBody, $this->encoding);
|
||||
$this->data = $messageBody;
|
||||
}
|
||||
return $this->data;
|
||||
$this->mimeType = Message::typeIdToString($structure->type);
|
||||
|
||||
if (isset($structure->subtype))
|
||||
$this->mimeType .= '/' . strtolower($structure->subtype);
|
||||
|
||||
$this->encoding = $structure->encoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function returns the data of the attachment. Combined with getMimeType() it can be used to directly output
|
||||
* data to a browser.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getData()
|
||||
{
|
||||
if (!isset($this->data)) {
|
||||
$messageBody = isset($this->partId) ?
|
||||
imap_fetchbody($this->imapStream, $this->messageId, $this->partId, FT_UID)
|
||||
: imap_body($this->imapStream, $this->messageId, FT_UID);
|
||||
|
||||
$messageBody = Message::decode($messageBody, $this->encoding);
|
||||
$this->data = $messageBody;
|
||||
}
|
||||
|
||||
/**
|
||||
* This returns the filename of the attachment, or false if one isn't given.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFileName()
|
||||
{
|
||||
return (isset($this->filename)) ? $this->filename : false;
|
||||
}
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function returns the mimetype of the attachment.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMimeType()
|
||||
{
|
||||
return $this->mimeType;
|
||||
}
|
||||
/**
|
||||
* This returns the filename of the attachment, or false if one isn't given.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFileName()
|
||||
{
|
||||
return (isset($this->filename)) ? $this->filename : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* This returns the size of the attachment.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getSize()
|
||||
{
|
||||
return $this->size;
|
||||
}
|
||||
/**
|
||||
* This function returns the mimetype of the attachment.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMimeType()
|
||||
{
|
||||
return $this->mimeType;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function saves the attachment to the passed directory, keeping the original name of the file.
|
||||
*
|
||||
* @param string $path
|
||||
*/
|
||||
public function saveToDirectory($path)
|
||||
{
|
||||
$path = rtrim($path, '/') . '/';
|
||||
/**
|
||||
* This returns the size of the attachment.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getSize()
|
||||
{
|
||||
return $this->size;
|
||||
}
|
||||
|
||||
if(is_dir($path))
|
||||
return $this->saveAs($path . $this->getFileName());
|
||||
/**
|
||||
* This function saves the attachment to the passed directory, keeping the original name of the file.
|
||||
*
|
||||
* @param string $path
|
||||
* @return bool
|
||||
*/
|
||||
public function saveToDirectory($path)
|
||||
{
|
||||
$path = rtrim($path, '/') . '/';
|
||||
|
||||
if (is_dir($path))
|
||||
return $this->saveAs($path . $this->getFileName());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function saves the attachment to the exact specified location.
|
||||
*
|
||||
* @param string $path
|
||||
* @return bool
|
||||
*/
|
||||
public function saveAs($path)
|
||||
{
|
||||
$dirname = dirname($path);
|
||||
if (file_exists($path)) {
|
||||
if (!is_writable($path))
|
||||
return false;
|
||||
} elseif (!is_dir($dirname) || !is_writable($dirname)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function saves the attachment to the exact specified location.
|
||||
*
|
||||
* @param path $path
|
||||
*/
|
||||
public function saveAs($path)
|
||||
{
|
||||
$dirname = dirname($path);
|
||||
if(file_exists($path))
|
||||
{
|
||||
if(!is_writable($path))
|
||||
return false;
|
||||
}elseif(!is_dir($dirname) || !is_writable($dirname)){
|
||||
return false;
|
||||
}
|
||||
if (($filePointer = fopen($path, 'w')) == false)
|
||||
return false;
|
||||
|
||||
if(($filePointer = fopen($path, 'w')) == false)
|
||||
return false;
|
||||
$results = fwrite($filePointer, $this->getData());
|
||||
fclose($filePointer);
|
||||
|
||||
$results = fwrite($filePointer, $this->getData());
|
||||
fclose($filePointer);
|
||||
return is_numeric($results);
|
||||
}
|
||||
return is_numeric($results);
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -20,387 +20,380 @@ namespace Fetch;
|
||||
*/
|
||||
class Server
|
||||
{
|
||||
/**
|
||||
* When SSL isn't compiled into PHP we need to make some adjustments to prevent soul crushing annoyances.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
static $sslEnable = true;
|
||||
/**
|
||||
* When SSL isn't compiled into PHP we need to make some adjustments to prevent soul crushing annoyances.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public static $sslEnable = true;
|
||||
|
||||
/**
|
||||
* These are the flags that depend on ssl support being compiled into imap.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
static $sslFlags = array('ssl', 'validate-cert', 'novalidate-cert', 'tls', 'notls');
|
||||
/**
|
||||
* These are the flags that depend on ssl support being compiled into imap.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $sslFlags = array('ssl', 'validate-cert', 'novalidate-cert', 'tls', 'notls');
|
||||
|
||||
/**
|
||||
* This is used to prevent the class from putting up conflicting tags. Both directions- key to value, value to key-
|
||||
* are checked, so if "novalidate-cert" is passed then "validate-cert" is removed, and vice-versa.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
static $exclusiveFlags = array('validate-cert' => 'novalidate-cert', 'tls' => 'notls');
|
||||
/**
|
||||
* This is used to prevent the class from putting up conflicting tags. Both directions- key to value, value to key-
|
||||
* are checked, so if "novalidate-cert" is passed then "validate-cert" is removed, and vice-versa.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $exclusiveFlags = array('validate-cert' => 'novalidate-cert', 'tls' => 'notls');
|
||||
|
||||
/**
|
||||
* This is the domain or server path the class is connecting to.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $serverPath;
|
||||
/**
|
||||
* This is the domain or server path the class is connecting to.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $serverPath;
|
||||
|
||||
/**
|
||||
* This is the name of the current mailbox the connection is using.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $mailbox;
|
||||
/**
|
||||
* This is the name of the current mailbox the connection is using.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $mailbox;
|
||||
|
||||
/**
|
||||
* This is the username used to connect to the server.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $username;
|
||||
/**
|
||||
* This is the username used to connect to the server.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $username;
|
||||
|
||||
/**
|
||||
* This is the password used to connect to the server.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $password;
|
||||
/**
|
||||
* This is the password used to connect to the server.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $password;
|
||||
|
||||
/**
|
||||
* This is an array of flags that modify how the class connects to the server. Examples include "ssl" to enforce a
|
||||
* secure connection or "novalidate-cert" to allow for self-signed certificates.
|
||||
*
|
||||
* @link http://us.php.net/manual/en/function.imap-open.php
|
||||
* @var array
|
||||
*/
|
||||
protected $flags = array();
|
||||
/**
|
||||
* This is an array of flags that modify how the class connects to the server. Examples include "ssl" to enforce a
|
||||
* secure connection or "novalidate-cert" to allow for self-signed certificates.
|
||||
*
|
||||
* @link http://us.php.net/manual/en/function.imap-open.php
|
||||
* @var array
|
||||
*/
|
||||
protected $flags = array();
|
||||
|
||||
/**
|
||||
* This is the port used to connect to the server
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $port;
|
||||
/**
|
||||
* This is the port used to connect to the server
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $port;
|
||||
|
||||
/**
|
||||
* This is the set of options, represented by a bitmask, to be passed to the server during connection.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $options = 0;
|
||||
/**
|
||||
* This is the set of options, represented by a bitmask, to be passed to the server during connection.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $options = 0;
|
||||
|
||||
/**
|
||||
* This is the resource connection to the server. It is required by a number of imap based functions to specify how
|
||||
* to connect.
|
||||
*
|
||||
* @var resource
|
||||
*/
|
||||
protected $imapStream;
|
||||
/**
|
||||
* This is the resource connection to the server. It is required by a number of imap based functions to specify how
|
||||
* to connect.
|
||||
*
|
||||
* @var resource
|
||||
*/
|
||||
protected $imapStream;
|
||||
|
||||
/**
|
||||
* This is the name of the service currently being used. Imap is the default, although pop3 and nntp are also
|
||||
* options
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $service = 'imap';
|
||||
/**
|
||||
* This is the name of the service currently being used. Imap is the default, although pop3 and nntp are also
|
||||
* options
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $service = 'imap';
|
||||
|
||||
/**
|
||||
* This constructor takes the location and service thats trying to be connected to as its arguments.
|
||||
*
|
||||
* @param string $serverPath
|
||||
* @param null|int $port
|
||||
* @param null|string $service
|
||||
*/
|
||||
public function __construct($serverPath, $port = 143, $service = 'imap')
|
||||
{
|
||||
$this->serverPath = $serverPath;
|
||||
/**
|
||||
* This constructor takes the location and service thats trying to be connected to as its arguments.
|
||||
*
|
||||
* @param string $serverPath
|
||||
* @param null|int $port
|
||||
* @param null|string $service
|
||||
*/
|
||||
public function __construct($serverPath, $port = 143, $service = 'imap')
|
||||
{
|
||||
$this->serverPath = $serverPath;
|
||||
|
||||
$this->port = $port;
|
||||
$this->port = $port;
|
||||
|
||||
switch ($port) {
|
||||
case 143:
|
||||
$this->setFlag('novalidate-cert');
|
||||
break;
|
||||
switch ($port) {
|
||||
case 143:
|
||||
$this->setFlag('novalidate-cert');
|
||||
break;
|
||||
|
||||
case 993:
|
||||
$this->setFlag('ssl');
|
||||
break;
|
||||
}
|
||||
|
||||
$this->service = $service;
|
||||
case 993:
|
||||
$this->setFlag('ssl');
|
||||
break;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function sets the username and password used to connect to the server.
|
||||
*
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
*/
|
||||
public function setAuthentication($username, $password)
|
||||
{
|
||||
$this->username = $username;
|
||||
$this->password = $password;
|
||||
$this->service = $service;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function sets the username and password used to connect to the server.
|
||||
*
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
*/
|
||||
public function setAuthentication($username, $password)
|
||||
{
|
||||
$this->username = $username;
|
||||
$this->password = $password;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function sets the mailbox to connect to.
|
||||
*
|
||||
* @param string $mailbox
|
||||
*/
|
||||
public function setMailBox($mailbox = '')
|
||||
{
|
||||
$this->mailbox = $mailbox;
|
||||
if (isset($this->imapStream)) {
|
||||
$this->setImapStream();
|
||||
}
|
||||
}
|
||||
|
||||
public function getMailBox()
|
||||
{
|
||||
return $this->mailbox;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function sets or removes flag specifying connection behavior. In many cases the flag is just a one word
|
||||
* deal, so the value attribute is not required. However, if the value parameter is passed false it will clear that
|
||||
* flag.
|
||||
*
|
||||
* @param string $flag
|
||||
* @param null|string|bool $value
|
||||
*/
|
||||
public function setFlag($flag, $value = null)
|
||||
{
|
||||
if (!self::$sslEnable && in_array($flag, self::$sslFlags))
|
||||
return;
|
||||
|
||||
if (isset(self::$exclusiveFlags[$flag])) {
|
||||
$kill = $flag;
|
||||
} elseif ($index = array_search($flag, self::$exclusiveFlags)) {
|
||||
$kill = $index;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function sets the mailbox to connect to.
|
||||
*
|
||||
* @param string $mailbox
|
||||
*/
|
||||
public function setMailBox($mailbox = '')
|
||||
{
|
||||
$this->mailbox = $mailbox;
|
||||
if(isset($this->imapStream))
|
||||
{
|
||||
$this->setImapStream();
|
||||
}
|
||||
if (isset($kill) && isset($this->flags[$kill]))
|
||||
unset($this->flags[$kill]);
|
||||
|
||||
if (isset($value) && $value !== true) {
|
||||
if ($value == false) {
|
||||
unset($this->flags[$flag]);
|
||||
} else {
|
||||
$this->flags[] = $flag . '=' . $value;
|
||||
}
|
||||
} else {
|
||||
$this->flags[] = $flag;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This funtion is used to set various options for connecting to the server.
|
||||
*
|
||||
* @param int $bitmask
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function setOptions($bitmask = 0)
|
||||
{
|
||||
if (!is_numeric($bitmask))
|
||||
throw new \Exception();
|
||||
|
||||
$this->options = $bitmask;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function gets the current saved imap resource and returns it.
|
||||
*
|
||||
* @return resource
|
||||
*/
|
||||
public function getImapStream()
|
||||
{
|
||||
if (!isset($this->imapStream))
|
||||
$this->setImapStream();
|
||||
|
||||
return $this->imapStream;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function takes in all of the connection date (server, port, service, flags, mailbox) and creates the string
|
||||
* thats passed to the imap_open function.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getServerString()
|
||||
{
|
||||
$mailboxPath = $this->getServerSpecification();
|
||||
|
||||
if (isset($this->mailbox))
|
||||
$mailboxPath .= $this->mailbox;
|
||||
|
||||
return $mailboxPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the server specification, without adding any mailbox.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getServerSpecification()
|
||||
{
|
||||
$mailboxPath = '{' . $this->serverPath;
|
||||
|
||||
if (isset($this->port))
|
||||
$mailboxPath .= ':' . $this->port;
|
||||
|
||||
if ($this->service != 'imap')
|
||||
$mailboxPath .= '/' . $this->service;
|
||||
|
||||
foreach ($this->flags as $flag) {
|
||||
$mailboxPath .= '/' . $flag;
|
||||
}
|
||||
|
||||
public function getMailBox()
|
||||
{
|
||||
return $this->mailbox;
|
||||
$mailboxPath .= '}';
|
||||
|
||||
return $mailboxPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function creates or reopens an imapStream when called.
|
||||
*
|
||||
*/
|
||||
protected function setImapStream()
|
||||
{
|
||||
if (isset($this->imapStream)) {
|
||||
if (!imap_reopen($this->imapStream, $this->getServerString(), $this->options, 1))
|
||||
throw new \RuntimeException(imap_last_error());
|
||||
} else {
|
||||
$imapStream = imap_open($this->getServerString(), $this->username, $this->password, $this->options, 1);
|
||||
|
||||
if ($imapStream === false)
|
||||
throw new \RuntimeException(imap_last_error());
|
||||
|
||||
$this->imapStream = $imapStream;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This returns the number of messages that the current mailbox contains.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function numMessages()
|
||||
{
|
||||
return imap_num_msg($this->getImapStream());
|
||||
}
|
||||
|
||||
/**
|
||||
* This function returns an array of ImapMessage object for emails that fit the criteria passed. The criteria string
|
||||
* should be formatted according to the imap search standard, which can be found on the php "imap_search" page or in
|
||||
* section 6.4.4 of RFC 2060
|
||||
*
|
||||
* @link http://us.php.net/imap_search
|
||||
* @link http://www.faqs.org/rfcs/rfc2060
|
||||
* @param string $criteria
|
||||
* @param null|int $limit
|
||||
* @return array An array of ImapMessage objects
|
||||
*/
|
||||
public function search($criteria = 'ALL', $limit = null)
|
||||
{
|
||||
if ($results = imap_search($this->getImapStream(), $criteria, SE_UID)) {
|
||||
if (isset($limit) && count($results) > $limit)
|
||||
$results = array_slice($results, 0, $limit);
|
||||
|
||||
$messages = array();
|
||||
|
||||
foreach ($results as $messageId)
|
||||
$messages[] = new Message($messageId, $this);
|
||||
|
||||
return $messages;
|
||||
} else {
|
||||
return array();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This function returns the recently received emails as an array of ImapMessage objects.
|
||||
*
|
||||
* @param null|int $limit
|
||||
* @return array An array of ImapMessage objects for emails that were recently received by the server.
|
||||
*/
|
||||
public function getRecentMessages($limit = null)
|
||||
{
|
||||
return $this->search('Recent', $limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the emails in the current mailbox as an array of ImapMessage objects.
|
||||
*
|
||||
* @param null|int $limit
|
||||
* @return Message[]
|
||||
*/
|
||||
public function getMessages($limit = null)
|
||||
{
|
||||
$numMessages = $this->numMessages();
|
||||
|
||||
if (isset($limit) && is_numeric($limit) && $limit < $numMessages)
|
||||
$numMessages = $limit;
|
||||
|
||||
if ($numMessages < 1)
|
||||
return array();
|
||||
|
||||
$stream = $this->getImapStream();
|
||||
$messages = array();
|
||||
for ($i = 1; $i <= $numMessages; $i++) {
|
||||
$uid = imap_uid($stream, $i);
|
||||
$messages[] = new Message($uid, $this);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function sets or removes flag specifying connection behavior. In many cases the flag is just a one word
|
||||
* deal, so the value attribute is not required. However, if the value parameter is passed false it will clear that
|
||||
* flag.
|
||||
*
|
||||
* @param string $flag
|
||||
* @param null|string|bool $value
|
||||
*/
|
||||
public function setFlag($flag, $value = null)
|
||||
{
|
||||
if(!self::$sslEnable && in_array($flag, self::$sslFlags))
|
||||
return;
|
||||
return $messages;
|
||||
}
|
||||
|
||||
if(isset(self::$exclusiveFlags[$flag]))
|
||||
{
|
||||
$kill = $flag;
|
||||
}elseif($index = array_search($flag, self::$exclusiveFlags)){
|
||||
$kill = $index;
|
||||
}
|
||||
/**
|
||||
* This function removes all of the messages flagged for deletion from the mailbox.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function expunge()
|
||||
{
|
||||
return imap_expunge($this->getImapStream());
|
||||
}
|
||||
|
||||
if(isset($kill) && isset($this->flags[$kill]))
|
||||
unset($this->flags[$kill]);
|
||||
/**
|
||||
* Checks if the given mailbox exists.
|
||||
*
|
||||
* @param $mailbox
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasMailBox($mailbox)
|
||||
{
|
||||
return (boolean) imap_getmailboxes(
|
||||
$this->getImapStream(),
|
||||
$this->getServerString(),
|
||||
$this->getServerSpecification() . $mailbox
|
||||
);
|
||||
}
|
||||
|
||||
if(isset($value) && $value !== true)
|
||||
{
|
||||
if($value == false)
|
||||
{
|
||||
unset($this->flags[$flag]);
|
||||
}else{
|
||||
$this->flags[] = $flag . '=' . $value;
|
||||
}
|
||||
}else{
|
||||
$this->flags[] = $flag;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This funtion is used to set various options for connecting to the server.
|
||||
*
|
||||
* @param int $bitmask
|
||||
*/
|
||||
public function setOptions($bitmask = 0)
|
||||
{
|
||||
if(!is_numeric($bitmask))
|
||||
throw new ImapException();
|
||||
|
||||
$this->options = $bitmask;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function gets the current saved imap resource and returns it.
|
||||
*
|
||||
* @return resource
|
||||
*/
|
||||
public function getImapStream()
|
||||
{
|
||||
if(!isset($this->imapStream))
|
||||
$this->setImapStream();
|
||||
|
||||
return $this->imapStream;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function takes in all of the connection date (server, port, service, flags, mailbox) and creates the string
|
||||
* thats passed to the imap_open function.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getServerString()
|
||||
{
|
||||
$mailboxPath = $this->getServerSpecification();
|
||||
|
||||
if(isset($this->mailbox))
|
||||
$mailboxPath .= $this->mailbox;
|
||||
|
||||
return $mailboxPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the server specification, without adding any mailbox.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getServerSpecification()
|
||||
{
|
||||
$mailboxPath = '{' . $this->serverPath;
|
||||
|
||||
if (isset($this->port))
|
||||
$mailboxPath .= ':' . $this->port;
|
||||
|
||||
if ($this->service != 'imap')
|
||||
$mailboxPath .= '/' . $this->service;
|
||||
|
||||
foreach ($this->flags as $flag) {
|
||||
$mailboxPath .= '/' . $flag;
|
||||
}
|
||||
|
||||
$mailboxPath .= '}';
|
||||
|
||||
return $mailboxPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function creates or reopens an imapStream when called.
|
||||
*
|
||||
*/
|
||||
protected function setImapStream()
|
||||
{
|
||||
if(isset($this->imapStream))
|
||||
{
|
||||
if(!imap_reopen($this->imapStream, $this->getServerString(), $this->options, 1))
|
||||
throw new \RuntimeException(imap_last_error());
|
||||
}else{
|
||||
$imapStream = imap_open($this->getServerString(), $this->username, $this->password, $this->options, 1);
|
||||
|
||||
if($imapStream === false)
|
||||
throw new \RuntimeException(imap_last_error());
|
||||
|
||||
$this->imapStream = $imapStream;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This returns the number of messages that the current mailbox contains.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function numMessages()
|
||||
{
|
||||
return imap_num_msg($this->getImapStream());
|
||||
}
|
||||
|
||||
/**
|
||||
* This function returns an array of ImapMessage object for emails that fit the criteria passed. The criteria string
|
||||
* should be formatted according to the imap search standard, which can be found on the php "imap_search" page or in
|
||||
* section 6.4.4 of RFC 2060
|
||||
*
|
||||
* @link http://us.php.net/imap_search
|
||||
* @link http://www.faqs.org/rfcs/rfc2060
|
||||
* @param string $criteria
|
||||
* @param null|int $limit
|
||||
* @return array An array of ImapMessage objects
|
||||
*/
|
||||
public function search($criteria = 'ALL', $limit = null)
|
||||
{
|
||||
if($results = imap_search($this->getImapStream(), $criteria, SE_UID))
|
||||
{
|
||||
if(isset($limit) && count($results) > $limit)
|
||||
$results = array_slice($results, 0, $limit);
|
||||
|
||||
$stream = $this->getImapStream();
|
||||
$messages = array();
|
||||
|
||||
foreach($results as $messageId)
|
||||
$messages[] = new Message($messageId, $this);
|
||||
|
||||
return $messages;
|
||||
}else{
|
||||
return array();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This function returns the recently received emails as an array of ImapMessage objects.
|
||||
*
|
||||
* @param null|int $limit
|
||||
* @return array An array of ImapMessage objects for emails that were recently received by the server.
|
||||
*/
|
||||
public function getRecentMessages($limit = null)
|
||||
{
|
||||
return $this->search('Recent', $limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the emails in the current mailbox as an array of ImapMessage objects.
|
||||
*
|
||||
* @param null|int $limit
|
||||
* @return array
|
||||
*/
|
||||
public function getMessages($limit = null)
|
||||
{
|
||||
$numMessages = $this->numMessages();
|
||||
|
||||
if(isset($limit) && is_numeric($limit) && $limit < $numMessages)
|
||||
$numMessages = $limit;
|
||||
|
||||
if($numMessages < 1)
|
||||
return array();
|
||||
|
||||
$stream = $this->getImapStream();
|
||||
$messages = array();
|
||||
for($i = 1; $i <= $numMessages; $i++)
|
||||
{
|
||||
$uid = imap_uid($stream, $i);
|
||||
$messages[] = new Message($uid, $this);
|
||||
}
|
||||
|
||||
return $messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function removes all of the messages flagged for deletion from the mailbox.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function expunge()
|
||||
{
|
||||
return imap_expunge($this->getImapStream());
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given mailbox exists.
|
||||
*
|
||||
* @param $mailbox
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasMailBox($mailbox)
|
||||
{
|
||||
return (boolean) imap_getmailboxes(
|
||||
$this->getImapStream(),
|
||||
$this->getServerString(),
|
||||
$this->getServerSpecification() . $mailbox
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the given mailbox.
|
||||
*
|
||||
* @param $mailbox
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function createMailBox($mailbox)
|
||||
{
|
||||
return imap_createmailbox($this->getImapStream(), $this->getServerSpecification() . $mailbox);
|
||||
}
|
||||
/**
|
||||
* Creates the given mailbox.
|
||||
*
|
||||
* @param $mailbox
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function createMailBox($mailbox)
|
||||
{
|
||||
return imap_createmailbox($this->getImapStream(), $this->getServerSpecification() . $mailbox);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user