|
TYPO3 API
SVNRelease
|
00001 <?php 00002 00003 /* 00004 * This file is part of SwiftMailer. 00005 * (c) 2004-2009 Chris Corbyn 00006 * 00007 * For the full copyright and license information, please view the LICENSE 00008 * file that was distributed with this source code. 00009 */ 00010 00011 //@require 'Swift/Mime/HeaderSet.php'; 00012 //@require 'Swift/Mime/HeaderFactory.php'; 00013 00014 /** 00015 * A collection of MIME headers. 00016 * 00017 * @package Swift 00018 * @subpackage Mime 00019 * 00020 * @author Chris Corbyn 00021 */ 00022 class Swift_Mime_SimpleHeaderSet implements Swift_Mime_HeaderSet 00023 { 00024 00025 /** HeaderFactory */ 00026 private $_factory; 00027 00028 /** Collection of set Headers */ 00029 private $_headers = array(); 00030 00031 /** Field ordering details */ 00032 private $_order = array(); 00033 00034 /** List of fields which are required to be displayed */ 00035 private $_required = array(); 00036 00037 /** The charset used by Headers */ 00038 private $_charset; 00039 00040 /** 00041 * Create a new SimpleHeaderSet with the given $factory. 00042 * 00043 * @param Swift_Mime_HeaderFactory $factory 00044 * @param string $charset 00045 */ 00046 public function __construct(Swift_Mime_HeaderFactory $factory, 00047 $charset = null) 00048 { 00049 $this->_factory = $factory; 00050 if (isset($charset)) 00051 { 00052 $this->setCharset($charset); 00053 } 00054 } 00055 00056 /** 00057 * Set the charset used by these headers. 00058 * 00059 * @param string $charset 00060 */ 00061 public function setCharset($charset) 00062 { 00063 $this->_charset = $charset; 00064 $this->_factory->charsetChanged($charset); 00065 $this->_notifyHeadersOfCharset($charset); 00066 } 00067 00068 /** 00069 * Add a new Mailbox Header with a list of $addresses. 00070 * 00071 * @param string $name 00072 * @param array|string $addresses 00073 */ 00074 public function addMailboxHeader($name, $addresses = null) 00075 { 00076 $this->_storeHeader($name, 00077 $this->_factory->createMailboxHeader($name, $addresses)); 00078 } 00079 00080 /** 00081 * Add a new Date header using $timestamp (UNIX time). 00082 * 00083 * @param string $name 00084 * @param int $timestamp 00085 */ 00086 public function addDateHeader($name, $timestamp = null) 00087 { 00088 $this->_storeHeader($name, 00089 $this->_factory->createDateHeader($name, $timestamp)); 00090 } 00091 00092 /** 00093 * Add a new basic text header with $name and $value. 00094 * 00095 * @param string $name 00096 * @param string $value 00097 */ 00098 public function addTextHeader($name, $value = null) 00099 { 00100 $this->_storeHeader($name, 00101 $this->_factory->createTextHeader($name, $value)); 00102 } 00103 00104 /** 00105 * Add a new ParameterizedHeader with $name, $value and $params. 00106 * 00107 * @param string $name 00108 * @param string $value 00109 * @param array $params 00110 */ 00111 public function addParameterizedHeader($name, $value = null, 00112 $params = array()) 00113 { 00114 $this->_storeHeader($name, 00115 $this->_factory->createParameterizedHeader($name, $value, 00116 $params)); 00117 } 00118 00119 /** 00120 * Add a new ID header for Message-ID or Content-ID. 00121 * 00122 * @param string $name 00123 * @param string|array $ids 00124 */ 00125 public function addIdHeader($name, $ids = null) 00126 { 00127 $this->_storeHeader($name, $this->_factory->createIdHeader($name, $ids)); 00128 } 00129 00130 /** 00131 * Add a new Path header with an address (path) in it. 00132 * 00133 * @param string $name 00134 * @param string $path 00135 */ 00136 public function addPathHeader($name, $path = null) 00137 { 00138 $this->_storeHeader($name, $this->_factory->createPathHeader($name, $path)); 00139 } 00140 00141 /** 00142 * Returns true if at least one header with the given $name exists. 00143 * 00144 * If multiple headers match, the actual one may be specified by $index. 00145 * 00146 * @param string $name 00147 * @param int $index 00148 * 00149 * @return boolean 00150 */ 00151 public function has($name, $index = 0) 00152 { 00153 $lowerName = strtolower($name); 00154 return array_key_exists($lowerName, $this->_headers) 00155 && array_key_exists($index, $this->_headers[$lowerName]); 00156 } 00157 00158 /** 00159 * Set a header in the HeaderSet. 00160 * 00161 * The header may be a previously fetched header via {@link get()} or it may 00162 * be one that has been created separately. 00163 * 00164 * If $index is specified, the header will be inserted into the set at this 00165 * offset. 00166 * 00167 * @param Swift_Mime_Header $header 00168 * @param int $index 00169 */ 00170 public function set(Swift_Mime_Header $header, $index = 0) 00171 { 00172 $this->_storeHeader($header->getFieldName(), $header, $index); 00173 } 00174 00175 /** 00176 * Get the header with the given $name. 00177 * 00178 * If multiple headers match, the actual one may be specified by $index. 00179 * Returns NULL if none present. 00180 * 00181 * @param string $name 00182 * @param int $index 00183 * 00184 * @return Swift_Mime_Header 00185 */ 00186 public function get($name, $index = 0) 00187 { 00188 if ($this->has($name, $index)) 00189 { 00190 $lowerName = strtolower($name); 00191 return $this->_headers[$lowerName][$index]; 00192 } 00193 } 00194 00195 /** 00196 * Get all headers with the given $name. 00197 * 00198 * @param string $name 00199 * 00200 * @return array 00201 */ 00202 public function getAll($name = null) 00203 { 00204 if (!isset($name)) 00205 { 00206 $headers = array(); 00207 foreach ($this->_headers as $collection) 00208 { 00209 $headers = array_merge($headers, $collection); 00210 } 00211 return $headers; 00212 } 00213 00214 $lowerName = strtolower($name); 00215 if (!array_key_exists($lowerName, $this->_headers)) 00216 { 00217 return array(); 00218 } 00219 return $this->_headers[$lowerName]; 00220 } 00221 00222 /** 00223 * Remove the header with the given $name if it's set. 00224 * 00225 * If multiple headers match, the actual one may be specified by $index. 00226 * 00227 * @param string $name 00228 * @param int $index 00229 */ 00230 public function remove($name, $index = 0) 00231 { 00232 $lowerName = strtolower($name); 00233 unset($this->_headers[$lowerName][$index]); 00234 } 00235 00236 /** 00237 * Remove all headers with the given $name. 00238 * 00239 * @param string $name 00240 */ 00241 public function removeAll($name) 00242 { 00243 $lowerName = strtolower($name); 00244 unset($this->_headers[$lowerName]); 00245 } 00246 00247 /** 00248 * Create a new instance of this HeaderSet. 00249 * 00250 * @return Swift_Mime_HeaderSet 00251 */ 00252 public function newInstance() 00253 { 00254 return new self($this->_factory); 00255 } 00256 00257 /** 00258 * Define a list of Header names as an array in the correct order. 00259 * 00260 * These Headers will be output in the given order where present. 00261 * 00262 * @param array $sequence 00263 */ 00264 public function defineOrdering(array $sequence) 00265 { 00266 $this->_order = array_flip(array_map('strtolower', $sequence)); 00267 } 00268 00269 /** 00270 * Set a list of header names which must always be displayed when set. 00271 * 00272 * Usually headers without a field value won't be output unless set here. 00273 * 00274 * @param array $names 00275 */ 00276 public function setAlwaysDisplayed(array $names) 00277 { 00278 $this->_required = array_flip(array_map('strtolower', $names)); 00279 } 00280 00281 /** 00282 * Notify this observer that the entity's charset has changed. 00283 * 00284 * @param string $charset 00285 */ 00286 public function charsetChanged($charset) 00287 { 00288 $this->setCharset($charset); 00289 } 00290 00291 /** 00292 * Returns a string with a representation of all headers. 00293 * 00294 * @return string 00295 */ 00296 public function toString() 00297 { 00298 $string = ''; 00299 $headers = $this->_headers; 00300 if ($this->_canSort()) 00301 { 00302 uksort($headers, array($this, '_sortHeaders')); 00303 } 00304 foreach ($headers as $collection) 00305 { 00306 foreach ($collection as $header) 00307 { 00308 if ($this->_isDisplayed($header) || $header->getFieldBody() != '') 00309 { 00310 $string .= $header->toString(); 00311 } 00312 } 00313 } 00314 return $string; 00315 } 00316 00317 /** 00318 * Returns a string representation of this object. 00319 * 00320 * @return string 00321 * 00322 * @see toString() 00323 */ 00324 public function __toString() 00325 { 00326 return $this->toString(); 00327 } 00328 00329 // -- Private methods 00330 00331 /** Save a Header to the internal collection */ 00332 private function _storeHeader($name, Swift_Mime_Header $header, $offset = null) 00333 { 00334 if (!isset($this->_headers[strtolower($name)])) 00335 { 00336 $this->_headers[strtolower($name)] = array(); 00337 } 00338 if (!isset($offset)) 00339 { 00340 $this->_headers[strtolower($name)][] = $header; 00341 } 00342 else 00343 { 00344 $this->_headers[strtolower($name)][$offset] = $header; 00345 } 00346 } 00347 00348 /** Test if the headers can be sorted */ 00349 private function _canSort() 00350 { 00351 return count($this->_order) > 0; 00352 } 00353 00354 /** uksort() algorithm for Header ordering */ 00355 private function _sortHeaders($a, $b) 00356 { 00357 $lowerA = strtolower($a); 00358 $lowerB = strtolower($b); 00359 $aPos = array_key_exists($lowerA, $this->_order) 00360 ? $this->_order[$lowerA] 00361 : -1; 00362 $bPos = array_key_exists($lowerB, $this->_order) 00363 ? $this->_order[$lowerB] 00364 : -1; 00365 00366 if ($aPos == -1) 00367 { 00368 return 1; 00369 } 00370 elseif ($bPos == -1) 00371 { 00372 return -1; 00373 } 00374 00375 return ($aPos < $bPos) ? -1 : 1; 00376 } 00377 00378 /** Test if the given Header is always displayed */ 00379 private function _isDisplayed(Swift_Mime_Header $header) 00380 { 00381 return array_key_exists(strtolower($header->getFieldName()), $this->_required); 00382 } 00383 00384 /** Notify all Headers of the new charset */ 00385 private function _notifyHeadersOfCharset($charset) 00386 { 00387 foreach ($this->_headers as $headerGroup) 00388 { 00389 foreach ($headerGroup as $header) 00390 { 00391 $header->setCharset($charset); 00392 } 00393 } 00394 } 00395 00396 }
1.8.0