summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2011-10-27 16:59:36 (GMT)
committerJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2011-10-27 16:59:36 (GMT)
commit7173c7a7cb6d9583d7d0a99f3d419db6c8614964 (patch)
tree6aa99342a23607506a3c15451221303a70628880
parent0c08c0645f60e7fa046695087de334c9a384b634 (diff)
downloadNet_IMAP4-7173c7a7cb6d9583d7d0a99f3d419db6c8614964.tar.gz
Allow __list() to be called with a set of metadata options, compatible with both ANNOTATEMORE and METADATA.
-rw-r--r--lib/Net/IMAP4.php251
1 files changed, 200 insertions, 51 deletions
diff --git a/lib/Net/IMAP4.php b/lib/Net/IMAP4.php
index eca88f9..13a39ac 100644
--- a/lib/Net/IMAP4.php
+++ b/lib/Net/IMAP4.php
@@ -3256,13 +3256,22 @@
Possible Values: SUBSCRIBED,
RECURSIVEMATCH, REMOTE
+ @param array $metadata_opts RFC5464 METADATA annotations,
+ shared/private, matching value.
+
@return array List of mailboxes or hash of options if $status_ops
argument is non-empty.
@access private
*/
- private function __list($ref, $mailbox, $subscribed=false,
- $status_opts=array(), $select_opts=array()) {
+ private function __list(
+ $ref,
+ $mailbox,
+ $subscribed=false,
+ $status_opts=array(),
+ $select_opts=array(),
+ $metadata_opts=array()
+ ) {
$lstatus = false;
@@ -3270,74 +3279,214 @@
$mailbox = '*';
}
- $args = array();
+ if (is_array($metadata_opts) && !empty($metadata_opts)) {
+ $anno_namespaces = Array(
+ "/private",
+ "/shared"
+ );
+
+ // Escape not supporting any annotations
+ if (
+ !$this->_get_capability('ANNOTATEMORE') &&
+ !$this->_get_capability('METADATA')
+ ) {
+ return false;
+ }
- if (!empty($select_opts) && $this->_get_capability('LIST-EXTENDED')) {
- $select_opts = (array) $select_opts;
+ $args = array();
+ $args[] = $this->_escape($mailbox);
- $args[] = '(' . implode(' ', $select_opts) . ')';
- }
+ // Our method now becomes to GETANNOTATION or GETMETADATA on
+ // folders using the values in $metadata_opts, matching against
+ // LIST or LSUB depending on the way we were called.
+ if (array_key_exists('annotations', $metadata_opts)) {
+ $_arg = Array();
- $args[] = $this->_escape($ref);
- $args[] = $this->_escape($mailbox);
+ if ($this->_get_capability('ANNOTATEMORE')) {
+ foreach ($metadata_opts['annotations'] as $num => $annotation) {
+ foreach($anno_namespaces as $num => $prefix) {
+ if (substr($annotation, 0, strlen($prefix)) == $prefix) {
+ $annotation = substr($annotation, strlen($prefix));
+ }
+ }
+ $_arg[] = $this->_escape($annotation);
+ }
- if (!empty($status_opts) && $this->_get_capability('LIST-STATUS')) {
- $status_opts = (array) $status_opts;
- $lstatus = true;
+ $args[] = '(' . implode(' ', $_arg) . ')';
+
+ } elseif ($this->_get_capability('METADATA')) {
+ $annotations = Array();
+ foreach ($metadata_opts['annotations'] as $num => $annotation) {
+ // Prefix with namespaces where necessary
+ //
+ // If namespaces to query had been provided, only
+ // use those. Otherwise, use the namespaces we know
+ // off (private, shared).
+ if (array_key_exists('namespaces', $metadata_opts)) {
+ $anno_namespaces = $metadata_opts['namespaces'];
+ }
- $args[] = 'RETURN (STATUS (' . implode(' ', $status_opts) . '))';
- }
+ foreach($anno_namespaces as $num => $prefix) {
+ if (!substr($prefix, 0, 1) == "/" ) {
+ $prefix = "/" . $prefix;
+ }
- list($code, $response) = $this->__execute($subscribed ? 'LSUB' : 'LIST', $args);
+ if (!substr($annotation, 0, strlen($prefix)) == $prefix) {
+ $annotations[] = $prefix . $annotation;
+ } else {
+ $annotations[] = $annotation;
+ }
+ }
- if ($code == self::ERROR_OK) {
- $folders = array();
- while ($this->__tokenize_response($response, 1) == '*') {
- $cmd = strtoupper($this->__tokenize_response($response, 1));
- // * LIST (<options>) <delimiter> <mailbox>
- if ($cmd == 'LIST' || $cmd == 'LSUB') {
- list($opts, $delim, $mailbox) = $this->__tokenize_response($response, 3);
-
- // Add to result array
- if (!$lstatus) {
- $folders[] = $mailbox;
+ $_arg[] = $this->_escape($annotation);
}
- else {
- $folders[$mailbox] = array();
+
+ $args[] = '(' . implode(' ', $_arg) . ')';
+
+ } else {
+ return false;
+ }
+
+ } else {
+ $args[] = '("*")';
+ }
+
+ if (array_key_exists('namespaces', $metadata_opts)) {
+ if ($this->_has_capability('ANNOTATEMORE')) {
+ $_arg = Array();
+
+ foreach ($metadata_opts['namespaces'] as $num => $namespace) {
+ switch ($namespace) {
+ case "/private":
+ case "private":
+ case "priv":
+ case "value.priv":
+ $_arg[] = '"value.priv"';
+ break;
+
+ case "/shared":
+ case "shared":
+ case "value.shared":
+ $_arg[] = '"value.shared"';
+ break;
+
+ }
+ }
+
+ $args[] = '(' . implode(' ', $_arg) . ')';
+ }
+ }
+
+ list($code, $response) = $this->__execute($this->_has_capability('ANNOTATEMORE') ? 'GETANNOTATION' : "GETMETADATA", $args);
+
+ if ($code == self::ERROR_OK) {
+ $annotated_folders = array();
+
+ $folders = $this->imap_lsub($ref, $mailbox, $subscribed);
+
+ while ($this->__tokenize_response($response, 1) == '*') {
+ $cmd = strtoupper($this->__tokenize_response($response, 1));
+
+ // * ANNOTATION mailbox annotation (values)
+ if ($cmd == 'METADATA' || $cmd == 'ANNOTATION') {
+ list($mailbox, $annotation, $value) = $this->__tokenize_response($response, 3);
+ if (in_array($mailbox, $folders)) {
+ $annotated_folders[] = Array(
+ $mailbox,
+ $annotation,
+ $value
+ );
+ }
}
- // Add to options array
- if (!empty($opts)) {
- if (empty($this->data['LIST'][$mailbox]))
- $this->data['LIST'][$mailbox] = $opts;
+ // other untagged response line, skip it
+ else {
+ $response = ltrim($response);
+ if (($position = strpos($response, "\n")) !== false)
+ $response = substr($response, $position+1);
else
- $this->data['LIST'][$mailbox] = array_unique(array_merge(
- $this->data['LIST'][$mailbox], $opts));
+ $response = '';
}
}
- // * STATUS <mailbox> (<result>)
- else if ($cmd == 'STATUS') {
- list($mailbox, $status) = $this->__tokenize_response($response, 2);
- for ($i=0, $len=count($status); $i<$len; $i += 2) {
- list($name, $value) = $this->__tokenize_response($status, 2);
- $folders[$mailbox][$name] = $value;
+ return $annotated_folders;
+
+ } else {
+ return false;
+ }
+
+
+ } else {
+
+ $args = array();
+
+ if (!empty($select_opts) && $this->_get_capability('LIST-EXTENDED')) {
+ $select_opts = (array) $select_opts;
+
+ $args[] = '(' . implode(' ', $select_opts) . ')';
+ }
+
+ $args[] = $this->_escape($ref);
+ $args[] = $this->_escape($mailbox);
+
+ if (!empty($status_opts) && $this->_get_capability('LIST-STATUS')) {
+ $status_opts = (array) $status_opts;
+ $lstatus = true;
+
+ $args[] = 'RETURN (STATUS (' . implode(' ', $status_opts) . '))';
+ }
+
+ list($code, $response) = $this->__execute($subscribed ? 'LSUB' : 'LIST', $args);
+
+ if ($code == self::ERROR_OK) {
+ $folders = array();
+ while ($this->__tokenize_response($response, 1) == '*') {
+ $cmd = strtoupper($this->__tokenize_response($response, 1));
+ // * LIST (<options>) <delimiter> <mailbox>
+ if ($cmd == 'LIST' || $cmd == 'LSUB') {
+ list($opts, $delim, $mailbox) = $this->__tokenize_response($response, 3);
+
+ // Add to result array
+ if (!$lstatus) {
+ $folders[] = $mailbox;
+ }
+ else {
+ $folders[$mailbox] = array();
+ }
+
+ // Add to options array
+ if (!empty($opts)) {
+ if (empty($this->data['LIST'][$mailbox]))
+ $this->data['LIST'][$mailbox] = $opts;
+ else
+ $this->data['LIST'][$mailbox] = array_unique(array_merge(
+ $this->data['LIST'][$mailbox], $opts));
+ }
+ }
+ // * STATUS <mailbox> (<result>)
+ else if ($cmd == 'STATUS') {
+ list($mailbox, $status) = $this->__tokenize_response($response, 2);
+
+ for ($i=0, $len=count($status); $i<$len; $i += 2) {
+ list($name, $value) = $this->__tokenize_response($status, 2);
+ $folders[$mailbox][$name] = $value;
+ }
+ }
+ // other untagged response line, skip it
+ else {
+ $response = ltrim($response);
+ if (($position = strpos($response, "\n")) !== false)
+ $response = substr($response, $position+1);
+ else
+ $response = '';
}
}
- // other untagged response line, skip it
- else {
- $response = ltrim($response);
- if (($position = strpos($response, "\n")) !== false)
- $response = substr($response, $position+1);
- else
- $response = '';
- }
+
+ return $folders;
}
- return $folders;
+ return false;
}
-
- return false;
}
private function __read_line($size=1024) {