#!/usr/local/bin/perl
# exec_files.cgi
# Execute some SQL commands from a file and display the output

require './mysql-lib.pl';
&ReadParseMime();
&can_edit_db($in{'db'}) || &error($text{'dbase_ecannot'});
$access{'edonly'} && &error($text{'dbase_ecannot'});
&error_setup($text{'exec_err'});

if ($in{'mode'}) {
	# From uploaded file
	$in{'upload'} || &error($text{'exec_eupload'});
	$file = &tempname();
	open(TEMP, ">$file");
	print TEMP $in{'upload'};
	close(TEMP);
	&header($text{'exec_title'}, "");
	print "<hr>\n";
	print "<p>$text{'exec_uploadout'}<p>\n";
	}
else {
	# From local file
	-r $in{'file'} || &error($text{'exec_efile'});
	$file = $in{'file'};
	&header($text{'exec_title'}, "");
	print "<hr>\n";
	print "<p>",&text('exec_fileout', "<tt>$in{'file'}</tt>"),"<p>\n";
	}

print "<pre>";
&additional_log('exec', undef, "$config{'mysql'} $authstr -t $in{'db'} <$file");
open(SQL, "$config{'mysql'} $authstr -t $in{'db'} <$file 2>&1 |");
while(<SQL>) {
	print &html_escape($_);
	$got++ if (/\S/);
	}
close(SQL);
print "<i>$text{'exec_noout'}</i>\n" if (!$got);
print "</pre>\n";
&webmin_log("execfile", undef, $in{'db'}, { 'mode' => $in{'mode'},
					    'file' => $in{'file'} });
unlink($file) if ($in{'mode'});

print "<hr>\n";
&footer("edit_dbase.cgi?db=$in{'db'}", $text{'dbase_return'});

