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