Copyright 2004-2012 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.
Author: Michael Slusarz (slusarz@horde.org)
Inheritance: implements Countable, implements IteratorAggregate
Exemplo n.º 1
0
 /**
  * @dataProvider noUpdateOfListObjectProvider
  */
 public function testNoUpdateOfListObject($mboxes, $expected)
 {
     $list_ob = new Horde_Imap_Client_Mailbox_List($mboxes);
     $sorted = $list_ob->sort(array('noupdate' => true));
     $this->assertEquals($expected, array_values($sorted));
     $this->assertEquals($mboxes, array_values(iterator_to_array($list_ob)));
     $list_ob = new Horde_Imap_Client_Mailbox_List($mboxes);
     $sorted = $list_ob->sort();
     $this->assertEquals($expected, array_values($sorted));
     $this->assertEquals($expected, array_values(iterator_to_array($list_ob)));
 }
Exemplo n.º 2
0
 /**
  * Sorts a list of mailboxes.
  *
  * @param array &$mbox             The list of mailboxes to sort.
  * @param IMP_Ftree_Element $base  The base element.
  */
 public function sortList(&$mbox, $base = false)
 {
     if (count($mbox) < 2) {
         return;
     }
     if (!$base || !$base->base_elt && !$base->remote_auth) {
         $list_ob = new Horde_Imap_Client_Mailbox_List($mbox);
         $mbox = $list_ob->sort();
         return;
     }
     $prefix = $base->base_elt ? '' : strval($this->getAccount($base)) . "";
     $basesort = $othersort = array();
     /* INBOX always appears first. */
     $sorted = array($prefix . 'INBOX');
     foreach ($mbox as $key => $val) {
         $ob = $this[$val];
         if ($ob->nonimap) {
             $othersort[$key] = $ob->mbox_ob->label;
         } elseif ($val !== $prefix . 'INBOX') {
             $basesort[$key] = $ob->mbox_ob->label;
         }
     }
     natcasesort($basesort);
     natcasesort($othersort);
     foreach (array_merge(array_keys($basesort), array_keys($othersort)) as $key) {
         $sorted[] = $mbox[$key];
     }
     $mbox = $sorted;
 }
