diff --git a/plugins/zipdownload/zipdownload.php b/plugins/zipdownload/zipdownload.php index 9aea8151a..157b53a8f 100644 --- a/plugins/zipdownload/zipdownload.php +++ b/plugins/zipdownload/zipdownload.php @@ -18,6 +18,8 @@ class zipdownload extends rcube_plugin private $charset = 'ASCII'; + private $names = []; + // RFC4155: mbox date format const MBOX_DATE_FORMAT = 'D M d H:i:s Y'; @@ -141,18 +143,8 @@ class zipdownload extends rcube_plugin foreach ($message->attachments as $part) { $pid = $part->mime_id; $part = $message->mime_parts[$pid]; - $filename = $part->filename; - - if ($filename === null || $filename === '') { - $ext = (array) rcube_mime::get_mime_extensions($part->mimetype); - $ext = array_shift($ext); - $filename = $rcmail->gettext('messagepart') . ' ' . $pid; - if ($ext) { - $filename .= '.' . $ext; - } - } + $disp_name = $this->_create_displayname($part); - $disp_name = $this->_convert_filename($filename); $tmpfn = tempnam($temp_dir, 'zipattach'); $tmpfp = fopen($tmpfn, 'w'); $tempfiles[] = $tmpfn; @@ -190,6 +182,48 @@ class zipdownload extends rcube_plugin } } } + + + /** + * Create and get display name of attachment part to add on zip file + * + * @param $part stdClass Part of attachment on message + * + * @return string Display name of attachment part + */ + private function _create_displayname($part) + { + $rcmail = rcmail::get_instance(); + $filename = $part->filename; + + if ($filename === null || $filename === '') { + $ext = (array) rcube_mime::get_mime_extensions($part->mimetype); + $ext = array_shift($ext); + $filename = $rcmail->gettext('messagepart') . ' ' . $part->mime_id; + if ($ext) { + $filename .= '.' . $ext; + } + } + + $displayname = $this->_convert_filename($filename); + /** + * Adding a number before dot of extension on a name of file with same name on zip + * Ext: attach(1).txt on attach filename that has a attach.txt filename on same zip + */ + if (in_array($displayname, $this->names)) { + $attachNumber = 1; + list($filename, $ext) = preg_split("/\.(?=[^\.]*$)/", $displayname); + foreach ($this->names as $name) { + if (preg_match("/{$filename}\((\d+)\)\.{$ext}/", $name, $match)) { + $attachNumber = ((int) $match[1]) + 1; + } + } + $displayname = $filename . '(' .$attachNumber . ').' . $ext; + } + $this->names[] = $displayname; + + return $displayname; + } /** * Helper method to packs all the given messages into a zip archive