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