Exemplo n.º 3
0
Arquivo: Base.php Projeto: Gomez/horde
 /**
  * Obtain status information for a mailbox.
  *
  * @param mixed $mailbox  The mailbox(es) to query. Either a
  *                        Horde_Imap_Client_Mailbox object, a string
  *                        (UTF-8), or an array of objects/strings (since
  *                        2.10.0).
  * @param integer $flags  A bitmask of information requested from the
  *                        server. Allowed flags:
  * <pre>
  *   - Horde_Imap_Client::STATUS_MESSAGES
  *     Return key: messages
  *     Return format: (integer) The number of messages in the mailbox.
  *
  *   - Horde_Imap_Client::STATUS_RECENT
  *     Return key: recent
  *     Return format: (integer) The number of messages with the \Recent
  *                    flag set as currently reported in the mailbox
  *
  *   - Horde_Imap_Client::STATUS_RECENT_TOTAL
  *     Return key: recent_total
  *     Return format: (integer) The number of messages with the \Recent
  *                    flag set. This returns the total number of messages
  *                    that have been marked as recent in this mailbox
  *                    since the PHP process began. (since 2.12.0)
  *
  *   - Horde_Imap_Client::STATUS_UIDNEXT
  *     Return key: uidnext
  *     Return format: (integer) The next UID to be assigned in the
  *                    mailbox. Only returned if the server automatically
  *                    provides the data.
  *
  *   - Horde_Imap_Client::STATUS_UIDNEXT_FORCE
  *     Return key: uidnext
  *     Return format: (integer) The next UID to be assigned in the
  *                    mailbox. This option will always determine this
  *                    value, even if the server does not automatically
  *                    provide this data.
  *
  *   - Horde_Imap_Client::STATUS_UIDVALIDITY
  *     Return key: uidvalidity
  *     Return format: (integer) The unique identifier validity of the
  *                    mailbox.
  *
  *   - Horde_Imap_Client::STATUS_UNSEEN
  *     Return key: unseen
  *     Return format: (integer) The number of messages which do not have
  *                    the \Seen flag set.
  *
  *   - Horde_Imap_Client::STATUS_FIRSTUNSEEN
  *     Return key: firstunseen
  *     Return format: (integer) The sequence number of the first unseen
  *                    message in the mailbox.
  *
  *   - Horde_Imap_Client::STATUS_FLAGS
  *     Return key: flags
  *     Return format: (array) The list of defined flags in the mailbox
  *                    (all flags are in lowercase).
  *
  *   - Horde_Imap_Client::STATUS_PERMFLAGS
  *     Return key: permflags
  *     Return format: (array) The list of flags that a client can change
  *                    permanently (all flags are in lowercase).
  *
  *   - Horde_Imap_Client::STATUS_HIGHESTMODSEQ
  *     Return key: highestmodseq
  *     Return format: (integer) If the server supports the CONDSTORE
  *                    IMAP extension, this will be the highest
  *                    mod-sequence value of all messages in the mailbox.
  *                    Else 0 if CONDSTORE not available or the mailbox
  *                    does not support mod-sequences.
  *
  *   - Horde_Imap_Client::STATUS_SYNCMODSEQ
  *     Return key: syncmodseq
  *     Return format: (integer) If caching, and the server supports the
  *                    CONDSTORE IMAP extension, this is the cached
  *                    mod-sequence value of the mailbox when it was opened
  *                    for the first time in this access. Will be null if
  *                    not caching, CONDSTORE not available, or the mailbox
  *                    does not support mod-sequences.
  *
  *   - Horde_Imap_Client::STATUS_SYNCFLAGUIDS
  *     Return key: syncflaguids
  *     Return format: (Horde_Imap_Client_Ids) If caching, the server
  *                    supports the CONDSTORE IMAP extension, and the
  *                    mailbox contained cached data when opened for the
  *                    first time in this access, this is the list of UIDs
  *                    in which flags have changed since STATUS_SYNCMODSEQ.
  *
  *   - Horde_Imap_Client::STATUS_SYNCVANISHED
  *     Return key: syncvanished
  *     Return format: (Horde_Imap_Client_Ids) If caching, the server
  *                    supports the CONDSTORE IMAP extension, and the
  *                    mailbox contained cached data when opened for the
  *                    first time in this access, this is the list of UIDs
  *                    which have been deleted since STATUS_SYNCMODSEQ.
  *
  *   - Horde_Imap_Client::STATUS_UIDNOTSTICKY
  *     Return key: uidnotsticky
  *     Return format: (boolean) If the server supports the UIDPLUS IMAP
  *                    extension, and the queried mailbox does not support
  *                    persistent UIDs, this value will be true. In all
  *                    other cases, this value will be false.
  *
  *   - Horde_Imap_Client::STATUS_FORCE_REFRESH
  *     Normally, the status information will be cached for a given
  *     mailbox. Since most PHP requests are generally less than a second,
  *     this is fine. However, if your script is long running, the status
  *     information may not be up-to-date. Specifying this flag will ensure
  *     that the server is always polled for the current mailbox status
  *     before results are returned. (since 2.14.0)
  *
  *   - Horde_Imap_Client::STATUS_ALL (DEFAULT)
  *     Shortcut to return 'messages', 'recent', 'uidnext', 'uidvalidity',
  *     and 'unseen' values.
  * </ul>
  * @param array $opts     Additional options:
  * <pre>
  *   - sort: (boolean) If true, sort the list of mailboxes? (since 2.10.0)
  *           DEFAULT: Do not sort the list.
  *   - sort_delimiter: (string) If 'sort' is true, this is the delimiter
  *                     used to sort the mailboxes. (since 2.10.0)
  *                     DEFAULT: '.'
  * </pre>
  *
  * @return array  If $mailbox contains multiple mailboxes, an array with
  *                keys being the UTF-8 mailbox name and values as arrays
  *                containing the requested keys (see above).
  *                Otherwise, an array with keys as the requested keys (see
  *                above) and values as the key data.
  *
  * @throws Horde_Imap_Client_Exception
  */
 public function status($mailbox, $flags = Horde_Imap_Client::STATUS_ALL, array $opts = array())
 {
     $opts = array_merge(array('sort' => false, 'sort_delimiter' => '.'), $opts);
     $this->login();
     if (is_array($mailbox)) {
         if (empty($mailbox)) {
             return array();
         }
         $ret_array = true;
     } else {
         $mailbox = array($mailbox);
         $ret_array = false;
     }
     $mlist = array_map(array('Horde_Imap_Client_Mailbox', 'get'), $mailbox);
     $unselected_flags = array('messages' => Horde_Imap_Client::STATUS_MESSAGES, 'recent' => Horde_Imap_Client::STATUS_RECENT, 'uidnext' => Horde_Imap_Client::STATUS_UIDNEXT, 'uidvalidity' => Horde_Imap_Client::STATUS_UIDVALIDITY, 'unseen' => Horde_Imap_Client::STATUS_UNSEEN);
     if (!$this->statuscache) {
         $flags |= Horde_Imap_Client::STATUS_FORCE_REFRESH;
     }
     if ($flags & Horde_Imap_Client::STATUS_ALL) {
         foreach ($unselected_flags as $val) {
             $flags |= $val;
         }
     }
     $master = $ret = array();
     /* Catch flags that are not supported. */
     if ($flags & Horde_Imap_Client::STATUS_HIGHESTMODSEQ && !$this->_capability()->isEnabled('CONDSTORE')) {
         $master['highestmodseq'] = 0;
         $flags &= ~Horde_Imap_Client::STATUS_HIGHESTMODSEQ;
     }
     if ($flags & Horde_Imap_Client::STATUS_UIDNOTSTICKY && !$this->_capability('UIDPLUS')) {
         $master['uidnotsticky'] = false;
         $flags &= ~Horde_Imap_Client::STATUS_UIDNOTSTICKY;
     }
     /* UIDNEXT return options. */
     if ($flags & Horde_Imap_Client::STATUS_UIDNEXT_FORCE) {
         $flags |= Horde_Imap_Client::STATUS_UIDNEXT;
     }
     foreach ($mlist as $val) {
         $name = strval($val);
         $tmp_flags = $flags;
         if ($val->equals($this->_selected)) {
             /* Check if already in mailbox. */
             $opened = true;
             if ($flags & Horde_Imap_Client::STATUS_FORCE_REFRESH) {
                 $this->noop();
             }
         } else {
             /* A list of STATUS options (other than those handled directly
              * below) that require the mailbox to be explicitly opened. */
             $opened = $flags & Horde_Imap_Client::STATUS_FIRSTUNSEEN || $flags & Horde_Imap_Client::STATUS_FLAGS || $flags & Horde_Imap_Client::STATUS_PERMFLAGS || $flags & Horde_Imap_Client::STATUS_UIDNOTSTICKY || strpbrk($name, '*%') !== false;
         }
         $ret[$name] = $master;
         $ptr =& $ret[$name];
         /* STATUS_PERMFLAGS requires a read/write mailbox. */
         if ($flags & Horde_Imap_Client::STATUS_PERMFLAGS) {
             $this->openMailbox($val, Horde_Imap_Client::OPEN_READWRITE);
             $opened = true;
         }
         /* Handle SYNC related return options. These require the mailbox
          * to be opened at least once. */
         if ($flags & Horde_Imap_Client::STATUS_SYNCMODSEQ) {
             $this->openMailbox($val);
             $ptr['syncmodseq'] = $this->_mailboxOb($val)->getStatus(Horde_Imap_Client::STATUS_SYNCMODSEQ);
             $tmp_flags &= ~Horde_Imap_Client::STATUS_SYNCMODSEQ;
             $opened = true;
         }
         if ($flags & Horde_Imap_Client::STATUS_SYNCFLAGUIDS) {
             $this->openMailbox($val);
             $ptr['syncflaguids'] = $this->getIdsOb($this->_mailboxOb($val)->getStatus(Horde_Imap_Client::STATUS_SYNCFLAGUIDS));
             $tmp_flags &= ~Horde_Imap_Client::STATUS_SYNCFLAGUIDS;
             $opened = true;
         }
         if ($flags & Horde_Imap_Client::STATUS_SYNCVANISHED) {
             $this->openMailbox($val);
             $ptr['syncvanished'] = $this->getIdsOb($this->_mailboxOb($val)->getStatus(Horde_Imap_Client::STATUS_SYNCVANISHED));
             $tmp_flags &= ~Horde_Imap_Client::STATUS_SYNCVANISHED;
             $opened = true;
         }
         /* Handle RECENT_TOTAL option. */
         if ($flags & Horde_Imap_Client::STATUS_RECENT_TOTAL) {
             $this->openMailbox($val);
             $ptr['recent_total'] = $this->_mailboxOb($val)->getStatus(Horde_Imap_Client::STATUS_RECENT_TOTAL);
             $tmp_flags &= ~Horde_Imap_Client::STATUS_RECENT_TOTAL;
             $opened = true;
         }
         if ($opened) {
             if ($tmp_flags) {
                 $tmp = $this->_status(array($val), $tmp_flags);
                 $ptr += reset($tmp);
             }
         } else {
             $to_process[] = $val;
         }
     }
     if ($flags && !empty($to_process)) {
         if (count($to_process) > 1 && $this->_capability('LIST-STATUS')) {
             foreach ($this->listMailboxes($to_process, Horde_Imap_Client::MBOX_ALL, array('status' => $flags)) as $key => $val) {
                 if (isset($val['status'])) {
                     $ret[$key] += $val['status'];
                 }
             }
         } else {
             foreach ($this->_status($to_process, $flags) as $key => $val) {
                 $ret[$key] += $val;
             }
         }
     }
     if (!$opts['sort'] || count($ret) === 1) {
         return $ret_array ? $ret : reset($ret);
     }
     $list_ob = new Horde_Imap_Client_Mailbox_List(array_keys($ret));
     $sorted = $list_ob->sort(array('delimiter' => $opts['sort_delimiter']));
     $out = array();
     foreach ($sorted as $val) {
         $out[$val] = $ret[$val];
     }
     return $out;
 }
