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
|
|
|
* A CharacterStream implementation which stores characters in an internal array. |
13
|
|
|
* |
14
|
|
|
* @author Xavier De Cock <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class Swift_CharacterStream_NgCharacterStream implements Swift_CharacterStream |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The char reader (lazy-loaded) for the current charset. |
20
|
|
|
* |
21
|
|
|
* @var Swift_CharacterReader |
22
|
|
|
*/ |
23
|
|
|
private $_charReader; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* A factory for creating CharacterReader instances. |
27
|
|
|
* |
28
|
|
|
* @var Swift_CharacterReaderFactory |
29
|
|
|
*/ |
30
|
|
|
private $_charReaderFactory; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The character set this stream is using. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $_charset; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The data's stored as-is. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $_datas = ''; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Number of bytes in the stream. |
48
|
|
|
* |
49
|
|
|
* @var int |
50
|
|
|
*/ |
51
|
|
|
private $_datasSize = 0; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Map. |
55
|
|
|
* |
56
|
|
|
* @var mixed |
57
|
|
|
*/ |
58
|
|
|
private $_map; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Map Type. |
62
|
|
|
* |
63
|
|
|
* @var int |
64
|
|
|
*/ |
65
|
|
|
private $_mapType = 0; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Number of characters in the stream. |
69
|
|
|
* |
70
|
|
|
* @var int |
71
|
|
|
*/ |
72
|
|
|
private $_charCount = 0; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Position in the stream. |
76
|
|
|
* |
77
|
|
|
* @var int |
78
|
|
|
*/ |
79
|
|
|
private $_currentPos = 0; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Constructor. |
83
|
|
|
* |
84
|
|
|
* @param Swift_CharacterReaderFactory $factory |
85
|
|
|
* @param string $charset |
86
|
|
|
*/ |
87
|
9 |
|
public function __construct(Swift_CharacterReaderFactory $factory, $charset) |
88
|
|
|
{ |
89
|
9 |
|
$this->setCharacterReaderFactory($factory); |
90
|
9 |
|
$this->setCharacterSet($charset); |
91
|
9 |
|
} |
92
|
|
|
|
93
|
|
|
/* -- Changing parameters of the stream -- */ |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set the character set used in this CharacterStream. |
97
|
|
|
* |
98
|
|
|
* @param string $charset |
99
|
|
|
*/ |
100
|
9 |
|
public function setCharacterSet($charset) |
101
|
|
|
{ |
102
|
9 |
|
if ($charset) { |
103
|
9 |
|
$this->_charset = $charset; |
104
|
9 |
|
$this->_charReader = null; |
105
|
9 |
|
$this->_mapType = 0; |
106
|
|
|
|
107
|
9 |
|
$this->setCharReaderForCurrentCharset(); |
108
|
9 |
|
} |
109
|
9 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set the CharacterReaderFactory for multi charset support. |
113
|
|
|
* |
114
|
|
|
* @param Swift_CharacterReaderFactory $factory |
115
|
|
|
*/ |
116
|
9 |
|
public function setCharacterReaderFactory(Swift_CharacterReaderFactory $factory) |
117
|
|
|
{ |
118
|
9 |
|
$this->_charReaderFactory = $factory; |
119
|
9 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @see Swift_CharacterStream::flushContents() |
123
|
|
|
*/ |
124
|
9 |
|
public function flushContents() |
125
|
|
|
{ |
126
|
9 |
|
$this->_datas = null; |
127
|
9 |
|
$this->_map = null; |
128
|
9 |
|
$this->_charCount = 0; |
129
|
9 |
|
$this->_currentPos = 0; |
130
|
9 |
|
$this->_datasSize = 0; |
131
|
9 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @see Swift_CharacterStream::importByteStream() |
135
|
|
|
* |
136
|
|
|
* @param Swift_OutputByteStream $os |
137
|
|
|
*/ |
138
|
2 |
View Code Duplication |
public function importByteStream(Swift_OutputByteStream $os) |
|
|
|
|
139
|
|
|
{ |
140
|
2 |
|
$this->flushContents(); |
141
|
2 |
|
$blocks = 512; |
142
|
2 |
|
$os->setReadPointer(0); |
143
|
|
|
|
144
|
2 |
|
while (false !== ($read = $os->read($blocks))) { |
145
|
2 |
|
$this->write($read); |
|
|
|
|
146
|
2 |
|
} |
147
|
2 |
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @see Swift_CharacterStream::importString() |
151
|
|
|
* |
152
|
|
|
* @param string $string |
153
|
|
|
*/ |
154
|
7 |
|
public function importString($string) |
155
|
|
|
{ |
156
|
7 |
|
$this->flushContents(); |
157
|
7 |
|
$this->write($string); |
158
|
7 |
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @see Swift_CharacterStream::read() |
162
|
|
|
* |
163
|
|
|
* @param int $length |
164
|
|
|
* |
165
|
|
|
* @return string|false false on error |
166
|
|
|
*/ |
167
|
9 |
|
public function read($length) |
168
|
|
|
{ |
169
|
9 |
|
if ($this->_currentPos >= $this->_charCount) { |
170
|
9 |
|
return false; |
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
9 |
|
$ret = false; |
|
|
|
|
174
|
9 |
|
$length = $this->_currentPos + $length > $this->_charCount ? $this->_charCount - $this->_currentPos : $length; |
175
|
9 |
|
switch ($this->_mapType) { |
176
|
9 |
|
case Swift_CharacterReader::MAP_TYPE_FIXED_LEN: |
177
|
1 |
|
$len = $length * $this->_map; |
178
|
1 |
|
$ret = substr($this->_datas, $this->_currentPos * $this->_map, $len); |
179
|
1 |
|
$this->_currentPos += $length; |
180
|
1 |
|
break; |
181
|
|
|
|
182
|
9 |
|
case Swift_CharacterReader::MAP_TYPE_INVALID: |
183
|
|
|
$ret = ''; |
184
|
|
|
for (; $this->_currentPos < $length; ++$this->_currentPos) { |
185
|
|
|
if (isset($this->_map[$this->_currentPos])) { |
186
|
|
|
$ret .= '?'; |
187
|
|
|
} else { |
188
|
|
|
$ret .= $this->_datas[$this->_currentPos]; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
break; |
192
|
|
|
|
193
|
9 |
|
case Swift_CharacterReader::MAP_TYPE_POSITIONS: |
194
|
9 |
|
$end = $this->_currentPos + $length; |
195
|
9 |
|
$end = $end > $this->_charCount ? $this->_charCount : $end; |
196
|
9 |
|
$ret = ''; |
197
|
|
|
|
198
|
9 |
|
$start = 0; |
199
|
9 |
|
if ($this->_currentPos > 0) { |
200
|
3 |
|
$start = $this->_map['p'][$this->_currentPos - 1]; |
201
|
3 |
|
} |
202
|
|
|
|
203
|
9 |
|
$to = $start; |
204
|
9 |
|
for (; $this->_currentPos < $end; ++$this->_currentPos) { |
205
|
9 |
|
if (isset($this->_map['i'], $this->_map['i'][$this->_currentPos])) { |
206
|
|
|
$ret .= substr($this->_datas, $start, $to - $start) . '?'; |
207
|
|
|
$start = $this->_map['p'][$this->_currentPos]; |
208
|
|
|
} else { |
209
|
9 |
|
$to = $this->_map['p'][$this->_currentPos]; |
210
|
|
|
} |
211
|
9 |
|
} |
212
|
|
|
|
213
|
9 |
|
$ret .= substr($this->_datas, $start, $to - $start); |
214
|
9 |
|
break; |
215
|
9 |
|
} |
216
|
|
|
|
217
|
9 |
|
return $ret; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @see Swift_CharacterStream::readBytes() |
222
|
|
|
* |
223
|
|
|
* @param int $length |
224
|
|
|
* |
225
|
|
|
* @return int[]|false false on error |
226
|
|
|
*/ |
227
|
9 |
View Code Duplication |
public function readBytes($length) |
|
|
|
|
228
|
|
|
{ |
229
|
9 |
|
$read = $this->read($length); |
230
|
9 |
|
if ($read !== false) { |
231
|
9 |
|
return \array_map('\ord', \str_split($read, 1)); |
232
|
|
|
} |
233
|
|
|
|
234
|
9 |
|
return false; |
|
|
|
|
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @see Swift_CharacterStream::setPointer() |
239
|
|
|
* |
240
|
|
|
* @param int $charOffset |
241
|
|
|
*/ |
242
|
|
|
public function setPointer($charOffset) |
243
|
|
|
{ |
244
|
|
|
if ($this->_charCount < $charOffset) { |
245
|
|
|
$charOffset = $this->_charCount; |
246
|
|
|
} |
247
|
|
|
$this->_currentPos = $charOffset; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Set the char-reader for the current charset. |
252
|
|
|
*/ |
253
|
9 |
|
private function setCharReaderForCurrentCharset() |
254
|
|
|
{ |
255
|
9 |
|
if (null === $this->_charReader) { |
256
|
9 |
|
$this->_charReader = $this->_charReaderFactory->getReaderFor($this->_charset); |
257
|
9 |
|
$this->_map = array(); |
258
|
9 |
|
$this->_mapType = $this->_charReader->getMapType(); |
259
|
9 |
|
} |
260
|
9 |
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @see Swift_CharacterStream::write() |
264
|
|
|
* |
265
|
|
|
* @param string $chars |
266
|
|
|
*/ |
267
|
9 |
|
public function write($chars) |
268
|
|
|
{ |
269
|
9 |
|
$this->setCharReaderForCurrentCharset(); |
270
|
|
|
|
271
|
9 |
|
$ignored = ''; |
272
|
9 |
|
$this->_datas .= $chars; |
273
|
9 |
|
$this->_charCount += $this->_charReader->getCharPositions( |
274
|
9 |
|
substr($this->_datas, $this->_datasSize), |
275
|
9 |
|
$this->_datasSize, |
276
|
9 |
|
$this->_map, |
277
|
|
|
$ignored |
278
|
9 |
|
); |
279
|
|
|
|
280
|
9 |
|
if ($ignored !== false) { |
281
|
9 |
|
$this->_datasSize = strlen($this->_datas) - strlen($ignored); |
282
|
9 |
|
} else { |
283
|
|
|
$this->_datasSize = strlen($this->_datas); |
284
|
|
|
} |
285
|
9 |
|
} |
286
|
|
|
} |
287
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.