Added 3rd party KH modules

This commit is contained in:
Deon George
2013-04-22 14:19:54 +10:00
parent 8888719653
commit 711c11dc03
176 changed files with 20483 additions and 0 deletions

View File

@@ -0,0 +1,596 @@
<?php
/*
* This file is part of SwiftMailer.
* (c) 2004-2009 Chris Corbyn
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
//@require 'Swift/Mime/Header.php';
//@require 'Swift/Mime/HeaderEncoder.php';
//@require 'Swift/RfcComplianceException.php';
/**
* An abstract base MIME Header.
* @package Swift
* @subpackage Mime
* @author Chris Corbyn
*/
abstract class Swift_Mime_Headers_AbstractHeader implements Swift_Mime_Header
{
/**
* Special characters used in the syntax which need to be escaped.
* @var string[]
* @access private
*/
private $_specials = array();
/**
* Tokens defined in RFC 2822 (and some related RFCs).
* @var string[]
* @access private
*/
private $_grammar = array();
/**
* The name of this Header.
* @var string
* @access private
*/
private $_name;
/**
* The Encoder used to encode this Header.
* @var Swift_Encoder
* @access private
*/
private $_encoder;
/**
* The maximum length of a line in the header.
* @var int
* @access private
*/
private $_lineLength = 78;
/**
* The language used in this Header.
* @var string
*/
private $_lang;
/**
* The character set of the text in this Header.
* @var string
* @access private
*/
private $_charset = 'utf-8';
/**
* The value of this Header, cached.
* @var string
* @access private
*/
private $_cachedValue = null;
/**
* Set the character set used in this Header.
* @param string $charset
*/
public function setCharset($charset)
{
$this->clearCachedValueIf($charset != $this->_charset);
$this->_charset = $charset;
if (isset($this->_encoder))
{
$this->_encoder->charsetChanged($charset);
}
}
/**
* Get the character set used in this Header.
* @return string
*/
public function getCharset()
{
return $this->_charset;
}
/**
* Set the language used in this Header.
* For example, for US English, 'en-us'.
* This can be unspecified.
* @param string $lang
*/
public function setLanguage($lang)
{
$this->clearCachedValueIf($this->_lang != $lang);
$this->_lang = $lang;
}
/**
* Get the language used in this Header.
* @return string
*/
public function getLanguage()
{
return $this->_lang;
}
/**
* Set the encoder used for encoding the header.
* @param Swift_Mime_HeaderEncoder $encoder
*/
public function setEncoder(Swift_Mime_HeaderEncoder $encoder)
{
$this->_encoder = $encoder;
$this->setCachedValue(null);
}
/**
* Get the encoder used for encoding this Header.
* @return Swift_Mime_HeaderEncoder
*/
public function getEncoder()
{
return $this->_encoder;
}
/**
* Get the name of this header (e.g. charset).
* @return string
*/
public function getFieldName()
{
return $this->_name;
}
/**
* Set the maximum length of lines in the header (excluding EOL).
* @param int $lineLength
*/
public function setMaxLineLength($lineLength)
{
$this->clearCachedValueIf($this->_lineLength != $lineLength);
$this->_lineLength = $lineLength;
}
/**
* Get the maximum permitted length of lines in this Header.
* @return int
*/
public function getMaxLineLength()
{
return $this->_lineLength;
}
/**
* Get this Header rendered as a RFC 2822 compliant string.
* @return string
* @throws Swift_RfcComplianceException
*/
public function toString()
{
return $this->_tokensToString($this->toTokens());
}
/**
* Returns a string representation of this object.
*
* @return string
*
* @see toString()
*/
public function __toString()
{
return $this->toString();
}
// -- Points of extension
/**
* Set the name of this Header field.
* @param string $name
* @access protected
*/
protected function setFieldName($name)
{
$this->_name = $name;
}
/**
* Initialize some RFC 2822 (and friends) ABNF grammar definitions.
* @access protected
*/
protected function initializeGrammar()
{
$this->_specials = array(
'(', ')', '<', '>', '[', ']',
':', ';', '@', ',', '.', '"'
);
/*** Refer to RFC 2822 for ABNF grammar ***/
//All basic building blocks
$this->_grammar['NO-WS-CTL'] = '[\x01-\x08\x0B\x0C\x0E-\x19\x7F]';
$this->_grammar['WSP'] = '[ \t]';
$this->_grammar['CRLF'] = '(?:\r\n)';
$this->_grammar['FWS'] = '(?:(?:' . $this->_grammar['WSP'] . '*' .
$this->_grammar['CRLF'] . ')?' . $this->_grammar['WSP'] . ')';
$this->_grammar['text'] = '[\x00-\x08\x0B\x0C\x0E-\x7F]';
$this->_grammar['quoted-pair'] = '(?:\\\\' . $this->_grammar['text'] . ')';
$this->_grammar['ctext'] = '(?:' . $this->_grammar['NO-WS-CTL'] .
'|[\x21-\x27\x2A-\x5B\x5D-\x7E])';
//Uses recursive PCRE (?1) -- could be a weak point??
$this->_grammar['ccontent'] = '(?:' . $this->_grammar['ctext'] . '|' .
$this->_grammar['quoted-pair'] . '|(?1))';
$this->_grammar['comment'] = '(\((?:' . $this->_grammar['FWS'] . '|' .
$this->_grammar['ccontent']. ')*' . $this->_grammar['FWS'] . '?\))';
$this->_grammar['CFWS'] = '(?:(?:' . $this->_grammar['FWS'] . '?' .
$this->_grammar['comment'] . ')*(?:(?:' . $this->_grammar['FWS'] . '?' .
$this->_grammar['comment'] . ')|' . $this->_grammar['FWS'] . '))';
$this->_grammar['qtext'] = '(?:' . $this->_grammar['NO-WS-CTL'] .
'|[\x21\x23-\x5B\x5D-\x7E])';
$this->_grammar['qcontent'] = '(?:' . $this->_grammar['qtext'] . '|' .
$this->_grammar['quoted-pair'] . ')';
$this->_grammar['quoted-string'] = '(?:' . $this->_grammar['CFWS'] . '?"' .
'(' . $this->_grammar['FWS'] . '?' . $this->_grammar['qcontent'] . ')*' .
$this->_grammar['FWS'] . '?"' . $this->_grammar['CFWS'] . '?)';
$this->_grammar['atext'] = '[a-zA-Z0-9!#\$%&\'\*\+\-\/=\?\^_`\{\}\|~]';
$this->_grammar['atom'] = '(?:' . $this->_grammar['CFWS'] . '?' .
$this->_grammar['atext'] . '+' . $this->_grammar['CFWS'] . '?)';
$this->_grammar['dot-atom-text'] = '(?:' . $this->_grammar['atext'] . '+' .
'(\.' . $this->_grammar['atext'] . '+)*)';
$this->_grammar['dot-atom'] = '(?:' . $this->_grammar['CFWS'] . '?' .
$this->_grammar['dot-atom-text'] . '+' . $this->_grammar['CFWS'] . '?)';
$this->_grammar['word'] = '(?:' . $this->_grammar['atom'] . '|' .
$this->_grammar['quoted-string'] . ')';
$this->_grammar['phrase'] = '(?:' . $this->_grammar['word'] . '+?)';
$this->_grammar['no-fold-quote'] = '(?:"(?:' . $this->_grammar['qtext'] .
'|' . $this->_grammar['quoted-pair'] . ')*")';
$this->_grammar['dtext'] = '(?:' . $this->_grammar['NO-WS-CTL'] .
'|[\x21-\x5A\x5E-\x7E])';
$this->_grammar['no-fold-literal'] = '(?:\[(?:' . $this->_grammar['dtext'] .
'|' . $this->_grammar['quoted-pair'] . ')*\])';
//Message IDs
$this->_grammar['id-left'] = '(?:' . $this->_grammar['dot-atom-text'] . '|' .
$this->_grammar['no-fold-quote'] . ')';
$this->_grammar['id-right'] = '(?:' . $this->_grammar['dot-atom-text'] . '|' .
$this->_grammar['no-fold-literal'] . ')';
//Addresses, mailboxes and paths
$this->_grammar['local-part'] = '(?:' . $this->_grammar['dot-atom'] . '|' .
$this->_grammar['quoted-string'] . ')';
$this->_grammar['dcontent'] = '(?:' . $this->_grammar['dtext'] . '|' .
$this->_grammar['quoted-pair'] . ')';
$this->_grammar['domain-literal'] = '(?:' . $this->_grammar['CFWS'] . '?\[(' .
$this->_grammar['FWS'] . '?' . $this->_grammar['dcontent'] . ')*?' .
$this->_grammar['FWS'] . '?\]' . $this->_grammar['CFWS'] . '?)';
$this->_grammar['domain'] = '(?:' . $this->_grammar['dot-atom'] . '|' .
$this->_grammar['domain-literal'] . ')';
$this->_grammar['addr-spec'] = '(?:' . $this->_grammar['local-part'] . '@' .
$this->_grammar['domain'] . ')';
}
/**
* Get the grammar defined for $name token.
* @param string $name execatly as written in the RFC
* @return string
*/
protected function getGrammar($name)
{
if (array_key_exists($name, $this->_grammar))
{
return $this->_grammar[$name];
}
else
{
throw new Swift_RfcComplianceException(
"No such grammar '" . $name . "' defined."
);
}
}
/**
* Escape special characters in a string (convert to quoted-pairs).
* @param string $token
* @param string[] $include additonal chars to escape
* @param string[] $exclude chars from escaping
* @return string
*/
protected function escapeSpecials($token, $include = array(),
$exclude = array())
{
foreach (
array_merge(array('\\'), array_diff($this->_specials, $exclude), $include) as $char)
{
$token = str_replace($char, '\\' . $char, $token);
}
return $token;
}
/**
* Produces a compliant, formatted RFC 2822 'phrase' based on the string given.
* @param Swift_Mime_Header $header
* @param string $string as displayed
* @param string $charset of the text
* @param Swift_Mime_HeaderEncoder $encoder
* @param boolean $shorten the first line to make remove for header name
* @return string
*/
protected function createPhrase(Swift_Mime_Header $header, $string, $charset,
Swift_Mime_HeaderEncoder $encoder = null, $shorten = false)
{
//Treat token as exactly what was given
$phraseStr = $string;
//If it's not valid
if (!preg_match('/^' . $this->_grammar['phrase'] . '$/D', $phraseStr))
{
// .. but it is just ascii text, try escaping some characters
// and make it a quoted-string
if (preg_match('/^' . $this->_grammar['text'] . '*$/D', $phraseStr))
{
$phraseStr = $this->escapeSpecials(
$phraseStr, array('"'), $this->_specials
);
$phraseStr = '"' . $phraseStr . '"';
}
else // ... otherwise it needs encoding
{
//Determine space remaining on line if first line
if ($shorten)
{
$usedLength = strlen($header->getFieldName() . ': ');
}
else
{
$usedLength = 0;
}
$phraseStr = $this->encodeWords($header, $string, $usedLength);
}
}
return $phraseStr;
}
/**
* Encode needed word tokens within a string of input.
* @param string $input
* @param string $usedLength, optional
* @return string
*/
protected function encodeWords(Swift_Mime_Header $header, $input,
$usedLength = -1)
{
$value = '';
$tokens = $this->getEncodableWordTokens($input);
foreach ($tokens as $token)
{
//See RFC 2822, Sect 2.2 (really 2.2 ??)
if ($this->tokenNeedsEncoding($token))
{
//Don't encode starting WSP
$firstChar = substr($token, 0, 1);
switch($firstChar)
{
case ' ':
case "\t":
$value .= $firstChar;
$token = substr($token, 1);
}
if (-1 == $usedLength)
{
$usedLength = strlen($header->getFieldName() . ': ') + strlen($value);
}
$value .= $this->getTokenAsEncodedWord($token, $usedLength);
$header->setMaxLineLength(76); //Forefully override
}
else
{
$value .= $token;
}
}
return $value;
}
/**
* Test if a token needs to be encoded or not.
* @param string $token
* @return boolean
*/
protected function tokenNeedsEncoding($token)
{
return preg_match('~[\x00-\x08\x10-\x19\x7F-\xFF\r\n]~', $token);
}
/**
* Splits a string into tokens in blocks of words which can be encoded quickly.
* @param string $string
* @return string[]
*/
protected function getEncodableWordTokens($string)
{
$tokens = array();
$encodedToken = '';
//Split at all whitespace boundaries
foreach (preg_split('~(?=[\t ])~', $string) as $token)
{
if ($this->tokenNeedsEncoding($token))
{
$encodedToken .= $token;
}
else
{
if (strlen($encodedToken) > 0)
{
$tokens[] = $encodedToken;
$encodedToken = '';
}
$tokens[] = $token;
}
}
if (strlen($encodedToken))
{
$tokens[] = $encodedToken;
}
return $tokens;
}
/**
* Get a token as an encoded word for safe insertion into headers.
* @param string $token to encode
* @param int $firstLineOffset, optional
* @return string
*/
protected function getTokenAsEncodedWord($token, $firstLineOffset = 0)
{
//Adjust $firstLineOffset to account for space needed for syntax
$charsetDecl = $this->_charset;
if (isset($this->_lang))
{
$charsetDecl .= '*' . $this->_lang;
}
$encodingWrapperLength = strlen(
'=?' . $charsetDecl . '?' . $this->_encoder->getName() . '??='
);
if ($firstLineOffset >= 75) //Does this logic need to be here?
{
$firstLineOffset = 0;
}
$encodedTextLines = explode("\r\n",
$this->_encoder->encodeString(
$token, $firstLineOffset, 75 - $encodingWrapperLength
)
);
foreach ($encodedTextLines as $lineNum => $line)
{
$encodedTextLines[$lineNum] = '=?' . $charsetDecl .
'?' . $this->_encoder->getName() .
'?' . $line . '?=';
}
return implode("\r\n ", $encodedTextLines);
}
/**
* Generates tokens from the given string which include CRLF as individual tokens.
* @param string $token
* @return string[]
* @access protected
*/
protected function generateTokenLines($token)
{
return preg_split('~(\r\n)~', $token, -1, PREG_SPLIT_DELIM_CAPTURE);
}
/**
* Set a value into the cache.
* @param string $value
* @access protected
*/
protected function setCachedValue($value)
{
$this->_cachedValue = $value;
}
/**
* Get the value in the cache.
* @return string
* @access protected
*/
protected function getCachedValue()
{
return $this->_cachedValue;
}
/**
* Clear the cached value if $condition is met.
* @param boolean $condition
* @access protected
*/
protected function clearCachedValueIf($condition)
{
if ($condition)
{
$this->setCachedValue(null);
}
}
// -- Private methods
/**
* Generate a list of all tokens in the final header.
* @param string $string input, optional
* @return string[]
* @access private
*/
protected function toTokens($string = null)
{
if (is_null($string))
{
$string = $this->getFieldBody();
}
$tokens = array();
//Generate atoms; split at all invisible boundaries followed by WSP
foreach (preg_split('~(?=[ \t])~', $string) as $token)
{
$tokens = array_merge($tokens, $this->generateTokenLines($token));
}
return $tokens;
}
/**
* Takes an array of tokens which appear in the header and turns them into
* an RFC 2822 compliant string, adding FWSP where needed.
* @param string[] $tokens
* @return string
* @access private
*/
private function _tokensToString(array $tokens)
{
$lineCount = 0;
$headerLines = array();
$headerLines[] = $this->_name . ': ';
$currentLine =& $headerLines[$lineCount++];
//Build all tokens back into compliant header
foreach ($tokens as $i => $token)
{
//Line longer than specified maximum or token was just a new line
if (("\r\n" == $token) ||
($i > 0 && strlen($currentLine . $token) > $this->_lineLength)
&& 0 < strlen($currentLine))
{
$headerLines[] = '';
$currentLine =& $headerLines[$lineCount++];
}
//Append token to the line
if ("\r\n" != $token)
{
$currentLine .= $token;
}
}
//Implode with FWS (RFC 2822, 2.2.3)
return implode("\r\n", $headerLines) . "\r\n";
}
}

