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
|
137 |
|
public function __construct(Swift_CharacterReaderFactory $factory, $charset) |
88
|
|
|
{ |
89
|
137 |
|
$this->setCharacterReaderFactory($factory); |
90
|
137 |
|
$this->setCharacterSet($charset); |
91
|
137 |
|
} |
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
|
137 |
|
public function setCharacterSet($charset) |
101
|
|
|
{ |
102
|
137 |
|
$this->_charset = $charset; |
103
|
137 |
|
$this->_charReader = null; |
104
|
137 |
|
$this->_mapType = 0; |
105
|
137 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set the CharacterReaderFactory for multi charset support. |
109
|
|
|
* |
110
|
|
|
* @param Swift_CharacterReaderFactory $factory |
111
|
|
|
*/ |
112
|
137 |
|
public function setCharacterReaderFactory(Swift_CharacterReaderFactory $factory) |
113
|
|
|
{ |
114
|
137 |
|
$this->_charReaderFactory = $factory; |
115
|
137 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @see Swift_CharacterStream::flushContents() |
119
|
|
|
*/ |
120
|
54 |
|
public function flushContents() |
121
|
|
|
{ |
122
|
54 |
|
$this->_datas = null; |
123
|
54 |
|
$this->_map = null; |
124
|
54 |
|
$this->_charCount = 0; |
125
|
54 |
|
$this->_currentPos = 0; |
126
|
54 |
|
$this->_datasSize = 0; |
127
|
54 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @see Swift_CharacterStream::importByteStream() |
131
|
|
|
* |
132
|
|
|
* @param Swift_OutputByteStream $os |
133
|
|
|
*/ |
134
|
4 |
View Code Duplication |
public function importByteStream(Swift_OutputByteStream $os) |
|
|
|
|
135
|
|
|
{ |
136
|
4 |
|
$this->flushContents(); |
137
|
4 |
|
$blocks = 512; |
138
|
4 |
|
$os->setReadPointer(0); |
139
|
4 |
|
while (false !== ($read = $os->read($blocks))) { |
140
|
4 |
|
$this->write($read); |
|
|
|
|
141
|
|
|
} |
142
|
4 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @see Swift_CharacterStream::importString() |
146
|
|
|
* |
147
|
|
|
* @param string $string |
148
|
|
|
*/ |
149
|
50 |
|
public function importString($string) |
150
|
|
|
{ |
151
|
50 |
|
$this->flushContents(); |
152
|
50 |
|
$this->write($string); |
153
|
50 |
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @see Swift_CharacterStream::read() |
157
|
|
|
* |
158
|
|
|
* @param int $length |
159
|
|
|
* |
160
|
|
|
* @return string|false false on error |
161
|
|
|
*/ |
162
|
54 |
|
public function read($length) |
163
|
|
|
{ |
164
|
54 |
|
if ($this->_currentPos >= $this->_charCount) { |
165
|
52 |
|
return false; |
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
168
|
54 |
|
$ret = false; |
|
|
|
|
169
|
54 |
|
$length = $this->_currentPos + $length > $this->_charCount ? $this->_charCount - $this->_currentPos : $length; |
170
|
54 |
|
switch ($this->_mapType) { |
171
|
54 |
|
case Swift_CharacterReader::MAP_TYPE_FIXED_LEN: |
172
|
35 |
|
$len = $length * $this->_map; |
173
|
35 |
|
$ret = substr($this->_datas, $this->_currentPos * $this->_map, $len); |
174
|
35 |
|
$this->_currentPos += $length; |
175
|
35 |
|
break; |
176
|
|
|
|
177
|
20 |
|
case Swift_CharacterReader::MAP_TYPE_INVALID: |
178
|
|
|
$ret = ''; |
179
|
|
|
for (; $this->_currentPos < $length; ++$this->_currentPos) { |
180
|
|
|
if (isset($this->_map[$this->_currentPos])) { |
181
|
|
|
$ret .= '?'; |
182
|
|
|
} else { |
183
|
|
|
$ret .= $this->_datas[$this->_currentPos]; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
break; |
187
|
|
|
|
188
|
20 |
|
case Swift_CharacterReader::MAP_TYPE_POSITIONS: |
189
|
20 |
|
$end = $this->_currentPos + $length; |
190
|
20 |
|
$end = $end > $this->_charCount ? $this->_charCount : $end; |
191
|
20 |
|
$ret = ''; |
192
|
|
|
|
193
|
20 |
|
$start = 0; |
194
|
20 |
|
if ($this->_currentPos > 0) { |
195
|
2 |
|
$start = $this->_map['p'][$this->_currentPos - 1]; |
196
|
|
|
} |
197
|
|
|
|
198
|
20 |
|
$to = $start; |
199
|
20 |
|
for (; $this->_currentPos < $end; ++$this->_currentPos) { |
200
|
20 |
|
if (isset($this->_map['i'][$this->_currentPos])) { |
201
|
|
|
$ret .= substr($this->_datas, $start, $to - $start) . '?'; |
202
|
|
|
$start = $this->_map['p'][$this->_currentPos]; |
203
|
|
|
} else { |
204
|
20 |
|
$to = $this->_map['p'][$this->_currentPos]; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
20 |
|
$ret .= substr($this->_datas, $start, $to - $start); |
209
|
20 |
|
break; |
210
|
|
|
} |
211
|
|
|
|
212
|
54 |
|
return $ret; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @see Swift_CharacterStream::readBytes() |
217
|
|
|
* |
218
|
|
|
* @param int $length |
219
|
|
|
* |
220
|
|
|
* @return integer[]|false false on error |
221
|
|
|
*/ |
222
|
54 |
View Code Duplication |
public function readBytes($length) |
|
|
|
|
223
|
|
|
{ |
224
|
54 |
|
$read = $this->read($length); |
225
|
54 |
|
if ($read !== false) { |
226
|
54 |
|
$ret = array_map('ord', str_split($read, 1)); |
227
|
|
|
|
228
|
54 |
|
return $ret; |
229
|
|
|
} |
230
|
|
|
|
231
|
54 |
|
return false; |
|
|
|
|
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @see Swift_CharacterStream::setPointer() |
236
|
|
|
* |
237
|
|
|
* @param int $charOffset |
238
|
|
|
*/ |
239
|
|
|
public function setPointer($charOffset) |
240
|
|
|
{ |
241
|
|
|
if ($this->_charCount < $charOffset) { |
242
|
|
|
$charOffset = $this->_charCount; |
243
|
|
|
} |
244
|
|
|
$this->_currentPos = $charOffset; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @see Swift_CharacterStream::write() |
249
|
|
|
* |
250
|
|
|
* @param string $chars |
251
|
|
|
*/ |
252
|
54 |
|
public function write($chars) |
253
|
|
|
{ |
254
|
54 |
|
if (!isset($this->_charReader)) { |
255
|
54 |
|
$this->_charReader = $this->_charReaderFactory->getReaderFor($this->_charset); |
256
|
54 |
|
$this->_map = array(); |
257
|
54 |
|
$this->_mapType = $this->_charReader->getMapType(); |
258
|
|
|
} |
259
|
|
|
|
260
|
54 |
|
$ignored = ''; |
261
|
54 |
|
$this->_datas .= $chars; |
262
|
54 |
|
$this->_charCount += $this->_charReader->getCharPositions( |
263
|
54 |
|
substr($this->_datas, $this->_datasSize), $this->_datasSize, $this->_map, $ignored |
264
|
|
|
); |
265
|
|
|
|
266
|
54 |
|
if ($ignored !== false) { |
267
|
54 |
|
$this->_datasSize = strlen($this->_datas) - strlen($ignored); |
268
|
|
|
} else { |
269
|
|
|
$this->_datasSize = strlen($this->_datas); |
270
|
|
|
} |
271
|
54 |
|
} |
272
|
|
|
} |
273
|
|
|
|
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.