1: <?php
2: /**
3: * Copyright 2013-2014 Horde LLC (http://www.horde.org/)
4: *
5: * See the enclosed file COPYING for license information (GPL). If you
6: * did not receive this file, see http://www.horde.org/licenses/gpl.
7: *
8: * @category Horde
9: * @copyright 2013-2014 Horde LLC
10: * @license http://www.fsf.org/copyleft/gpl.html GPL
11: * @package IMP
12: */
13:
14: /**
15: * Object representation of a remote mail account.
16: *
17: * @author Michael Slusarz <slusarz@horde.org>
18: * @category Horde
19: * @copyright 2013-2014 Horde LLC
20: * @license http://www.horde.org/licenses/gpl GPL
21: * @package IMP
22: *
23: * @property string $hostspec Remote host.
24: * @property-read string $id Remote account storage ID.
25: * @property-read IMP_Imap_Remote $imp_imap IMP IMAP object.
26: * @property string $label Remote account label.
27: * @property integer $port Remote server port.
28: * @property mixed $secure See backends.php ('secure' parameter).
29: * @property integer $type The connection type (self::IMAP or self::POP3).
30: * @property string $username Remote username.
31: */
32: class IMP_Remote_Account implements Serializable
33: {
34: /* Constants used for the 'type' property. */
35: const IMAP = 1;
36: const POP3 = 2;
37:
38: /**
39: * Configuration.
40: *
41: * @var array
42: */
43: protected $_config = array();
44:
45: /**
46: */
47: public function __construct()
48: {
49: $this->_config['id'] = strval(new Horde_Support_Randomid());
50: }
51:
52: /**
53: * String representation of object.
54: *
55: * @return string The identifier (mailbox) ID.
56: */
57: public function __toString()
58: {
59: return IMP_Remote::MBOX_PREFIX . $this->_config['id'];
60: }
61:
62: /**
63: */
64: public function __get($name)
65: {
66: if (isset($this->_config[$name])) {
67: return $this->_config[$name];
68: }
69:
70: switch ($name) {
71: case 'hostspec':
72: return 'localhost';
73:
74: case 'imp_imap':
75: return $GLOBALS['injector']->getInstance('IMP_Factory_Imap')->create(strval($this));
76:
77: case 'label':
78: return $this->hostspec;
79:
80: case 'port':
81: return ($this->type == self::POP3) ? 110 : 143;
82:
83: case 'secure':
84: return null;
85:
86: case 'type':
87: return self::IMAP;
88:
89: case 'username':
90: return '';
91: }
92: }
93:
94: /**
95: */
96: public function __set($name, $value)
97: {
98: switch ($name) {
99: case 'hostspec':
100: case 'label':
101: case 'username':
102: $this->_config[$name] = strval($value);
103: break;
104:
105: case 'port':
106: case 'type':
107: $this->_config[$name] = intval($value);
108: break;
109:
110: case 'secure':
111: $this->_config[$name] = $value;
112: break;
113: }
114: }
115:
116: /**
117: * Create the IMAP object in the session.
118: *
119: * @param string $password Password.
120: *
121: * @throws IMP_Imap_Exception
122: */
123: public function createImapObject($password)
124: {
125: $this->imp_imap->createImapObject(array(
126: 'hostspec' => $this->hostspec,
127: 'password' => new IMP_Imap_Password($password),
128: 'port' => $this->port,
129: 'secure' => $this->secure,
130: 'username' => $this->username,
131: ), $this->type == self::IMAP, strval($this));
132: }
133:
134: /**
135: * Return mailbox name.
136: *
137: * @param string $id Base IMAP name.
138: *
139: * @return string IMP mailbox name.
140: */
141: public function mailbox($id)
142: {
143: return strval($this) . "\0" . $id;
144: }
145:
146: /* Serializable methods. */
147:
148: /**
149: */
150: public function serialize()
151: {
152: return json_encode($this->_config);
153: }
154:
155: /**
156: */
157: public function unserialize($data)
158: {
159: $this->_config = json_decode($data, true);
160: }
161:
162: }
163: