extends Horde_Kolab_Storage_Driver_Base
{
/**
+ * IMAP resource.
+ *
+ * @var resource
+ */
+ private $_imap;
+
+ /**
+ * Server name.
+ *
+ * @var string
+ */
+ private $_host;
+
+ /**
+ * Basic IMAP connection string.
+ *
+ * @var string
+ */
+ private $_base_mbox;
+
+ /**
+ * Lazy connect to the IMAP server.
+ *
+ * @return resource The IMAP connection.
+ *
+ * @throws Horde_Kolab_Storage_Exception In case the connection failed.
+ */
+ private function _getImap()
+ {
+ if (!isset($this->_imap)) {
+ $result = @imap_open(
+ $this->_getBaseMbox(),
+ $this->getParam('username'),
+ $this->getParam('password'),
+ OP_HALFOPEN
+ );
+ if (!$result) {
+ throw new Horde_Kolab_Storage_Exception(
+ sprintf(
+ Horde_Kolab_Storage_Translation::t(
+ "Connecting to server %s failed. Error: %s"
+ ),
+ $this->_getHost(),
+ @imap_last_error()
+ )
+ );
+ }
+ $this->_imap = $result;
+ }
+ return $this->_imap;
+ }
+
+ /**
+ * Return the root mailbox of the current user.
+ *
+ * @return string The id of the user that opened the IMAP connection.
+ */
+ private function _getBaseMbox()
+ {
+ if (!isset($this->_base_mbox)) {
+ $this->_base_mbox = '{' . $this->_getHost() . ':143/notls}';
+ }
+ return $this->_base_mbox;
+ }
+
+ /**
* Return the id of the user currently authenticated.
*
* @return string The id of the user that opened the IMAP connection.
*/
public function getAuth()
{
- return $this->_imap->getParam('username');
+ return $this->getParam('username');
+ }
+
+ /**
+ * Return the root mailbox of the current user.
+ *
+ * @return string The id of the user that opened the IMAP connection.
+ */
+ private function _getHost()
+ {
+ if (!isset($this->_host)) {
+ $this->_host = $this->getParam('host');
+ if (empty($this->_host)) {
+ throw new Horde_Kolab_Storage_Exception(
+ Horde_Kolab_Storage_Translation::t(
+ "Missing \"host\" parameter!"
+ )
+ );
+ }
+ }
+ return $this->_host;
}
/**
* Retrieves a list of mailboxes on the server.
*
* @return array The list of mailboxes.
+ *
+ * @throws Horde_Kolab_Storage_Exception In case listing the folders failed.
*/
public function getMailboxes()
{
- return $this->_imap->listMailboxes('*', Horde_Imap_Client::MBOX_ALL, array('flat' => true));
+ $folders = array();
+
+ $result = @imap_list($this->_getImap(), $this->_getBaseMbox(), '*');
+ if (!$result) {
+ throw new Horde_Kolab_Storage_Exception(
+ sprintf(
+ Horde_Kolab_Storage_Translation::t(
+ "Listing folders for %s failed. Error: %s"
+ ),
+ $this->_getBaseMbox(),
+ @imap_last_error()
+ )
+ );
+ }
+
+ $root = $this->_getBaseMbox();
+ $server_len = strlen($root);
+ foreach ($result as $folder) {
+ if (substr($folder, 0, $server_len) == $root) {
+ $folders[] = substr($folder, $server_len);
+ }
+ }
+
+ return $folders;
}
/**
--- /dev/null
+<?php
+/**
+ * A Roundcube Imap based Kolab storage driver.
+ *
+ * PHP version 5
+ *
+ * @category Kolab
+ * @package Kolab_Storage
+ * @author Gunnar Wrobel <wrobel@pardus.de>
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=Kolab_Storage
+ */
+
+/**
+ * A Roundcube Imap based Kolab storage driver.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Kolab
+ * @package Kolab_Storage
+ * @author Gunnar Wrobel <wrobel@pardus.de>
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=Kolab_Storage
+ */
+class Horde_Kolab_Storage_Driver_Rcube
+extends Horde_Kolab_Storage_Driver_Base
+{
+ /**
+ * The IMAP client.
+ *
+ * @var rcube_imap_generic
+ */
+ private $_imap;
+
+ /**
+ * Constructor.
+ *
+ * @param rcube_imap_generic $imap The IMAP connection handler.
+ * @param array $params Connection parameters.
+ */
+ public function __construct(
+ rcube_imap_generic $imap,
+ $params = array()
+ ) {
+ $this->_imap = $imap;
+ parent::__construct($params);
+ }
+
+ /**
+ * Return the id of the user currently authenticated.
+ *
+ * @return string The id of the user that opened the IMAP connection.
+ */
+ public function getAuth()
+ {
+ return $this->getParam('username');
+ }
+
+ /**
+ * Retrieves a list of mailboxes on the server.
+ *
+ * @return array The list of mailboxes.
+ */
+ public function getMailboxes()
+ {
+ return $this->_imap->listMailboxes('', '*');
+ }
+
+ /**
+ * Opens the given folder.
+ *
+ * @param string $folder The folder to open
+ *
+ * @return mixed True in case the folder was opened successfully, a PEAR
+ * error otherwise.
+ */
+ public function select($folder)
+ {
+ $this->_imap->openMailbox($folder, Horde_Imap_Client::OPEN_AUTO);
+ return true;
+ }
+
+ /**
+ * Does the given folder exist?
+ *
+ * @param string $folder The folder to check.
+ *
+ * @return boolean True in case the folder exists, false otherwise.
+ */
+ public function exists($folder)
+ {
+ $folders = $this->getMailboxes();
+ if (in_array($folder, $folders)) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * 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)
+ {
+ return $this->_imap->status($folder,
+ Horde_Imap_Client::STATUS_UIDNEXT
+ | Horde_Imap_Client::STATUS_UIDVALIDITY);
+ }
+
+ /**
+ * 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);
+ $uidsearch = $this->_imap->search($folder, $search_query);
+ $uids = $uidsearch['match'];
+ return $uids;
+ }
+
+ /**
+ * Create the specified folder.
+ *
+ * @param string $folder The folder to create.
+ *
+ * @return mixed True in case the operation was successfull, a
+ * PEAR error otherwise.
+ */
+ public function create($folder)
+ {
+ return $this->_imap->createMailbox($folder);
+ }
+
+ /**
+ * Delete the specified folder.
+ *
+ * @param string $folder The folder to delete.
+ *
+ * @return mixed True in case the operation was successfull, a
+ * PEAR error otherwise.
+ */
+ public function delete($folder)
+ {
+ return $this->_imap->deleteMailbox($folder);
+ }
+
+ /**
+ * Rename the specified folder.
+ *
+ * @param string $old The folder to rename.
+ * @param string $new The new name of the folder.
+ *
+ * @return mixed True in case the operation was successfull, a
+ * PEAR error otherwise.
+ */
+ public function rename($old, $new)
+ {
+ return $this->_imap->renameMailbox($old, $new);
+ }
+
+ /**
+ * Appends a message to the current folder.
+ *
+ * @param string $mailbox The mailbox to append the message(s) to. Either
+ * in UTF7-IMAP or UTF-8.
+ * @param string $msg The message to append.
+ *
+ * @return mixed True or a PEAR error in case of an error.
+ */
+ public function appendMessage($mailbox, $msg)
+ {
+ return $this->_imap->append($mailbox, array(array('data' => $msg)));
+ }
+
+ /**
+ * Deletes messages from the current folder.
+ *
+ * @param integer $uids IMAP message ids.
+ *
+ * @return mixed True or a PEAR error in case of an error.
+ */
+ public function deleteMessages($mailbox, $uids)
+ {
+ if (!is_array($uids)) {
+ $uids = array($uids);
+ }
+ return $this->_imap->store($mailbox, array('add' => array('\\deleted'), 'ids' => $uids));
+ }
+
+ /**
+ * Moves a message to a new folder.
+ *
+ * @param integer $uid IMAP message id.
+ * @param string $new_folder Target folder.
+ *
+ * @return mixed True or a PEAR error in case of an error.
+ */
+ public function moveMessage($old_folder, $uid, $new_folder)
+ {
+ $options = array('ids' => array($uid), 'move' => true);
+ return $this->_imap->copy($old_folder, $new_folder, $options);
+ }
+
+ /**
+ * Expunges messages in the current folder.
+ *
+ * @param string $mailbox The mailbox to append the message(s) to. Either
+ * in UTF7-IMAP or UTF-8.
+ *
+ * @return mixed True or a PEAR error in case of an error.
+ */
+ public function expunge($mailbox)
+ {
+ return $this->_imap->expunge($mailbox);
+ }
+
+ /**
+ * Retrieves the message headers for a given message id.
+ *
+ * @param string $mailbox The mailbox to append the message(s) to. Either
+ * in UTF7-IMAP or UTF-8.
+ * @param int $uid The message id.
+ * @param boolean $peek_for_body Prefetch the body.
+ *
+ * @return mixed The message header or a PEAR error in case of an error.
+ */
+ function getMessageHeader($mailbox, $uid, $peek_for_body = true)
+ {
+ $options = array('ids' => array($uid));
+ $criteria = array(
+ Horde_Imap_Client::FETCH_HEADERTEXT => array(
+ array(
+ )
+ )
+ );
+ $result = $this->_imap->fetch($mailbox, $criteria, $options);
+ return $result[$uid]['headertext'][0];
+ }
+
+ /**
+ * Retrieves the message body for a given message id.
+ *
+ * @param string $mailbox The mailbox to append the message(s) to. Either
+ * in UTF7-IMAP or UTF-8.
+ * @param integet $uid The message id.
+ *
+ * @return mixed The message body or a PEAR error in case of an error.
+ */
+ function getMessageBody($mailbox, $uid)
+ {
+ $options = array('ids' => array($uid));
+ $criteria = array(
+ Horde_Imap_Client::FETCH_BODYTEXT => array(
+ array(
+ )
+ )
+ );
+ $result = $this->_imap->fetch($mailbox, $criteria, $options);
+ return $result[$uid]['bodytext'][0];
+ }
+
+ /**
+ * 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(Horde_Kolab_Storage_Folder $folder)
+ {
+ //@todo: Separate driver class
+ if ($this->_imap->queryCapability('ACL') === true) {
+ if ($folder->getOwner() == $this->getAuth()) {
+ try {
+ return $this->_getAcl($folder->getName());
+ } catch (Exception $e) {
+ return array($this->getAuth() => $this->_getMyAcl($folder->getName()));
+ }
+ } else {
+ $acl = $this->_getMyAcl($folder->getName());
+ if (strpos($acl, 'a')) {
+ try {
+ return $this->_getAcl($folder->getName());
+ } catch (Exception $e) {
+ }
+ }
+ return array($this->getAuth() => $acl);
+ }
+ } else {
+ return array($this->getAuth() => 'lrid');
+ }
+ }
+
+ /**
+ * Retrieve the access rights for a folder.
+ *
+ * @param string $folder The folder to retrieve the ACL for.
+ *
+ * @return An array of rights.
+ */
+ private function _getAcl($folder)
+ {
+ $acl = $this->_imap->getACL($folder);
+ $result = array();
+ foreach ($acl as $user => $rights) {
+ $result[$user] = join('', $rights);
+ }
+ return $result;
+ }
+
+ /**
+ * Retrieve the access rights on a folder for the current user.
+ *
+ * @param string $folder The folder to retrieve the ACL for.
+ *
+ * @return An array of rights.
+ */
+ private function _getMyAcl($folder)
+ {
+ return $this->_imap->getMyACLRights($folder);
+ }
+
+ /**
+ * 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)
+ {
+ //@todo: Separate driver class
+ if ($this->_imap->queryCapability('ACL') === true) {
+ $this->_imap->setACL($folder, $user, array('rights' => $acl));
+ }
+ }
+
+ /**
+ * 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)
+ {
+ //@todo: Separate driver class
+ if ($this->_imap->queryCapability('ACL') === true) {
+ $this->_imap->setACL($folder, $user, array('remove' => true));
+ }
+ }
+
+ /**
+ * Fetches the annotation on a folder.
+ *
+ * @param string $entry The entry to fetch.
+ * @param string $mailbox_name The name of the folder.
+ *
+ * @return mixed The annotation value or a PEAR error in case of an error.
+ */
+ public function getAnnotation($entry, $mailbox_name)
+ {
+ try {
+ $result = $this->_imap->getMetadata($mailbox_name, $entry);
+ } catch (Exception $e) {
+ return '';
+ }
+ return isset($result[$mailbox_name][$entry]) ? $result[$mailbox_name][$entry] : '';
+ }
+
+ /**
+ * Sets the annotation on a folder.
+ *
+ * @param string $entry The entry to set.
+ * @param array $value The values to set
+ * @param string $mailbox_name The name of the folder.
+ *
+ * @return mixed True if successfull, a PEAR error otherwise.
+ */
+ public function setAnnotation($entry, $value, $mailbox_name)
+ {
+ return $this->_imap->setMetadata($mailbox_name,
+ array($entry => $value));
+ }
+
+
+ /**
+ * Retrieve the namespace information for this connection.
+ *
+ * @return Horde_Kolab_Storage_Driver_Namespace The initialized namespace handler.
+ */
+ public function getNamespace()
+ {
+ if ($this->_imap->hasCapability('NAMESPACE') === true) {
+ $namespaces = array();
+ foreach ($this->_imap->getNamespace() as $type => $elements) {
+ foreach ($elements as $namespace) {
+ switch ($type) {
+ case 'personal':
+ $namespace['type'] = 'personal';
+ break;
+ case 'others':
+ $namespace['type'] = 'other';
+ break;
+ case 'shared':
+ $namespace['type'] = 'shared';
+ break;
+ }
+ $namespace['delimiter'] = $namespace['delimter'];
+ $namespaces[] = $namespace;
+ }
+ }
+ return new Horde_Kolab_Storage_Driver_Namespace_Imap(
+ $namespaces,
+ $this->getParam('namespaces', array())
+ );
+ }
+ return parent::getNamespace();
+ }
+}
\ No newline at end of file
<active>yes</active>
</lead>
<date>2010-12-14</date>
- <time>10:46:22</time>
+ <time>18:18:05</time>
<version>
<release>0.4.0</release>
<api>0.1.0</api>
<file name="Mock.php" role="php" />
<file name="Namespace.php" role="php" />
<file name="Pear.php" role="php" />
+ <file name="Rcube.php" role="php" />
</dir> <!-- /lib/Horde/Kolab/Storage/Driver -->
+ <dir name="Exception">
+ <file name="Pear.php" role="php" />
+ </dir> <!-- /lib/Horde/Kolab/Storage/Exception -->
<dir name="Folder">
<dir name="Decorator">
<file name="Base.php" role="php" />
<file name="Exception.php" role="php" />
<file name="Factory.php" role="php" />
<file name="Folder.php" role="php" />
+ <file name="Pear.php" role="php" />
<file name="Translation.php" role="php">
<tasks:replace from="@data_dir@" to="data_dir" type="pear-config" />
</file>
<channel>pear.php.net</channel>
</package>
<package>
+ <name>Exception</name>
+ <channel>pear.horde.org</channel>
+ </package>
+ <package>
<name>HTTP_Request</name>
<channel>pear.php.net</channel>
</package>
<install as="Horde/Kolab/Storage/Exception.php" name="lib/Horde/Kolab/Storage/Exception.php" />
<install as="Horde/Kolab/Storage/Factory.php" name="lib/Horde/Kolab/Storage/Factory.php" />
<install as="Horde/Kolab/Storage/Folder.php" name="lib/Horde/Kolab/Storage/Folder.php" />
+ <install as="Horde/Kolab/Storage/Pear.php" name="lib/Horde/Kolab/Storage/Pear.php" />
<install as="Horde/Kolab/Storage/Translation.php" name="lib/Horde/Kolab/Storage/Translation.php" />
<install as="Horde/Kolab/Storage/Driver/Base.php" name="lib/Horde/Kolab/Storage/Driver/Base.php" />
<install as="Horde/Kolab/Storage/Driver/Cclient.php" name="lib/Horde/Kolab/Storage/Driver/Cclient.php" />
<install as="Horde/Kolab/Storage/Driver/Mock.php" name="lib/Horde/Kolab/Storage/Driver/Mock.php" />
<install as="Horde/Kolab/Storage/Driver/Namespace.php" name="lib/Horde/Kolab/Storage/Driver/Namespace.php" />
<install as="Horde/Kolab/Storage/Driver/Pear.php" name="lib/Horde/Kolab/Storage/Driver/Pear.php" />
+ <install as="Horde/Kolab/Storage/Driver/Rcube.php" name="lib/Horde/Kolab/Storage/Driver/Rcube.php" />
<install as="Horde/Kolab/Storage/Driver/Decorator/Base.php" name="lib/Horde/Kolab/Storage/Driver/Decorator/Base.php" />
<install as="Horde/Kolab/Storage/Driver/Decorator/Log.php" name="lib/Horde/Kolab/Storage/Driver/Decorator/Log.php" />
<install as="Horde/Kolab/Storage/Driver/Namespace/Config.php" name="lib/Horde/Kolab/Storage/Driver/Namespace/Config.php" />
<install as="Horde/Kolab/Storage/Driver/Namespace/Element/Personal.php" name="lib/Horde/Kolab/Storage/Driver/Namespace/Element/Personal.php" />
<install as="Horde/Kolab/Storage/Driver/Namespace/Element/Shared.php" name="lib/Horde/Kolab/Storage/Driver/Namespace/Element/Shared.php" />
<install as="Horde/Kolab/Storage/Driver/Namespace/Element/SharedWithPrefix.php" name="lib/Horde/Kolab/Storage/Driver/Namespace/Element/SharedWithPrefix.php" />
+ <install as="Horde/Kolab/Storage/Exception/Pear.php" name="lib/Horde/Kolab/Storage/Exception/Pear.php" />
<install as="Horde/Kolab/Storage/Folder/Base.php" name="lib/Horde/Kolab/Storage/Folder/Base.php" />
<install as="Horde/Kolab/Storage/Folder/Permission.php" name="lib/Horde/Kolab/Storage/Folder/Permission.php" />
<install as="Horde/Kolab/Storage/Folder/Decorator/Base.php" name="lib/Horde/Kolab/Storage/Folder/Decorator/Base.php" />