2 # Download all attachments in a ZIP file
4 require './mailboxes-lib.pl';
6 &error_setup($text{'detachall_err'});
7 &can_user($in{'user'}) || &error($text{'mail_ecannot'});
9 @folders = &list_user_folders($in{'user'});
10 $folder = $folders[$in{'folder'}];
11 @mail = &mailbox_list_mails($in{'idx'}, $in{'idx'}, $folder);
12 $mail = $mail[$in{'idx'}];
14 @sub = split(/\0/, $in{'sub'});
16 # We are looking at a mail within a mail ..
17 local $amail = &extract_mail($mail->{'attach'}->[$s]->{'data'});
22 # Save each attachment to a temporary directory
23 @attach = @{$mail->{'attach'}};
24 @attach = &remove_body_attachments($mail, \@attach);
25 @attach = &remove_cid_attachments($mail, \@attach);
27 &make_dir($temp, 0755) || &error(&text('detachall_emkdir', $!));
29 foreach $a (@attach) {
31 if (!$a->{'type'} || $a->{'type'} eq 'message/rfc822') {
32 $fn = "mail".(++$n).".txt";
34 elsif ($a->{'filename'}) {
35 $fn = &decode_mimewords($a->{'filename'});
38 $fn = "file".(++$n).".".&type_to_extension($a->{'type'});
42 &open_tempfile(FILE, ">$temp/$fn", 0, 1);
43 &print_tempfile(FILE, $a->{'data'});
44 &close_tempfile(FILE);
47 # Make and output the zip
48 $zip = &transname("$$.zip");
49 $out = &backquote_command(
50 "cd ".quotemeta($temp)." && zip ".quotemeta($zip)." * 2>&1");
52 &error(&text('detachall_ezip', "<tt>".&html_escape($out)."</tt>"));
56 print "Content-type: application/zip\n\n";
58 while(read(ZIP, $buf, 1024) > 0) {