<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
  <title>Generated Config File for Web Forms</title>
  <LINK REL="stylesheet"   HREF="/PHP/style.css">
  <META NAME-EQUIV="Pragma" CONTENT="no-cache">
  <META NAME="author"      CONTENT="Denice Deatrich">
  <META NAME="description" CONTENT="PHP MySQL DB Webpage Generator">
  <META NAME="keywords"    CONTENT="PHP web MySQL database generator">
</head>

<body>
  <h1>Configuration of MySQL Web Forms
   <A TARGET="help" HREF="help_WebDB.html">
   <img src="/question.gif" align="top" alt="(Help)"></a></h1>

<?php
  
/** $arr[] will contain the forms data; $names[] is used for loop-control **/
  
$names = array( "page", "contact", "host", "db", "table", "user", "passwd" );
  
$arr["page"] = "Database";
  
$arr["contact"] = "";
  
$arr["host"] = "";
  
$arr["db"] = "";
  
$arr["table"] = "";
  
$arr["user"] = "";
  
$arr["passwd"] = "";

  
/** the number of column definitions found **/
  
$numCols = 0;

  
/** the data columns from MySQL defn. dumped into this assoc. 2D array **/
  
$cols[0]["name"] = "";
  
$cols[0]["type"] = "";
  
$cols[0]["prec"] = 10;
  
$cols[0]["default"] = '';

  
/** 'types' is an array of legal MySQL data types **/
  
$types = array(
   
"tinyint", "smallint", "mediumint", "bigint", "int", "integer", "bigint",
   
"float", "double", "real", "decimal", "numeric",
   
"date", "datetime", "timestamp",  "time", "year",
   
"char",  "character", "varchar", "nchar",
   
"tinyblob", "tinytext", "blob", "text", "mediumblob", "mediumtext",
   
"longblob", "longtext", "enum",  "set" );
  
$special = array( "national" ); // ignore for now..

  /**   Function : test if string is empty
   **   Returns:   TRUE or FALSE
   **/
  
function isEmpty( $s ) {
    
$ns = ereg_replace( "[[:space:]]+", "", $s );
    return (
strlen($ns) == 0);
  }


  
/**   Function : get the $nth word in the string $s counting from 0!!
   **   Returns:   $word (which will be the empty string if $nth out of range)
   **/
  
function getword( $s, $nth ) {
    
$word = "";
    
$sep = "([\t \n\r]+)";
    
$words = split( $sep, $s );
    
$nwords = count( $words );
    if(
$nth >= 0 && $nth < $nwords ) {
      
$word = $words[$nth];
    }
    return
$word;
  }

  
/**
    * FUNCTION: parseDescription()
    * PURPOSE:  parse mysqldump table description into $cols[][]
    * RETURNS:  the number of data columns parsed
    * LOGIC:
    * for each line
    *   if comment or empty then continue;
    *   if tablename found then tablename=TRUE; continue;
    *   if tablename
    *     then parse thisLine for column definitions
    * end-for
   **/
  
