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