]> git.joonet.de Git - adminer.git/commitdiff
IMAP: Delete message
authorJakub Vrana <jakub@vrana.cz>
Tue, 18 Mar 2025 11:35:36 +0000 (12:35 +0100)
committerJakub Vrana <jakub@vrana.cz>
Tue, 18 Mar 2025 11:35:36 +0000 (12:35 +0100)
Also support mailboxes with spaces

plugins/drivers/imap.php

index f0f454f3a034ac4f6aff3c316844277598b06728..b316fc7ad4654f399d54634f55f5a452b81968e5 100644 (file)
@@ -4,7 +4,8 @@
 * - list messages in each mailbox - limit and offset works but there's no search and order
 * - for each message, there's subject, from, to, date and some flags
 * - editing the message shows some other information
-* - inserting, deleting or updating the message does nothing
+* - deleting marks the message for deletion but doesn't expunge the mailbox
+* - inserting or updating the message does nothing
 */
 
 namespace Adminer;
@@ -36,30 +37,33 @@ if (isset($_GET["imap"])) {
                        }
 
                        function query($query, $unbuffered = false) {
-                               if (!preg_match('~^SELECT (.+)\sFROM (\w+)(?:\sWHERE uid = (\d+))?.*?(?:\sLIMIT (\d+)(?:\sOFFSET (\d+))?)?~s', $query, $match)) {
-                                       return false;
-                               }
-                               list(, $columns, $table, $uid, $limit, $offset) = $match;
-                               if ($uid) {
-                                       $return = array((array) imap_fetchstructure($this->imap, $uid, FT_UID));
-                               } else {
-                                       imap_reopen($this->imap, "$this->mailbox$table");
-                                       $check = imap_check($this->imap);
-                                       $range = ($offset + 1) . ":" . ($limit ? min($check->Nmsgs, $offset + $limit) : $check->Nmsgs);
-                                       $return = array();
-                                       $fields = fields($table);
-                                       $columns = ($columns == "*" ? $fields : array_flip(explode(", ", $columns)));
-                                       $empty = array_fill_keys(array_keys($fields), null);
-                                       foreach (imap_fetch_overview($this->imap, $range) as $row) {
-                                               // imap_utf8 doesn't work with some strings
-                                               $row->subject = iconv_mime_decode($row->subject, 2, "utf-8");
-                                               $row->from = iconv_mime_decode($row->from, 2, "utf-8");
-                                               $row->to = iconv_mime_decode($row->to, 2, "utf-8");
-                                               $row->udate = gmdate("Y-m-d H:i:s", $row->udate);
-                                               $return[] = array_intersect_key(array_merge($empty, (array) $row), $columns);
+                               if (preg_match('~DELETE FROM "(.+?)"~', $query)) {
+                                       preg_match_all('~"uid" = (\d+)~', $query, $matches);
+                                       imap_delete($this->imap, implode(",", $matches[1]), FT_UID);
+                               } elseif (preg_match('~^SELECT (.+)\sFROM "(.+?)"(?:\sWHERE "uid" = (\d+))?.*?(?:\sLIMIT (\d+)(?:\sOFFSET (\d+))?)?~s', $query, $match)) {
+                                       list(, $columns, $table, $uid, $limit, $offset) = $match;
+                                       if ($uid) {
+                                               $return = array((array) imap_fetchstructure($this->imap, $uid, FT_UID));
+                                       } else {
+                                               imap_reopen($this->imap, "$this->mailbox$table");
+                                               $check = imap_check($this->imap);
+                                               $range = ($offset + 1) . ":" . ($limit ? min($check->Nmsgs, $offset + $limit) : $check->Nmsgs);
+                                               $return = array();
+                                               $fields = fields($table);
+                                               $columns = ($columns == "*" ? $fields : array_flip(explode(", ", $columns)));
+                                               $empty = array_fill_keys(array_keys($fields), null);
+                                               foreach (imap_fetch_overview($this->imap, $range) as $row) {
+                                                       // imap_utf8 doesn't work with some strings
+                                                       $row->subject = iconv_mime_decode($row->subject, 2, "utf-8");
+                                                       $row->from = iconv_mime_decode($row->from, 2, "utf-8");
+                                                       $row->to = iconv_mime_decode($row->to, 2, "utf-8");
+                                                       $row->udate = gmdate("Y-m-d H:i:s", $row->udate);
+                                                       $return[] = array_intersect_key(array_merge($empty, (array) $row), $columns);
+                                               }
                                        }
+                                       return new Result($return);
                                }
-                               return new Result($return);
+                               return false;
                        }
 
                        function quote($string) {
@@ -181,7 +185,7 @@ if (isset($_GET["imap"])) {
        }
 
        function idf_escape($idf) {
-               return $idf; //! maybe {}
+               return '"' . str_replace('"', '""', $idf) . '"';
        }
 
        function table($idf) {