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

View File

@ -23,7 +23,7 @@ class Message
/**
* This is the connection/mailbox class that the email came from.
*
* @var Imap
* @var Server
*/
protected $imapConnection;
@ -45,21 +45,21 @@ class Message
/**
* This as an object which contains header information for the message.
*
* @var stdClass
* @var \stdClass
*/
protected $headers;
/**
* This is an object which contains various status messages and other information about the message.
*
* @var stdClass
* @var \stdClass
*/
protected $messageOverview;
/**
* This is an object which contains information about the structure of the message body.
*
* @var stdClass
* @var \stdClass
*/
protected $structure;
@ -76,7 +76,7 @@ class Message
*
* @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.
@ -120,6 +120,13 @@ class Message
*/
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.
*
@ -137,7 +144,7 @@ class Message
/**
* This is an array of ImapAttachments retrieved from the message.
*
* @var array
* @var Attachment[]
*/
protected $attachments = array();
@ -146,7 +153,7 @@ class Message
*
* @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
@ -154,7 +161,7 @@ class Message
* through the apprioriate Imap functions.
*
* @param int $messageUniqueId
* @param Imap $mailbox
* @param Server $mailbox
*/
public function __construct($messageUniqueId, Server $mailbox)
{
@ -180,19 +187,17 @@ class Message
$this->date = strtotime($messageOverview->date);
$this->size = $messageOverview->size;
foreach(self::$flagTypes as $flag)
foreach (self::$flagTypes as $flag)
$this->status[$flag] = ($messageOverview->$flag == 1);
/* Next load in all of the header information */
$headers = $this->getHeaders();
if(isset($headers->to))
if (isset($headers->to))
$this->to = $this->processAddressObject($headers->to);
if(isset($headers->cc))
if (isset($headers->cc))
$this->cc = $this->processAddressObject($headers->cc);
$this->from = $this->processAddressObject($headers->from);
@ -202,13 +207,12 @@ class Message
$structure = $this->getStructure();
if(!isset($structure->parts))
{
if (!isset($structure->parts)) {
// not multipart
$this->processStructure($structure);
}else{
} else {
// multipart
foreach($structure->parts as $id => $part)
foreach ($structure->parts as $id => $part)
$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.
*
* @param bool $forceReload
* @return stdClass
* @return \stdClass
*/
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
$results = imap_fetch_overview($this->imapStream, $this->uid, FT_UID);
$this->messageOverview = array_shift($results);
}
return $this->messageOverview;
}
@ -238,12 +242,11 @@ class Message
* once unless passed true as a parameter.
*
* @param bool $forceReload
* @return stdClass
* @return \stdClass
*/
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)
$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
* parameter.
*
* @return stdClass
* @param bool $forceReload
* @return \stdClass
*/
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);
}
return $this->structure;
}
@ -286,25 +290,25 @@ class Message
*/
public function getMessageBody($html = false)
{
if($html)
{
if(!isset($this->htmlMessage) && isset($this->plaintextMessage))
{
if ($html) {
if (!isset($this->htmlMessage) && isset($this->plaintextMessage)) {
$output = nl2br($this->plaintextMessage);
return $output;
}elseif(isset($this->htmlMessage)){
} elseif (isset($this->htmlMessage)) {
return $this->htmlMessage;
}
}else{
if(!isset($this->plaintextMessage) && isset($this->htmlMessage))
{
} else {
if (!isset($this->plaintextMessage) && isset($this->htmlMessage)) {
$output = strip_tags($this->htmlMessage);
return $output;
}elseif(isset($this->plaintextMessage)){
} elseif (isset($this->plaintextMessage)) {
return $this->plaintextMessage;
}
}
return false;
}
@ -320,29 +324,28 @@ class Message
{
$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;
if(!$asString)
{
if($type == 'from')
if (!$asString) {
if ($type == 'from')
return $this->from[0];
return $this->$type;
}else{
} else {
$outputString = '';
foreach($this->$type as $address)
{
if(isset($set))
foreach ($this->$type as $address) {
if (isset($set))
$outputString .= ', ';
if(!isset($set))
if (!isset($set))
$set = true;
$outputString .= isset($address['name']) ?
$address['name'] . ' <' . $address['address'] . '>'
: $address['address'];
}
return $outputString;
}
}
@ -381,7 +384,7 @@ class Message
/**
* This function returns Imap this message came from.
*
* @return Imap
* @return Server
*/
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
* message has its own subparts, those are recursively processed using this function.
*
* @param stdClass $structure
* @param \stdClass $structure
* @param string $partIdentifier
* @todoa process attachments.
*/
@ -400,11 +403,10 @@ class Message
{
$parameters = self::getParametersFromStructure($structure);
if(isset($parameters['name']) || isset($parameters['filename']))
{
if (isset($parameters['name']) || isset($parameters['filename'])) {
$attachment = new Attachment($this, $structure, $partIdentifier);
$this->attachments[] = $attachment;
}elseif($structure->type == 0 || $structure->type == 1){
} elseif ($structure->type == 0 || $structure->type == 1) {
$messageBody = isset($partIdentifier) ?
imap_fetchbody($this->imapStream, $this->uid, $partIdentifier, FT_UID)
@ -412,25 +414,22 @@ class Message
$messageBody = self::decode($messageBody, $structure->encoding);
if($parameters['charset'] !== self::$charset)
if ($parameters['charset'] !== self::$charset)
$messageBody = iconv($parameters['charset'], self::$charset, $messageBody);
if(strtolower($structure->subtype) == 'plain' || $structure->type == 1)
{
if(isset($this->plaintextMessage))
{
if (strtolower($structure->subtype) == 'plain' || $structure->type == 1) {
if (isset($this->plaintextMessage)) {
$this->plaintextMessage .= PHP_EOL . PHP_EOL;
}else{
} else {
$this->plaintextMessage = '';
}
$this->plaintextMessage .= trim($messageBody);
}else{
} else {
if(isset($this->htmlMessage))
{
if (isset($this->htmlMessage)) {
$this->htmlMessage .= '<br><br>';
}else{
} else {
$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;
if(isset($partIdentifier))
if (isset($partIdentifier))
$partId = $partIdentifier . '.' . $partId;
$this->processStructure($part, $partId);
@ -459,13 +457,12 @@ class Message
* @param int|string $encoding
* @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);
switch($encoding)
{
switch ($encoding) {
case 'quoted-printable':
case 4:
return quoted_printable_decode($data);
@ -485,10 +482,9 @@ class Message
* @param int $id
* @return string
*/
static public function typeIdToString($id)
{
switch($id)
public static function typeIdToString($id)
{
switch ($id) {
case 0:
return 'text';
@ -519,18 +515,18 @@ class Message
/**
* Takes in a section structure and returns its parameters as an associative array.
*
* @param stdClass $structure
* @param \stdClass $structure
* @return array
*/
static function getParametersFromStructure($structure)
public static function getParametersFromStructure($structure)
{
$parameters = array();
if(isset($structure->parameters))
foreach($structure->parameters as $parameter)
if (isset($structure->parameters))
foreach ($structure->parameters as $parameter)
$parameters[strtolower($parameter->attribute)] = $parameter->value;
if(isset($structure->dparameters))
foreach($structure->dparameters as $parameter)
if (isset($structure->dparameters))
foreach ($structure->dparameters as $parameter)
$parameters[strtolower($parameter->attribute)] = $parameter->value;
return $parameters;
@ -546,15 +542,15 @@ class Message
protected function processAddressObject($addresses)
{
$outputAddresses = array();
if(is_array($addresses))
foreach($addresses as $address)
{
if (is_array($addresses))
foreach ($addresses as $address) {
$currentAddress = array();
$currentAddress['address'] = $address->mailbox . '@' . $address->host;
if(isset($address->personal))
if (isset($address->personal))
$currentAddress['name'] = $address->personal;
$outputAddresses[] = $currentAddress;
}
return $outputAddresses;
}
@ -573,20 +569,19 @@ class Message
* is returned, unless
*
* @param null|string $filename
* @return array|bool|ImapAttachments
* @return array|bool|Attachment[]
*/
public function getAttachments($filename = null)
{
if(!isset($this->attachments) || count($this->attachments) < 1)
if (!isset($this->attachments) || count($this->attachments) < 1)
return false;
if(!isset($filename))
if (!isset($filename))
return $this->attachments;
$results = array();
foreach($this->attachments as $attachment)
{
if($attachment->getFileName() == $filename)
foreach ($this->attachments as $attachment) {
if ($attachment->getFileName() == $filename)
$results[] = $attachment;
}
@ -619,19 +614,19 @@ class Message
*
* @param string $flag Flagged, Answered, Deleted, Seen, Draft
* @param bool $enable
* @throws \InvalidArgumentException
* @return bool
*/
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 . '"');
$flag = '\\' . ucfirst($flag);
if($enable)
{
if ($enable) {
return imap_setflag_full($this->imapStream, $this->uid, $flag, ST_UID);
}else{
} else {
return imap_clearflag_full($this->imapStream, $this->uid, $flag, ST_UID);
}
}

View File

@ -25,14 +25,14 @@ class Server
*
* @var bool
*/
static $sslEnable = true;
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');
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-
@ -40,7 +40,7 @@ class Server
*
* @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.
@ -155,8 +155,7 @@ class Server
public function setMailBox($mailbox = '')
{
$this->mailbox = $mailbox;
if(isset($this->imapStream))
{
if (isset($this->imapStream)) {
$this->setImapStream();
}
}
@ -176,28 +175,25 @@ class Server
*/
public function setFlag($flag, $value = null)
{
if(!self::$sslEnable && in_array($flag, self::$sslFlags))
if (!self::$sslEnable && in_array($flag, self::$sslFlags))
return;
if(isset(self::$exclusiveFlags[$flag]))
{
if (isset(self::$exclusiveFlags[$flag])) {
$kill = $flag;
}elseif($index = array_search($flag, self::$exclusiveFlags)){
} elseif ($index = array_search($flag, self::$exclusiveFlags)) {
$kill = $index;
}
if(isset($kill) && isset($this->flags[$kill]))
if (isset($kill) && isset($this->flags[$kill]))
unset($this->flags[$kill]);
if(isset($value) && $value !== true)
{
if($value == false)
{
if (isset($value) && $value !== true) {
if ($value == false) {
unset($this->flags[$flag]);
}else{
} else {
$this->flags[] = $flag . '=' . $value;
}
}else{
} else {
$this->flags[] = $flag;
}
}
@ -206,11 +202,12 @@ class Server
* 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 ImapException();
if (!is_numeric($bitmask))
throw new \Exception();
$this->options = $bitmask;
}
@ -222,7 +219,7 @@ class Server
*/
public function getImapStream()
{
if(!isset($this->imapStream))
if (!isset($this->imapStream))
$this->setImapStream();
return $this->imapStream;
@ -238,7 +235,7 @@ class Server
{
$mailboxPath = $this->getServerSpecification();
if(isset($this->mailbox))
if (isset($this->mailbox))
$mailboxPath .= $this->mailbox;
return $mailboxPath;
@ -274,14 +271,13 @@ class Server
*/
protected function setImapStream()
{
if(isset($this->imapStream))
{
if(!imap_reopen($this->imapStream, $this->getServerString(), $this->options, 1))
if (isset($this->imapStream)) {
if (!imap_reopen($this->imapStream, $this->getServerString(), $this->options, 1))
throw new \RuntimeException(imap_last_error());
}else{
} else {
$imapStream = imap_open($this->getServerString(), $this->username, $this->password, $this->options, 1);
if($imapStream === false)
if ($imapStream === false)
throw new \RuntimeException(imap_last_error());
$this->imapStream = $imapStream;
@ -311,19 +307,17 @@ class Server
*/
public function search($criteria = 'ALL', $limit = null)
{
if($results = imap_search($this->getImapStream(), $criteria, SE_UID))
{
if(isset($limit) && count($results) > $limit)
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)
foreach ($results as $messageId)
$messages[] = new Message($messageId, $this);
return $messages;
}else{
} else {
return array();
}
}
@ -343,22 +337,21 @@ class Server
* Returns the emails in the current mailbox as an array of ImapMessage objects.
*
* @param null|int $limit
* @return array
* @return Message[]
*/
public function getMessages($limit = null)
{
$numMessages = $this->numMessages();
if(isset($limit) && is_numeric($limit) && $limit < $numMessages)
if (isset($limit) && is_numeric($limit) && $limit < $numMessages)
$numMessages = $limit;
if($numMessages < 1)
if ($numMessages < 1)
return array();
$stream = $this->getImapStream();
$messages = array();
for($i = 1; $i <= $numMessages; $i++)
{
for ($i = 1; $i <= $numMessages; $i++) {
$uid = imap_uid($stream, $i);
$messages[] = new Message($uid, $this);
}