PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/main/php/peer/mail/InternetAddress.class.php

http://github.com/xp-framework/xp-framework
PHP | 144 lines | 66 code | 12 blank | 66 comment | 6 complexity | bf8b43244356b475daec4c613b41c1f0 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /* This class is part of the XP framework
  3. *
  4. * $Id$
  5. */
  6. uses(
  7. 'text.encode.QuotedPrintable',
  8. 'text.encode.Base64',
  9. 'peer.mail.MessagingAddress'
  10. );
  11. /**
  12. * Internet address
  13. *
  14. * @test xp://net.xp_framework.unittest.peer.InternetAddressTest
  15. * @see http://www.remote.org/jochen/mail/info/chars.html
  16. * @see http://www.cs.tut.fi/~jkorpela/rfc/822addr.html
  17. * @see rfc://2822
  18. * @see rfc://2822#3.4.1
  19. * @purpose Represents an Internet address
  20. */
  21. class InternetAddress extends Object implements MessagingAddress {
  22. public
  23. $personal = '',
  24. $localpart = '',
  25. $domain = '';
  26. /**
  27. * Constructor
  28. *
  29. * @param var mail
  30. * @param string personal default ''
  31. */
  32. public function __construct($mail, $personal= '') {
  33. if (is_array($mail)) {
  34. $this->localpart= $mail[0];
  35. $this->domain= $mail[1];
  36. } else {
  37. sscanf($mail, "%[^@]@%[^\r]", $this->localpart, $this->domain);
  38. }
  39. $this->personal= $personal;
  40. }
  41. /**
  42. * Retrieves address
  43. *
  44. * @return string
  45. */
  46. public function getAddress() {
  47. return $this->localpart.'@'.$this->domain;
  48. }
  49. /**
  50. * Retrieve hashcode
  51. *
  52. * @return string
  53. */
  54. public function hashCode() {
  55. return md5($this->localpart.'@'.$this->domain);
  56. }
  57. /**
  58. * Retrieve whether another object is equal to this
  59. *
  60. * @param lang.Object cmp
  61. * @return bool
  62. */
  63. public function equals($cmp) {
  64. return (
  65. $cmp instanceof InternetAddress and
  66. $this->personal.$this->localpart.$this->domain === $cmp->personal.$cmp->localpart.$cmp->domain
  67. );
  68. }
  69. /**
  70. * Create an InternetAddress object from a string
  71. *
  72. * Recognizes:
  73. * <pre>
  74. * Timm Friebe <friebe@example.com>
  75. * friebe@example.com (Timm Friebe)
  76. * "Timm Friebe" <friebe@example.com>
  77. * friebe@example.com
  78. * <friebe@example.com>
  79. * =?iso-8859-1?Q?Timm_Friebe?= <friebe@example.com>
  80. * </pre>
  81. *
  82. * @param string str
  83. * @return peer.mail.InternetAddress address object
  84. * @throws lang.FormatException in case the string could not be parsed into an address
  85. */
  86. public static function fromString($str) {
  87. static $matches= array(
  88. '/^=\?([^\?])+\?([QB])\?([^\?]+)\?= <([^ @]+@[0-9a-z.-]+)>$/i' => 3,
  89. '/^<?([^ @]+@[0-9a-z.-]+)>?$/i' => 0,
  90. '/^"([^"]+)" <([^ @]+@[0-9a-z.-]+)>$/i' => 2,
  91. '/^([^<]+) <([^ @]+@[0-9a-z.-]+)>$/i' => 2,
  92. '/^([^ @]+@[0-9a-z.-]+) \(([^\)]+)\)$/i' => 1,
  93. );
  94. $str= trim(chop($str));
  95. foreach ($matches as $match => $def) {
  96. if (!preg_match($match, $str, $_)) continue;
  97. switch ($def) {
  98. case 0: $mail= $_[1]; $personal= ''; break;
  99. case 1: $mail= $_[1]; $personal= $_[2]; break;
  100. case 2: $mail= $_[2]; $personal= $_[1]; break;
  101. case 3: $mail= $_[4]; switch (strtoupper($_[2])) {
  102. case 'Q': $personal= QuotedPrintable::decode($_[3]); break;
  103. case 'B': $personal= Base64::decode($_[3]); break;
  104. }
  105. break;
  106. }
  107. break;
  108. }
  109. // Was it unparsable?
  110. if (!isset($mail)) throw new FormatException('String "'.$str.'" could not be parsed');
  111. return new InternetAddress($mail, $personal);
  112. }
  113. /**
  114. * Create string representation
  115. *
  116. * Return values:
  117. * <pre>
  118. * - personal specified: =?iso-8859-1?Q?Timm_Friebe?= <friebe@example.com>
  119. * - Empty personal: <friebe@example.com>
  120. * </pre>
  121. *
  122. * @param string charset defaults to XP default encoding
  123. * @return string
  124. */
  125. public function toString($charset= xp::ENCODING) {
  126. return (
  127. empty($this->personal) ? '' :
  128. QuotedPrintable::encode(iconv(xp::ENCODING, $charset, $this->personal), $charset).' '
  129. ).'<'.$this->localpart.'@'.$this->domain.'>';
  130. }
  131. }
  132. ?>