Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractHeader.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Mime\Header;
  11. use Symfony\Component\Mime\Encoder\QpMimeHeaderEncoder;
  12. /**
  13. * An abstract base MIME Header.
  14. *
  15. * @author Chris Corbyn
  16. *
  17. * @experimental in 4.3
  18. */
  19. abstract class AbstractHeader implements HeaderInterface
  20. {
  21. 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))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(?:(?:[ \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])?(\((?:(?:(?:[ \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])))?))+?)';
  22. private static $encoder;
  23. private $name;
  24. private $lineLength = 76;
  25. private $lang;
  26. private $charset = 'utf-8';
  27. public function __construct(string $name)
  28. {
  29. $this->name = $name;
  30. }
  31. public function setCharset(string $charset)
  32. {
  33. $this->charset = $charset;
  34. }
  35. public function getCharset(): ?string
  36. {
  37. return $this->charset;
  38. }
  39. /**
  40. * Set the language used in this Header.
  41. *
  42. * For example, for US English, 'en-us'.
  43. */
  44. public function setLanguage(string $lang)
  45. {
  46. $this->lang = $lang;
  47. }
  48. public function getLanguage(): ?string
  49. {
  50. return $this->lang;
  51. }
  52. public function getName(): string
  53. {
  54. return $this->name;
  55. }
  56. public function setMaxLineLength(int $lineLength)
  57. {
  58. $this->lineLength = $lineLength;
  59. }
  60. public function getMaxLineLength(): int
  61. {
  62. return $this->lineLength;
  63. }
  64. public function toString(): string
  65. {
  66. return $this->tokensToString($this->toTokens());
  67. }
  68. /**
  69. * Produces a compliant, formatted RFC 2822 'phrase' based on the string given.
  70. *
  71. * @param string $string as displayed
  72. * @param bool $shorten the first line to make remove for header name
  73. */
  74. protected function createPhrase(HeaderInterface $header, string $string, string $charset, bool $shorten = false): string
  75. {
  76. // Treat token as exactly what was given
  77. $phraseStr = $string;
  78. // If it's not valid
  79. if (!preg_match('/^'.self::PHRASE_PATTERN.'$/D', $phraseStr)) {
  80. // .. but it is just ascii text, try escaping some characters
  81. // and make it a quoted-string
  82. if (preg_match('/^[\x00-\x08\x0B\x0C\x0E-\x7F]*$/D', $phraseStr)) {
  83. foreach (['\\', '"'] as $char) {
  84. $phraseStr = str_replace($char, '\\'.$char, $phraseStr);
  85. }
  86. $phraseStr = '"'.$phraseStr.'"';
  87. } else {
  88. // ... otherwise it needs encoding
  89. // Determine space remaining on line if first line
  90. if ($shorten) {
  91. $usedLength = \strlen($header->getName().': ');
  92. } else {
  93. $usedLength = 0;
  94. }
  95. $phraseStr = $this->encodeWords($header, $string, $usedLength);
  96. }
  97. }
  98. return $phraseStr;
  99. }
  100. /**
  101. * Encode needed word tokens within a string of input.
  102. */
  103. protected function encodeWords(HeaderInterface $header, string $input, int $usedLength = -1): string
  104. {
  105. $value = '';
  106. $tokens = $this->getEncodableWordTokens($input);
  107. foreach ($tokens as $token) {
  108. // See RFC 2822, Sect 2.2 (really 2.2 ??)
  109. if ($this->tokenNeedsEncoding($token)) {
  110. // Don't encode starting WSP
  111. $firstChar = substr($token, 0, 1);
  112. switch ($firstChar) {
  113. case ' ':
  114. case "\t":
  115. $value .= $firstChar;
  116. $token = substr($token, 1);
  117. }
  118. if (-1 == $usedLength) {
  119. $usedLength = \strlen($header->getName().': ') + \strlen($value);
  120. }
  121. $value .= $this->getTokenAsEncodedWord($token, $usedLength);
  122. } else {
  123. $value .= $token;
  124. }
  125. }
  126. return $value;
  127. }
  128. protected function tokenNeedsEncoding(string $token): bool
  129. {
  130. return (bool) preg_match('~[\x00-\x08\x10-\x19\x7F-\xFF\r\n]~', $token);
  131. }
  132. /**
  133. * Splits a string into tokens in blocks of words which can be encoded quickly.
  134. *
  135. * @return string[]
  136. */
  137. protected function getEncodableWordTokens(string $string): array
  138. {
  139. $tokens = [];
  140. $encodedToken = '';
  141. // Split at all whitespace boundaries
  142. foreach (preg_split('~(?=[\t ])~', $string) as $token) {
  143. if ($this->tokenNeedsEncoding($token)) {
  144. $encodedToken .= $token;
  145. } else {
  146. if (\strlen($encodedToken) > 0) {
  147. $tokens[] = $encodedToken;
  148. $encodedToken = '';
  149. }
  150. $tokens[] = $token;
  151. }
  152. }
  153. if (\strlen($encodedToken)) {
  154. $tokens[] = $encodedToken;
  155. }
  156. return $tokens;
  157. }
  158. /**
  159. * Get a token as an encoded word for safe insertion into headers.
  160. */
  161. protected function getTokenAsEncodedWord(string $token, int $firstLineOffset = 0): string
  162. {
  163. if (null === self::$encoder) {
  164. self::$encoder = new QpMimeHeaderEncoder();
  165. }
  166. // Adjust $firstLineOffset to account for space needed for syntax
  167. $charsetDecl = $this->charset;
  168. if (null !== $this->lang) {
  169. $charsetDecl .= '*'.$this->lang;
  170. }
  171. $encodingWrapperLength = \strlen('=?'.$charsetDecl.'?'.self::$encoder->getName().'??=');
  172. if ($firstLineOffset >= 75) {
  173. //Does this logic need to be here?
  174. $firstLineOffset = 0;
  175. }
  176. $encodedTextLines = explode("\r\n",
  177. self::$encoder->encodeString($token, $this->charset, $firstLineOffset, 75 - $encodingWrapperLength)
  178. );
  179. if ('iso-2022-jp' !== strtolower($this->charset)) {
  180. // special encoding for iso-2022-jp using mb_encode_mimeheader
  181. foreach ($encodedTextLines as $lineNum => $line) {
  182. $encodedTextLines[$lineNum] = '=?'.$charsetDecl.'?'.self::$encoder->getName().'?'.$line.'?=';
  183. }
  184. }
  185. return implode("\r\n ", $encodedTextLines);
  186. }
  187. /**
  188. * Generates tokens from the given string which include CRLF as individual tokens.
  189. *
  190. * @return string[]
  191. */
  192. protected function generateTokenLines(string $token): array
  193. {
  194. return preg_split('~(\r\n)~', $token, -1, PREG_SPLIT_DELIM_CAPTURE);
  195. }
  196. /**
  197. * Generate a list of all tokens in the final header.
  198. */
  199. protected function toTokens(string $string = null): array
  200. {
  201. if (null === $string) {
  202. $string = $this->getBodyAsString();
  203. }
  204. $tokens = [];
  205. // Generate atoms; split at all invisible boundaries followed by WSP
  206. foreach (preg_split('~(?=[ \t])~', $string) as $token) {
  207. $newTokens = $this->generateTokenLines($token);
  208. foreach ($newTokens as $newToken) {
  209. $tokens[] = $newToken;
  210. }
  211. }
  212. return $tokens;
  213. }
  214. /**
  215. * Takes an array of tokens which appear in the header and turns them into
  216. * an RFC 2822 compliant string, adding FWSP where needed.
  217. *
  218. * @param string[] $tokens
  219. */
  220. private function tokensToString(array $tokens): string
  221. {
  222. $lineCount = 0;
  223. $headerLines = [];
  224. $headerLines[] = $this->name.': ';
  225. $currentLine = &$headerLines[$lineCount++];
  226. // Build all tokens back into compliant header
  227. foreach ($tokens as $i => $token) {
  228. // Line longer than specified maximum or token was just a new line
  229. if (("\r\n" === $token) ||
  230. ($i > 0 && \strlen($currentLine.$token) > $this->lineLength)
  231. && 0 < \strlen($currentLine)) {
  232. $headerLines[] = '';
  233. $currentLine = &$headerLines[$lineCount++];
  234. }
  235. // Append token to the line
  236. if ("\r\n" !== $token) {
  237. $currentLine .= $token;
  238. }
  239. }
  240. // Implode with FWS (RFC 2822, 2.2.3)
  241. return implode("\r\n", $headerLines);
  242. }
  243. }