Merge pull request #10 from sgrodzicki/master

Fixed PHPDoc & PSR Coding Standards
This commit is contained in:
Robert Hafner 2012-11-08 09:46:14 -08:00
commit b7c53f620e
3 changed files with 1078 additions and 1089 deletions

View File

@ -24,14 +24,14 @@ class Attachment
/** /**
* This is the structure object for the piece of the message body that the attachment is located it. * This is the structure object for the piece of the message body that the attachment is located it.
* *
* @var stdClass * @var \stdClass
*/ */
protected $structure; protected $structure;
/** /**
* This is the unique identifier for the message this attachment belongs to. * This is the unique identifier for the message this attachment belongs to.
* *
* @var unknown_type * @var int
*/ */
protected $messageId; protected $messageId;
@ -45,14 +45,14 @@ class Attachment
/** /**
* This is the id pointing to the section of the message body that contains the attachment. * This is the id pointing to the section of the message body that contains the attachment.
* *
* @var unknown_type * @var int
*/ */
protected $partId; protected $partId;
/** /**
* This is the attachments filename. * This is the attachments filename.
* *
* @var unknown_type * @var string
*/ */
protected $filename; protected $filename;
@ -68,7 +68,7 @@ class Attachment
* only populated if the getData() function is called and should not be directly used. * only populated if the getData() function is called and should not be directly used.
* *
* @internal * @internal
* @var unknown_type * @var array
*/ */
protected $data; protected $data;
@ -77,8 +77,8 @@ class Attachment
* attachment is located at, and the identifier for that body part. As a general rule you should not be creating * 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. * instances of this yourself, but rather should get them from an ImapMessage class.
* *
* @param ImapMessage $message * @param Message $message
* @param stdClass $structure * @param \stdClass $structure
* @param string $partIdentifier * @param string $partIdentifier
*/ */
public function __construct(Message $message, $structure, $partIdentifier = null) public function __construct(Message $message, $structure, $partIdentifier = null)
@ -87,15 +87,14 @@ class Attachment
$this->imapStream = $message->getImapBox()->getImapStream(); $this->imapStream = $message->getImapBox()->getImapStream();
$this->structure = $structure; $this->structure = $structure;
if(isset($partIdentifier)) if (isset($partIdentifier))
$this->partId = $partIdentifier; $this->partId = $partIdentifier;
$parameters = Message::getParametersFromStructure($structure); $parameters = Message::getParametersFromStructure($structure);
if(isset($parameters['filename'])) if (isset($parameters['filename'])) {
{
$this->filename = $parameters['filename']; $this->filename = $parameters['filename'];
}elseif(isset($parameters['name'])){ } elseif (isset($parameters['name'])) {
$this->filename = $parameters['name']; $this->filename = $parameters['name'];
} }
@ -103,7 +102,7 @@ class Attachment
$this->mimeType = Message::typeIdToString($structure->type); $this->mimeType = Message::typeIdToString($structure->type);
if(isset($structure->subtype)) if (isset($structure->subtype))
$this->mimeType .= '/' . strtolower($structure->subtype); $this->mimeType .= '/' . strtolower($structure->subtype);
$this->encoding = $structure->encoding; $this->encoding = $structure->encoding;
@ -113,12 +112,11 @@ class Attachment
* This function returns the data of the attachment. Combined with getMimeType() it can be used to directly output * This function returns the data of the attachment. Combined with getMimeType() it can be used to directly output
* data to a browser. * data to a browser.
* *
* @return binary * @return string
*/ */
public function getData() public function getData()
{ {
if(!isset($this->data)) if (!isset($this->data)) {
{
$messageBody = isset($this->partId) ? $messageBody = isset($this->partId) ?
imap_fetchbody($this->imapStream, $this->messageId, $this->partId, FT_UID) imap_fetchbody($this->imapStream, $this->messageId, $this->partId, FT_UID)
: imap_body($this->imapStream, $this->messageId, FT_UID); : imap_body($this->imapStream, $this->messageId, FT_UID);
@ -126,6 +124,7 @@ class Attachment
$messageBody = Message::decode($messageBody, $this->encoding); $messageBody = Message::decode($messageBody, $this->encoding);
$this->data = $messageBody; $this->data = $messageBody;
} }
return $this->data; return $this->data;
} }
@ -163,12 +162,13 @@ class Attachment
* This function saves the attachment to the passed directory, keeping the original name of the file. * This function saves the attachment to the passed directory, keeping the original name of the file.
* *
* @param string $path * @param string $path
* @return bool
*/ */
public function saveToDirectory($path) public function saveToDirectory($path)
{ {
$path = rtrim($path, '/') . '/'; $path = rtrim($path, '/') . '/';
if(is_dir($path)) if (is_dir($path))
return $this->saveAs($path . $this->getFileName()); return $this->saveAs($path . $this->getFileName());
return false; return false;
@ -177,24 +177,25 @@ class Attachment
/** /**
* This function saves the attachment to the exact specified location. * This function saves the attachment to the exact specified location.
* *
* @param path $path * @param string $path
* @return bool
*/ */
public function saveAs($path) public function saveAs($path)
{ {
$dirname = dirname($path); $dirname = dirname($path);
if(file_exists($path)) if (file_exists($path)) {
{ if (!is_writable($path))
if(!is_writable($path))
return false; return false;
}elseif(!is_dir($dirname) || !is_writable($dirname)){ } elseif (!is_dir($dirname) || !is_writable($dirname)) {
return false; return false;
} }
if(($filePointer = fopen($path, 'w')) == false) if (($filePointer = fopen($path, 'w')) == false)
return false; return false;
$results = fwrite($filePointer, $this->getData()); $results = fwrite($filePointer, $this->getData());
fclose($filePointer); fclose($filePointer);
return is_numeric($results); return is_numeric($results);
} }
} }