View File

@@ -0,0 +1,118 @@
<?php
/*
* This file is part of SwiftMailer.
* (c) 2004-2009 Chris Corbyn
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
//@require 'Swift/Mime/Headers/AbstractHeader.php';
/**
* A Date MIME Header for Swift Mailer.
* @package Swift
* @subpackage Mime
* @author Chris Corbyn
*/
class Swift_Mime_Headers_DateHeader extends Swift_Mime_Headers_AbstractHeader
{
/**
* The UNIX timestamp value of this Header.
* @var int
* @access private
*/
private $_timestamp;
/**
* Creates a new DateHeader with $name and $timestamp.
* Example:
* <code>
* <?php
* $header = new Swift_Mime_Headers_DateHeader('Date', time());
* ?>
* </code>
* @param string $name of Header
*/
public function __construct($name)
{
$this->setFieldName($name);
}
/**
* Get the type of Header that this instance represents.
* @return int
* @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX
* @see TYPE_DATE, TYPE_ID, TYPE_PATH
*/
public function getFieldType()
{
return self::TYPE_DATE;
}
/**
* Set the model for the field body.
* This method takes a UNIX timestamp.
* @param int $model
*/
public function setFieldBodyModel($model)
{
$this->setTimestamp($model);
}
/**
* Get the model for the field body.
* This method returns a UNIX timestamp.
* @return mixed
*/
public function getFieldBodyModel()
{
return $this->getTimestamp();
}
/**
* Get the UNIX timestamp of the Date in this Header.
* @return int
*/
public function getTimestamp()
{
return $this->_timestamp;
}
/**
* Set the UNIX timestamp of the Date in this Header.
* @param int $timestamp
*/
public function setTimestamp($timestamp)
{
if (!is_null($timestamp))
{
$timestamp = (int) $timestamp;
}
$this->clearCachedValueIf($this->_timestamp != $timestamp);
$this->_timestamp = $timestamp;
}
/**
* Get the string value of the body in this Header.
* This is not necessarily RFC 2822 compliant since folding white space will
* not be added at this stage (see {@link toString()} for that).
* @return string
* @see toString()
*/
public function getFieldBody()
{
if (!$this->getCachedValue())
{
if (isset($this->_timestamp))
{
$this->setCachedValue(date('r', $this->_timestamp));
}
}
return $this->getCachedValue();
}
}

