PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 11.0.2
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v11.0.2
11.1.1 11.1.0 11.0.5 11.0.4 11.0.3 11.0.2 11.0.1 11.0.0 10.11.1 10.11.0 10.10.2 10.10.1 10.10.0 10.9.1 trunk 10.0.0 10.0.1 10.1.0 10.1.1 10.1.2 10.1.3 10.1.4 10.2.0 10.2.1 10.2.2 All 62 releases
acymailing / back / Libraries / Imap2 / Connection.php

Connection.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress 11.0.2, at back/Libraries/Imap2/Connection.php

301 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the PHP IMAP2 package.
5 *
6 * (c) Francesco Bianco <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace AcyMailing\Libraries\Imap2;
13
14 use AcyMailing\Libraries\Imap2\Roundcube\ImapClient;
15
16 class Connection
17 {
18 const DEFAULT_MAILBOX = 'INBOX';
19 const DEFAULT_MAILBOX_GMAIL = '[Gmail]/All Mail';
20 const DEFAULT_OUTLOOK = 'Inbox';
21 const GMAIL_TRASH_MAILBOX = '[Gmail]/Trash';
22
23 protected $mailbox;
24 protected $user;
25 protected $password;
26 protected $flags;
27 protected $retries;
28 protected $options;
29 protected $client;
30 protected $host;
31 protected $port;
32 protected $sslMode;
33 protected $currentMailbox;
34 protected $connected;
35 protected $registry;
36
37 /**
38 *
39 */
40 public function __construct($mailbox, $user, $password, $flags = 0, $retries = 0, $options = [])
41 {
42 $this->user = $user;
43 $this->password = $password;
44 $this->flags = $flags;
45 $this->retries = $retries;
46 $this->options = $options;
47
48 $this->openMailbox($mailbox);
49
50 $this->client = new ImapClient();
51 }
52
53 public function openMailbox($mailbox)
54 {
55 $this->mailbox = $mailbox;
56
57 $mailboxParts = Functions::parseMailboxString($mailbox);
58
59 $this->host = Functions::getHostFromMailbox($mailboxParts);
60 $this->port = @$mailboxParts['port'];
61 $this->sslMode = Functions::getSslModeFromMailbox($mailboxParts);
62 $this->currentMailbox = $mailboxParts['mailbox'];
63 }
64
65 /**
66 * Open an IMAP stream to a mailbox.
67 *
68 * @param $mailbox
69 * @param $user
70 * @param $password
71 * @param int $flags
72 * @param int $retries
73 * @param array $options
74 *
75 * @return void
76 */
77 public static function open($mailbox, $user, $password, $flags = 0, $retries = 0, $options = [])
78 {
79 $connection = new Connection($mailbox, $user, $password, $flags, $retries, $options);
80
81 $success = $connection->connect();
82
83 if (empty($success)) {
84 Errors::appendErrorCanNotOpen($mailbox, $connection->getLastError());
85
86 trigger_error(Errors::couldNotOpenStream($mailbox, debug_backtrace(), 1), E_USER_WARNING);
87
88 return false;
89 }
90
91 return $connection;
92 }
93
94 public static function reopen($imap, $mailbox, $flags = 0, $retries = 0)
95 {
96 if (!is_a($imap, Connection::class)) {
97 return Errors::invalidImapConnection(debug_backtrace(), 1, null);
98 }
99
100 $imap->openMailbox($mailbox);
101
102 $success = $imap->connect();
103
104 if (empty($success)) {
105 trigger_error('imap2_reopen(): Couldn\'t re-open stream', E_USER_WARNING);
106
107 return false;
108 }
109
110 $imap->selectMailbox();
111
112 return true;
113 }
114
115 public static function ping($imap)
116 {
117 if (!is_a($imap, Connection::class)) {
118 return Errors::invalidImapConnection(debug_backtrace(), 1, null);
119 }
120
121 $client = $imap->getClient();
122 #$client->setDebug(true);
123 $status = $client->status($imap->getMailboxName(), ['UIDNEXT']);
124
125 return isset($status['UIDNEXT']) && $status['UIDNEXT'] > 0;
126 }
127
128 /**
129 *
130 */
131 protected function connect()
132 {
133 $this->connected = false;
134 $client = $this->getClient();
135 #$client->setDebug(true);
136
137 $success = $client->connect($this->host, $this->user, $this->password, [
138 'port' => $this->port,
139 'ssl_mode' => $this->sslMode,
140 'auth_type' => $this->flags & OP_XOAUTH2 ? 'XOAUTH2' : 'CHECK',
141 'timeout' => -1,
142 'force_caps' => false,
143 ]);
144
145 if (empty($success)) {
146 return false;
147 }
148
149 if (empty($this->currentMailbox)) {
150 $mailboxes = $this->client->listMailboxes('', '*');
151
152 if (in_array(self::DEFAULT_MAILBOX_GMAIL, $mailboxes)) {
153 $this->currentMailbox = self::DEFAULT_MAILBOX_GMAIL;
154 $this->mailbox .= self::DEFAULT_MAILBOX_GMAIL;
155 } elseif (in_array(self::DEFAULT_OUTLOOK, $mailboxes)) {
156 $this->currentMailbox = self::DEFAULT_OUTLOOK;
157 $this->mailbox .= self::DEFAULT_OUTLOOK;
158 } elseif (in_array(self::DEFAULT_MAILBOX, $mailboxes)) {
159 $this->currentMailbox = self::DEFAULT_MAILBOX;
160 $this->mailbox .= self::DEFAULT_MAILBOX;
161 }
162 }
163
164 $this->rewriteMailbox();
165
166 $this->connected = true;
167
168 return $this;
169 }
170
171 /**
172 *
173 */
174 protected function rewriteMailbox($forceMailbox = null)
175 {
176 $mailboxParts = Functions::parseMailboxString($this->mailbox);
177
178 // '{imap.gmail.com:993/imap/notls/ssl/user="[email protected]"}INBOX'
179 $params = [];
180
181 $params[] = 'imap';
182 if ($this->sslMode == 'ssl') {
183 $params[] = 'notls';
184 $params[] = 'ssl';
185 }
186 $params[] = 'user="'.$this->user.'"';
187
188 $mailboxName = $forceMailbox ? $forceMailbox : $mailboxParts['mailbox'];
189
190 $updatedMailbox = '{'.$mailboxParts['host'].':'.$mailboxParts['port'].'/'.implode('/', $params).'}'.$mailboxName;
191
192 $this->mailbox = $updatedMailbox;
193 }
194
195 /**
196 *
197 */
198 public function getClient()
199 {
200 return $this->client;
201 }
202
203 /**
204 *
205 */
206 public function getMailbox()
207 {
208 return $this->mailbox;
209 }
210
211 /**
212 *
213 */
214 public function getMailboxName()
215 {
216 return $this->currentMailbox;
217 }
218
219 /**
220 *
221 */
222 public function getHost()
223 {
224 return $this->host;
225 }
226
227 /**
228 *
229 */
230 public function selectMailbox()
231 {
232 $success = $this->client->select($this->currentMailbox);
233
234 if (empty($success)) {
235 $this->rewriteMailbox('<no_mailbox>');
236 }
237 }
238
239 /**
240 *
241 */
242 public static function close($imap, $flags = 0)
243 {
244 if (!is_a($imap, Connection::class)) {
245 return Errors::invalidImapConnection(debug_backtrace(), 1, false);
246 }
247
248 $client = $imap->getClient();
249 if ($client->close()) {
250 return true;
251 }
252
253 $client->closeConnection();
254
255 return true;
256 }
257
258 public function isConnected()
259 {
260 return boolval($this->connected);
261 }
262
263 public static function isValid($imap)
264 {
265 return is_a($imap, Connection::class) && $imap->isConnected();
266 }
267
268 public function setRegistryValue($space, $item, $key, $value)
269 {
270 if (empty($this->registry)) {
271 $this->registry = [];
272 }
273
274 if (empty($this->registry[$space])) {
275 $this->registry[$space] = [];
276 }
277
278 if (empty($this->registry[$space][$item])) {
279 $this->registry[$space][$item] = [];
280 }
281
282 $this->registry[$space][$item][$key] = $value;
283 }
284
285 public function getRegistryValue($space, $item, $key)
286 {
287 if (isset($this->registry[$space][$item][$key])) {
288 return $this->registry[$space][$item][$key];
289 }
290
291 return false;
292 }
293
294 public function getLastError()
295 {
296 $client = $this->getClient();
297
298 return $client->error;
299 }
300 }
301