Handle hostnames with upper-case letters
[webmin.git] / mysql / exec_file.cgi
1 #!/usr/local/bin/perl
2 # exec_file.cgi
3 # Execute some SQL commands from a file and display the output
4
5 require './mysql-lib.pl';
6 &ReadParseMime();
7 &can_edit_db($in{'db'}) || &error($text{'dbase_ecannot'});
8 $access{'edonly'} && &error($text{'dbase_ecannot'});
9 &error_setup($text{'exec_err'});
10
11 if ($in{'mode'}) {
12         # From uploaded file
13         $in{'upload'} || &error($text{'exec_eupload'});
14         $file = &transname();
15         open(TEMP, ">$file");
16         print TEMP $in{'upload'};
17         close(TEMP);
18         &ui_print_header(undef, $text{'exec_title'}, "");
19         print "$text{'exec_uploadout'}<p>\n";
20         $need_unlink = 1;
21         }
22 else {
23         # From local file
24         -r $in{'file'} || &error($text{'exec_efile'});
25         $file = $in{'file'};
26         &ui_print_header(undef, $text{'exec_title'}, "");
27         print &text('exec_fileout', "<tt>$in{'file'}</tt>"),"<p>\n";
28         $need_unlink = 0;
29         }
30
31 # Un-compress file if needed
32 $cf = &compression_format($file);
33 $cmd = $cf == 1 ? "gunzip -c" :
34        $cf == 2 ? "uncompress -C" :
35        $cf == 3 ? "bunzip2 -c" : undef;
36 if ($cmd) {
37         ($prog, @args) = split(/\s+/, $cmd);
38         &has_command($prog) ||
39                 &error(&text('exec_ecompress', "<tt>$prog</tt>"));
40         $tempfile = &transname();
41         $out = &backquote_command("$cmd <$file 2>&1 >$tempfile");
42         if ($?) {
43                 &error(&text('exec_ecompress2', "<pre>$out</pre>"));
44                 }
45         unlink($file) if ($need_unlink);
46         $file = $tempfile;
47         $need_unlink = 1;
48         }
49
50 # Check the file for tables created and rows inserted
51 $create_count = 0;
52 $insert_count = 0;
53 open(SQL, $file);
54 while(<SQL>) {
55         if (/^\s*insert\s+into\s+`(\S+)`/i ||
56             /^\s*insert\s+into\s+(\S+)/i) {
57                 $insert_count++;
58                 }
59         if (/^\s*create\s+table\s+`(\S+)`/i ||
60             /^\s*create\s+table\s+(\S+)/i) {
61                 $create_count++;
62                 }
63         }
64 close(SQL);
65
66 print "<pre>";
67 ($ex, $out) = &execute_sql_file($in{'db'}, $file,
68                                 undef, undef, $access{'buser'});
69 print &html_escape($out);
70 $got++ if ($out =~ /\S/);
71 print "<i>$text{'exec_noout'}</i>\n" if (!$got);
72 print "</pre>\n";
73 if (!$ex) {
74         if ($create_count) {
75                 print &text('exec_created', $create_count),"\n";
76                 }
77         if ($insert_count) {
78                 print &text('exec_inserted', $insert_count),"\n";
79                 }
80         if ($create_count || $insert_count) {
81                 print "<p>\n";
82                 }
83         }
84 &webmin_log("execfile", undef, $in{'db'}, { 'mode' => $in{'mode'},
85                                             'file' => $in{'file'} });
86 unlink($file) if ($need_unlink);
87
88 &ui_print_footer("exec_form.cgi?db=$in{'db'}&mode=file", $text{'exec_return'},
89         "edit_dbase.cgi?db=$in{'db'}", $text{'dbase_return'},
90         "", $text{'index_return'});
91