From bde3ba5e93f9b02ac8bd1d10e33fa65cc4da31c5 Mon Sep 17 00:00:00 2001
From: Thomas Bruederli <thomas@roundcube.net>
Date: Mon, 07 Apr 2014 11:32:31 -0400
Subject: [PATCH] Update zipdownload plugin to work with multi-folder search results

---
 plugins/zipdownload/composer.json   |    2 +-
 plugins/zipdownload/zipdownload.php |   47 +++++++++++++++++++++++++++--------------------
 plugins/zipdownload/package.xml     |    6 +++---
 3 files changed, 31 insertions(+), 24 deletions(-)

diff --git a/plugins/zipdownload/composer.json b/plugins/zipdownload/composer.json
index 168415c..7f6e309 100644
--- a/plugins/zipdownload/composer.json
+++ b/plugins/zipdownload/composer.json
@@ -3,7 +3,7 @@
     "type": "roundcube-plugin",
     "description": "Adds an option to download all attachments to a message in one zip file, when a message has multiple attachments. Also allows the download of a selection of messages in one zip file and the download of entire folders.",
     "license": "GNU GPLv3+",
-    "version": "2.0",
+    "version": "2.1",
     "authors": [
         {
             "name": "Thomas Bruederli",
diff --git a/plugins/zipdownload/package.xml b/plugins/zipdownload/package.xml
index bf55115..3e57220 100644
--- a/plugins/zipdownload/package.xml
+++ b/plugins/zipdownload/package.xml
@@ -19,10 +19,10 @@
 		<email>roundcube@gmail.com</email>
 		<active>yes</active>
 	</lead>
-	<date>2012-09-20</date>
-	<time>19:16:00</time>
+	<date>2014-04-07</date>
+	<time>17:28:00</time>
 	<version>
-		<release>2.0</release>
+		<release>2.1</release>
 		<api>2.0</api>
 	</version>
 	<stability>
diff --git a/plugins/zipdownload/zipdownload.php b/plugins/zipdownload/zipdownload.php
index 3bab286..32ff1b8 100644
--- a/plugins/zipdownload/zipdownload.php
+++ b/plugins/zipdownload/zipdownload.php
@@ -5,7 +5,7 @@
  *
  * Plugin to allow the download of all message attachments in one zip file
  *
- * @version @package_version@
+ * @version 2.1
  * @requires php_zip extension (including ZipArchive class)
  * @author Philip Weir
  * @author Thomas Bruderli
@@ -156,10 +156,10 @@
     public function download_selection()
     {
         if (isset($_REQUEST['_uid'])) {
-            $uids = explode(",", rcube_utils::get_input_value('_uid', rcube_utils::INPUT_GPC));
+            $messageset = rcmail::get_uids();
 
-            if (sizeof($uids) > 0) {
-                $this->_download_messages($uids);
+            if (sizeof($messageset) > 0) {
+                $this->_download_messages($messageset);
             }
         }
     }
@@ -193,7 +193,7 @@
         }
 
         if (sizeof($uids) > 0)
-            $this->_download_messages($uids);
+            $this->_download_messages(array($mbox_name => $uids));
     }
 
     /**
@@ -201,38 +201,45 @@
      *
      * @param array List of message UIDs to download
      */
-    private function _download_messages($uids)
+    private function _download_messages($messageset)
     {
         $rcmail    = rcmail::get_instance();
         $imap      = $rcmail->get_storage();
         $temp_dir  = $rcmail->config->get('temp_dir');
         $tmpfname  = tempnam($temp_dir, 'zipdownload');
         $tempfiles = array($tmpfname);
+        $folders   = count($messageset) > 1;
 
         // open zip file
         $zip = new ZipArchive();
         $zip->open($tmpfname, ZIPARCHIVE::OVERWRITE);
 
-        foreach ($uids as $uid){
-            $headers = $imap->get_message_headers($uid);
-            $subject = rcube_mime::decode_mime_string((string)$headers->subject);
-            $subject = $this->_convert_filename($subject);
-            $subject = substr($subject, 0, 16);
+        foreach ($messageset as $mbox => $uids){
+            $imap->set_folder($mbox);
+            $path = $folders ? str_replace($imap->get_hierarchy_delimiter(), '/', $mbox) . '/' : '';
 
-            $disp_name = ($subject ? $subject : 'message_rfc822') . ".eml";
-            $disp_name = $uid . "_" . $disp_name;
+            foreach ($uids as $uid){
+                $headers = $imap->get_message_headers($uid);
+                $subject = rcube_mime::decode_mime_string((string)$headers->subject);
+                $subject = $this->_convert_filename($subject);
+                $subject = substr($subject, 0, 16);
 
-            $tmpfn = tempnam($temp_dir, 'zipmessage');
-            $tmpfp = fopen($tmpfn, 'w');
-            $imap->get_raw_body($uid, $tmpfp);
-            $tempfiles[] = $tmpfn;
-            fclose($tmpfp);
-            $zip->addFile($tmpfn, $disp_name);
+                $disp_name = ($subject ? $subject : 'message_rfc822') . ".eml";
+                $disp_name = $path . $uid . "_" . $disp_name;
+
+                $tmpfn = tempnam($temp_dir, 'zipmessage');
+                $tmpfp = fopen($tmpfn, 'w');
+                $imap->get_raw_body($uid, $tmpfp);
+                $tempfiles[] = $tmpfn;
+                fclose($tmpfp);
+                $zip->addFile($tmpfn, $disp_name);
+            }
         }
 
         $zip->close();
 
-        $this->_deliver_zipfile($tmpfname, $imap->get_folder() . '.zip');
+        $filename = $folders ? 'messages' : $imap->get_folder();
+        $this->_deliver_zipfile($tmpfname, $filename . '.zip');
 
         // delete temporary files from disk
         foreach ($tempfiles as $tmpfn) {

--
Gitblit v1.9.1