View File

@ -23,7 +23,7 @@ class Message
/** /**
* This is the connection/mailbox class that the email came from. * This is the connection/mailbox class that the email came from.
* *
* @var Imap * @var Server
*/ */
protected $imapConnection; protected $imapConnection;
@ -45,21 +45,21 @@ class Message
/** /**
* This as an object which contains header information for the message. * This as an object which contains header information for the message.
* *
* @var stdClass * @var \stdClass
*/ */
protected $headers; protected $headers;
/** /**
* This is an object which contains various status messages and other information about the message. * This is an object which contains various status messages and other information about the message.
* *
* @var stdClass * @var \stdClass
*/ */
protected $messageOverview; protected $messageOverview;
/** /**
* This is an object which contains information about the structure of the message body. * This is an object which contains information about the structure of the message body.
* *
* @var stdClass * @var \stdClass
*/ */
protected $structure; protected $structure;
@ -76,7 +76,7 @@ class Message
* *
* @var string * @var string
*/ */
static protected $flagTypes = array('recent', 'flagged', 'answered', 'deleted', 'seen', 'draft'); protected static $flagTypes = array('recent', 'flagged', 'answered', 'deleted', 'seen', 'draft');
/** /**
* This holds the plantext email message. * This holds the plantext email message.
@ -120,6 +120,13 @@ class Message
*/ */
protected $from; protected $from;
/**
* This is an array of arrays that contain information about the addresses the email was cc'd to.
*
* @var array
*/
protected $to;
/** /**
* This is an array of arrays that contain information about the addresses the email was cc'd to. * This is an array of arrays that contain information about the addresses the email was cc'd to.
* *
@ -137,7 +144,7 @@ class Message
/** /**
* This is an array of ImapAttachments retrieved from the message. * This is an array of ImapAttachments retrieved from the message.
* *
* @var array * @var Attachment[]
*/ */
protected $attachments = array(); protected $attachments = array();
@ -146,7 +153,7 @@ class Message
* *
* @var string * @var string
*/ */
static public $charset = 'UTF-8//TRANSLIT'; public static $charset = 'UTF-8//TRANSLIT';
/** /**
* This constructor takes in the uid for the message and the Imap class representing the mailbox the * This constructor takes in the uid for the message and the Imap class representing the mailbox the
@ -154,7 +161,7 @@ class Message
* through the apprioriate Imap functions. * through the apprioriate Imap functions.
* *
* @param int $messageUniqueId * @param int $messageUniqueId
* @param Imap $mailbox * @param Server $mailbox
*/ */
public function __construct($messageUniqueId, Server $mailbox) public function __construct($messageUniqueId, Server $mailbox)
{ {
@ -180,19 +187,17 @@ class Message
$this->date = strtotime($messageOverview->date); $this->date = strtotime($messageOverview->date);
$this->size = $messageOverview->size; $this->size = $messageOverview->size;
foreach(self::$flagTypes as $flag) foreach (self::$flagTypes as $flag)
$this->status[$flag] = ($messageOverview->$flag == 1); $this->status[$flag] = ($messageOverview->$flag == 1);
/* Next load in all of the header information */ /* Next load in all of the header information */
$headers = $this->getHeaders(); $headers = $this->getHeaders();
if(isset($headers->to)) if (isset($headers->to))
$this->to = $this->processAddressObject($headers->to); $this->to = $this->processAddressObject($headers->to);
if(isset($headers->cc)) if (isset($headers->cc))
$this->cc = $this->processAddressObject($headers->cc); $this->cc = $this->processAddressObject($headers->cc);
$this->from = $this->processAddressObject($headers->from); $this->from = $this->processAddressObject($headers->from);
@ -202,13 +207,12 @@ class Message
$structure = $this->getStructure(); $structure = $this->getStructure();
if(!isset($structure->parts)) if (!isset($structure->parts)) {
{
// not multipart // not multipart
$this->processStructure($structure); $this->processStructure($structure);
}else{ } else {
// multipart // multipart
foreach($structure->parts as $id => $part) foreach ($structure->parts as $id => $part)
$this->processStructure($part, $id + 1); $this->processStructure($part, $id + 1);
} }
} }
@ -219,16 +223,16 @@ class Message
* results are only retrieved from the server once unless passed true as a parameter. * results are only retrieved from the server once unless passed true as a parameter.
* *
* @param bool $forceReload * @param bool $forceReload
* @return stdClass * @return \stdClass
*/ */
public function getOverview($forceReload = false) public function getOverview($forceReload = false)
{ {
if($forceReload || !isset($this->messageOverview)) if ($forceReload || !isset($this->messageOverview)) {
{
// returns an array, and since we just want one message we can grab the only result // returns an array, and since we just want one message we can grab the only result
$results = imap_fetch_overview($this->imapStream, $this->uid, FT_UID); $results = imap_fetch_overview($this->imapStream, $this->uid, FT_UID);
$this->messageOverview = array_shift($results); $this->messageOverview = array_shift($results);
} }
return $this->messageOverview; return $this->messageOverview;
} }
@ -238,12 +242,11 @@ class Message
* once unless passed true as a parameter. * once unless passed true as a parameter.
* *
* @param bool $forceReload * @param bool $forceReload
* @return stdClass * @return \stdClass
*/ */
public function getHeaders($forceReload = false) public function getHeaders($forceReload = false)
{ {
if($forceReload || !isset($this->headers)) if ($forceReload || !isset($this->headers)) {
{
// raw headers (since imap_headerinfo doesn't use the unique id) // raw headers (since imap_headerinfo doesn't use the unique id)
$rawHeaders = imap_fetchheader($this->imapStream, $this->uid, FT_UID); $rawHeaders = imap_fetchheader($this->imapStream, $this->uid, FT_UID);
@ -264,14 +267,15 @@ class Message
* returned by imap_fetchstructure. The results are only retrieved from the server once unless passed true as a * returned by imap_fetchstructure. The results are only retrieved from the server once unless passed true as a
* parameter. * parameter.
* *
* @return stdClass * @param bool $forceReload
* @return \stdClass
*/ */
public function getStructure($forceReload = false) public function getStructure($forceReload = false)
{ {
if($forceReload || !isset($this->structure)) if ($forceReload || !isset($this->structure)) {
{
$this->structure = imap_fetchstructure($this->imapStream, $this->uid, FT_UID); $this->structure = imap_fetchstructure($this->imapStream, $this->uid, FT_UID);
} }
return $this->structure; return $this->structure;
} }
@ -286,25 +290,25 @@ class Message
*/ */
public function getMessageBody($html = false) public function getMessageBody($html = false)
{ {
if($html) if ($html) {
{ if (!isset($this->htmlMessage) && isset($this->plaintextMessage)) {
if(!isset($this->htmlMessage) && isset($this->plaintextMessage))
{
$output = nl2br($this->plaintextMessage); $output = nl2br($this->plaintextMessage);
return $output; return $output;
}elseif(isset($this->htmlMessage)){ } elseif (isset($this->htmlMessage)) {
return $this->htmlMessage; return $this->htmlMessage;
} }
}else{ } else {
if(!isset($this->plaintextMessage) && isset($this->htmlMessage)) if (!isset($this->plaintextMessage) && isset($this->htmlMessage)) {
{
$output = strip_tags($this->htmlMessage); $output = strip_tags($this->htmlMessage);
return $output; return $output;
}elseif(isset($this->plaintextMessage)){ } elseif (isset($this->plaintextMessage)) {
return $this->plaintextMessage; return $this->plaintextMessage;
} }
} }
return false; return false;
} }
@ -320,29 +324,28 @@ class Message
{ {
$addressTypes = array('to', 'cc', 'from', 'reply-to'); $addressTypes = array('to', 'cc', 'from', 'reply-to');
if(!in_array($type, $addressTypes) || !isset($this->$type) || count($this->$type) < 1) if (!in_array($type, $addressTypes) || !isset($this->$type) || count($this->$type) < 1)
return false; return false;
if(!$asString) if (!$asString) {
{ if ($type == 'from')
if($type == 'from')
return $this->from[0]; return $this->from[0];
return $this->$type; return $this->$type;
}else{ } else {
$outputString = ''; $outputString = '';
foreach($this->$type as $address) foreach ($this->$type as $address) {
{ if (isset($set))
if(isset($set))
$outputString .= ', '; $outputString .= ', ';
if(!isset($set)) if (!isset($set))
$set = true; $set = true;
$outputString .= isset($address['name']) ? $outputString .= isset($address['name']) ?
$address['name'] . ' <' . $address['address'] . '>' $address['name'] . ' <' . $address['address'] . '>'
: $address['address']; : $address['address'];
} }
return $outputString; return $outputString;
} }
} }
@ -381,7 +384,7 @@ class Message
/** /**
* This function returns Imap this message came from. * This function returns Imap this message came from.
* *
* @return Imap * @return Server
*/ */
public function getImapBox() public function getImapBox()
{ {
@ -392,7 +395,7 @@ class Message
* This function takes in a structure and identifier and processes that part of the message. If that portion of the * This function takes in a structure and identifier and processes that part of the message. If that portion of the
* message has its own subparts, those are recursively processed using this function. * message has its own subparts, those are recursively processed using this function.
* *
* @param stdClass $structure * @param \stdClass $structure
* @param string $partIdentifier * @param string $partIdentifier
* @todoa process attachments. * @todoa process attachments.
*/ */
@ -400,11 +403,10 @@ class Message
{ {
$parameters = self::getParametersFromStructure($structure); $parameters = self::getParametersFromStructure($structure);
if(isset($parameters['name']) || isset($parameters['filename'])) if (isset($parameters['name']) || isset($parameters['filename'])) {
{
$attachment = new Attachment($this, $structure, $partIdentifier); $attachment = new Attachment($this, $structure, $partIdentifier);
$this->attachments[] = $attachment; $this->attachments[] = $attachment;
}elseif($structure->type == 0 || $structure->type == 1){ } elseif ($structure->type == 0 || $structure->type == 1) {
$messageBody = isset($partIdentifier) ? $messageBody = isset($partIdentifier) ?
imap_fetchbody($this->imapStream, $this->uid, $partIdentifier, FT_UID) imap_fetchbody($this->imapStream, $this->uid, $partIdentifier, FT_UID)
@ -412,25 +414,22 @@ class Message
$messageBody = self::decode($messageBody, $structure->encoding); $messageBody = self::decode($messageBody, $structure->encoding);
if($parameters['charset'] !== self::$charset) if ($parameters['charset'] !== self::$charset)
$messageBody = iconv($parameters['charset'], self::$charset, $messageBody); $messageBody = iconv($parameters['charset'], self::$charset, $messageBody);
if(strtolower($structure->subtype) == 'plain' || $structure->type == 1) if (strtolower($structure->subtype) == 'plain' || $structure->type == 1) {
{ if (isset($this->plaintextMessage)) {
if(isset($this->plaintextMessage))
{
$this->plaintextMessage .= PHP_EOL . PHP_EOL; $this->plaintextMessage .= PHP_EOL . PHP_EOL;
}else{ } else {
$this->plaintextMessage = ''; $this->plaintextMessage = '';
} }
$this->plaintextMessage .= trim($messageBody); $this->plaintextMessage .= trim($messageBody);
}else{ } else {
if(isset($this->htmlMessage)) if (isset($this->htmlMessage)) {
{
$this->htmlMessage .= '<br><br>'; $this->htmlMessage .= '<br><br>';
}else{ } else {
$this->htmlMessage = ''; $this->htmlMessage = '';
} }
@ -438,13 +437,12 @@ class Message
} }
} }
if(isset($structure->parts)){ // multipart: iterate through each part if (isset($structure->parts)) { // multipart: iterate through each part
foreach ($structure->parts as $partIndex => $part) foreach ($structure->parts as $partIndex => $part) {
{
$partId = $partIndex + 1; $partId = $partIndex + 1;
if(isset($partIdentifier)) if (isset($partIdentifier))
$partId = $partIdentifier . '.' . $partId; $partId = $partIdentifier . '.' . $partId;
$this->processStructure($part, $partId); $this->processStructure($part, $partId);
@ -459,13 +457,12 @@ class Message
* @param int|string $encoding * @param int|string $encoding
* @return string * @return string
*/ */
static public function decode($data, $encoding) public static function decode($data, $encoding)
{ {
if(!is_numeric($encoding)) if (!is_numeric($encoding))
$encoding = strtolower($encoding); $encoding = strtolower($encoding);
switch($encoding) switch ($encoding) {
{
case 'quoted-printable': case 'quoted-printable':
case 4: case 4:
return quoted_printable_decode($data); return quoted_printable_decode($data);
@ -485,10 +482,9 @@ class Message
* @param int $id * @param int $id
* @return string * @return string
*/ */
static public function typeIdToString($id) public static function typeIdToString($id)
{
switch($id)
{ {
switch ($id) {
case 0: case 0:
return 'text'; return 'text';
@ -519,18 +515,18 @@ class Message
/** /**
* Takes in a section structure and returns its parameters as an associative array. * Takes in a section structure and returns its parameters as an associative array.
* *
* @param stdClass $structure * @param \stdClass $structure
* @return array * @return array
*/ */
static function getParametersFromStructure($structure) public static function getParametersFromStructure($structure)
{ {
$parameters = array(); $parameters = array();
if(isset($structure->parameters)) if (isset($structure->parameters))
foreach($structure->parameters as $parameter) foreach ($structure->parameters as $parameter)
$parameters[strtolower($parameter->attribute)] = $parameter->value; $parameters[strtolower($parameter->attribute)] = $parameter->value;
if(isset($structure->dparameters)) if (isset($structure->dparameters))
foreach($structure->dparameters as $parameter) foreach ($structure->dparameters as $parameter)
$parameters[strtolower($parameter->attribute)] = $parameter->value; $parameters[strtolower($parameter->attribute)] = $parameter->value;
return $parameters; return $parameters;
@ -546,15 +542,15 @@ class Message
protected function processAddressObject($addresses) protected function processAddressObject($addresses)
{ {
$outputAddresses = array(); $outputAddresses = array();
if(is_array($addresses)) if (is_array($addresses))
foreach($addresses as $address) foreach ($addresses as $address) {
{
$currentAddress = array(); $currentAddress = array();
$currentAddress['address'] = $address->mailbox . '@' . $address->host; $currentAddress['address'] = $address->mailbox . '@' . $address->host;
if(isset($address->personal)) if (isset($address->personal))
$currentAddress['name'] = $address->personal; $currentAddress['name'] = $address->personal;
$outputAddresses[] = $currentAddress; $outputAddresses[] = $currentAddress;
} }
return $outputAddresses; return $outputAddresses;
} }
@ -573,20 +569,19 @@ class Message
* is returned, unless * is returned, unless
* *
* @param null|string $filename * @param null|string $filename
* @return array|bool|ImapAttachments * @return array|bool|Attachment[]
*/ */
public function getAttachments($filename = null) public function getAttachments($filename = null)
{ {
if(!isset($this->attachments) || count($this->attachments) < 1) if (!isset($this->attachments) || count($this->attachments) < 1)
return false; return false;
if(!isset($filename)) if (!isset($filename))
return $this->attachments; return $this->attachments;
$results = array(); $results = array();
foreach($this->attachments as $attachment) foreach ($this->attachments as $attachment) {
{ if ($attachment->getFileName() == $filename)
if($attachment->getFileName() == $filename)
$results[] = $attachment; $results[] = $attachment;
} }
@ -619,19 +614,19 @@ class Message
* *
* @param string $flag Flagged, Answered, Deleted, Seen, Draft * @param string $flag Flagged, Answered, Deleted, Seen, Draft
* @param bool $enable * @param bool $enable
* @throws \InvalidArgumentException
* @return bool * @return bool
*/ */
public function setFlag($flag, $enable = true) public function setFlag($flag, $enable = true)
{ {
if(!in_array($flag, self::$flagTypes) || $flag == 'recent') if (!in_array($flag, self::$flagTypes) || $flag == 'recent')
throw new \InvalidArgumentException('Unable to set invalid flag "' . $flag . '"'); throw new \InvalidArgumentException('Unable to set invalid flag "' . $flag . '"');
$flag = '\\' . ucfirst($flag); $flag = '\\' . ucfirst($flag);
if($enable) if ($enable) {
{
return imap_setflag_full($this->imapStream, $this->uid, $flag, ST_UID); return imap_setflag_full($this->imapStream, $this->uid, $flag, ST_UID);
}else{ } else {
return imap_clearflag_full($this->imapStream, $this->uid, $flag, ST_UID); return imap_clearflag_full($this->imapStream, $this->uid, $flag, ST_UID);
} }
} }

View File

@ -25,14 +25,14 @@ class Server
* *
* @var bool * @var bool
*/ */
static $sslEnable = true; public static $sslEnable = true;
/** /**
* These are the flags that depend on ssl support being compiled into imap. * These are the flags that depend on ssl support being compiled into imap.
* *
* @var array * @var array
*/ */
static $sslFlags = array('ssl', 'validate-cert', 'novalidate-cert', 'tls', 'notls'); 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- * This is used to prevent the class from putting up conflicting tags. Both directions- key to value, value to key-
@ -40,7 +40,7 @@ class Server
* *
* @var array * @var array
*/ */
static $exclusiveFlags = array('validate-cert' => 'novalidate-cert', 'tls' => 'notls'); public static $exclusiveFlags = array('validate-cert' => 'novalidate-cert', 'tls' => 'notls');
/** /**
* This is the domain or server path the class is connecting to. * This is the domain or server path the class is connecting to.
@ -155,8 +155,7 @@ class Server
public function setMailBox($mailbox = '') public function setMailBox($mailbox = '')
{ {
$this->mailbox = $mailbox; $this->mailbox = $mailbox;
if(isset($this->imapStream)) if (isset($this->imapStream)) {
{
$this->setImapStream(); $this->setImapStream();
} }
} }
@ -176,28 +175,25 @@ class Server
*/ */
public function setFlag($flag, $value = null) public function setFlag($flag, $value = null)
{ {
if(!self::$sslEnable && in_array($flag, self::$sslFlags)) if (!self::$sslEnable && in_array($flag, self::$sslFlags))
return; return;
if(isset(self::$exclusiveFlags[$flag])) if (isset(self::$exclusiveFlags[$flag])) {
{
$kill = $flag; $kill = $flag;
}elseif($index = array_search($flag, self::$exclusiveFlags)){ } elseif ($index = array_search($flag, self::$exclusiveFlags)) {
$kill = $index; $kill = $index;
} }
if(isset($kill) && isset($this->flags[$kill])) if (isset($kill) && isset($this->flags[$kill]))
unset($this->flags[$kill]); unset($this->flags[$kill]);
if(isset($value) && $value !== true) if (isset($value) && $value !== true) {
{ if ($value == false) {
if($value == false)
{
unset($this->flags[$flag]); unset($this->flags[$flag]);
}else{ } else {
$this->flags[] = $flag . '=' . $value; $this->flags[] = $flag . '=' . $value;
} }
}else{ } else {
$this->flags[] = $flag; $this->flags[] = $flag;
} }
} }
@ -206,11 +202,12 @@ class Server
* This funtion is used to set various options for connecting to the server. * This funtion is used to set various options for connecting to the server.
* *
* @param int $bitmask * @param int $bitmask
* @throws \Exception
*/ */
public function setOptions($bitmask = 0) public function setOptions($bitmask = 0)
{ {
if(!is_numeric($bitmask)) if (!is_numeric($bitmask))
throw new ImapException(); throw new \Exception();
$this->options = $bitmask; $this->options = $bitmask;
} }
@ -222,7 +219,7 @@ class Server
*/ */
public function getImapStream() public function getImapStream()
{ {
if(!isset($this->imapStream)) if (!isset($this->imapStream))
$this->setImapStream(); $this->setImapStream();
return $this->imapStream; return $this->imapStream;
@ -238,7 +235,7 @@ class Server
{ {
$mailboxPath = $this->getServerSpecification(); $mailboxPath = $this->getServerSpecification();
if(isset($this->mailbox)) if (isset($this->mailbox))
$mailboxPath .= $this->mailbox; $mailboxPath .= $this->mailbox;
return $mailboxPath; return $mailboxPath;
@ -274,14 +271,13 @@ class Server
*/ */
protected function setImapStream() protected function setImapStream()
{ {
if(isset($this->imapStream)) if (isset($this->imapStream)) {
{ if (!imap_reopen($this->imapStream, $this->getServerString(), $this->options, 1))
if(!imap_reopen($this->imapStream, $this->getServerString(), $this->options, 1))
throw new \RuntimeException(imap_last_error()); throw new \RuntimeException(imap_last_error());
}else{ } else {
$imapStream = imap_open($this->getServerString(), $this->username, $this->password, $this->options, 1); $imapStream = imap_open($this->getServerString(), $this->username, $this->password, $this->options, 1);
if($imapStream === false) if ($imapStream === false)
throw new \RuntimeException(imap_last_error()); throw new \RuntimeException(imap_last_error());
$this->imapStream = $imapStream; $this->imapStream = $imapStream;
@ -311,19 +307,17 @@ class Server
*/ */
public function search($criteria = 'ALL', $limit = null) public function search($criteria = 'ALL', $limit = null)
{ {
if($results = imap_search($this->getImapStream(), $criteria, SE_UID)) if ($results = imap_search($this->getImapStream(), $criteria, SE_UID)) {
{ if (isset($limit) && count($results) > $limit)
if(isset($limit) && count($results) > $limit)
$results = array_slice($results, 0, $limit); $results = array_slice($results, 0, $limit);
$stream = $this->getImapStream();
$messages = array(); $messages = array();
foreach($results as $messageId) foreach ($results as $messageId)
$messages[] = new Message($messageId, $this); $messages[] = new Message($messageId, $this);
return $messages; return $messages;
}else{ } else {
return array(); return array();
} }
} }
@ -343,22 +337,21 @@ class Server
* Returns the emails in the current mailbox as an array of ImapMessage objects. * Returns the emails in the current mailbox as an array of ImapMessage objects.
* *
* @param null|int $limit * @param null|int $limit
* @return array * @return Message[]
*/ */
public function getMessages($limit = null) public function getMessages($limit = null)
{ {
$numMessages = $this->numMessages(); $numMessages = $this->numMessages();
if(isset($limit) && is_numeric($limit) && $limit < $numMessages) if (isset($limit) && is_numeric($limit) && $limit < $numMessages)
$numMessages = $limit; $numMessages = $limit;
if($numMessages < 1) if ($numMessages < 1)
return array(); return array();
$stream = $this->getImapStream(); $stream = $this->getImapStream();
$messages = array(); $messages = array();
for($i = 1; $i <= $numMessages; $i++) for ($i = 1; $i <= $numMessages; $i++) {
{
$uid = imap_uid($stream, $i); $uid = imap_uid($stream, $i);
$messages[] = new Message($uid, $this); $messages[] = new Message($uid, $this);
} }