PageRenderTime 35ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/core/Associates/SwiftMailer/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/Headers/AbstractHeader.php

https://gitlab.com/fiesta-framework/Mail
PHP | 503 lines | 224 code | 53 blank | 226 comment | 29 complexity | bb24a1a42e979ccd86f4fd24729d5b63 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * An abstract base MIME Header.
  11. *
  12. * @author Chris Corbyn
  13. */
  14. abstract class Swift_Mime_Headers_AbstractHeader implements Swift_Mime_Header
  15. {
  16. /**
  17. * The name of this Header.
  18. *
  19. * @var string
  20. */
  21. private $_name;
  22. /**
  23. * The Grammar used for this Header.
  24. *
  25. * @var Swift_Mime_Grammar
  26. */
  27. private $_grammar;
  28. /**
  29. * The Encoder used to encode this Header.
  30. *
  31. * @var Swift_Encoder
  32. */
  33. private $_encoder;
  34. /**
  35. * The maximum length of a line in the header.
  36. *
  37. * @var int
  38. */
  39. private $_lineLength = 78;
  40. /**
  41. * The language used in this Header.
  42. *
  43. * @var string
  44. */
  45. private $_lang;
  46. /**
  47. * The character set of the text in this Header.
  48. *
  49. * @var string
  50. */
  51. private $_charset = 'utf-8';
  52. /**
  53. * The value of this Header, cached.
  54. *
  55. * @var string
  56. */
  57. private $_cachedValue = null;
  58. /**
  59. * Creates a new Header.
  60. *
  61. * @param Swift_Mime_Grammar $grammar
  62. */
  63. public function __construct(Swift_Mime_Grammar $grammar)
  64. {
  65. $this->setGrammar($grammar);
  66. }
  67. /**
  68. * Set the character set used in this Header.
  69. *
  70. * @param string $charset
  71. */
  72. public function setCharset($charset)
  73. {
  74. $this->clearCachedValueIf($charset != $this->_charset);
  75. $this->_charset = $charset;
  76. if (isset($this->_encoder)) {
  77. $this->_encoder->charsetChanged($charset);
  78. }
  79. }
  80. /**
  81. * Get the character set used in this Header.
  82. *
  83. * @return string
  84. */
  85. public function getCharset()
  86. {
  87. return $this->_charset;
  88. }
  89. /**
  90. * Set the language used in this Header.
  91. *
  92. * For example, for US English, 'en-us'.
  93. * This can be unspecified.
  94. *
  95. * @param string $lang
  96. */
  97. public function setLanguage($lang)
  98. {
  99. $this->clearCachedValueIf($this->_lang != $lang);
  100. $this->_lang = $lang;
  101. }
  102. /**
  103. * Get the language used in this Header.
  104. *
  105. * @return string
  106. */
  107. public function getLanguage()
  108. {
  109. return $this->_lang;
  110. }
  111. /**
  112. * Set the encoder used for encoding the header.
  113. *
  114. * @param Swift_Mime_HeaderEncoder $encoder
  115. */
  116. public function setEncoder(Swift_Mime_HeaderEncoder $encoder)
  117. {
  118. $this->_encoder = $encoder;
  119. $this->setCachedValue(null);
  120. }
  121. /**
  122. * Get the encoder used for encoding this Header.
  123. *
  124. * @return Swift_Mime_HeaderEncoder
  125. */
  126. public function getEncoder()
  127. {
  128. return $this->_encoder;
  129. }
  130. /**
  131. * Set the grammar used for the header.
  132. *
  133. * @param Swift_Mime_Grammar $grammar
  134. */
  135. public function setGrammar(Swift_Mime_Grammar $grammar)
  136. {
  137. $this->_grammar = $grammar;
  138. $this->setCachedValue(null);
  139. }
  140. /**
  141. * Get the grammar used for this Header.
  142. *
  143. * @return Swift_Mime_Grammar
  144. */
  145. public function getGrammar()
  146. {
  147. return $this->_grammar;
  148. }
  149. /**
  150. * Get the name of this header (e.g. charset).
  151. *
  152. * @return string
  153. */
  154. public function getFieldName()
  155. {
  156. return $this->_name;
  157. }
  158. /**
  159. * Set the maximum length of lines in the header (excluding EOL).
  160. *
  161. * @param int $lineLength
  162. */
  163. public function setMaxLineLength($lineLength)
  164. {
  165. $this->clearCachedValueIf($this->_lineLength != $lineLength);
  166. $this->_lineLength = $lineLength;
  167. }
  168. /**
  169. * Get the maximum permitted length of lines in this Header.
  170. *
  171. * @return int
  172. */
  173. public function getMaxLineLength()
  174. {
  175. return $this->_lineLength;
  176. }
  177. /**
  178. * Get this Header rendered as a RFC 2822 compliant string.
  179. *
  180. * @return string
  181. *
  182. * @throws Swift_RfcComplianceException
  183. */
  184. public function toString()
  185. {
  186. return $this->_tokensToString($this->toTokens());
  187. }
  188. /**
  189. * Returns a string representation of this object.
  190. *
  191. * @return string
  192. *
  193. * @see toString()
  194. */
  195. public function __toString()
  196. {
  197. return $this->toString();
  198. }
  199. // -- Points of extension
  200. /**
  201. * Set the name of this Header field.
  202. *
  203. * @param string $name
  204. */
  205. protected function setFieldName($name)
  206. {
  207. $this->_name = $name;
  208. }
  209. /**
  210. * Produces a compliant, formatted RFC 2822 'phrase' based on the string given.
  211. *
  212. * @param Swift_Mime_Header $header
  213. * @param string $string as displayed
  214. * @param string $charset of the text
  215. * @param Swift_Mime_HeaderEncoder $encoder
  216. * @param bool $shorten the first line to make remove for header name
  217. *
  218. * @return string
  219. */
  220. protected function createPhrase(Swift_Mime_Header $header, $string, $charset, Swift_Mime_HeaderEncoder $encoder = null, $shorten = false)
  221. {
  222. // Treat token as exactly what was given
  223. $phraseStr = $string;
  224. // If it's not valid
  225. if (!preg_match('/^'.$this->getGrammar()->getDefinition('phrase').'$/D', $phraseStr)) {
  226. // .. but it is just ascii text, try escaping some characters
  227. // and make it a quoted-string
  228. if (preg_match('/^'.$this->getGrammar()->getDefinition('text').'*$/D', $phraseStr)) {
  229. $phraseStr = $this->getGrammar()->escapeSpecials(
  230. $phraseStr, array('"'), $this->getGrammar()->getSpecials()
  231. );
  232. $phraseStr = '"'.$phraseStr.'"';
  233. } else {
  234. // ... otherwise it needs encoding
  235. // Determine space remaining on line if first line
  236. if ($shorten) {
  237. $usedLength = strlen($header->getFieldName().': ');
  238. } else {
  239. $usedLength = 0;
  240. }
  241. $phraseStr = $this->encodeWords($header, $string, $usedLength);
  242. }
  243. }
  244. return $phraseStr;
  245. }
  246. /**
  247. * Encode needed word tokens within a string of input.
  248. *
  249. * @param Swift_Mime_Header $header
  250. * @param string $input
  251. * @param string $usedLength optional
  252. *
  253. * @return string
  254. */
  255. protected function encodeWords(Swift_Mime_Header $header, $input, $usedLength = -1)
  256. {
  257. $value = '';
  258. $tokens = $this->getEncodableWordTokens($input);
  259. foreach ($tokens as $token) {
  260. // See RFC 2822, Sect 2.2 (really 2.2 ??)
  261. if ($this->tokenNeedsEncoding($token)) {
  262. // Don't encode starting WSP
  263. $firstChar = substr($token, 0, 1);
  264. switch ($firstChar) {
  265. case ' ':
  266. case "\t":
  267. $value .= $firstChar;
  268. $token = substr($token, 1);
  269. }
  270. if (-1 == $usedLength) {
  271. $usedLength = strlen($header->getFieldName().': ') + strlen($value);
  272. }
  273. $value .= $this->getTokenAsEncodedWord($token, $usedLength);
  274. $header->setMaxLineLength(76); // Forcefully override
  275. } else {
  276. $value .= $token;
  277. }
  278. }
  279. return $value;
  280. }
  281. /**
  282. * Test if a token needs to be encoded or not.
  283. *
  284. * @param string $token
  285. *
  286. * @return bool
  287. */
  288. protected function tokenNeedsEncoding($token)
  289. {
  290. return preg_match('~[\x00-\x08\x10-\x19\x7F-\xFF\r\n]~', $token);
  291. }
  292. /**
  293. * Splits a string into tokens in blocks of words which can be encoded quickly.
  294. *
  295. * @param string $string
  296. *
  297. * @return string[]
  298. */
  299. protected function getEncodableWordTokens($string)
  300. {
  301. $tokens = array();
  302. $encodedToken = '';
  303. // Split at all whitespace boundaries
  304. foreach (preg_split('~(?=[\t ])~', $string) as $token) {
  305. if ($this->tokenNeedsEncoding($token)) {
  306. $encodedToken .= $token;
  307. } else {
  308. if (strlen($encodedToken) > 0) {
  309. $tokens[] = $encodedToken;
  310. $encodedToken = '';
  311. }
  312. $tokens[] = $token;
  313. }
  314. }
  315. if (strlen($encodedToken)) {
  316. $tokens[] = $encodedToken;
  317. }
  318. return $tokens;
  319. }
  320. /**
  321. * Get a token as an encoded word for safe insertion into headers.
  322. *
  323. * @param string $token token to encode
  324. * @param int $firstLineOffset optional
  325. *
  326. * @return string
  327. */
  328. protected function getTokenAsEncodedWord($token, $firstLineOffset = 0)
  329. {
  330. // Adjust $firstLineOffset to account for space needed for syntax
  331. $charsetDecl = $this->_charset;
  332. if (isset($this->_lang)) {
  333. $charsetDecl .= '*'.$this->_lang;
  334. }
  335. $encodingWrapperLength = strlen(
  336. '=?'.$charsetDecl.'?'.$this->_encoder->getName().'??='
  337. );
  338. if ($firstLineOffset >= 75) {
  339. //Does this logic need to be here?
  340. $firstLineOffset = 0;
  341. }
  342. $encodedTextLines = explode("\r\n",
  343. $this->_encoder->encodeString(
  344. $token, $firstLineOffset, 75 - $encodingWrapperLength, $this->_charset
  345. )
  346. );
  347. if (strtolower($this->_charset) !== 'iso-2022-jp') {
  348. // special encoding for iso-2022-jp using mb_encode_mimeheader
  349. foreach ($encodedTextLines as $lineNum => $line) {
  350. $encodedTextLines[$lineNum] = '=?'.$charsetDecl.
  351. '?'.$this->_encoder->getName().
  352. '?'.$line.'?=';
  353. }
  354. }
  355. return implode("\r\n ", $encodedTextLines);
  356. }
  357. /**
  358. * Generates tokens from the given string which include CRLF as individual tokens.
  359. *
  360. * @param string $token
  361. *
  362. * @return string[]
  363. */
  364. protected function generateTokenLines($token)
  365. {
  366. return preg_split('~(\r\n)~', $token, -1, PREG_SPLIT_DELIM_CAPTURE);
  367. }
  368. /**
  369. * Set a value into the cache.
  370. *
  371. * @param string $value
  372. */
  373. protected function setCachedValue($value)
  374. {
  375. $this->_cachedValue = $value;
  376. }
  377. /**
  378. * Get the value in the cache.
  379. *
  380. * @return string
  381. */
  382. protected function getCachedValue()
  383. {
  384. return $this->_cachedValue;
  385. }
  386. /**
  387. * Clear the cached value if $condition is met.
  388. *
  389. * @param bool $condition
  390. */
  391. protected function clearCachedValueIf($condition)
  392. {
  393. if ($condition) {
  394. $this->setCachedValue(null);
  395. }
  396. }
  397. /**
  398. * Generate a list of all tokens in the final header.
  399. *
  400. * @param string $string The string to tokenize
  401. *
  402. * @return array An array of tokens as strings
  403. */
  404. protected function toTokens($string = null)
  405. {
  406. if (is_null($string)) {
  407. $string = $this->getFieldBody();
  408. }
  409. $tokens = array();
  410. // Generate atoms; split at all invisible boundaries followed by WSP
  411. foreach (preg_split('~(?=[ \t])~', $string) as $token) {
  412. $newTokens = $this->generateTokenLines($token);
  413. foreach ($newTokens as $newToken) {
  414. $tokens[] = $newToken;
  415. }
  416. }
  417. return $tokens;
  418. }
  419. /**
  420. * Takes an array of tokens which appear in the header and turns them into
  421. * an RFC 2822 compliant string, adding FWSP where needed.
  422. *
  423. * @param string[] $tokens
  424. *
  425. * @return string
  426. */
  427. private function _tokensToString(array $tokens)
  428. {
  429. $lineCount = 0;
  430. $headerLines = array();
  431. $headerLines[] = $this->_name.': ';
  432. $currentLine = & $headerLines[$lineCount++];
  433. // Build all tokens back into compliant header
  434. foreach ($tokens as $i => $token) {
  435. // Line longer than specified maximum or token was just a new line
  436. if (("\r\n" == $token) ||
  437. ($i > 0 && strlen($currentLine.$token) > $this->_lineLength)
  438. && 0 < strlen($currentLine)) {
  439. $headerLines[] = '';
  440. $currentLine = & $headerLines[$lineCount++];
  441. }
  442. // Append token to the line
  443. if ("\r\n" != $token) {
  444. $currentLine .= $token;
  445. }
  446. }
  447. // Implode with FWS (RFC 2822, 2.2.3)
  448. return implode("\r\n", $headerLines)."\r\n";
  449. }
  450. }