View File

@@ -0,0 +1,161 @@
<?php
/*
* This file is part of SwiftMailer.
* (c) 2004-2009 Chris Corbyn
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
//@require 'Swift/Mime/Headers/AbstractHeader.php';
//@require 'Swift/RfcComplianceException.php';
/**
* An ID MIME Header for something like Message-ID or Content-ID.
* @package Swift
* @subpackage Mime
* @author Chris Corbyn
*/
class Swift_Mime_Headers_IdentificationHeader
extends Swift_Mime_Headers_AbstractHeader
{
/**
* The IDs used in the value of this Header.
* This may hold multiple IDs or just a single ID.
* @var string[]
* @access private
*/
private $_ids = array();
/**
* Creates a new IdentificationHeader with the given $name and $id.
* @param string $name
*/
public function __construct($name)
{
$this->setFieldName($name);
$this->initializeGrammar();
}
/**
* Get the type of Header that this instance represents.
* @return int
* @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX
* @see TYPE_DATE, TYPE_ID, TYPE_PATH
*/
public function getFieldType()
{
return self::TYPE_ID;
}
/**
* Set the model for the field body.
* This method takes a string ID, or an array of IDs
* @param mixed $model
* @throws Swift_RfcComplianceException
*/
public function setFieldBodyModel($model)
{
$this->setId($model);
}
/**
* Get the model for the field body.
* This method returns an array of IDs
* @return array
*/
public function getFieldBodyModel()
{
return $this->getIds();
}
/**
* Set the ID used in the value of this header.
* @param string $id
* @throws Swift_RfcComplianceException
*/
public function setId($id)
{
return $this->setIds(array($id));
}
/**
* Get the ID used in the value of this Header.
* If multiple IDs are set only the first is returned.
* @return string
*/
public function getId()
{
if (count($this->_ids) > 0)
{
return $this->_ids[0];
}
}
/**
* Set a collection of IDs to use in the value of this Header.
* @param string[] $ids
* @throws Swift_RfcComplianceException
*/
public function setIds(array $ids)
{
$actualIds = array();
foreach ($ids as $k => $id)
{
if (preg_match(
'/^' . $this->getGrammar('id-left') . '@' .
$this->getGrammar('id-right') . '$/D',
$id
))
{
$actualIds[] = $id;
}
else
{
throw new Swift_RfcComplianceException(
'Invalid ID given <' . $id . '>'
);
}
}
$this->clearCachedValueIf($this->_ids != $actualIds);
$this->_ids = $actualIds;
}
/**
* Get the list of IDs used in this Header.
* @return string[]
*/
public function getIds()
{
return $this->_ids;
}
/**
* Get the string value of the body in this Header.
* This is not necessarily RFC 2822 compliant since folding white space will
* not be added at this stage (see {@link toString()} for that).
* @return string
* @see toString()
* @throws Swift_RfcComplianceException
*/
public function getFieldBody()
{
if (!$this->getCachedValue())
{
$angleAddrs = array();
foreach ($this->_ids as $id)
{
$angleAddrs[] = '<' . $id . '>';
}
$this->setCachedValue(implode(' ', $angleAddrs));
}
return $this->getCachedValue();
}
}

