<?php
function MyHeader () {
echo("<?xml version=\"1.0\"?>
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<title>PHPMyEdit Setup</title>
</head>
<style type=\"text/css\">
body {font-family: \"Verdana\", \"Arial\", \"Sans-Serif\"; text-align: left}
h1 {color: #004d9c; font-size: 12pt; font-weight: bold}
h2 {color: #004d9c; font-size: 10pt; font-weight: bold}
h3 {color: #004d9c; font-size: 10pt; }
p {color: #004d9c; font-size: 8pt; }
table {border: 2px solid #004d9c; font-size: 8pt; text-align: center; border-collapse: \"collapse\"; }
td {border: 2px solid; padding: 2px; color: #004d9c; font-size: 8pt; }
</style>
<body>
");
}
function echo_html($x) {
if ($GLOBALS['download']) {
echo $x."\n";
} else {
echo htmlentities($x)."\n";
}
}
function echonorm($x) {
if (!$GLOBALS['download']) {
echo ($x);
}
}
function do_headers($name) {
header ( "Expires: Mon, 1 Apr 1974 05:00:00 GMT" );
header ( "Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT" );
header ( "Cache-Control: no-cache, must-revalidate" );
header ( "Pragma: no-cache" );
header ( "Content-Type: application/rtf" );
header ( "Content-Disposition: attachment; filename=$name.php" );
header ( "Content-Description: PHP Generated Data" );
}
function get_db_table_arr() {
$tbs = mysql_list_tables($GLOBALS['db'],$GLOBALS['dbl']);
$num_tbs = mysql_numrows($tbs);
for ($j=0; $j<$num_tbs; $j++) {
$tb = mysql_tablename($tbs, $j);
$GLOBALS['tblst'][$j]=$tb;
}
}
function get_db_table_field_arr() {
while (list ($num, $name) = each ($GLOBALS['tblst'])) {
$fds = mysql_list_fields($GLOBALS['db'],$name,$GLOBALS['dbl']);
$j=0;
while (1) {
$fd = mysql_field_name($fds, $j);
if ($fd == "") {
break;
}
$GLOBALS['flds'][$name][$j] = $fd;
$ff = mysql_field_flags($fds, $j);
// echo "$fd flags $ff<br>\n";
if (stristr($ff,"primary_key")) {
$GLOBALS['flds'][$name]['key'] = $fd;
// echo "key=" . $ff . "<br>";
}
++$j;
}
}
reset($GLOBALS['tblst']);
}
function get_table_fields () {
$fds = mysql_list_fields($GLOBALS['db'],$GLOBALS['tb'],$GLOBALS['dbl']);
$j=0;
while (1) {
$fdt = mysql_field_name($fds, $j);
if ($fdt == "") {
break;
}
$GLOBALS['$fd'][$j] = $fdt;
$j++;
}
}
function DisplayArrayVar($arr) {
while (list ($num, $name) = each ($arr)) {
echo $num . " set to : " . $name . "<br> \n";
}
}
$self = basename($GLOBALS['PHP_SELF']);
#print_r ($_POST);
$dbl = mysql_connect($_POST['hn'],$_POST['un'],$_POST['pw']);
if ((!$dbl) or empty($_POST['submit'])) {
$stage=0;
} elseif (!isset($_POST['db'])) {
$stage=1;
} elseif (!isset($_POST['tb'])) {
$stage=2;
} elseif (!isset($_POST['id'])) {
$stage=3;
} elseif (isset($_POST['more']) && !isset($_POST['didmore'])) {
$stage=4;
} elseif (isset($_POST['didmore']) && isset($_POST['more'])) {
$stage=5;
}
// echo "stage = " . $stage;
switch ($stage) {
case 0:
MyHeader ();
echo " <h1>Please log in to your MySQL database</h1>\n";;
if (!empty($_POST['submit'])) {
echo " <h2>Sorry - login failed - please try again</h2>\n";
}
echo "\n";
echo " <form action=\"".$self."\" method=\"POST\">\n";
echo " <table border=\"1\" cellpadding=\"1\" cellspacing=\"0\" summary=\"Log in\">\n";
echo " <tr>\n";
echo " <td>Hostname:</td>\n";
echo " <td><input type=\"text\" name=\"hn\" value=\"".$_POST['hn']."\"></td>\n";
echo " </tr><tr>\n";
echo " <td>Username:</td>\n";
echo " <td><input type=\"text\" name=\"un\" value=\"".$_POST['un']."\"></td>\n";
echo " </tr><tr>\n";
echo " <td>Password:</td>\n";
echo " <td><input type=\"password\" name=\"pw\" value=\"".$_POST['pw']."\"></td>\n";
echo " </tr>\n";
echo " </table>\n";
echo " <br>\n";
echo " <input type=\"submit\" name=\"submit\" value=\"Submit\">\n";
echo " </form>\n";
break;
case 1:
MyHeader ();
$dbs = mysql_list_dbs($dbl);
$num_dbs = mysql_numrows($dbs);
echo " <h1>Please choose a database</h1>\n";
echo " <form action=\"" . $self . "\" method=\"POST\">\n";
echo " <input type=\"hidden\" name=\"hn\" value=\"".$_POST['hn']."\">\n";
echo " <input type=\"hidden\" name=\"un\" value=\"".$_POST['un']."\">\n";
echo " <input type=\"hidden\" name=\"pw\" value=\"".$_POST['pw']."\">\n";
echo " <table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" summary=\"Choose Database\">\n";
for ($i=0; $i<$num_dbs; $i++) {
$db = mysql_dbname($dbs, $i);
if ($i==0) {
echo " <tr><td><input checked type=\"radio\" name=\"db\" value=\"$db\"></td><td>$db</td></tr>\n";
} else {
echo " <tr><td><input type=\"radio\" name=\"db\" value=\"$db\"></td><td>$db</td></tr>\n";
}
}
echo " </table>\n";
echo " <br>\n";
echo " <input type=\"submit\" name=\"submit\" value=\"Submit\">\n";
echo " <input type=\"submit\" name=\"cancel\" value=\"Cancel\">\n";
echo " </form>\n";
break;
case 2:
MyHeader ();
echo " <h1>Please choose a table from database: ".$_POST['db']."</h1>\n";
echo " <form action=\"".$self."\" method=\"POST\">\n";
echo " <input type=\"hidden\" name=\"hn\" value=\"".$_POST['hn']."\">\n";
echo " <input type=\"hidden\" name=\"un\" value=\"".$_POST['un']."\">\n";
echo " <input type=\"hidden\" name=\"pw\" value=\"".$_POST['pw']."\">\n";
echo " <input type=\"hidden\" name=\"db\" value=\"".$_POST['db']."\">\n";
echo " <table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" summary=\"Choose Table\">\n";
$tbs = mysql_list_tables($_POST['db'],$dbl);
$num_tbs = mysql_numrows($tbs);
for ($j=0; $j<$num_tbs; $j++) {
$tb = mysql_tablename($tbs, $j);
if ($j==0) {
echo " <tr><td><input checked type=\"radio\" name=\"tb\" value=\"$tb\"></td><td>$tb</td></tr>\n";
} else {
echo " <tr><td><input type=\"radio\" name=\"tb\" value=\"$tb\"></td><td>$tb</td></tr>\n";
}
}
echo " </table>\n";
echo " <br>\n";
echo " <input type=\"submit\" name=\"submit\" value=\"Submit\">\n";
echo " <input type=\"submit\" name=\"cancel\" value=\"Cancel\">\n";
echo " </form>\n";
break;
case 3:
MyHeader ();
echo " <h1>Please choose an identifier from table: ".$_POST['tb']."</h1>\n";
echo " <h2>This field will be used in Changes or Deletes.</h2>\n";
echo " <p>\n";
echo " It must be numeric and must uniquely identify a record\n";
echo " If you haven\'t got a suitable field select <i>None</i>.\n";
echo " </p>\n";
echo " <form action=\"".$self."\" method=\"POST\">\n";
// echo " <form action=\"test.php\" method=\"POST\">\n";
echo " <input type=\"hidden\" name=\"hn\" value=\"".$_POST['hn']."\">\n";
echo " <input type=\"hidden\" name=\"un\" value=\"".$_POST['un']."\">\n";
echo " <input type=\"hidden\" name=\"pw\" value=\"".$_POST['pw']."\">\n";
echo " <input type=\"hidden\" name=\"db\" value=\"".$_POST['db']."\">\n";
echo " <input type=\"hidden\" name=\"tb\" value=\"".$_POST['tb']."\">\n";
echo " <input type=\"hidden\" name=\"more\" value=\"1\">\n";
echo " <table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" summary=\"Choose Key\">\n";
echo " <tr><td><input type=\"radio\" name=\"id\" value=\"\">\n";
echo " <td><i>None</i></td><td><i>No id field required</i></td></tr>\n";
$fds = mysql_list_fields($_POST['db'],$_POST['tb'],$dbl);
$j=0;
while (1) {
$fd = mysql_field_name($fds, $j);
if ($fd == "") {
break;
}
$ff = mysql_field_flags($fds, $j);
if (stristr($ff,"primary_key")) {
echo " <tr><td><input checked type=\"radio\" name=\"id\" value=\"".$fd."\"></td><td>".$fd."</td><td>".$ff."</td></tr>\n";
} else {
echo " <tr><td><input type=\"radio\" name=\"id\" value=\"".$fd."\"></td><td>".$fd."</td><td>".$ff."</td></tr>\n";
}
++$j;
}
echo " </table>\n";
echo " <br>\n";
echo " <input type=\"submit\" name=\"submit\" value=\"Submit\">\n";
echo " <input type=\"submit\" name=\"cancel\" value=\"Cancel\">\n";
echo " </form>\n";
break;
case 4:
get_db_table_arr();
get_db_table_field_arr();
get_table_fields();
MyHeader ();
echo '<h2>If there are any select tables you can do basic field selection here.</h2><br>'."\n";
echo " <form action=\"".$self."\" method=\"POST\">\n";
// echo " <form action=\"test.php\" method=\"POST\">\n";
echo " <input type=\"hidden\" name=\"hn\" value=\"".$_POST['hn']."\">\n";
echo " <input type=\"hidden\" name=\"un\" value=\"".$_POST['un']."\">\n";
echo " <input type=\"hidden\" name=\"pw\" value=\"".$_POST['pw']."\">\n";
echo " <input type=\"hidden\" name=\"db\" value=\"".$_POST['db']."\">\n";
echo " <input type=\"hidden\" name=\"tb\" value=\"".$_POST['tb']."\">\n";
echo " <input type=\"hidden\" name=\"id\" value=\"".$_POST['id']."\">\n";
echo " <input type=\"hidden\" name=\"more\" value=\"".$_POST['more']."\">\n";
echo " <input type=\"hidden\" name=\"didmore\" value=\"".$_POST['more']."\">\n";
echo " <table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" summary=\"Select linked table fields\">\n";
// echo "count: " . count($fd) . "tblist : " . count($tblist);
for ($j=0; $j<count($GLOBALS['fd']); $j++) {
echo " <tr>\n";
echo " <td>" . $GLOBALS['fd'][$j] . "</td>\n";
echo " <td><select name=\"" . $GLOBALS['fd'][$j] . "table\" size=\"1\">\n";
echo ' <option> </option>'."\n";
while (list ($num, $name) = each ($GLOBALS['tblst'])) {
if ($name != $_POST['tb']) {
for ($i=0; $i < count($GLOBALS['flds'][$name])-1; $i++) {
echo ' <option>' . $GLOBALS['flds'][$name]['key']. "." . $GLOBALS['flds'][$name][$i].'</option>'."\n";
}
}
}
reset($tblst);
echo " </select></td>\n";
echo " </tr>\n";
}
echo " </table>\n";
echo " <input type=\"checkbox\" name=\"download\" value=\"1\"> Select for file download\n";
echo " <br>\n";
echo " <input type=\"submit\" name=\"submit\" value=\"Submit\">\n";
echo " <input type=\"submit\" name=\"cancel\" value=\"Cancel\">\n";
echo " </form>\n";
break;
case 5:
if ($_POST['download']) {
do_headers($_POST['tb']);
} else {
MyHeader ();
}
echonorm ('<h1>Here is your PHPMyEdit calling program</h1>'."\n");
echonorm ('<h2>You may now copy and paste it into your PHP editor or press </h2>'."\n");
echonorm ('<h2>the browser back button and select download to download now</h2>'."\n");
echonorm ('<hr><pre>'."\n");
echo_html("<?php");
echo_html("function MyHeader(\$header) {");
echo_html("echo(\"<?xml version=\\\"1.0\\\"?>");
echo_html("<!DOCTYPE html PUBLIC \\\"-//W3C//DTD XHTML 1.0 Transitional//EN\\\"");
echo_html(" \\\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\\\">");
echo_html("<html xmlns=\\\"http://www.w3.org/1999/xhtml\\\">");
echo_html("<head>");
echo_html(" <title>\$header</title>");
echo_html("</head>");
echo_html("<body>");
echo_html(" <div class=\\\"main\\\">");
echo_html(" <h3>\$header</h3>");
echo_html("\");");
echo_html("}");
echo_html("\n\n");
echo_html("");
echo_html("// MySQL host name, user name, password, database, and table to edit");
echo_html("//\$hn='".$_POST['hn']."';");
echo_html("//\$un='".$_POST['un']."';");
echo_html("//\$pw='".$_POST['pw']."';");
echo_html("//\$db='".$_POST['db']."';");
echo_html("include 'dbaccess.php';");
echo_html("\$tb='".$_POST['tb']."';");
echo_html("");
echo_html("// Name of field which is the unique key");
echo_html("\$key='".$_POST['id']."';");
// Type of key field (int/real/string/date etc)');
if ($id=="") {
echo_html("$key_type='';");
} else {
$fds = mysql_list_fields($_POST['db'],$_POST['tb'],$dbl);
$j=0;
while (1) {
$fd = mysql_field_name($fds, $j);
if ($fd == '') {
break;
}
if ($fd == $id) {
echo_html("\$key_type=" . mysql_field_type($fds, $j) .";");
break;
}
++$j;
}
}
echo_html("// Page header, edit to taste ");
echo_html("\$header = '".$_POST['tb']."';");
echo_html("// Number of records to display on the screen");
echo_html("\$inc = 15;");
echo_html("// Options you wish to give the users - A(dd) C(hange) D(elete) F(ilter) Z(oom) J(ump)");
echo_html("\$options = 'ACDFZJ';");
echo_html("// Should authentication be used, '0' if no, anything else is minimum");
echo_html("// user level needed to be allowed access");
echo_html("\$auth = '0';");
echo_html("// Debugging on/off (0=off, 1=on) (Beware, royally screws up your display with lots of data");
echo_html("\$debug = '0';");
echo_html("//");
echo_html("// You should verify if the program works, then remove the // where appropriate. ");
echo_html("// Uncomment and check table selected fields one at the time to ease debugging.");
echo_html("//");
$fds = mysql_list_fields($_POST['db'], $_POST['tb'], dbl);
$num_fds = mysql_num_fields($fds);
for ($k=0 ; $k<$num_fds; $k++) {
$fd = mysql_field_name($fds,$k);
$tmp = $fd."table";
$fn = ucwords(strtr($fd,"_-."," "));
echo_html("\$fdd['$fd']['name'] = '$fn';");
echo_html("\$fdd['$fd']['sort'] = true;");
echo_html("\$fdd['$fd']['select'] = 'T';");
if (stristr(mysql_field_flags($fds,$k),'not_null')) {
echo_html("\$fdd['$fd']['required'] = true;");
}
if (mysql_field_type($fds,$k)=='blob') {
echo_html("\$fdd['$fd']['textarea']['rows'] = 5;");
echo_html("\$fdd['$fd']['textarea']['cols'] = 50;");
}
if (mysql_field_type($fds,$k)=='date') {
echo_html("//\$fdd['$fd']['date'] = true;");
echo_html("//\$fdd['$fd']['yearstart'] = 2000;");
echo_html("//\$fdd['$fd']['yearend'] = 2010;");
}
if (mysql_field_type($fds,$k)=='int') {
if (mysql_field_len($fds,$k) <= 5) {
echo_html("//\$fdd['$fd']['boolean'] = true;");
}
}
if (mysql_field_type($fds,$k)=='string') {
echo_html("\$fdd['$fd']['length'] = " . mysql_field_len($fds,$k) . ';');
}
if (stristr(mysql_field_flags($fds,$k),'auto_increment')) {
echo_html("//\$fdd['$fd']['auto'] = true;");
}
if ($$tmp) {
list($table, $column) = explode( ".", $$tmp);
echo_html("//\$fdd['$fd']['values']['table'] = '".$_POST['table']."';");
echo_html("//\$fdd['$fd']['values']['column'] = '".$_POST['column']."';");
echo_html("//\$fdd['$fd']['values']['description'] = '';");
}
echo_html("//\$fdd['$fd']['listview'] = false;");
// uncomment the following line to see what type mysql says a field is
// echo_html( '//' . mysql_field_type($fds,$k) );
}
echo_html('// and now the all-important call to PHPMyEdit');
echo_html('// warning - beware of case-sensitive operating systems!');
echo_html("include 'PHPMyEdit.php';");
echo_html("function Post_run() { ");
echo_html("echo \" </div>");
echo_html(" <p></p>");
echo_html(" <hr width='30%' >");
echo_html(" <table>");
echo_html(" <tr>");
echo_html(" <td><a href='index.php'>Main Page</a></td>");
echo_html(" <td> </td>");
echo_html(" <td><a href='index.php?page=menus'>Menus Maintenance Page</a></td>");
echo_html(" </tr>");
echo_html(" </table>");
echo_html(" <br><b> User : \".\$GLOBALS['auth_user'].\" </b><br>");
echo_html("}");
echo_html("function footer() { ");
echo_html(" </body>");
echo_html("</html>\";");
echo_html("}");
echonorm ("</pre><hr>\n");
break;
}
?>
</body>
</html>