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