Open Monograph Press  3.3.0
AbstractHeader.php
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
13 
15 
21 abstract class AbstractHeader implements HeaderInterface
22 {
23  const PHRASE_PATTERN = '(?:(?:(?:(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))*(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))|(?:(?:[ \t]*(?:\r\n))?[ \t])))?[a-zA-Z0-9!#\$%&\'\*\+\-\/=\?\^_`\{\}\|~]+(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(?:(?:[ \t]*(?:\r\n))?[ \t])?\)))*(?:(?:(?:(?:[ \t]*(?:\r\n))?[ \t])?(\((?:(?:(?:[ \t]*(?:\r\n))?[ \t])|(?:(?:[\x01-\x08\x0B\x0C\x0E-\x19\x7F]|[\x21-\x27\x2A-\x5B\x5D-\x7E])|(?:\\[\x00-\x08\x0B\x0C\x0E-\x7F])|(?1)))*(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t])?\)))|(?:(?:[ \t]*(?:\r\n))?[ \t])))?))+?)';
24 
25  private static $encoder;
26 
27  private $name;
28  private $lineLength = 76;
29  private $lang;
30  private $charset = 'utf-8';
31 
32  public function __construct(string $name)
33  {
34  $this->name = $name;
35  }
36 
37  public function setCharset(string $charset)
38  {
39  $this->charset = $charset;
40  }
41 
42  public function getCharset(): ?string
43  {
44  return $this->charset;
45  }
46 
52  public function setLanguage(string $lang)
53  {
54  $this->lang = $lang;
55  }
56 
57  public function getLanguage(): ?string
58  {
59  return $this->lang;
60  }
61 
62  public function getName(): string
63  {
64  return $this->name;
65  }
66 
67  public function setMaxLineLength(int $lineLength)
68  {
69  $this->lineLength = $lineLength;
70  }
71 
72  public function getMaxLineLength(): int
73  {
74  return $this->lineLength;
75  }
76 
77  public function toString(): string
78  {
79  return $this->tokensToString($this->toTokens());
80  }
81 
88  protected function createPhrase(HeaderInterface $header, string $string, string $charset, bool $shorten = false): string
89  {
90  // Treat token as exactly what was given
91  $phraseStr = $string;
92 
93  // If it's not valid
94  if (!preg_match('/^'.self::PHRASE_PATTERN.'$/D', $phraseStr)) {
95  // .. but it is just ascii text, try escaping some characters
96  // and make it a quoted-string
97  if (preg_match('/^[\x00-\x08\x0B\x0C\x0E-\x7F]*$/D', $phraseStr)) {
98  foreach (['\\', '"'] as $char) {
99  $phraseStr = str_replace($char, '\\'.$char, $phraseStr);
100  }
101  $phraseStr = '"'.$phraseStr.'"';
102  } else {
103  // ... otherwise it needs encoding
104  // Determine space remaining on line if first line
105  if ($shorten) {
106  $usedLength = \strlen($header->getName().': ');
107  } else {
108  $usedLength = 0;
109  }
110  $phraseStr = $this->encodeWords($header, $string, $usedLength);
111  }
112  }
113 
114  return $phraseStr;
115  }
116 
120  protected function encodeWords(HeaderInterface $header, string $input, int $usedLength = -1): string
121  {
122  $value = '';
123  $tokens = $this->getEncodableWordTokens($input);
124  foreach ($tokens as $token) {
125  // See RFC 2822, Sect 2.2 (really 2.2 ??)
126  if ($this->tokenNeedsEncoding($token)) {
127  // Don't encode starting WSP
128  $firstChar = substr($token, 0, 1);
129  switch ($firstChar) {
130  case ' ':
131  case "\t":
132  $value .= $firstChar;
133  $token = substr($token, 1);
134  }
135 
136  if (-1 == $usedLength) {
137  $usedLength = \strlen($header->getName().': ') + \strlen($value);
138  }
139  $value .= $this->getTokenAsEncodedWord($token, $usedLength);
140  } else {
141  $value .= $token;
142  }
143  }
144 
145  return $value;
146  }
147 
148  protected function tokenNeedsEncoding(string $token): bool
149  {
150  return (bool) preg_match('~[\x00-\x08\x10-\x19\x7F-\xFF\r\n]~', $token);
151  }
152 
158  protected function getEncodableWordTokens(string $string): array
159  {
160  $tokens = [];
161  $encodedToken = '';
162  // Split at all whitespace boundaries
163  foreach (preg_split('~(?=[\t ])~', $string) as $token) {
164  if ($this->tokenNeedsEncoding($token)) {
165  $encodedToken .= $token;
166  } else {
167  if (\strlen($encodedToken) > 0) {
168  $tokens[] = $encodedToken;
169  $encodedToken = '';
170  }
171  $tokens[] = $token;
172  }
173  }
174  if (\strlen($encodedToken)) {
175  $tokens[] = $encodedToken;
176  }
177 
178  return $tokens;
179  }
180 
184  protected function getTokenAsEncodedWord(string $token, int $firstLineOffset = 0): string
185  {
186  if (null === self::$encoder) {
187  self::$encoder = new QpMimeHeaderEncoder();
188  }
189 
190  // Adjust $firstLineOffset to account for space needed for syntax
191  $charsetDecl = $this->charset;
192  if (null !== $this->lang) {
193  $charsetDecl .= '*'.$this->lang;
194  }
195  $encodingWrapperLength = \strlen('=?'.$charsetDecl.'?'.self::$encoder->getName().'??=');
196 
197  if ($firstLineOffset >= 75) {
198  //Does this logic need to be here?
199  $firstLineOffset = 0;
200  }
201 
202  $encodedTextLines = explode("\r\n",
203  self::$encoder->encodeString($token, $this->charset, $firstLineOffset, 75 - $encodingWrapperLength)
204  );
205 
206  if ('iso-2022-jp' !== strtolower($this->charset)) {
207  // special encoding for iso-2022-jp using mb_encode_mimeheader
208  foreach ($encodedTextLines as $lineNum => $line) {
209  $encodedTextLines[$lineNum] = '=?'.$charsetDecl.'?'.self::$encoder->getName().'?'.$line.'?=';
210  }
211  }
212 
213  return implode("\r\n ", $encodedTextLines);
214  }
215 
221  protected function generateTokenLines(string $token): array
222  {
223  return preg_split('~(\r\n)~', $token, -1, PREG_SPLIT_DELIM_CAPTURE);
224  }
225 
229  protected function toTokens(string $string = null): array
230  {
231  if (null === $string) {
232  $string = $this->getBodyAsString();
233  }
234 
235  $tokens = [];
236  // Generate atoms; split at all invisible boundaries followed by WSP
237  foreach (preg_split('~(?=[ \t])~', $string) as $token) {
238  $newTokens = $this->generateTokenLines($token);
239  foreach ($newTokens as $newToken) {
240  $tokens[] = $newToken;
241  }
242  }
243 
244  return $tokens;
245  }
246 
253  private function tokensToString(array $tokens): string
254  {
255  $lineCount = 0;
256  $headerLines = [];
257  $headerLines[] = $this->name.': ';
258  $currentLine = &$headerLines[$lineCount++];
259 
260  // Build all tokens back into compliant header
261  foreach ($tokens as $i => $token) {
262  // Line longer than specified maximum or token was just a new line
263  if (("\r\n" === $token) ||
264  ($i > 0 && \strlen($currentLine.$token) > $this->lineLength)
265  && 0 < \strlen($currentLine)) {
266  $headerLines[] = '';
267  $currentLine = &$headerLines[$lineCount++];
268  }
269 
270  // Append token to the line
271  if ("\r\n" !== $token) {
272  $currentLine .= $token;
273  }
274  }
275 
276  // Implode with FWS (RFC 2822, 2.2.3)
277  return implode("\r\n", $headerLines);
278  }
279 }
Symfony\Component\Mime\Header\HeaderInterface
Definition: vendor/symfony/mime/Header/HeaderInterface.php:19
Symfony\Component\Mime\Header\AbstractHeader\setMaxLineLength
setMaxLineLength(int $lineLength)
Definition: AbstractHeader.php:67
Symfony\Component\Mime\Header\AbstractHeader\setLanguage
setLanguage(string $lang)
Definition: AbstractHeader.php:52
Symfony\Component\Mime\Header\AbstractHeader\toString
toString()
Definition: AbstractHeader.php:77
Symfony\Component\Mime\Header\AbstractHeader\tokenNeedsEncoding
tokenNeedsEncoding(string $token)
Definition: AbstractHeader.php:148
Symfony\Component\Mime\Encoder\QpMimeHeaderEncoder
Definition: QpMimeHeaderEncoder.php:17
Symfony\Component\Mime\Header\AbstractHeader\getName
getName()
Definition: AbstractHeader.php:62
Symfony\Component\Mime\Header\AbstractHeader\generateTokenLines
generateTokenLines(string $token)
Definition: AbstractHeader.php:221
Symfony\Component\Mime\Header\AbstractHeader\PHRASE_PATTERN
const PHRASE_PATTERN
Definition: AbstractHeader.php:23
Symfony\Component\Mime\Header\AbstractHeader\getLanguage
getLanguage()
Definition: AbstractHeader.php:57
Symfony\Component\Mime\Header\AbstractHeader\toTokens
toTokens(string $string=null)
Definition: AbstractHeader.php:229
Symfony\Component\Mime\Header\HeaderInterface\getBodyAsString
getBodyAsString()
Symfony\Component\Mime\Header\AbstractHeader\encodeWords
encodeWords(HeaderInterface $header, string $input, int $usedLength=-1)
Definition: AbstractHeader.php:120
Symfony\Component\Mime\Header\AbstractHeader\__construct
__construct(string $name)
Definition: AbstractHeader.php:32
Symfony\Component\Mime\Header\AbstractHeader\getEncodableWordTokens
getEncodableWordTokens(string $string)
Definition: AbstractHeader.php:158
Symfony\Component\Mime\Header
Definition: AbstractHeader.php:12
Symfony\Component\Mime\Header\AbstractHeader\getMaxLineLength
getMaxLineLength()
Definition: AbstractHeader.php:72
Symfony\Component\Mime\Header\AbstractHeader\getCharset
getCharset()
Definition: AbstractHeader.php:42
Symfony\Component\Mime\Header\AbstractHeader
Definition: AbstractHeader.php:21
Symfony\Component\Mime\Header\AbstractHeader\createPhrase
createPhrase(HeaderInterface $header, string $string, string $charset, bool $shorten=false)
Definition: AbstractHeader.php:88
Symfony\Component\Mime\Header\AbstractHeader\setCharset
setCharset(string $charset)
Definition: AbstractHeader.php:37
Symfony\Component\Mime\Header\AbstractHeader\getTokenAsEncodedWord
getTokenAsEncodedWord(string $token, int $firstLineOffset=0)
Definition: AbstractHeader.php:184
Symfony\Component\Mime\Header\HeaderInterface\getName
getName()