AddressStructure.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * AddressStructure.class.php
  4. *
  5. * This file contains functions needed to extract email address headers from
  6. * mime messages.
  7. *
  8. * @copyright 2003-2025 The SquirrelMail Project Team
  9. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10. * @version $Id$
  11. * @package squirrelmail
  12. * @subpackage mime
  13. * @since 1.3.2
  14. */
  15. /**
  16. * Class used to work with email address headers
  17. * @package squirrelmail
  18. * @subpackage mime
  19. * @since 1.3.2
  20. */
  21. class AddressStructure {
  22. /**
  23. * Personal information
  24. * @var string
  25. */
  26. var $personal = '';
  27. /**
  28. * @todo check use of this variable. var is not used in class.
  29. * @var string
  30. */
  31. var $adl = '';
  32. /**
  33. * Mailbox name.
  34. * @var string
  35. */
  36. var $mailbox = '';
  37. /**
  38. * Server address.
  39. * @var string
  40. */
  41. var $host = '';
  42. /**
  43. * @todo check use of this variable. var is not used in class.
  44. * @var string
  45. */
  46. var $group = '';
  47. /**
  48. * Return address information from mime headers.
  49. * @param boolean $full return full address (true) or only personal if it exists, otherwise email (false)
  50. * @param boolean $encoded (since 1.4.0) return rfc2047 encoded address (true) or plain text (false).
  51. * @param boolean $unconditionally_quote (since 1.4.21/1.5.2) when TRUE, always quote the personal part, whether or not it is encoded, otherwise quoting is only added if the personal part is not encoded
  52. *
  53. * @return string
  54. */
  55. function getAddress($full = true, $encoded = false, $unconditionally_quote = FALSE) {
  56. $result = '';
  57. if (is_object($this)) {
  58. $email = ($this->host ? $this->mailbox.'@'.$this->host
  59. : $this->mailbox);
  60. $personal = trim($this->personal);
  61. $is_encoded = false;
  62. if (preg_match('/(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)(.*)/i',$personal,$reg)) {
  63. $is_encoded = true;
  64. }
  65. if ($personal) {
  66. if ($encoded && !$is_encoded) {
  67. $personal_encoded = encodeHeader('"' . $personal . '"');
  68. if ($personal !== $personal_encoded) {
  69. $personal = $personal_encoded;
  70. } else {
  71. //FIXME: this probably adds quotes around an encoded string which itself is already quoted
  72. $personal = '"' . $this->personal . '"';
  73. }
  74. } else {
  75. if (!$is_encoded || $unconditionally_quote) {
  76. $personal = '"' . $this->personal . '"';
  77. }
  78. }
  79. $addr = ($email ? $personal . ' <' .$email.'>'
  80. : $this->personal);
  81. $best_dpl = $this->personal;
  82. } else {
  83. $addr = $email;
  84. $best_dpl = $email;
  85. }
  86. $result = ($full ? $addr : $best_dpl);
  87. }
  88. return $result;
  89. }
  90. /**
  91. * Shorter version of getAddress() function
  92. * Returns full encoded address.
  93. * @param boolean $unconditionally_quote (since 1.4.21/1.5.2) when TRUE, always quote the personal part, whether or not it is encoded, otherwise quoting is only added if the personal part is not encoded
  94. *
  95. * @return string
  96. * @since 1.4.0
  97. */
  98. function getEncodedAddress($unconditionally_quote=FALSE) {
  99. return $this->getAddress(true, true, $unconditionally_quote);
  100. }
  101. /**
  102. * Return just the email portion of this address
  103. * @return string
  104. * @since 1.5.2
  105. */
  106. function getEmail () {
  107. $r = '';
  108. if (is_object($this)) {
  109. $r = $this->host ? $this->mailbox.'@'.$this->host : $this->mailbox;
  110. }
  111. return $r;
  112. }
  113. }