Handle hostnames with upper-case letters
[webmin.git] / mysql / csv.cgi
1 #!/usr/local/bin/perl
2 # Export the CSV
3
4 require './mysql-lib.pl';
5 &ReadParse();
6 &error_setup($text{'csv_err'});
7 &can_edit_db($in{'db'}) || &error($text{'dbase_ecannot'});
8 $access{'edonly'} && &error($text{'dbase_ecannot'});
9
10 # Validate inputs
11 if ($in{'dest'}) {
12         $in{'file'} =~ /^([a-z]:)?\/\S/ || &error($text{'csv_efile'});
13         $access{'buser'} || &error($text{'cvs_ebuser'});
14         }
15 if (!$in{'where_def'}) {
16         $in{'where'} =~ /\S/ || &error($text{'csv_ewhere'});
17         }
18
19 # Execute the SQL
20 @cols = split(/\0/, $in{'cols'});
21 @cols || &error($text{'csv_ecols'});
22 $cmd = "select ".join(",", @cols)." from ".&quotestr($in{'table'});
23 if (!$in{'where_def'}) {
24         $cmd .= " where ".$in{'where'};
25         }
26 $rv = &execute_sql($in{'db'}, $cmd);
27
28 # Open the destination
29 if (!$in{'dest'}) {
30         print "Content-type: text/plain\n\n";
31         $fh = STDOUT;
32         }
33 elsif ($access{'buser'} eq 'root') {
34         # Open target file directly
35         &open_tempfile(OUT, ">$in{'file'}");
36         $fh = OUT;
37         }
38 else {
39         # Run through cat command
40         $cmd = &command_as_user($access{'buser'}, 0,
41                                 "cat >".&quote_path($in{'file'}));
42         &open_execute_command(OUT, $cmd, 0);
43         $fh = OUT;
44         }
45
46 # Send the data
47 if ($in{'headers'}) {
48         unshift(@{$rv->{'data'}}, $rv->{'titles'});
49         }
50 foreach $r (@{$rv->{'data'}}) {
51         if ($in{'format'} == 0) {
52                 print $fh join(",", map { "\"".&quote_csv($_, "\"\n")."\"" } @$r);
53                 }
54         elsif ($in{'format'} == 1) {
55                 print $fh join(",", map { &quote_csv($_, ",\n") } @$r);
56                 }
57         elsif ($in{'format'} == 2) {
58                 print $fh join("\t", map { &quote_csv($_, "\t\n") } @$r);
59                 }
60         print $fh "\n";
61         }
62
63 # All done .. tell the user
64 if ($in{'dest'}) {
65         &close_tempfile(OUT);
66         $desc = &text('table_header', "<tt>$in{'table'}</tt>", "<tt>$in{'db'}</tt>");
67         &ui_print_header($desc, $text{'csv_title'}, "", "csv");
68
69         @st = stat($in{'file'});
70         print &text('csv_done', "<tt>$in{'file'}</tt>",
71                                 &nice_size($st[7])),"<p>\n";
72
73         &ui_print_footer(
74            "$gconfig{'webprefix'}/$module_name/edit_dbase.cgi?db=$in{'db'}",
75            $text{'dbase_return'},
76            "", $text{'index_return'});
77         }
78
79 sub quote_csv
80 {
81 local ($str, $q) = @_;
82 $str =~ s/\r//g;
83 foreach my $c (split(//, $q)) {
84         local $qc = $c eq "\"" ? "\\\"" :
85                     $c eq "\n" ? "\\n" :
86                     $c eq "," ? "\\," :
87                     $c eq "\t" ? "\\t" : $c;
88         $str =~ s/\Q$c\E/$qc/g;
89         }
90 return $str;
91 }
92