| 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 Mailbox |
| 17 |
{ |
| 18 |
public static function check($imap) |
| 19 |
{ |
| 20 |
if (is_a($imap, Connection::class)) { |
| 21 |
$imap->selectMailbox(); |
| 22 |
|
| 23 |
$client = $imap->getClient(); |
| 24 |
$status = $client->status($imap->getMailboxName(), ['MESSAGES', 'RECENT']); |
| 25 |
|
| 26 |
return (object)[ |
| 27 |
'Date' => date('D, j M Y G:i:s').' +0000 (UTC)', |
| 28 |
'Driver' => 'imap', |
| 29 |
'Mailbox' => $imap->getMailbox(), |
| 30 |
'Nmsgs' => intval($status['MESSAGES']), |
| 31 |
'Recent' => intval($status['RECENT']), |
| 32 |
]; |
| 33 |
} elseif (IMAP2_RETROFIT_MODE && is_resource($imap) && get_resource_type($imap) == 'imap') { |
| 34 |
return imap_check($imap); |
| 35 |
} |
| 36 |
|
| 37 |
trigger_error(Errors::invalidImapConnection(debug_backtrace(), 1), E_USER_WARNING); |
| 38 |
|
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
public static function numMsg($imap) |
| 43 |
{ |
| 44 |
if (!is_a($imap, Connection::class)) { |
| 45 |
return Errors::invalidImapConnection(debug_backtrace(), 1, false); |
| 46 |
} |
| 47 |
|
| 48 |
$imap->selectMailbox(); |
| 49 |
$client = $imap->getClient(); |
| 50 |
|
| 51 |
$status = $client->status($imap->getMailboxName(), ['MESSAGES']); |
| 52 |
|
| 53 |
return intval($status['MESSAGES']); |
| 54 |
} |
| 55 |
|
| 56 |
public static function numRecent($imap) |
| 57 |
{ |
| 58 |
if (is_a($imap, Connection::class)) { |
| 59 |
$client = $imap->getClient(); |
| 60 |
$imap->selectMailbox(); |
| 61 |
|
| 62 |
return (object)[ |
| 63 |
'Driver' => 'imap', |
| 64 |
'Mailbox' => $imap->getMailbox(), |
| 65 |
'Nmsgs' => $client->data['EXISTS'], |
| 66 |
'Recent' => $client->data['RECENT'], |
| 67 |
]; |
| 68 |
} |
| 69 |
|
| 70 |
return imap_check($imap); |
| 71 |
} |
| 72 |
|
| 73 |
public static function status($imap, $mailbox, $flags) |
| 74 |
{ |
| 75 |
if (!is_a($imap, Connection::class)) { |
| 76 |
return Errors::invalidImapConnection(debug_backtrace(), 1, false); |
| 77 |
} |
| 78 |
|
| 79 |
$mailboxName = Functions::getMailboxName($mailbox); |
| 80 |
|
| 81 |
$client = $imap->getClient(); |
| 82 |
|
| 83 |
$items = []; |
| 84 |
|
| 85 |
$statusKeys = [ |
| 86 |
'MESSAGES' => 'messages', |
| 87 |
'UNSEEN' => 'unseen', |
| 88 |
'RECENT' => 'recent', |
| 89 |
'UIDNEXT' => 'uidnext', |
| 90 |
'UIDVALIDITY' => 'uidvalidity', |
| 91 |
]; |
| 92 |
|
| 93 |
if ($flags & SA_MESSAGES || $flags & SA_ALL) { |
| 94 |
$items[] = 'MESSAGES'; |
| 95 |
} |
| 96 |
if ($flags & SA_RECENT || $flags & SA_ALL) { |
| 97 |
$items[] = 'RECENT'; |
| 98 |
} |
| 99 |
if ($flags & SA_UNSEEN || $flags & SA_ALL) { |
| 100 |
$items[] = 'UNSEEN'; |
| 101 |
} |
| 102 |
if ($flags & SA_UIDNEXT || $flags & SA_ALL) { |
| 103 |
$items[] = 'UIDNEXT'; |
| 104 |
} |
| 105 |
if ($flags & SA_UIDVALIDITY || $flags & SA_ALL) { |
| 106 |
$items[] = 'UIDVALIDITY'; |
| 107 |
} |
| 108 |
|
| 109 |
$status = $client->status($mailboxName, $items); |
| 110 |
|
| 111 |
if (empty($status)) { |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
$returnStatus = []; |
| 116 |
foreach ($status as $key => $value) { |
| 117 |
$returnStatus[$statusKeys[$key]] = is_numeric($value) ? intval($value) : $value; |
| 118 |
} |
| 119 |
|
| 120 |
return (object)$returnStatus; |
| 121 |
} |
| 122 |
|
| 123 |
public static function mailboxMsgInfo($imap) |
| 124 |
{ |
| 125 |
if (!is_a($imap, Connection::class)) { |
| 126 |
return Errors::invalidImapConnection(debug_backtrace(), 1, false); |
| 127 |
} |
| 128 |
|
| 129 |
$client = $imap->getClient(); |
| 130 |
#$client->setDebug(true); |
| 131 |
|
| 132 |
$imap->selectMailbox(); |
| 133 |
$mailboxName = $imap->getMailboxName(); |
| 134 |
|
| 135 |
$status = $client->status($mailboxName, [ |
| 136 |
'MESSAGES', |
| 137 |
'UNSEEN', |
| 138 |
'RECENT', |
| 139 |
'UIDNEXT', |
| 140 |
'UIDVALIDITY', |
| 141 |
]); |
| 142 |
|
| 143 |
$mailboxInfo = [ |
| 144 |
'Unread' => intval($status['UNSEEN']), |
| 145 |
'Deleted' => 0, |
| 146 |
'Nmsgs' => intval($status['MESSAGES']), |
| 147 |
'Size' => 0, |
| 148 |
'Date' => date('D, j M Y G:i:s').' +0000 (UTC)', |
| 149 |
'Driver' => 'imap', |
| 150 |
'Mailbox' => $imap->getMailbox(), |
| 151 |
'Recent' => intval($status['RECENT']), |
| 152 |
]; |
| 153 |
|
| 154 |
return (object)$mailboxInfo; |
| 155 |
} |
| 156 |
|
| 157 |
public static function list($imap, $reference, $pattern) |
| 158 |
{ |
| 159 |
if (is_a($imap, Connection::class)) { |
| 160 |
$referenceParts = explode('}', $reference); |
| 161 |
$client = $imap->getClient(); |
| 162 |
$return = []; |
| 163 |
$mailboxes = $client->listMailboxes($referenceParts[1], $pattern); |
| 164 |
foreach ($mailboxes as $mailbox) { |
| 165 |
if (in_array('\\Noselect', $client->data['LIST'][$mailbox])) { |
| 166 |
continue; |
| 167 |
} |
| 168 |
$return[] = $referenceParts[0].'}'.$mailbox; |
| 169 |
} |
| 170 |
|
| 171 |
return $return; |
| 172 |
} |
| 173 |
|
| 174 |
return imap_list($imap, $reference, $pattern); |
| 175 |
} |
| 176 |
|
| 177 |
public static function listScan($imap, $reference, $pattern) |
| 178 |
{ |
| 179 |
if (is_a($imap, Connection::class)) { |
| 180 |
$referenceParts = explode('}', $reference); |
| 181 |
$client = $imap->getClient(); |
| 182 |
$return = []; |
| 183 |
$mailboxes = $client->listMailboxes($referenceParts[1], $pattern); |
| 184 |
foreach ($mailboxes as $mailbox) { |
| 185 |
if (in_array('\\Noselect', $client->data['LIST'][$mailbox])) { |
| 186 |
continue; |
| 187 |
} |
| 188 |
$return[] = $referenceParts[0].'}'.$mailbox; |
| 189 |
} |
| 190 |
|
| 191 |
return $return; |
| 192 |
} |
| 193 |
|
| 194 |
return imap_list($imap, $reference, $pattern); |
| 195 |
} |
| 196 |
|
| 197 |
public static function getMailboxes($imap, $reference, $pattern) |
| 198 |
{ |
| 199 |
if (!is_a($imap, Connection::class)) { |
| 200 |
return Errors::invalidImapConnection(debug_backtrace(), 1, false); |
| 201 |
} |
| 202 |
|
| 203 |
$referenceParts = explode('}', $reference); |
| 204 |
$client = $imap->getClient(); |
| 205 |
#$client->setDebug(true); |
| 206 |
$return = []; |
| 207 |
$delimiter = $client->getHierarchyDelimiter(); |
| 208 |
$mailboxes = $client->listMailboxes($referenceParts[1], $pattern); |
| 209 |
foreach ($mailboxes as $mailbox) { |
| 210 |
$attributesValue = Functions::getListAttributesValue($client->data['LIST'][$mailbox]); |
| 211 |
if ($mailbox == '[Gmail]' && $imap->getHost() == 'imap.gmail.com') { |
| 212 |
$attributesValue = 34; |
| 213 |
} |
| 214 |
$return[] = (object)[ |
| 215 |
'name' => $referenceParts[0].'}'.$mailbox, |
| 216 |
'attributes' => $attributesValue, |
| 217 |
'delimiter' => $delimiter, |
| 218 |
]; |
| 219 |
} |
| 220 |
|
| 221 |
return $return; |
| 222 |
} |
| 223 |
|
| 224 |
public static function createMailbox($imap, $mailbox) |
| 225 |
{ |
| 226 |
if (!is_a($imap, Connection::class)) { |
| 227 |
return Errors::invalidImapConnection(debug_backtrace(), 1, false); |
| 228 |
} |
| 229 |
|
| 230 |
$client = $imap->getClient(); |
| 231 |
|
| 232 |
if ($mailbox[0] == '{') { |
| 233 |
$mailbox = (string)\preg_replace('/^{.+}/', '', $mailbox); |
| 234 |
} |
| 235 |
|
| 236 |
$success = $client->createFolder($mailbox); |
| 237 |
|
| 238 |
if (!$success) { |
| 239 |
Errors::appendError($client->getRawLastLine()); |
| 240 |
} |
| 241 |
|
| 242 |
return $success; |
| 243 |
} |
| 244 |
|
| 245 |
public static function deleteMailbox($imap, $mailbox) |
| 246 |
{ |
| 247 |
if (!is_a($imap, Connection::class)) { |
| 248 |
return Errors::invalidImapConnection(debug_backtrace(), 1, false); |
| 249 |
} |
| 250 |
|
| 251 |
$client = $imap->getClient(); |
| 252 |
|
| 253 |
if ($mailbox[0] == '{') { |
| 254 |
$mailbox = (string)\preg_replace('/^{.+}/', '', $mailbox); |
| 255 |
} |
| 256 |
|
| 257 |
$result = $client->execute('DELETE', [$client->escape($mailbox)], ImapClient::COMMAND_RAW_LASTLINE); |
| 258 |
|
| 259 |
$success = $result[0] == ImapClient::ERROR_OK; |
| 260 |
|
| 261 |
if (!$success && $imap->getRegistryValue('mailbox', $mailbox, 'deleted')) { |
| 262 |
Errors::appendError($result[1]); |
| 263 |
} elseif (!$success) { |
| 264 |
Errors::appendError("Can't delete mailbox {$mailbox}: no such mailbox"); |
| 265 |
} else { |
| 266 |
$imap->setRegistryValue('mailbox', $mailbox, 'deleted', true); |
| 267 |
} |
| 268 |
|
| 269 |
return $success; |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Append a string message to a specified mailbox. |
| 274 |
* |
| 275 |
* @param $imap |
| 276 |
* @param $folder |
| 277 |
* @param $message |
| 278 |
* @param $options |
| 279 |
* @param $internalDate |
| 280 |
* |
| 281 |
* @return bool |
| 282 |
*/ |
| 283 |
public static function append($imap, $folder, $message, $options = null, $internalDate = null) |
| 284 |
{ |
| 285 |
if (!is_a($imap, Connection::class)) { |
| 286 |
return Errors::invalidImapConnection(debug_backtrace(), 1, false); |
| 287 |
} |
| 288 |
|
| 289 |
$folderParts = explode('}', $folder); |
| 290 |
$client = $imap->getClient(); |
| 291 |
|
| 292 |
$mailbox = empty($folderParts[1]) ? 'INBOX' : $folderParts[1]; |
| 293 |
|
| 294 |
$success = $client->append($mailbox, $message); |
| 295 |
|
| 296 |
return boolval($success); |
| 297 |
} |
| 298 |
|
| 299 |
public static function getSubscribed($imap, $mailbox) |
| 300 |
{ |
| 301 |
if (is_a($imap, Connection::class)) { |
| 302 |
$client = $imap->getClient(); |
| 303 |
|
| 304 |
return $client->deleteFolder($mailbox); |
| 305 |
} |
| 306 |
|
| 307 |
return imap_deletemailbox($imap, $mailbox); |
| 308 |
} |
| 309 |
|
| 310 |
public static function listSubscribed($imap, $mailbox) |
| 311 |
{ |
| 312 |
if (is_a($imap, Connection::class)) { |
| 313 |
$client = $imap->getClient(); |
| 314 |
|
| 315 |
return $client->deleteFolder($mailbox); |
| 316 |
} |
| 317 |
|
| 318 |
return imap_deletemailbox($imap, $mailbox); |
| 319 |
} |
| 320 |
|
| 321 |
public static function subscribe($imap, $mailbox) |
| 322 |
{ |
| 323 |
if (is_a($imap, Connection::class)) { |
| 324 |
$client = $imap->getClient(); |
| 325 |
|
| 326 |
return $client->deleteFolder($mailbox); |
| 327 |
} |
| 328 |
|
| 329 |
return imap_deletemailbox($imap, $mailbox); |
| 330 |
} |
| 331 |
|
| 332 |
public static function unsubscribe($imap, $mailbox) |
| 333 |
{ |
| 334 |
if (is_a($imap, Connection::class)) { |
| 335 |
$client = $imap->getClient(); |
| 336 |
|
| 337 |
return $client->deleteFolder($mailbox); |
| 338 |
} |
| 339 |
|
| 340 |
return imap_deletemailbox($imap, $mailbox); |
| 341 |
} |
| 342 |
} |
| 343 |
|