View File

@@ -0,0 +1,316 @@
<?php
/*
* This file is part of SwiftMailer.
* (c) 2004-2009 Chris Corbyn
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
//@require 'Swift/Mime/Headers/AbstractHeader.php';
//@require 'Swift/Mime/HeaderEncoder.php';
/**
* A Mailbox Address MIME Header for something like From or Sender.
* @package Swift
* @subpackage Mime
* @author Chris Corbyn
*/
class Swift_Mime_Headers_MailboxHeader extends Swift_Mime_Headers_AbstractHeader
{
/**
* The mailboxes used in this Header.
* @var string[]
* @access private
*/
private $_mailboxes = array();
/**
* Creates a new MailboxHeader with $name.
* @param string $name of Header
* @param Swift_Mime_HeaderEncoder $encoder
*/
public function __construct($name, Swift_Mime_HeaderEncoder $encoder)
{
$this->setFieldName($name);
$this->setEncoder($encoder);
$this->initializeGrammar();
}
/**
* Get the type of Header that this instance represents.
* @return int
* @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX
* @see TYPE_DATE, TYPE_ID, TYPE_PATH
*/
public function getFieldType()
{
return self::TYPE_MAILBOX;
}
/**
* Set the model for the field body.
* This method takes a string, or an array of addresses.
* @param mixed $model
* @throws Swift_RfcComplianceException
*/
public function setFieldBodyModel($model)
{
$this->setNameAddresses($model);
}
/**
* Get the model for the field body.
* This method returns an associative array like {@link getNameAddresses()}
* @return array
* @throws Swift_RfcComplianceException
*/
public function getFieldBodyModel()
{
return $this->getNameAddresses();
}
/**
* Set a list of mailboxes to be shown in this Header.
* The mailboxes can be a simple array of addresses, or an array of
* key=>value pairs where (email => personalName).
* Example:
* <code>
* <?php
* //Sets two mailboxes in the Header, one with a personal name
* $header->setNameAddresses(array(
* 'chris@swiftmailer.org' => 'Chris Corbyn',
* 'mark@swiftmailer.org' //No associated personal name
* ));
* ?>
* </code>
* @param string|string[] $mailboxes
* @throws Swift_RfcComplianceException
* @see __construct()
* @see setAddresses()
* @see setValue()
*/
public function setNameAddresses($mailboxes)
{
$this->_mailboxes = $this->normalizeMailboxes((array) $mailboxes);
$this->setCachedValue(null); //Clear any cached value
}
/**
* Get the full mailbox list of this Header as an array of valid RFC 2822 strings.
* Example:
* <code>
* <?php
* $header = new Swift_Mime_Headers_MailboxHeader('From',
* array('chris@swiftmailer.org' => 'Chris Corbyn',
* 'mark@swiftmailer.org' => 'Mark Corbyn')
* );
* print_r($header->getNameAddressStrings());
* // array (
* // 0 => Chris Corbyn <chris@swiftmailer.org>,
* // 1 => Mark Corbyn <mark@swiftmailer.org>
* // )
* ?>
* </code>
* @return string[]
* @throws Swift_RfcComplianceException
* @see getNameAddresses()
* @see toString()
*/
public function getNameAddressStrings()
{
return $this->_createNameAddressStrings($this->getNameAddresses());
}
/**
* Get all mailboxes in this Header as key=>value pairs.
* The key is the address and the value is the name (or null if none set).
* Example:
* <code>
* <?php
* $header = new Swift_Mime_Headers_MailboxHeader('From',
* array('chris@swiftmailer.org' => 'Chris Corbyn',
* 'mark@swiftmailer.org' => 'Mark Corbyn')
* );
* print_r($header->getNameAddresses());
* // array (
* // chris@swiftmailer.org => Chris Corbyn,
* // mark@swiftmailer.org => Mark Corbyn
* // )
* ?>
* </code>
* @return string[]
* @see getAddresses()
* @see getNameAddressStrings()
*/
public function getNameAddresses()
{
return $this->_mailboxes;
}
/**
* Makes this Header represent a list of plain email addresses with no names.
* Example:
* <code>
* <?php
* //Sets three email addresses as the Header data
* $header->setAddresses(
* array('one@domain.tld', 'two@domain.tld', 'three@domain.tld')
* );
* ?>
* </code>
* @param string[] $addresses
* @throws Swift_RfcComplianceException
* @see setNameAddresses()
* @see setValue()
*/
public function setAddresses($addresses)
{
return $this->setNameAddresses(array_values((array) $addresses));
}
/**
* Get all email addresses in this Header.
* @return string[]
* @see getNameAddresses()
*/
public function getAddresses()
{
return array_keys($this->_mailboxes);
}
/**
* Remove one or more addresses from this Header.
* @param string|string[] $addresses
*/
public function removeAddresses($addresses)
{
$this->setCachedValue(null);
foreach ((array) $addresses as $address)
{
unset($this->_mailboxes[$address]);
}
}
/**
* Get the string value of the body in this Header.
* This is not necessarily RFC 2822 compliant since folding white space will
* not be added at this stage (see {@link toString()} for that).
* @return string
* @throws Swift_RfcComplianceException
* @see toString()
*/
public function getFieldBody()
{
//Compute the string value of the header only if needed
if (is_null($this->getCachedValue()))
{
$this->setCachedValue($this->createMailboxListString($this->_mailboxes));
}
return $this->getCachedValue();
}
// -- Points of extension
/**
* Normalizes a user-input list of mailboxes into consistent key=>value pairs.
* @param string[] $mailboxes
* @return string[]
* @access protected
*/
protected function normalizeMailboxes(array $mailboxes)
{
$actualMailboxes = array();
foreach ($mailboxes as $key => $value)
{
if (is_string($key)) //key is email addr
{
$address = $key;
$name = $value;
}
else
{
$address = $value;
$name = null;
}
$this->_assertValidAddress($address);
$actualMailboxes[$address] = $name;
}
return $actualMailboxes;
}
/**
* Produces a compliant, formatted display-name based on the string given.
* @param string $displayName as displayed
* @param boolean $shorten the first line to make remove for header name
* @return string
* @access protected
*/
protected function createDisplayNameString($displayName, $shorten = false)
{
return $this->createPhrase($this, $displayName,
$this->getCharset(), $this->getEncoder(), $shorten
);
}
/**
* Creates a string form of all the mailboxes in the passed array.
* @param string[] $mailboxes
* @return string
* @throws Swift_RfcComplianceException
* @access protected
*/
protected function createMailboxListString(array $mailboxes)
{
return implode(', ', $this->_createNameAddressStrings($mailboxes));
}
// -- Private methods
/**
* Return an array of strings conforming the the name-addr spec of RFC 2822.
* @param string[] $mailboxes
* @return string[]
* @access private
*/
private function _createNameAddressStrings(array $mailboxes)
{
$strings = array();
foreach ($mailboxes as $email => $name)
{
$mailboxStr = $email;
if (!is_null($name))
{
$nameStr = $this->createDisplayNameString($name, empty($strings));
$mailboxStr = $nameStr . ' <' . $mailboxStr . '>';
}
$strings[] = $mailboxStr;
}
return $strings;
}
/**
* Throws an Exception if the address passed does not comply with RFC 2822.
* @param string $address
* @throws Exception If invalid.
* @access protected
*/
private function _assertValidAddress($address)
{
if (!preg_match('/^' . $this->getGrammar('addr-spec') . '$/D',
$address))
{
throw new Swift_RfcComplianceException(
'Address in mailbox given [' . $address .
'] does not comply with RFC 2822, 3.6.2.'
);
}
}
}

