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