Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

zipdownload.php 12KB

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