View File

@@ -0,0 +1,274 @@
<?php
/*
* This file is part of SwiftMailer.
* (c) 2004-2009 Chris Corbyn
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
//@require 'Swift/Mime/Headers/UnstructuredHeader.php';
//@require 'Swift/Mime/HeaderEncoder.php';
//@require 'Swift/Mime/ParameterizedHeader.php';
//@require 'Swift/Encoder.php';
/**
* An abstract base MIME Header.
* @package Swift
* @subpackage Mime
* @author Chris Corbyn
*/
class Swift_Mime_Headers_ParameterizedHeader
extends Swift_Mime_Headers_UnstructuredHeader
implements Swift_Mime_ParameterizedHeader
{
/**
* The Encoder used to encode the parameters.
* @var Swift_Encoder
* @access private
*/
private $_paramEncoder;
/**
* The parameters as an associative array.
* @var string[]
* @access private
*/
private $_params = array();
/**
* RFC 2231's definition of a token.
* @var string
* @access private
*/
private $_tokenRe;
/**
* Creates a new ParameterizedHeader with $name.
* @param string $name
* @param Swift_Mime_HeaderEncoder $encoder
* @param Swift_Encoder $paramEncoder, optional
*/
public function __construct($name, Swift_Mime_HeaderEncoder $encoder,
Swift_Encoder $paramEncoder = null)
{
$this->setFieldName($name);
$this->setEncoder($encoder);
$this->_paramEncoder = $paramEncoder;
$this->initializeGrammar();
$this->_tokenRe = '(?:[\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7E]+)';
}
/**
* Get the type of Header that this instance represents.
* @return int
* @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX
* @see TYPE_DATE, TYPE_ID, TYPE_PATH
*/
public function getFieldType()
{
return self::TYPE_PARAMETERIZED;
}
/**
* Set the character set used in this Header.
* @param string $charset
*/
public function setCharset($charset)
{
parent::setCharset($charset);
if (isset($this->_paramEncoder))
{
$this->_paramEncoder->charsetChanged($charset);
}
}
/**
* Set the value of $parameter.
* @param string $parameter
* @param string $value
*/
public function setParameter($parameter, $value)
{
$this->setParameters(array_merge($this->getParameters(), array($parameter => $value)));
}
/**
* Get the value of $parameter.
* @return string
*/
public function getParameter($parameter)
{
$params = $this->getParameters();
return array_key_exists($parameter, $params)
? $params[$parameter]
: null;
}
/**
* Set an associative array of parameter names mapped to values.
* @param string[]
*/
public function setParameters(array $parameters)
{
$this->clearCachedValueIf($this->_params != $parameters);
$this->_params = $parameters;
}
/**
* Returns an associative array of parameter names mapped to values.
* @return string[]
*/
public function getParameters()
{
return $this->_params;
}
/**
* Get the value of this header prepared for rendering.
* @return string
*/
public function getFieldBody() //TODO: Check caching here
{
$body = parent::getFieldBody();
foreach ($this->_params as $name => $value)
{
if (!is_null($value))
{
//Add the parameter
$body .= '; ' . $this->_createParameter($name, $value);
}
}
return $body;
}
// -- Protected methods
/**
* Generate a list of all tokens in the final header.
* This doesn't need to be overridden in theory, but it is for implementation
* reasons to prevent potential breakage of attributes.
* @return string[]
* @access protected
*/
protected function toTokens($string = null)
{
$tokens = parent::toTokens(parent::getFieldBody());
//Try creating any parameters
foreach ($this->_params as $name => $value)
{
if (!is_null($value))
{
//Add the semi-colon separator
$tokens[count($tokens)-1] .= ';';
$tokens = array_merge($tokens, $this->generateTokenLines(
' ' . $this->_createParameter($name, $value)
));
}
}
return $tokens;
}
// -- Private methods
/**
* Render a RFC 2047 compliant header parameter from the $name and $value.
* @param string $name
* @param string $value
* @return string
* @access private
*/
private function _createParameter($name, $value)
{
$origValue = $value;
$encoded = false;
//Allow room for parameter name, indices, "=" and DQUOTEs
$maxValueLength = $this->getMaxLineLength() - strlen($name . '=*N"";') - 1;
$firstLineOffset = 0;
//If it's not already a valid parameter value...
if (!preg_match('/^' . $this->_tokenRe . '$/D', $value))
{
//TODO: text, or something else??
//... and it's not ascii
if (!preg_match('/^' . $this->getGrammar('text') . '*$/D', $value))
{
$encoded = true;
//Allow space for the indices, charset and language
$maxValueLength = $this->getMaxLineLength() - strlen($name . '*N*="";') - 1;
$firstLineOffset = strlen(
$this->getCharset() . "'" . $this->getLanguage() . "'"
);
}
}
//Encode if we need to
if ($encoded || strlen($value) > $maxValueLength)
{
if (isset($this->_paramEncoder))
{
$value = $this->_paramEncoder->encodeString(
$origValue, $firstLineOffset, $maxValueLength
);
}
else //We have to go against RFC 2183/2231 in some areas for interoperability
{
$value = $this->getTokenAsEncodedWord($origValue);
$encoded = false;
}
}
$valueLines = isset($this->_paramEncoder) ? explode("\r\n", $value) : array($value);
//Need to add indices
if (count($valueLines) > 1)
{
$paramLines = array();
foreach ($valueLines as $i => $line)
{
$paramLines[] = $name . '*' . $i .
$this->_getEndOfParameterValue($line, $encoded, $i == 0);
}
return implode(";\r\n ", $paramLines);
}
else
{
return $name . $this->_getEndOfParameterValue(
$valueLines[0], $encoded, true
);
}
}
/**
* Returns the parameter value from the "=" and beyond.
* @param string $value to append
* @param boolean $encoded
* @param boolean $firstLine
* @return string
* @access private
*/
private function _getEndOfParameterValue($value, $encoded = false, $firstLine = false)
{
if (!preg_match('/^' . $this->_tokenRe . '$/D', $value))
{
$value = '"' . $value . '"';
}
$prepend = '=';
if ($encoded)
{
$prepend = '*=';
if ($firstLine)
{
$prepend = '*=' . $this->getCharset() . "'" . $this->getLanguage() .
"'";
}
}
return $prepend . $value;
}
}

