Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentRssCreator
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob
  • Smarty
    • Cacher
    • Compiler
    • Config
    • Debug
    • PluginsBlock
    • PluginsFilter
    • PluginsFunction
    • PluginsInternal
    • PluginsModifier
    • PluginsModifierCompiler
    • PluginsShared
    • Security
    • Template
    • TemplateResources
  • Swift
    • ByteStream
    • CharacterStream
    • Encoder
    • Events
    • KeyCache
    • Mailer
    • Mime
    • Plugins
    • Transport

Classes

  • Swift_FailoverTransport
  • Swift_LoadBalancedTransport
  • Swift_MailTransport
  • Swift_Plugins_Loggers_ArrayLogger
  • Swift_Plugins_Loggers_EchoLogger
  • Swift_SendmailTransport
  • Swift_SmtpTransport
  • Swift_Transport_AbstractSmtpTransport
  • Swift_Transport_Esmtp_Auth_CramMd5Authenticator
  • Swift_Transport_Esmtp_Auth_LoginAuthenticator
  • Swift_Transport_Esmtp_Auth_PlainAuthenticator
  • Swift_Transport_Esmtp_AuthHandler
  • Swift_Transport_EsmtpTransport
  • Swift_Transport_FailoverTransport
  • Swift_Transport_LoadBalancedTransport
  • Swift_Transport_MailTransport
  • Swift_Transport_SendmailTransport
  • Swift_Transport_SimpleMailInvoker
  • Swift_Transport_StreamBuffer