function parseDescription()
  {
    global
$desc, $arr, $cols, $types;

    
//echo "DBG:<pre>\n" . stripslashes($desc) . "</pre>";
    
$lines = explode("\r\n", $desc);  
    
$tableFound = false;
    
$count = 0;
    while( list(
$k, $v) = each($lines) ) {
      
$line = trim( $v );                // strip white space
      
if( isEmpty($line) ) {
        continue;
      }
      if(
ereg( "^#", $line) ) {   // ignore comments
        
continue;
      }
      if(
eregi( "^CREATE TABLE ", $line) ) {   // find table name
        
$tname = getword( $line, 2 );
        if(
strlen($tname) > 0 ) {
          if(
strcmp(strtolower($tname), strtolower($arr["table"])) == 0 ) {
            
$tableFound = true;
          }
          else {
            echo
"<font color=\"red\">Error: table name <em>" .$tname.
             
"</em> does not match <em>" .$arr["table"]. "</em></font><br>";
            break;
          }
        }
        continue;
      }
      if(
$tableFound ) {
        
//echo "DBG: " .$line . "<br>";
        
$prec = 10;                        //arbitrary default for precision
        
$colname = getword( $line, 0 );    // get the column name and data type
        
$type = $typeAndPrec = getword( $line, 1 );
        
/** type probably looks like this: 'int(10)' so parse precision out **/
        
if( ($pos1 = strpos($typeAndPrec, "(")) > 0 ) { // parse precision
          
$type = substr( $typeAndPrec, 0, $pos1 );
          
$posn = strpos( $typeAndPrec, ")" );
          if(
$posn > 0 && $posn > $pos1 ) {
            
$prec = substr( $typeAndPrec, $pos1+1, ($posn-$pos1-1) );
          }
        }
        
reset( $types );
        while( list(
$k, $v) = each($types) ) {
          if(
strcmp(strtolower($type), strtolower($v)) == 0 ) {
            
/** only add data column definition if type is legal **/
            
$cols[$count]["name"] = $colname;
            
$cols[$count]["type"] = $type;
            
$cols[$count]["prec"] = $prec;

            
/** now pick out the default value if it exists **/
            
$w = getword( $line, 2 );
            if( (
strlen($w) > 0) && strcmp($w, "DEFAULT") == 0 ) {
              
$w = getword( $line, 3);
              
$w =  ereg_replace( "\\\'", "", $w );
              
$cols[$count]["default"] = $w;
            }
            else {
              
$cols[$count]["default"] = "";
            }

            
$count++;
            break;
          }
        }
      }
    }
    return
$count;
  }
  if(
strlen($SERVER_ADMIN) > 0 ) {
    
$arr["contact"] = "mailto:" . $SERVER_ADMIN;
  }
  if(
is_array($HTTP_POST_VARS) ) { // then we got here normally with POST
    
while( list($key, $val) = each($HTTP_POST_VARS) ) {
      if(
strlen($val) > 0 ) {      // paranoid..
        
switch( $key ) {
        case
"page":
          
$arr["page"] = trim($page);
          break;
        case
"contact":
          
$arr["contact"] = trim($contact);
          break;
        case
"db":
          
$arr["db"] = trim($db);
          break;
        case
"table":
            
$arr["table"] = trim($table);
          break;
        case
"host":
            
$arr["host"] = trim($host);
          break;
        case
"user":
            
$arr["user"] = trim($user);
          break;
        case
"passwd":
            
$arr["passwd"] = trim($passwd);
          break;
        case
"desc":
          
$numCols = parseDescription();
          break;
        default:
          break;
        }
      }
    }

    
// check post vars for completeness
    
$bad = false;
    while( list(
$k, $v) = each($names) ) {
      
//echo "DBG: key: " .$k. "   value: " .$v. "<BR>";
      
if( strlen($arr[$v]) <= 0 ) {
        
$bad = true;
        echo
"<font color=\"red\">Error: " .$v. " name required.</font><br>";
      }
    }
    if(
$numCols <= 0 ) {
      
$bad = true;
      echo
"<font color=\"red\">Error: table definition required.</font><br>";
    }

    if( !
$bad ) {
      echo
"
      <form action=\"confgen2.php\" target=\"_blank\" method=\"post\">
      <input type=\"hidden\" name=\"page\" value=\"$page\">
      <input type=\"hidden\" name=\"contact\" value=\"$contact\">
      <input type=\"hidden\" name=\"host\" value=\"$host\">
      <input type=\"hidden\" name=\"db\" value=\"$db\">
      <input type=\"hidden\" name=\"table\" value=\"$table\">
      <input type=\"hidden\" name=\"user\" value=\"$user\">
      <input type=\"hidden\" name=\"passwd\" value=\"$passwd\">
      <input type=\"hidden\" name=\"numCols\" value=\"$numCols\">
      <table border=1 ncols=8>
      <tr>
      <th><font size=-1>#</font></th>
      <th><font size=-1>Column</font></th>
      <th><font size=-1>P. Key</font></th>
      <th><font size=-1>Type</font></th>
      <th><font size=-1>Precision</font></th>
      <th><font size=-1>Viewable</font></th>
      <th><font size=-1>Sortable</font></th>
      <th><font size=-1>Searchable</font></th>
      <th><font size=-1>Defaults</font></th>
      <th><font size=-1>Edit Field</font></th>
      <th><font size=-1>Select Options</font></th>
      </tr>"
;
      for(
$i=0; $i<$numCols; $i++ ) {
        
//echo "DBG: Name:" .$cols[$i]["name"].
        // " Type:" .$cols[$i]["type"]. " Prec:" .$cols[$i]["prec"]. "<br>";
        
echo "<tr>
        <td align=\"right\"><font size=-1><input type=\"hidden\" name=\""
.$i.
        
"\" value=\"" .$cols[$i]["name"]. "\">" .$i. "</font></td>
        <td><font size=-1>
         <input type=\"input\" size=15 value=\""
.$cols[$i]["name"].
         
"\" name=\"col_" .$i. "\"></font></td>";
        if(
$i == 0 ) {
          echo
"
        <td width=60><input type=\"radio\" name=\"primary\"
         value=\""
.$i. "\" checked></td>";
        }
        else {
          echo
"
        <td width=60><input type=\"radio\" name=\"primary\"
         value=\""
.$i. "\"></td>";
        }
        echo
"
        <td width=60><font size=-1><input type=\"hidden\" name=\"type_"
.$i.
         
"\" value=\"" .$cols[$i]["type"]. "\">" .$cols[$i]["type"].
        
"</font></td>
        <td align=\"right\"><font size=-1>
         <input type=\"input\" size=5 value=\""
         
.$cols[$i]["prec"]. "\" name=\"prec_" .$i. "\"></font></td>
        <td align=\"center\"><font size=-1>
         <input type=\"checkbox\" checked name=\"view_"
         
.$i. "\"></font></td>
        <td align=\"center\"><font size=-1>
         <input type=\"checkbox\" name=\"sort_"
         
.$i. "\"></font></td>
        <td align=\"center\"><font size=-1>
         <input type=\"checkbox\" name=\"search_"
         
.$i. "\"></font></td>

        <td width=100><font size=-1><input type=\"hidden\" name=\"def_"
.$i.
         
"\" value=\"" .$cols[$i]["default"]. "\">&nbsp;"
         
.$cols[$i]["default"]. "</font></td>

        <td align=\"center\"><font size=-1><select name=\"edit_"
.$i. "\">
         <option value=\"text\">text
         <option value=\"select\">select
         <option value=\"textarea\">textarea
         </select></font></td>
        <td><font size=-1>
         <input type=\"input\" size=30 name=\"opt_"
.$i. "\"></font></td>
        </tr>"
;
      }
      echo
"</table>
      <input type=\"reset\" name=\"reset\" value=\"Reset Form\">
      <input type=\"submit\" name=\"submit\" VALUE=\"Go!\">
      </form>"
;
    }
  }
?>

  <hr>
  <div class="footer">
  <address>
    <a href="mailto&#058;Denice&#046;Deatrich&#064;epfl.ch">Denice Deatrich</a>
  </address>
<!-- Created: Sat Feb 19 15:18:18 MET 2000 -->
<!-- hhmts start -->
Last modified: Sun Jul  2 16:48:16 MEST 2000
<!-- hhmts end -->
  </div>
</body>
</html>