HeaderValue.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Mail
  17. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @category Zend
  23. * @package Zend_Mail
  24. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  25. * @license http://framework.zend.com/license/new-bsd New BSD License
  26. */
  27. final class Zend_Mail_Header_HeaderValue
  28. {
  29. /**
  30. * No public constructor.
  31. */
  32. private function __construct()
  33. {
  34. }
  35. /**
  36. * Filter the header value according to RFC 2822
  37. *
  38. * @see http://www.rfc-base.org/txt/rfc-2822.txt (section 2.2)
  39. * @param string $value
  40. * @return string
  41. */
  42. public static function filter($value)
  43. {
  44. $result = '';
  45. $tot = strlen($value);
  46. // Filter for CR and LF characters, leaving CRLF + WSP sequences for
  47. // Long Header Fields (section 2.2.3 of RFC 2822)
  48. for ($i = 0; $i < $tot; $i += 1) {
  49. $ord = ord($value[$i]);
  50. if (($ord < 32 || $ord > 126)
  51. && $ord !== 13
  52. ) {
  53. continue;
  54. }
  55. if ($ord === 13) {
  56. if ($i + 2 >= $tot) {
  57. continue;
  58. }
  59. $lf = ord($value[$i + 1]);
  60. $sp = ord($value[$i + 2]);
  61. if ($lf !== 10 || $sp !== 32) {
  62. continue;
  63. }
  64. $result .= "\r\n ";
  65. $i += 2;
  66. continue;
  67. }
  68. $result .= $value[$i];
  69. }
  70. return $result;
  71. }
  72. /**
  73. * Determine if the header value contains any invalid characters.
  74. *
  75. * @see http://www.rfc-base.org/txt/rfc-2822.txt (section 2.2)
  76. * @param string $value
  77. * @return bool
  78. */
  79. public static function isValid($value)
  80. {
  81. $tot = strlen($value);
  82. for ($i = 0; $i < $tot; $i += 1) {
  83. $ord = ord($value[$i]);
  84. if (($ord < 32 || $ord > 126)
  85. && $ord !== 13
  86. ) {
  87. return false;
  88. }
  89. if ($ord === 13) {
  90. if ($i + 2 >= $tot) {
  91. return false;
  92. }
  93. $lf = ord($value[$i + 1]);
  94. $sp = ord($value[$i + 2]);
  95. if ($lf !== 10 || $sp !== 32) {
  96. return false;
  97. }
  98. $i += 2;
  99. }
  100. }
  101. return true;
  102. }
  103. /**
  104. * Assert that the header value is valid.
  105. *
  106. * Raises an exception if invalid.
  107. *
  108. * @param string $value
  109. * @throws Exception\RuntimeException
  110. * @return void
  111. */
  112. public static function assertValid($value)
  113. {
  114. if (! self::isValid($value)) {
  115. #require_once 'Zend/Mail/Exception.php';
  116. throw new Zend_Mail_Exception('Invalid header value detected');
  117. }
  118. }
  119. }