HeaderName.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_HeaderName
  28. {
  29. /**
  30. * No public constructor.
  31. */
  32. private function __construct()
  33. {
  34. }
  35. /**
  36. * Filter the header name according to RFC 2822
  37. *
  38. * @see http://www.rfc-base.org/txt/rfc-2822.txt (section 2.2)
  39. * @param string $name
  40. * @return string
  41. */
  42. public static function filter($name)
  43. {
  44. $result = '';
  45. $tot = strlen($name);
  46. for ($i = 0; $i < $tot; $i += 1) {
  47. $ord = ord($name[$i]);
  48. if ($ord > 32 && $ord < 127 && $ord !== 58) {
  49. $result .= $name[$i];
  50. }
  51. }
  52. return $result;
  53. }
  54. /**
  55. * Determine if the header name contains any invalid characters.
  56. *
  57. * @param string $name
  58. * @return bool
  59. */
  60. public static function isValid($name)
  61. {
  62. $tot = strlen($name);
  63. for ($i = 0; $i < $tot; $i += 1) {
  64. $ord = ord($name[$i]);
  65. if ($ord < 33 || $ord > 126 || $ord === 58) {
  66. return false;
  67. }
  68. }
  69. return true;
  70. }
  71. /**
  72. * Assert that the header name is valid.
  73. *
  74. * Raises an exception if invalid.
  75. *
  76. * @param string $name
  77. * @throws Exception\RuntimeException
  78. * @return void
  79. */
  80. public static function assertValid($name)
  81. {
  82. if (! self::isValid($name)) {
  83. #require_once 'Zend/Mail/Exception.php';
  84. throw new Zend_Mail_Exception('Invalid header name detected');
  85. }
  86. }
  87. }