|
0
|
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 |
/** |
|
|
13 |
* Creates MIME headers. |
|
|
14 |
* @package Swift |
|
|
15 |
* @subpackage Mime |
|
|
16 |
* @author Chris Corbyn |
|
|
17 |
*/ |
|
|
18 |
class Swift_Mime_SimpleHeaderFactory implements Swift_Mime_HeaderFactory |
|
|
19 |
{ |
|
|
20 |
|
|
|
21 |
/** The HeaderEncoder used by these headers */ |
|
|
22 |
private $_encoder; |
|
|
23 |
|
|
|
24 |
/** The Encoder used by parameters */ |
|
|
25 |
private $_paramEncoder; |
|
|
26 |
|
|
|
27 |
/** The Grammar */ |
|
|
28 |
private $_grammar; |
|
|
29 |
|
|
|
30 |
/** The charset of created Headers */ |
|
|
31 |
private $_charset; |
|
|
32 |
|
|
|
33 |
/** |
|
|
34 |
* Creates a new SimpleHeaderFactory using $encoder and $paramEncoder. |
|
|
35 |
* @param Swift_Mime_HeaderEncoder $encoder |
|
|
36 |
* @param Swift_Encoder $paramEncoder |
|
|
37 |
* @param Swift_Mime_Grammar $grammar |
|
|
38 |
* @param string $charset |
|
|
39 |
*/ |
|
|
40 |
public function __construct(Swift_Mime_HeaderEncoder $encoder, |
|
|
41 |
Swift_Encoder $paramEncoder, Swift_Mime_Grammar $grammar, $charset = null) |
|
|
42 |
{ |
|
|
43 |
$this->_encoder = $encoder; |
|
|
44 |
$this->_paramEncoder = $paramEncoder; |
|
|
45 |
$this->_grammar = $grammar; |
|
|
46 |
$this->_charset = $charset; |
|
|
47 |
} |
|
|
48 |
|
|
|
49 |
/** |
|
|
50 |
* Create a new Mailbox Header with a list of $addresses. |
|
|
51 |
* @param string $name |
|
|
52 |
* @param array|string $addresses |
|
|
53 |
* @return Swift_Mime_Header |
|
|
54 |
*/ |
|
|
55 |
public function createMailboxHeader($name, $addresses = null) |
|
|
56 |
{ |
|
|
57 |
$header = new Swift_Mime_Headers_MailboxHeader($name, $this->_encoder, $this->_grammar); |
|
|
58 |
if (isset($addresses)) |
|
|
59 |
{ |
|
|
60 |
$header->setFieldBodyModel($addresses); |
|
|
61 |
} |
|
|
62 |
$this->_setHeaderCharset($header); |
|
|
63 |
return $header; |
|
|
64 |
} |
|
|
65 |
|
|
|
66 |
/** |
|
|
67 |
* Create a new Date header using $timestamp (UNIX time). |
|
|
68 |
* @param string $name |
|
|
69 |
* @param int $timestamp |
|
|
70 |
* @return Swift_Mime_Header |
|
|
71 |
*/ |
|
|
72 |
public function createDateHeader($name, $timestamp = null) |
|
|
73 |
{ |
|
|
74 |
$header = new Swift_Mime_Headers_DateHeader($name, $this->_grammar); |
|
|
75 |
if (isset($timestamp)) |
|
|
76 |
{ |
|
|
77 |
$header->setFieldBodyModel($timestamp); |
|
|
78 |
} |
|
|
79 |
$this->_setHeaderCharset($header); |
|
|
80 |
return $header; |
|
|
81 |
} |
|
|
82 |
|
|
|
83 |
/** |
|
|
84 |
* Create a new basic text header with $name and $value. |
|
|
85 |
* @param string $name |
|
|
86 |
* @param string $value |
|
|
87 |
* @return Swift_Mime_Header |
|
|
88 |
*/ |
|
|
89 |
public function createTextHeader($name, $value = null) |
|
|
90 |
{ |
|
|
91 |
$header = new Swift_Mime_Headers_UnstructuredHeader($name, $this->_encoder, $this->_grammar); |
|
|
92 |
if (isset($value)) |
|
|
93 |
{ |
|
|
94 |
$header->setFieldBodyModel($value); |
|
|
95 |
} |
|
|
96 |
$this->_setHeaderCharset($header); |
|
|
97 |
return $header; |
|
|
98 |
} |
|
|
99 |
|
|
|
100 |
/** |
|
|
101 |
* Create a new ParameterizedHeader with $name, $value and $params. |
|
|
102 |
* @param string $name |
|
|
103 |
* @param string $value |
|
|
104 |
* @param array $params |
|
|
105 |
* @return Swift_Mime_ParameterizedHeader |
|
|
106 |
*/ |
|
|
107 |
public function createParameterizedHeader($name, $value = null, |
|
|
108 |
$params = array()) |
|
|
109 |
{ |
|
|
110 |
$header = new Swift_Mime_Headers_ParameterizedHeader($name, |
|
|
111 |
$this->_encoder, (strtolower($name) == 'content-disposition') |
|
|
112 |
? $this->_paramEncoder |
|
|
113 |
: null, |
|
|
114 |
$this->_grammar |
|
|
115 |
); |
|
|
116 |
if (isset($value)) |
|
|
117 |
{ |
|
|
118 |
$header->setFieldBodyModel($value); |
|
|
119 |
} |
|
|
120 |
foreach ($params as $k => $v) |
|
|
121 |
{ |
|
|
122 |
$header->setParameter($k, $v); |
|
|
123 |
} |
|
|
124 |
$this->_setHeaderCharset($header); |
|
|
125 |
return $header; |
|
|
126 |
} |
|
|
127 |
|
|
|
128 |
/** |
|
|
129 |
* Create a new ID header for Message-ID or Content-ID. |
|
|
130 |
* @param string $name |
|
|
131 |
* @param string|array $ids |
|
|
132 |
* @return Swift_Mime_Header |
|
|
133 |
*/ |
|
|
134 |
public function createIdHeader($name, $ids = null) |
|
|
135 |
{ |
|
|
136 |
$header = new Swift_Mime_Headers_IdentificationHeader($name, $this->_grammar); |
|
|
137 |
if (isset($ids)) |
|
|
138 |
{ |
|
|
139 |
$header->setFieldBodyModel($ids); |
|
|
140 |
} |
|
|
141 |
$this->_setHeaderCharset($header); |
|
|
142 |
return $header; |
|
|
143 |
} |
|
|
144 |
|
|
|
145 |
/** |
|
|
146 |
* Create a new Path header with an address (path) in it. |
|
|
147 |
* @param string $name |
|
|
148 |
* @param string $path |
|
|
149 |
* @return Swift_Mime_Header |
|
|
150 |
*/ |
|
|
151 |
public function createPathHeader($name, $path = null) |
|
|
152 |
{ |
|
|
153 |
$header = new Swift_Mime_Headers_PathHeader($name, $this->_grammar); |
|
|
154 |
if (isset($path)) |
|
|
155 |
{ |
|
|
156 |
$header->setFieldBodyModel($path); |
|
|
157 |
} |
|
|
158 |
$this->_setHeaderCharset($header); |
|
|
159 |
return $header; |
|
|
160 |
} |
|
|
161 |
|
|
|
162 |
/** |
|
|
163 |
* Notify this observer that the entity's charset has changed. |
|
|
164 |
* @param string $charset |
|
|
165 |
*/ |
|
|
166 |
public function charsetChanged($charset) |
|
|
167 |
{ |
|
|
168 |
$this->_charset = $charset; |
|
|
169 |
$this->_encoder->charsetChanged($charset); |
|
|
170 |
$this->_paramEncoder->charsetChanged($charset); |
|
|
171 |
} |
|
|
172 |
|
|
|
173 |
// -- Private methods |
|
|
174 |
|
|
|
175 |
/** Apply the charset to the Header */ |
|
|
176 |
private function _setHeaderCharset(Swift_Mime_Header $header) |
|
|
177 |
{ |
|
|
178 |
if (isset($this->_charset)) |
|
|
179 |
{ |
|
|
180 |
$header->setCharset($this->_charset); |
|
|
181 |
} |
|
|
182 |
} |
|
|
183 |
|
|
|
184 |
} |