PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/laradock/vendor/symfony/mime/Header/AbstractHeader.php

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