Fixed PHPDoc & PSR Coding Standards

This commit is contained in:
Sebastian Grodzicki 2012-11-08 11:01:04 +01:00
parent baaae55c0c
commit 755d50a064
3 changed files with 1078 additions and 1089 deletions

View File

@ -21,180 +21,181 @@ namespace Fetch;
class Attachment 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;
/** /**
* This is the ImapResource. * This is the ImapResource.
* *
* @var resource * @var resource
*/ */
protected $imapStream; protected $imapStream;
/** /**
* 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;
/** /**
* This is the size of the attachment. * This is the size of the attachment.
* *
* @var int * @var int
*/ */
protected $size; protected $size;
/** /**
* This stores the data of the attachment so it doesn't have to be retrieved from the server multiple times. It is * 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. * 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;
/** /**
* This function takes in an ImapMessage, the structure object for the particular piece of the message body that the * 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 * 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)
{ {
$this->messageId = $message->getUid(); $this->messageId = $message->getUid();
$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'];
}
$this->size = $structure->bytes;
$this->mimeType = Message::typeIdToString($structure->type);
if(isset($structure->subtype))
$this->mimeType .= '/' . strtolower($structure->subtype);
$this->encoding = $structure->encoding;
} }
/** $this->size = $structure->bytes;
* 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);
$messageBody = Message::decode($messageBody, $this->encoding); $this->mimeType = Message::typeIdToString($structure->type);
$this->data = $messageBody;
} if (isset($structure->subtype))
return $this->data; $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;
} }
/** return $this->data;
* 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 function returns the mimetype of the attachment. * This returns the filename of the attachment, or false if one isn't given.
* *
* @return string * @return string
*/ */
public function getMimeType() public function getFileName()
{ {
return $this->mimeType; return (isset($this->filename)) ? $this->filename : false;
} }
/** /**
* This returns the size of the attachment. * This function returns the mimetype of the attachment.
* *
* @return int * @return string
*/ */
public function getSize() public function getMimeType()
{ {
return $this->size; return $this->mimeType;
} }
/** /**
* This function saves the attachment to the passed directory, keeping the original name of the file. * This returns the size of the attachment.
* *
* @param string $path * @return int
*/ */
public function saveToDirectory($path) public function getSize()
{ {
$path = rtrim($path, '/') . '/'; 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; return false;
} elseif (!is_dir($dirname) || !is_writable($dirname)) {
return false;
} }
/** if (($filePointer = fopen($path, 'w')) == false)
* This function saves the attachment to the exact specified location. return false;
*
* @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) $results = fwrite($filePointer, $this->getData());
return false; fclose($filePointer);
$results = fwrite($filePointer, $this->getData()); return is_numeric($results);
fclose($filePointer); }
return is_numeric($results);
}
} }

File diff suppressed because it is too large Load Diff

View File

@ -20,387 +20,380 @@ namespace Fetch;
*/ */
class Server class Server
{ {
/** /**
* When SSL isn't compiled into PHP we need to make some adjustments to prevent soul crushing annoyances. * When SSL isn't compiled into PHP we need to make some adjustments to prevent soul crushing annoyances.
* *
* @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-
* are checked, so if "novalidate-cert" is passed then "validate-cert" is removed, and vice-versa. * are checked, so if "novalidate-cert" is passed then "validate-cert" is removed, and vice-versa.
* *
* @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.
* *
* @var string * @var string
*/ */
protected $serverPath; protected $serverPath;
/** /**
* This is the name of the current mailbox the connection is using. * This is the name of the current mailbox the connection is using.
* *
* @var string * @var string
*/ */
protected $mailbox; protected $mailbox;
/** /**
* This is the username used to connect to the server. * This is the username used to connect to the server.
* *
* @var string * @var string
*/ */
protected $username; protected $username;
/** /**
* This is the password used to connect to the server. * This is the password used to connect to the server.
* *
* @var string * @var string
*/ */
protected $password; protected $password;
/** /**
* This is an array of flags that modify how the class connects to the server. Examples include "ssl" to enforce a * 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. * secure connection or "novalidate-cert" to allow for self-signed certificates.
* *
* @link http://us.php.net/manual/en/function.imap-open.php * @link http://us.php.net/manual/en/function.imap-open.php
* @var array * @var array
*/ */
protected $flags = array(); protected $flags = array();
/** /**
* This is the port used to connect to the server * This is the port used to connect to the server
* *
* @var int * @var int
*/ */
protected $port; protected $port;
/** /**
* This is the set of options, represented by a bitmask, to be passed to the server during connection. * This is the set of options, represented by a bitmask, to be passed to the server during connection.
* *
* @var int * @var int
*/ */
protected $options = 0; protected $options = 0;
/** /**
* This is the resource connection to the server. It is required by a number of imap based functions to specify how * This is the resource connection to the server. It is required by a number of imap based functions to specify how
* to connect. * to connect.
* *
* @var resource * @var resource
*/ */
protected $imapStream; protected $imapStream;
/** /**
* This is the name of the service currently being used. Imap is the default, although pop3 and nntp are also * This is the name of the service currently being used. Imap is the default, although pop3 and nntp are also
* options * options
* *
* @var string * @var string
*/ */
protected $service = 'imap'; protected $service = 'imap';
/** /**
* This constructor takes the location and service thats trying to be connected to as its arguments. * This constructor takes the location and service thats trying to be connected to as its arguments.
* *
* @param string $serverPath * @param string $serverPath
* @param null|int $port * @param null|int $port
* @param null|string $service * @param null|string $service
*/ */
public function __construct($serverPath, $port = 143, $service = 'imap') public function __construct($serverPath, $port = 143, $service = 'imap')
{ {
$this->serverPath = $serverPath; $this->serverPath = $serverPath;
$this->port = $port; $this->port = $port;
switch ($port) { switch ($port) {
case 143: case 143:
$this->setFlag('novalidate-cert'); $this->setFlag('novalidate-cert');
break; break;
case 993: case 993:
$this->setFlag('ssl'); $this->setFlag('ssl');
break; break;
}
$this->service = $service;
} }
/** $this->service = $service;
* This function sets the username and password used to connect to the server. }
*
* @param string $username /**
* @param string $password * This function sets the username and password used to connect to the server.
*/ *
public function setAuthentication($username, $password) * @param string $username
{ * @param string $password
$this->username = $username; */
$this->password = $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;
} }
/** if (isset($kill) && isset($this->flags[$kill]))
* This function sets the mailbox to connect to. unset($this->flags[$kill]);
*
* @param string $mailbox if (isset($value) && $value !== true) {
*/ if ($value == false) {
public function setMailBox($mailbox = '') unset($this->flags[$flag]);
{ } else {
$this->mailbox = $mailbox; $this->flags[] = $flag . '=' . $value;
if(isset($this->imapStream)) }
{ } else {
$this->setImapStream(); $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() $mailboxPath .= '}';
{
return $this->mailbox; 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);
} }
/** return $messages;
* 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])) /**
{ * This function removes all of the messages flagged for deletion from the mailbox.
$kill = $flag; *
}elseif($index = array_search($flag, self::$exclusiveFlags)){ * @return bool
$kill = $index; */
} 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) /**
{ * Creates the given mailbox.
if($value == false) *
{ * @param $mailbox
unset($this->flags[$flag]); *
}else{ * @return bool
$this->flags[] = $flag . '=' . $value; */
} public function createMailBox($mailbox)
}else{ {
$this->flags[] = $flag; return imap_createmailbox($this->getImapStream(), $this->getServerSpecification() . $mailbox);
} }
}
/**
* 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);
}
} }