/usr/share/php/Horde/Kolab/Storage/Driver/Imap.php is in php-horde-kolab-storage 2.0.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 | <?php
/**
* An IMAP based driver for accessing Kolab storage.
*
* PHP version 5
*
* @category Kolab
* @package Kolab_Storage
* @author Gunnar Wrobel <wrobel@pardus.de>
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @link http://pear.horde.org/index.php?package=Kolab_Storage
*/
/**
* The IMAP driver class for accessing Kolab storage.
*
* Copyright 2009-2013 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Kolab
* @package Kolab_Storage
* @author Gunnar Wrobel <wrobel@pardus.de>
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @link http://pear.horde.org/index.php?package=Kolab_Storage
*/
class Horde_Kolab_Storage_Driver_Imap
extends Horde_Kolab_Storage_Driver_Base
{
/**
* Create the backend driver.
*
* @return mixed The backend driver.
*/
public function createBackend()
{
$config = $this->getParams();
$config['hostspec'] = $config['host'];
unset($config['host']);
if (isset($config['debug']) && $config['debug'] == 'STDOUT') {
$config['debug'] = STDOUT;
}
return new Horde_Imap_Client_Socket($config);
}
/**
* Retrieves a list of folders from the server.
*
* @return array The list of folders.
*/
public function listFolders()
{
try {
return $this->getBackend()->listMailboxes(
'*', Horde_Imap_Client::MBOX_ALL, array('flat' => true));
} catch (Horde_Imap_Client_Exception_ServerResponse $e) {
throw new Horde_Kolab_Storage_Exception($e->details);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_Kolab_Storage_Exception($e);
}
}
/**
* Create the specified folder.
*
* @param string $folder The folder to create.
*
* @return NULL
*/
public function create($folder)
{
try {
return $this->getBackend()->createMailbox($folder);
} catch (Horde_Imap_Client_Exception_ServerResponse $e) {
throw new Horde_Kolab_Storage_Exception($e->details);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_Kolab_Storage_Exception($e);
}
}
/**
* Delete the specified folder.
*
* @param string $folder The folder to delete.
*
* @return NULL
*/
public function delete($folder)
{
try {
$this->getBackend()->deleteMailbox($folder);
} catch (Horde_Imap_Client_Exception_ServerResponse $e) {
throw new Horde_Kolab_Storage_Exception($e->details);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_Kolab_Storage_Exception($e);
}
}
/**
* Rename the specified folder.
*
* @param string $old The folder to rename.
* @param string $new The new name of the folder.
*
* @return NULL
*/
public function rename($old, $new)
{
try {
$this->getBackend()->renameMailbox($old, $new);
} catch (Horde_Imap_Client_Exception_ServerResponse $e) {
throw new Horde_Kolab_Storage_Exception($e->details);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_Kolab_Storage_Exception($e);
}
}
/**
* Does the backend support ACL?
*
* @return boolean True if the backend supports ACLs.
*/
public function hasAclSupport()
{
try {
$this->getBackend()->login();
return $this->getBackend()->queryCapability('ACL');
} catch (Horde_Imap_Client_Exception_ServerResponse $e) {
throw new Horde_Kolab_Storage_Exception($e->details);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_Kolab_Storage_Exception($e);
}
}
/**
* Retrieve the access rights for a folder.
*
* @param Horde_Kolab_Storage_Folder $folder The folder to retrieve the ACL for.
*
* @return An array of rights.
*/
public function getAcl($folder)
{
try {
$acl = $this->getBackend()->getACL($folder);
} catch (Horde_Imap_Client_Exception_ServerResponse $e) {
throw new Horde_Kolab_Storage_Exception($e->details);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_Kolab_Storage_Exception($e);
}
$result = array();
foreach ($acl as $user => $rights) {
$result[$user] = strval($rights);
}
return $result;
}
/**
* Retrieve the access rights the current user has on a folder.
*
* @param string $folder The folder to retrieve the user ACL for.
*
* @return string The user rights.
*/
public function getMyAcl($folder)
{
try {
return strval($this->getBackend()->getMyACLRights($folder));
} catch (Horde_Imap_Client_Exception_ServerResponse $e) {
throw new Horde_Kolab_Storage_Exception($e->details);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_Kolab_Storage_Exception($e);
}
}
/**
* Set the access rights for a folder.
*
* @param string $folder The folder to act upon.
* @param string $user The user to set the ACL for.
* @param string $acl The ACL.
*
* @return NULL
*/
public function setAcl($folder, $user, $acl)
{
try {
$this->getBackend()->setACL($folder, $user, array('rights' => $acl));
} catch (Horde_Imap_Client_Exception_ServerResponse $e) {
throw new Horde_Kolab_Storage_Exception($e->details);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_Kolab_Storage_Exception($e);
}
}
/**
* Delete the access rights for user on a folder.
*
* @param string $folder The folder to act upon.
* @param string $user The user to delete the ACL for
*
* @return NULL
*/
public function deleteAcl($folder, $user)
{
try {
$this->getBackend()->deleteACL($folder, $user);
} catch (Horde_Imap_Client_Exception_ServerResponse $e) {
throw new Horde_Kolab_Storage_Exception($e->details);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_Kolab_Storage_Exception($e);
}
}
/**
* Retrieves the specified annotation for the complete list of folders.
*
* @param string $annotation The name of the annotation to retrieve.
*
* @return array An associative array combining the folder names as key with
* the corresponding annotation value.
*/
public function listAnnotation($annotation)
{
try {
$result = $this->getBackend()->getMetadata('*', $annotation);
} catch (Horde_Imap_Client_Exception_ServerResponse $e) {
throw new Horde_Kolab_Storage_Exception($e->details);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_Kolab_Storage_Exception($e);
}
$data = array();
foreach ($result as $folder => $annotations) {
if (isset($annotations[$annotation])) {
$data[$folder] = $annotations[$annotation];
}
}
return $data;
}
/**
* Fetches the annotation from a folder.
*
* @param string $folder The name of the folder.
* @param string $annotation The annotation to get.
*
* @return string The annotation value.
*/
public function getAnnotation($folder, $annotation)
{
try {
$this->getBackend()->login();
$result = $this->getBackend()->getMetadata($folder, $annotation);
} catch (Exception $e) {
return '';
}
return isset($result[$folder][$annotation]) ? $result[$folder][$annotation] : '';
}
/**
* Sets the annotation on a folder.
*
* @param string $folder The name of the folder.
* @param string $annotation The annotation to set.
* @param array $value The values to set
*
* @return NULL
*/
public function setAnnotation($folder, $annotation, $value)
{
try {
$this->getBackend()->login();
return $this->getBackend()->setMetadata(
$folder, array($annotation => $value)
);
} catch (Horde_Imap_Client_Exception_ServerResponse $e) {
throw new Horde_Kolab_Storage_Exception($e->details);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_Kolab_Storage_Exception($e);
}
}
/**
* Retrieve the namespace information for this connection.
*
* @return Horde_Kolab_Storage_Driver_Namespace The initialized namespace handler.
*/
public function getNamespace()
{
if ($this->_namespace !== null) {
return parent::getNamespace();
}
try {
$this->getBackend()->login();
if ($this->getBackend()->queryCapability('NAMESPACE') === true) {
$c = array();
$configuration = $this->getParam('namespaces', array());
foreach ($this->getBackend()->getNamespaces() as $namespace) {
if (in_array($namespace['name'], array_keys($configuration))) {
$namespace = array_merge($namespace, $configuration[$namespace['name']]);
}
switch ($namespace['type']) {
case Horde_Imap_Client::NS_PERSONAL:
$namespace['type'] = Horde_Kolab_Storage_Folder_Namespace::PERSONAL;
break;
case Horde_Imap_Client::NS_OTHER:
$namespace['type'] = Horde_Kolab_Storage_Folder_Namespace::OTHER;
break;
case Horde_Imap_Client::NS_SHARED:
$namespace['type'] = Horde_Kolab_Storage_Folder_Namespace::SHARED;
break;
}
$c[] = $namespace;
}
$this->_namespace = $this->getFactory()->createNamespace('imap', $this->getAuth(), $c);
}
} catch (Horde_Imap_Client_Exception_ServerResponse $e) {
throw new Horde_Kolab_Storage_Exception($e->details);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_Kolab_Storage_Exception($e);
}
return parent::getNamespace();
}
/**
* Opens the given folder.
*
* @param string $folder The folder to open
*
* @return NULL
*/
public function select($folder, $mode = Horde_Imap_Client::OPEN_AUTO)
{
try {
$this->getBackend()->openMailbox($folder, $mode);
} catch (Horde_Imap_Client_Exception_ServerResponse $e) {
throw new Horde_Kolab_Storage_Exception($e->details);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_Kolab_Storage_Exception($e);
}
}
/**
* Returns the status of the current folder.
*
* @param string $folder Check the status of this folder.
*
* @return array An array that contains 'uidvalidity' and 'uidnext'.
*/
public function status($folder)
{
// @todo: Condstore
try {
return $this->getBackend()->status(
$folder,
Horde_Imap_Client::STATUS_UIDNEXT |
Horde_Imap_Client::STATUS_UIDVALIDITY |
Horde_Imap_Client::STATUS_FORCE_REFRESH
);
} catch (Horde_Imap_Client_Exception_ServerResponse $e) {
throw new Horde_Kolab_Storage_Exception($e->details);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_Kolab_Storage_Exception($e);
}
}
/**
* Returns the message ids of the messages in this folder.
*
* @param string $folder Check the status of this folder.
*
* @return array The message ids.
*/
public function getUids($folder)
{
$search_query = new Horde_Imap_Client_Search_Query();
$search_query->flag('DELETED', false);
try {
$uidsearch = $this->getBackend()->search($folder, $search_query);
} catch (Horde_Imap_Client_Exception_ServerResponse $e) {
throw new Horde_Kolab_Storage_Exception($e->details);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_Kolab_Storage_Exception($e);
}
$uids = $uidsearch['match'];
return $uids->ids;
}
/**
* Retrieves a complete message.
*
* @param string $folder The folder to fetch the messages from.
* @param array $uid The message UID.
*
* @return array The message encapsuled as an array that contains a
* Horde_Mime_Headers and a Horde_Mime_Part object.
*/
public function fetchComplete($folder, $uid)
{
$query = new Horde_Imap_Client_Fetch_Query();
$query->fullText();
try {
$ret = $this->getBackend()->fetch(
$folder,
$query,
array('ids' => new Horde_Imap_Client_Ids($uid))
);
if (!isset($ret[$uid])) {
throw new Horde_Kolab_Storage_Exception(
sprintf(
Horde_Kolab_Storage_Translation::t(
"Failed fetching message %s in folder %s."
), $uid, $folder
)
);
}
$msg = $ret[$uid]->getFullMsg();
} catch (Horde_Imap_Client_Exception_ServerResponse $e) {
throw new Horde_Kolab_Storage_Exception($e->details);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_Kolab_Storage_Exception($e);
}
return array(
Horde_Mime_Headers::parseHeaders($msg),
Horde_Mime_Part::parseMessage($msg)
);
}
/**
* Retrieves the message headers.
*
* @param string $folder The folder to fetch the message from.
* @param array $uid The message UID.
*
* @return Horde_Mime_Headers The message headers.
*/
public function fetchHeaders($folder, $uid)
{
$query = new Horde_Imap_Client_Fetch_Query();
$query->headerText();
try {
$ret = $this->getBackend()->fetch(
$folder,
$query,
array('ids' => new Horde_Imap_Client_Ids($uid))
);
$msg = $ret[$uid]->getHeaderText();
} catch (Horde_Imap_Client_Exception_ServerResponse $e) {
throw new Horde_Kolab_Storage_Exception($e->details);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_Kolab_Storage_Exception($e);
}
return Horde_Mime_Headers::parseHeaders($msg);
}
/**
* Retrieves the messages for the given message ids.
*
* @param string $folder The folder to fetch the messages from.
* @param array $uids The message UIDs.
*
* @return array An array of message structures parsed into Horde_Mime_Part
* instances.
*/
public function fetchStructure($folder, $uids)
{
if (empty($uids)) {
return array();
}
$query = new Horde_Imap_Client_Fetch_Query();
$query->structure();
try {
$ret = $this->getBackend()->fetch(
$folder,
$query,
array('ids' => new Horde_Imap_Client_Ids($uids))
);
$out = array();
foreach ($ret as $key => $result) {
$out[$key]['structure'] = $result->getStructure();
}
} catch (Horde_Imap_Client_Exception_ServerResponse $e) {
throw new Horde_Kolab_Storage_Exception($e->details);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_Kolab_Storage_Exception($e);
}
return $out;
}
/**
* Retrieves a bodypart for the given message ID and mime part ID.
*
* @param string $folder The folder to fetch the messages from.
* @param array $uid The message UID.
* @param array $id The mime part ID.
*
* @return resource|string The body part, as a stream resource or string.
*/
public function fetchBodypart($folder, $uid, $id)
{
$query = new Horde_Imap_Client_Fetch_Query();
$query->bodyPart($id);
try {
$ret = $this->getBackend()->fetch(
$folder,
$query,
array('ids' => new Horde_Imap_Client_Ids($uid))
);
return $ret[$uid]->getBodyPart($id, true);
} catch (Horde_Imap_Client_Exception_ServerResponse $e) {
throw new Horde_Kolab_Storage_Exception($e->details);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_Kolab_Storage_Exception($e);
}
}
/**
* Appends a message to the given folder.
*
* @param string $folder The folder to append the message(s) to.
* @param resource $msg The message to append.
*
* @return mixed True or the UID of the new message in case the backend
* supports UIDPLUS.
*/
public function appendMessage($folder, $msg)
{
try {
$result = $this->getBackend()
->append($folder, array(array('data' => $msg)));
} catch (Horde_Imap_Client_Exception_ServerResponse $e) {
throw new Horde_Kolab_Storage_Exception($e->details);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_Kolab_Storage_Exception($e);
}
return $result->ids[0];
}
/**
* Deletes messages from the specified folder.
*
* @param string $folder The folder to delete messages from.
* @param integer $uids IMAP message ids.
*
* @return NULL
*/
public function deleteMessages($folder, $uids)
{
try {
return $this->getBackend()->store($folder, array(
'add' => array('\\deleted'),
'ids' => new Horde_Imap_Client_Ids($uids)
));
} catch (Horde_Imap_Client_Exception_ServerResponse $e) {
throw new Horde_Kolab_Storage_Exception($e->details);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_Kolab_Storage_Exception($e);
}
}
/**
* Moves a message to a new folder.
*
* @param integer $uid IMAP message id.
* @param string $old_folder Source folder.
* @param string $new_folder Target folder.
*
* @return NULL
*/
public function moveMessage($uid, $old_folder, $new_folder)
{
$options = array('ids' => new Horde_Imap_Client_Ids($uid),
'move' => true);
try {
return $this->getBackend()
->copy($old_folder, $new_folder, $options);
} catch (Horde_Imap_Client_Exception_ServerResponse $e) {
throw new Horde_Kolab_Storage_Exception($e->details);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_Kolab_Storage_Exception($e);
}
}
/**
* Expunges messages in the current folder.
*
* @param string $folder The folder to expunge.
*
* @return NULL
*/
public function expunge($folder)
{
try {
return $this->getBackend()->expunge($folder);
} catch (Horde_Imap_Client_Exception_ServerResponse $e) {
throw new Horde_Kolab_Storage_Exception($e->details);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_Kolab_Storage_Exception($e);
}
}
}
|