View File

@@ -0,0 +1,126 @@
<?php
/*
* This file is part of SwiftMailer.
* (c) 2004-2009 Chris Corbyn
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
//@require 'Swift/Mime/Headers/AbstractHeader.php';
//@require 'Swift/RfcComplianceException.php';
/**
* A Path Header in Swift Mailer, such a Return-Path.
* @package Swift
* @subpackage Mime
* @author Chris Corbyn
*/
class Swift_Mime_Headers_PathHeader extends Swift_Mime_Headers_AbstractHeader
{
/**
* The address in this Header (if specified).
* @var string
* @access private
*/
private $_address;
/**
* Creates a new PathHeader with the given $name.
* @param string $name
*/
public function __construct($name)
{
$this->setFieldName($name);
$this->initializeGrammar();
}
/**
* Get the type of Header that this instance represents.
* @return int
* @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX
* @see TYPE_DATE, TYPE_ID, TYPE_PATH
*/
public function getFieldType()
{
return self::TYPE_PATH;
}
/**
* Set the model for the field body.
* This method takes a string for an address.
* @param string $model
* @throws Swift_RfcComplianceException
*/
public function setFieldBodyModel($model)
{
$this->setAddress($model);
}
/**
* Get the model for the field body.
* This method returns a string email address.
* @return mixed
*/
public function getFieldBodyModel()
{
return $this->getAddress();
}
/**
* Set the Address which should appear in this Header.
* @param string $address
* @throws Swift_RfcComplianceException
*/
public function setAddress($address)
{
if (is_null($address))
{
$this->_address = null;
}
elseif ('' == $address
|| preg_match('/^' . $this->getGrammar('addr-spec') . '$/D', $address))
{
$this->_address = $address;
}
else
{
throw new Swift_RfcComplianceException(
'Address set in PathHeader does not comply with addr-spec of RFC 2822.'
);
}
$this->setCachedValue(null);
}
/**
* Get the address which is used in this Header (if any).
* Null is returned if no address is set.
* @return string
*/
public function getAddress()
{
return $this->_address;
}
/**
* Get the string value of the body in this Header.
* This is not necessarily RFC 2822 compliant since folding white space will
* not be added at this stage (see {@link toString()} for that).
* @return string
* @see toString()
*/
public function getFieldBody()
{
if (!$this->getCachedValue())
{
if (isset($this->_address))
{
$this->setCachedValue('<' . $this->_address . '>');
}
}
return $this->getCachedValue();
}
}

