Handle hostnames with upper-case letters
[webmin.git] / mailboxes / detachall.cgi
1 #!/usr/local/bin/perl
2 # Download all attachments in a ZIP file
3
4 require './mailboxes-lib.pl';
5 &ReadParse();
6 &error_setup($text{'detachall_err'});
7 &can_user($in{'user'}) || &error($text{'mail_ecannot'});
8
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'}];
13 &parse_mail($mail);
14 @sub = split(/\0/, $in{'sub'});
15 foreach $s (@sub) {
16         # We are looking at a mail within a mail ..
17         local $amail = &extract_mail($mail->{'attach'}->[$s]->{'data'});
18         &parse_mail($amail);
19         $mail = $amail;
20         }
21
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);
26 $temp = &transname();
27 &make_dir($temp, 0755) || &error(&text('detachall_emkdir', $!));
28 $n = 0;
29 foreach $a (@attach) {
30         # Work out a filename
31         if (!$a->{'type'} || $a->{'type'} eq 'message/rfc822') {
32                 $fn = "mail".(++$n).".txt";
33                 }
34         elsif ($a->{'filename'}) {
35                 $fn = &decode_mimewords($a->{'filename'});
36                 }
37         else {
38                 $fn = "file".(++$n).".".&type_to_extension($a->{'type'});
39                 }
40
41         # Write the file
42         &open_tempfile(FILE, ">$temp/$fn", 0, 1);
43         &print_tempfile(FILE, $a->{'data'});
44         &close_tempfile(FILE);
45         }
46
47 # Make and output the zip
48 $zip = &transname("$$.zip");
49 $out = &backquote_command(
50         "cd ".quotemeta($temp)." && zip ".quotemeta($zip)." * 2>&1");
51 if ($?) {
52         &error(&text('detachall_ezip', "<tt>".&html_escape($out)."</tt>"));
53         }
54
55 # Output the ZIP
56 print "Content-type: application/zip\n\n";
57 open(ZIP, $zip);
58 while(read(ZIP, $buf, 1024) > 0) {
59         print $buf;
60         }
61 close(ZIP);
62 &unlink_file($zip);
63 &unlink_file($temp);
64