Interfaces

  • Swift_Plugins_Logger
  • Swift_Plugins_Pop_Pop3Exception
  • Swift_Transport
  • Swift_Transport_Esmtp_Authenticator
  • Swift_Transport_EsmtpHandler
  • Swift_Transport_IoBuffer
  • Swift_Transport_MailInvoker
  • Swift_Transport_SmtpAgent
  • Swift_TransportException
  • Overview
  • Package
  • Function
  • Todo
  • Download
  1: <?php
  2: 
  3: /*
  4:  * This file is part of SwiftMailer.
  5:  * (c) 2004-2009 Chris Corbyn
  6:  *
  7:  * For the full copyright and license information, please view the LICENSE
  8:  * file that was distributed with this source code.
  9:  */
 10: 
 11: /**
 12:  * An abstract base MIME Header.
 13:  * @package Swift
 14:  * @subpackage Mime
 15:  * @author Chris Corbyn
 16:  */
 17: class Swift_Mime_Headers_ParameterizedHeader extends Swift_Mime_Headers_UnstructuredHeader implements Swift_Mime_ParameterizedHeader
 18: {
 19:     /**
 20:      * RFC 2231's definition of a token.
 21:      * @var string
 22:      */
 23:     const TOKEN_REGEX = '(?:[\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7E]+)';
 24: 
 25:     /**
 26:      * The Encoder used to encode the parameters.
 27:      * @var Swift_Encoder
 28:      * @access private
 29:      */
 30:     private $_paramEncoder;
 31: 
 32:     /**
 33:      * The parameters as an associative array.
 34:      * @var string[]
 35:      * @access private
 36:      */
 37:     private $_params = array();
 38: 
 39:     /**
 40:      * Creates a new ParameterizedHeader with $name.
 41:      * @param string                   $name
 42:      * @param Swift_Mime_HeaderEncoder $encoder
 43:      * @param Swift_Encoder            $paramEncoder, optional
 44:      * @param Swift_Mime_Grammar       $grammar
 45:      */
 46:     public function __construct($name, Swift_Mime_HeaderEncoder $encoder, Swift_Encoder $paramEncoder = null, Swift_Mime_Grammar $grammar)
 47:     {
 48:         parent::__construct($name, $encoder, $grammar);
 49:         $this->_paramEncoder = $paramEncoder;
 50:     }
 51: 
 52:     /**
 53:      * Get the type of Header that this instance represents.
 54:      * @return int
 55:      * @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX
 56:      * @see TYPE_DATE, TYPE_ID, TYPE_PATH
 57:      */
 58:     public function getFieldType()
 59:     {
 60:         return self::TYPE_PARAMETERIZED;
 61:     }
 62: 
 63:     /**
 64:      * Set the character set used in this Header.
 65:      * @param string $charset
 66:      */
 67:     public function setCharset($charset)
 68:     {
 69:         parent::setCharset($charset);
 70:         if (isset($this->_paramEncoder)) {
 71:             $this->_paramEncoder->charsetChanged($charset);
 72:         }
 73:     }
 74: 
 75:     /**
 76:      * Set the value of $parameter.
 77:      * @param string $parameter
 78:      * @param string $value
 79:      */
 80:     public function setParameter($parameter, $value)
 81:     {
 82:         $this->setParameters(array_merge($this->getParameters(), array($parameter => $value)));
 83:     }
 84: 
 85:     /**
 86:      * Get the value of $parameter.
 87:      * @return string
 88:      */
 89:     public function getParameter($parameter)
 90:     {
 91:         $params = $this->getParameters();
 92: 
 93:         return array_key_exists($parameter, $params)
 94:             ? $params[$parameter]
 95:             : null;
 96:     }
 97: 
 98:     /**
 99:      * Set an associative array of parameter names mapped to values.
100:      * @param string[]
101:      */
102:     public function setParameters(array $parameters)
103:     {
104:         $this->clearCachedValueIf($this->_params != $parameters);
105:         $this->_params = $parameters;
106:     }
107: 
108:     /**
109:      * Returns an associative array of parameter names mapped to values.
110:      * @return string[]
111:      */
112:     public function getParameters()
113:     {
114:         return $this->_params;
115:     }
116: 
117:     /**
118:      * Get the value of this header prepared for rendering.
119:      * @return string
120:      */
121:     public function getFieldBody() //TODO: Check caching here
122:     {
123:         $body = parent::getFieldBody();
124:         foreach ($this->_params as $name => $value) {
125:             if (!is_null($value)) {
126:                 //Add the parameter
127:                 $body .= '; ' . $this->_createParameter($name, $value);
128:             }
129:         }
130: 
131:         return $body;
132:     }
133: 
134:     // -- Protected methods
135: 
136:     /**
137:      * Generate a list of all tokens in the final header.
138:      * This doesn't need to be overridden in theory, but it is for implementation
139:      * reasons to prevent potential breakage of attributes.
140:      * @param  string $string The string to tokenize
141:      * @return array  An array of tokens as strings
142:      * @access protected
143:      */
144:     protected function toTokens($string = null)
145:     {
146:         $tokens = parent::toTokens(parent::getFieldBody());
147: 
148:         //Try creating any parameters
149:         foreach ($this->_params as $name => $value) {
150:             if (!is_null($value)) {
151:                 //Add the semi-colon separator
152:                 $tokens[count($tokens)-1] .= ';';
153:                 $tokens = array_merge($tokens, $this->generateTokenLines(
154:                     ' ' . $this->_createParameter($name, $value)
155:                     ));
156:             }
157:         }
158: 
159:         return $tokens;
160:     }
161: 
162:     // -- Private methods
163: 
164:     /**
165:      * Render a RFC 2047 compliant header parameter from the $name and $value.
166:      * @param  string $name
167:      * @param  string $value
168:      * @return string
169:      * @access private
170:      */
171:     private function _createParameter($name, $value)
172:     {
173:         $origValue = $value;
174: 
175:         $encoded = false;
176:         //Allow room for parameter name, indices, "=" and DQUOTEs
177:         $maxValueLength = $this->getMaxLineLength() - strlen($name . '=*N"";') - 1;
178:         $firstLineOffset = 0;
179: 
180:         //If it's not already a valid parameter value...
181:         if (!preg_match('/^' . self::TOKEN_REGEX . '$/D', $value)) {
182:             //TODO: text, or something else??
183:             //... and it's not ascii
184:             if (!preg_match('/^' . $this->getGrammar()->getDefinition('text') . '*$/D', $value)) {
185:                 $encoded = true;
186:                 //Allow space for the indices, charset and language
187:                 $maxValueLength = $this->getMaxLineLength() - strlen($name . '*N*="";') - 1;
188:                 $firstLineOffset = strlen(
189:                     $this->getCharset() . "'" . $this->getLanguage() . "'"
190:                     );
191:             }
192:         }
193: 
194:         //Encode if we need to
195:         if ($encoded || strlen($value) > $maxValueLength) {
196:             if (isset($this->_paramEncoder)) {
197:                 $value = $this->_paramEncoder->encodeString(
198:                     $origValue, $firstLineOffset, $maxValueLength, $this->getCharset()
199:                     );
200:             } else { //We have to go against RFC 2183/2231 in some areas for interoperability
201:                 $value = $this->getTokenAsEncodedWord($origValue);
202:                 $encoded = false;
203:             }
204:         }
205: 
206:         $valueLines = isset($this->_paramEncoder) ? explode("\r\n", $value) : array($value);
207: 
208:         //Need to add indices
209:         if (count($valueLines) > 1) {
210:             $paramLines = array();
211:             foreach ($valueLines as $i => $line) {
212:                 $paramLines[] = $name . '*' . $i .
213:                     $this->_getEndOfParameterValue($line, true, $i == 0);
214:             }
215: 
216:             return implode(";\r\n ", $paramLines);
217:         } else {
218:             return $name . $this->_getEndOfParameterValue(
219:                 $valueLines[0], $encoded, true
220:                 );
221:         }
222:     }
223: 
224:     /**
225:      * Returns the parameter value from the "=" and beyond.
226:      * @param  string  $value     to append
227:      * @param  boolean $encoded
228:      * @param  boolean $firstLine
229:      * @return string
230:      * @access private
231:      */
232:     private function _getEndOfParameterValue($value, $encoded = false, $firstLine = false)
233:     {
234:         if (!preg_match('/^' . self::TOKEN_REGEX . '$/D', $value)) {
235:             $value = '"' . $value . '"';
236:         }
237:         $prepend = '=';
238:         if ($encoded) {
239:             $prepend = '*=';
240:             if ($firstLine) {
241:                 $prepend = '*=' . $this->getCharset() . "'" . $this->getLanguage() .
242:                     "'";
243:             }
244:         }
245: 
246:         return $prepend . $value;
247:     }
248: }
249: 
CMS CONTENIDO 4.9.7 API documentation generated by ApiGen