View File

@@ -0,0 +1,108 @@
<?php
/*
* This file is part of SwiftMailer.
* (c) 2004-2009 Chris Corbyn
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
//@require 'Swift/Mime/Headers/AbstractHeader.php';
//@require 'Swift/Mime/HeaderEncoder.php';
/**
* A Simple MIME Header.
* @package Swift
* @subpackage Mime
* @author Chris Corbyn
*/
class Swift_Mime_Headers_UnstructuredHeader
extends Swift_Mime_Headers_AbstractHeader
{
/**
* The value of this Header.
* @var string
* @access private
*/
private $_value;
/**
* Creates a new SimpleHeader with $name.
* @param string $name
* @param Swift_Mime_HeaderEncoder $encoder
*/
public function __construct($name, Swift_Mime_HeaderEncoder $encoder)
{
$this->setFieldName($name);
$this->setEncoder($encoder);
}
/**
* Get the type of Header that this instance represents.
* @return int
* @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX
* @see TYPE_DATE, TYPE_ID, TYPE_PATH
*/
public function getFieldType()
{
return self::TYPE_TEXT;
}
/**
* Set the model for the field body.
* This method takes a string for the field value.
* @param string $model
*/
public function setFieldBodyModel($model)
{
$this->setValue($model);
}
/**
* Get the model for the field body.
* This method returns a string.
* @return string
*/
public function getFieldBodyModel()
{
return $this->getValue();
}
/**
* Get the (unencoded) value of this header.
* @return string
*/
public function getValue()
{
return $this->_value;
}
/**
* Set the (unencoded) value of this header.
* @param string $value
*/
public function setValue($value)
{
$this->clearCachedValueIf($this->_value != $value);
$this->_value = $value;
}
/**
* Get the value of this header prepared for rendering.
* @return string
*/
public function getFieldBody()
{
if (!$this->getCachedValue())
{
$this->setCachedValue(
str_replace('\\', '\\\\', $this->encodeWords(
$this, $this->_value, -1, $this->getCharset(), $this->getEncoder()
))
);
}
return $this->getCachedValue();
}
}