/
Mailbox.php
146 lines (131 loc) · 3.74 KB
/
Mailbox.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* Copyright 2011-2014 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Horde
* @copyright 2011-2014 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Imap_Client
*/
/**
* An object that provides a way to switch between UTF7-IMAP and
* human-readable representations of a mailbox name.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2011-2014 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Imap_Client
*
* @property-read string $list_escape Escapes mailbox for use in LIST
* command (UTF-8).
* @property-read string $utf7imap Mailbox in UTF7-IMAP.
* @property-read string $utf8 Mailbox in UTF-8.
*/
class Horde_Imap_Client_Mailbox implements Serializable
{
/**
* UTF7-IMAP representation of mailbox.
* If boolean true, it is identical to UTF-8 representation.
*
* @var mixed
*/
protected $_utf7imap;
/**
* UTF8 representation of mailbox.
*
* @var string
*/
protected $_utf8;
/**
* Shortcut to obtaining mailbox object.
*
* @param string $mbox The mailbox name.
* @param boolean $utf7imap Is mailbox UTF7-IMAP encoded? Otherwise,
* mailbox is assumed to be UTF-8.
*
* @return Horde_Imap_Client_Mailbox A mailbox object.
*/
static public function get($mbox, $utf7imap = false)
{
return ($mbox instanceof Horde_Imap_Client_Mailbox)
? $mbox
: new Horde_Imap_Client_Mailbox($mbox, $utf7imap);
}
/**
* Constructor.
*
* @param string $mbox The mailbox name.
* @param mixed $utf7imap Is mailbox UTF7-IMAP encoded (true). Otherwise,
* mailbox is assumed to be UTF-8 encoded.
*/
public function __construct($mbox, $utf7imap = false)
{
if (strcasecmp($mbox, 'INBOX') === 0) {
$mbox = 'INBOX';
}
if ($utf7imap) {
$this->_utf7imap = $mbox;
} else {
$this->_utf8 = $mbox;
}
}
/**
*/
public function __get($name)
{
switch ($name) {
case 'list_escape':
return preg_replace("/\*+/", '%', $this->utf8);
case 'utf7imap':
if (!isset($this->_utf7imap)) {
$n = Horde_Imap_Client_Utf7imap::Utf8ToUtf7Imap($this->_utf8);
$this->_utf7imap = ($n == $this->_utf8)
? true
: $n;
}
return ($this->_utf7imap === true)
? $this->_utf8
: $this->_utf7imap;
case 'utf8':
if (!isset($this->_utf8)) {
$this->_utf8 = Horde_Imap_Client_Utf7imap::Utf7ImapToUtf8($this->_utf7imap);
if ($this->_utf8 == $this->_utf7imap) {
$this->_utf7imap = true;
}
}
return (string)$this->_utf8;
}
}
/**
*/
public function __toString()
{
return $this->utf8;
}
/**
* Compares this mailbox to another mailbox string.
*
* @return boolean True if the items are equal.
*/
public function equals($mbox)
{
return ($this->utf8 == $mbox);
}
/* Serializable methods. */
/**
*/
public function serialize()
{
return json_encode(array($this->_utf7imap, $this->_utf8));
}
/**
*/
public function unserialize($data)
{
list($this->_utf7imap, $this->_utf8) = json_decode($data, true);
}
}