zipdownload.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. /**
  3. * ZipDownload
  4. *
  5. * Plugin to allow the download of all message attachments in one zip file
  6. * and also download of many messages in one go.
  7. *
  8. * @requires php_zip extension (including ZipArchive class)
  9. *
  10. * @author Philip Weir
  11. * @author Thomas Bruderli
  12. * @author Aleksander Machniak
  13. */
  14. class zipdownload extends rcube_plugin
  15. {
  16. public $task = 'mail';
  17. private $charset = 'ASCII';
  18. // RFC4155: mbox date format
  19. const MBOX_DATE_FORMAT = 'D M d H:i:s Y';
  20. /**
  21. * Plugin initialization
  22. */
  23. public function init()
  24. {
  25. // check requirements first
  26. if (!class_exists('ZipArchive', false)) {
  27. rcmail::raise_error(array(
  28. 'code' => 520,
  29. 'file' => __FILE__,
  30. 'line' => __LINE__,
  31. 'message' => "php_zip extension is required for the zipdownload plugin"), true, false);
  32. return;
  33. }
  34. $rcmail = rcmail::get_instance();
  35. $this->load_config();
  36. $this->charset = $rcmail->config->get('zipdownload_charset', RCUBE_CHARSET);
  37. $this->add_texts('localization');
  38. if ($rcmail->config->get('zipdownload_attachments', 1) > -1 && ($rcmail->action == 'show' || $rcmail->action == 'preview')) {
  39. $this->add_hook('template_object_messageattachments', array($this, 'attachment_ziplink'));
  40. }
  41. $this->register_action('plugin.zipdownload.attachments', array($this, 'download_attachments'));
  42. $this->register_action('plugin.zipdownload.messages', array($this, 'download_messages'));
  43. if (!$rcmail->action && $rcmail->config->get('zipdownload_selection')) {
  44. $this->download_menu();
  45. }
  46. }
  47. /**
  48. * Place a link/button after attachments listing to trigger download
  49. */
  50. public function attachment_ziplink($p)
  51. {
  52. $rcmail = rcmail::get_instance();
  53. // only show the link if there is more than the configured number of attachments
  54. if (substr_count($p['content'], '<li') > $rcmail->config->get('zipdownload_attachments', 1)) {
  55. $href = $rcmail->url(array(
  56. '_action' => 'plugin.zipdownload.attachments',
  57. '_mbox' => $rcmail->output->env['mailbox'],
  58. '_uid' => $rcmail->output->env['uid'],
  59. ), false, false, true);
  60. $link = html::a(array('href' => $href, 'class' => 'button zipdownload'),
  61. rcube::Q($this->gettext('downloadall'))
  62. );
  63. // append link to attachments list, slightly different in some skins
  64. switch (rcmail::get_instance()->config->get('skin')) {
  65. case 'classic':
  66. $p['content'] = str_replace('</ul>', html::tag('li', array('class' => 'zipdownload'), $link) . '</ul>', $p['content']);
  67. break;
  68. default:
  69. $p['content'] .= $link;
  70. break;
  71. }
  72. $this->include_stylesheet($this->local_skin_path() . '/zipdownload.css');
  73. }
  74. return $p;
  75. }
  76. /**
  77. * Adds download options menu to the page
  78. */
  79. public function download_menu()
  80. {
  81. $this->include_script('zipdownload.js');
  82. $this->add_label('download');
  83. $rcmail = rcmail::get_instance();
  84. $menu = array();
  85. $ul_attr = array('role' => 'menu', 'aria-labelledby' => 'aria-label-zipdownloadmenu');
  86. if ($rcmail->config->get('skin') != 'classic') {
  87. $ul_attr['class'] = 'toolbarmenu';
  88. }
  89. foreach (array('eml', 'mbox', 'maildir') as $type) {
  90. $menu[] = html::tag('li', null, $rcmail->output->button(array(
  91. 'command' => "download-$type",
  92. 'label' => "zipdownload.download$type",
  93. 'classact' => 'active',
  94. )));
  95. }
  96. $rcmail->output->add_footer(html::div(array('id' => 'zipdownload-menu', 'class' => 'popupmenu', 'aria-hidden' => 'true'),
  97. html::tag('h2', array('class' => 'voice', 'id' => 'aria-label-zipdownloadmenu'), "Message Download Options Menu") .
  98. html::tag('ul', $ul_attr, implode('', $menu))));
  99. }
  100. /**
  101. * Handler for attachment download action
  102. */
  103. public function download_attachments()
  104. {
  105. $rcmail = rcmail::get_instance();
  106. // require CSRF protected request
  107. $rcmail->request_security_check(rcube_utils::INPUT_GET);
  108. $imap = $rcmail->get_storage();
  109. $temp_dir = $rcmail->config->get('temp_dir');
  110. $tmpfname = tempnam($temp_dir, 'zipdownload');
  111. $tempfiles = array($tmpfname);
  112. $message = new rcube_message(rcube_utils::get_input_value('_uid', rcube_utils::INPUT_GET));
  113. // open zip file
  114. $zip = new ZipArchive();
  115. $zip->open($tmpfname, ZIPARCHIVE::OVERWRITE);
  116. foreach ($message->attachments as $part) {
  117. $pid = $part->mime_id;
  118. $part = $message->mime_parts[$pid];
  119. $filename = $part->filename;
  120. if ($filename === null || $filename === '') {
  121. $ext = (array) rcube_mime::get_mime_extensions($part->mimetype);
  122. $ext = array_shift($ext);
  123. $filename = $rcmail->gettext('messagepart') . ' ' . $pid;
  124. if ($ext) {
  125. $filename .= '.' . $ext;
  126. }
  127. }
  128. $disp_name = $this->_convert_filename($filename);
  129. $tmpfn = tempnam($temp_dir, 'zipattach');
  130. $tmpfp = fopen($tmpfn, 'w');
  131. $tempfiles[] = $tmpfn;
  132. $message->get_part_body($part->mime_id, false, 0, $tmpfp);
  133. $zip->addFile($tmpfn, $disp_name);
  134. fclose($tmpfp);
  135. }
  136. $zip->close();
  137. $filename = ($this->_filename_from_subject($message->subject) ?: 'attachments') . '.zip';
  138. $this->_deliver_zipfile($tmpfname, $filename);
  139. // delete temporary files from disk
  140. foreach ($tempfiles as $tmpfn) {
  141. unlink($tmpfn);
  142. }
  143. exit;
  144. }
  145. /**
  146. * Handler for message download action
  147. */
  148. public function download_messages()
  149. {
  150. $rcmail = rcmail::get_instance();
  151. if ($rcmail->config->get('zipdownload_selection') && !empty($_POST['_uid'])) {
  152. $messageset = rcmail::get_uids();
  153. if (sizeof($messageset)) {
  154. $this->_download_messages($messageset);
  155. }
  156. }
  157. }
  158. /**
  159. * Helper method to packs all the given messages into a zip archive
  160. *
  161. * @param array List of message UIDs to download
  162. */
  163. private function _download_messages($messageset)
  164. {
  165. $rcmail = rcmail::get_instance();
  166. $imap = $rcmail->get_storage();
  167. $mode = rcube_utils::get_input_value('_mode', rcube_utils::INPUT_POST);
  168. $temp_dir = $rcmail->config->get('temp_dir');
  169. $tmpfname = tempnam($temp_dir, 'zipdownload');
  170. $tempfiles = array($tmpfname);
  171. $folders = count($messageset) > 1;
  172. // @TODO: file size limit
  173. // open zip file
  174. $zip = new ZipArchive();
  175. $zip->open($tmpfname, ZIPARCHIVE::OVERWRITE);
  176. if ($mode == 'mbox') {
  177. $tmpfp = fopen($tmpfname . '.mbox', 'w');
  178. }
  179. foreach ($messageset as $mbox => $uids) {
  180. $imap->set_folder($mbox);
  181. $path = $folders ? str_replace($imap->get_hierarchy_delimiter(), '/', $mbox) . '/' : '';
  182. if ($uids === '*') {
  183. $index = $imap->index($mbox, null, null, true);
  184. $uids = $index->get();
  185. }
  186. foreach ($uids as $uid) {
  187. $headers = $imap->get_message_headers($uid);
  188. if ($mode == 'mbox') {
  189. // Sender address
  190. $from = rcube_mime::decode_address_list($headers->from, null, true, $headers->charset, true);
  191. $from = array_shift($from);
  192. $from = preg_replace('/\s/', '-', $from);
  193. // Received (internal) date
  194. $date = rcube_utils::anytodatetime($headers->internaldate);
  195. if ($date) {
  196. $date->setTimezone(new DateTimeZone('UTC'));
  197. $date = $date->format(self::MBOX_DATE_FORMAT);
  198. }
  199. // Mbox format header (RFC4155)
  200. $header = sprintf("From %s %s\r\n",
  201. $from ?: 'MAILER-DAEMON',
  202. $date ?: ''
  203. );
  204. fwrite($tmpfp, $header);
  205. // Use stream filter to quote "From " in the message body
  206. stream_filter_register('mbox_filter', 'zipdownload_mbox_filter');
  207. $filter = stream_filter_append($tmpfp, 'mbox_filter');
  208. $imap->get_raw_body($uid, $tmpfp);
  209. stream_filter_remove($filter);
  210. fwrite($tmpfp, "\r\n");
  211. }
  212. else { // maildir
  213. $subject = rcube_mime::decode_header($headers->subject, $headers->charset);
  214. $subject = $this->_filename_from_subject(mb_substr($subject, 0, 16));
  215. $subject = $this->_convert_filename($subject);
  216. $disp_name = $path . $uid . ($subject ? " $subject" : '') . '.eml';
  217. $tmpfn = tempnam($temp_dir, 'zipmessage');
  218. $tmpfp = fopen($tmpfn, 'w');
  219. $imap->get_raw_body($uid, $tmpfp);
  220. $tempfiles[] = $tmpfn;
  221. fclose($tmpfp);
  222. $zip->addFile($tmpfn, $disp_name);
  223. }
  224. }
  225. }
  226. $filename = $folders ? 'messages' : $imap->get_folder();
  227. if ($mode == 'mbox') {
  228. $tempfiles[] = $tmpfname . '.mbox';
  229. fclose($tmpfp);
  230. $zip->addFile($tmpfname . '.mbox', $filename . '.mbox');
  231. }
  232. $zip->close();
  233. $this->_deliver_zipfile($tmpfname, $filename . '.zip');
  234. // delete temporary files from disk
  235. foreach ($tempfiles as $tmpfn) {
  236. unlink($tmpfn);
  237. }
  238. exit;
  239. }
  240. /**
  241. * Helper method to send the zip archive to the browser
  242. */
  243. private function _deliver_zipfile($tmpfname, $filename)
  244. {
  245. $browser = new rcube_browser;
  246. $rcmail = rcmail::get_instance();
  247. $rcmail->output->nocacheing_headers();
  248. if ($browser->ie)
  249. $filename = rawurlencode($filename);
  250. else
  251. $filename = addcslashes($filename, '"');
  252. // send download headers
  253. header("Content-Type: application/octet-stream");
  254. if ($browser->ie) {
  255. header("Content-Type: application/force-download");
  256. }
  257. // don't kill the connection if download takes more than 30 sec.
  258. @set_time_limit(0);
  259. header("Content-Disposition: attachment; filename=\"". $filename ."\"");
  260. header("Content-length: " . filesize($tmpfname));
  261. readfile($tmpfname);
  262. }
  263. /**
  264. * Helper function to convert filenames to the configured charset
  265. */
  266. private function _convert_filename($str)
  267. {
  268. $str = strtr($str, array(':' => '', '/' => '-'));
  269. return rcube_charset::convert($str, RCUBE_CHARSET, $this->charset);
  270. }
  271. /**
  272. * Helper function to convert message subject into filename
  273. */
  274. private function _filename_from_subject($str)
  275. {
  276. $str = preg_replace('/[\t\n\r\0\x0B]+\s*/', ' ', $str);
  277. return trim($str, " ./_");
  278. }
  279. }
  280. class zipdownload_mbox_filter extends php_user_filter
  281. {
  282. function filter($in, $out, &$consumed, $closing)
  283. {
  284. while ($bucket = stream_bucket_make_writeable($in)) {
  285. // messages are read line by line
  286. if (preg_match('/^>*From /', $bucket->data)) {
  287. $bucket->data = '>' . $bucket->data;
  288. $bucket->datalen += 1;
  289. }
  290. $consumed += $bucket->datalen;
  291. stream_bucket_append($out, $bucket);
  292. }
  293. return PSFS_PASS_ON;
  294. }
  295. }