Exemplo n.º 4
0
 /**
  * Perform a STATUS call on multiple mailboxes at the same time.
  *
  * This method leverages the LIST-EXTENDED and LIST-STATUS extensions on
  * the IMAP server to improve the efficiency of this operation.
  *
  * @param array $mailboxes  The mailboxes to query. Either
  *                          Horde_Imap_Client_Mailbox objects, strings
  *                          (UTF-8), or a combination of the two.
  * @param integer $flags    See status().
  * @param array $opts       Additional options:
  *   - sort: (boolean) If true, sort the list of mailboxes?
  *           DEFAULT: Do not sort the list.
  *   - sort_delimiter: (string) If 'sort' is true, this is the delimiter
  *                     used to sort the mailboxes.
  *                     DEFAULT: '.'
  *
  * @return array  An array with the keys as the mailbox names (UTF-8) and
  *                the values as arrays with the requested keys (from the
  *                mask given in $flags).
  */
 public function statusMultiple($mailboxes, $flags = Horde_Imap_Client::STATUS_ALL, array $opts = array())
 {
     if (empty($mailboxes)) {
         return array();
     }
     $this->login();
     $opts = array_merge(array('sort' => false, 'sort_delimiter' => '.'), $opts);
     $need_status = true;
     $ret = array();
     /* Optimization: If there is one mailbox in list, and we are already
      * in that mailbox, we should just do a straight STATUS call. */
     if ($this->queryCapability('LIST-STATUS') && (count($mailboxes) != 1 || !Horde_Imap_Client_Mailbox::get(reset($mailboxes))->equals($this->_selected))) {
         $status = $to_process = array();
         foreach ($mailboxes as $val) {
             // Force mailboxes containing wildcards to be accessed via
             // STATUS so that wildcards do not return a bunch of mailboxes
             // in the LIST-STATUS response.
             if (strpbrk($val, '*%') === false) {
                 $to_process[] = $val;
             }
             $status[strval($val)] = 1;
         }
         try {
             foreach ($this->listMailboxes($to_process, Horde_Imap_Client::MBOX_ALL, array_merge($opts, array('status' => $flags))) as $val) {
                 if (isset($val['status'])) {
                     $ret[strval($val['mailbox'])] = $val['status'];
                 }
                 unset($status[strval($val['mailbox'])]);
             }
         } catch (Horde_Imap_Client_Exception $e) {
         }
         if (empty($status)) {
             $need_status = false;
         } else {
             $mailboxes = array_keys($status);
         }
     }
     if ($need_status) {
         foreach ($mailboxes as $val) {
             $val = Horde_Imap_Client_Mailbox::get($val);
             try {
                 $ret[strval($val)] = $this->status($val, $flags);
             } catch (Horde_Imap_Client_Exception $e) {
             }
         }
     }
     if (!$opts['sort']) {
         return $ret;
     }
     $list_ob = new Horde_Imap_Client_Mailbox_List(array_keys($ret));
     $sorted = $list_ob->sort(array('delimiter' => $opts['sort_delimiter']));
     $out = array();
     foreach ($sorted as $val) {
         $out[$val] = $ret[$val];
     }
     return $out;
 }