<!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 Generation for MySQL Web Forms</h1>

<?php
  
if( ! file_exists("tmppath.php") ) {
    echo
"<em><font color=\"red\">
     Error: Programmer goofed up .. required file not found.
     </font></em><br><br>"
;
     exit;
  }
  require(
"tmppath.php" );

  
$files = array( USER_INPUT, CSS_TEMPLATE, CONNECT, PROC_ROW, APPLY_ROW );
  
$names = array( "page", "contact", "host", "db", "table", "user", "passwd" );
  
$fields = array( "name", "title", "type", "prec", "view", "sort", "search",
   
"edit", "opt" );

  
$arr["page"] = "";
  
$arr["contact"] = "";
  
$arr["host"] = "";
  
$arr["db"] = "";
  
$arr["table"] = "";
  
$arr["user"] = "";
  
$arr["passwd"] = "";

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

  
/** the column that serves as the primary key **/
  
$primaryKey = -1;

  
/**   Function : check HTTP post vars for anomolies
   **   Returns:   TRUE (ok) or FALSE (not...)
   **/
  
function checkPost()
  {
    global
$arr, $cols, $names, $HTTP_POST_VARS, $numCols, $primaryKey;
    
$res = false;
    if(
is_array($HTTP_POST_VARS) ) { // then we got here normally with POST
      
$more = true;
      while( (
$more) && (list($key, $val) = each($HTTP_POST_VARS)) ) {
        
//echo "DBG: <font color=\"green\">key: " .$key."\tvalue: " .$val.
        // "</font><BR>";
        
switch( $key ) {
        case
"page":    $arr["page"] = $val;    break;
        case
"contact": $arr["contact"] = $val; break;
        case
"db":      $arr["db"] = $val;      break;
        case
"table":   $arr["table"] = $val;   break;
        case
"host":    $arr["host"] = $val;    break;
        case
"user":    $arr["user"] = $val;    break;
        case
"passwd":  $arr["passwd"] = $val;  break;
        case
"numCols":
          
$numCols = $val;
          
/** at this point we should have all the info necessary to parse
            rest of post vars in the next section
           **/
          
$more = false;
          break;
        default:
          break;
        }
      }
      
// check post vars for completeness before continuing
      
$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 ) {
        
$ret = true; // then we can probably proceed with the rest of the vars
        
for( $i = 0; $i < $numCols; $i++ ) {  // init columns array
          
$cols[$i]["name"] = "";
          
$cols[$i]["title"] = "";
          
$cols[$i]["type"] = "";
          
$cols[$i]["prec"] = 10;
          
$cols[$i]["view"] = false;
          
$cols[$i]["sort"] = false;
          
$cols[$i]["search"] = false;
          
$cols[$i]["default"] = "";
          
$cols[$i]["edit"] = "";
          
$cols[$i]["opt"] = "";
        }
        
$index = -1;
        while( list(
$key, $val) = each($HTTP_POST_VARS) ) {
          
//echo "DBG: key: " .$key. "   value: " .$val. "<BR>";
          
if( (is_int($key)) && $key >= 0 && $key < $numCols ) {
            
$index = $key;
            
$cols[$index]["name"] = $val;
          }
          else {
            if(
ereg("^col_", $key) ) { // column title
              
$cols[$index]["title"] = $val;
            }
            else if(
ereg("^primary", $key) ) { // column title
              
$primaryKey = $val;
            }
            else if(
ereg("^type_", $key) ) { // column title
              
$cols[$index]["type"] = $val;
            }
            else if(
ereg("^prec_", $key) ) { // column title
              
$cols[$index]["prec"] = $val;
            }
            else if(
ereg("^view_", $key) ) { // column title
              
$cols[$index]["view"] = $val;
            }
            else if(
ereg("^sort_", $key) ) { // column title
              
$cols[$index]["sort"] = $val;
            }
            else if(
ereg("^search_", $key) ) { // column title
              
$cols[$index]["search"] = $val;
            }
            else if(
ereg("^def_", $key) ) { // column title
              
$cols[$index]["default"] = $val;
            }
            else if(
ereg("^edit_", $key) ) { // column title
              
$cols[$index]["edit"] = $val;
            }
            else if(
ereg("^opt_", $key) ) { // column title
              
$cols[$index]["opt"] = $val;
            }
          }
        }
// while 2nd pass thru post vars

        
$viewableFound = false;
        for(
$i = 0; $i < $numCols; $i++ ) {  // check for viewable cols!
          
if( $cols[$i]["view"] ) {
            
$viewableFound = true;
            break;
          }
        }
        if( !
$viewableFound ) {
          echo
"<font color=\"red\">Error: no viewable columns defined.
           </font><br>"
;
          
$ret = false;
        }
        if(
$primaryKey < 0 || $primaryKey > $numCols-1 ) {
          echo
"<font color=\"red\">Error: no sensible primary key defined.
           </font><br>"
;
          
$ret = false;
        }
        for(
$i = 0; $i < $numCols; $i++ ) {  // check for select options
          
if( strcmp($cols[$i]["edit"], "select") == 0 ) {
            if(
strlen($cols[$i]["opt"]) <= 0 ) {
              echo
"<font color=\"red\">
               Error: comma-seperated list of select options is required for "
               
.$cols[$i]["name"]. "</font><br>";
              
$ret = false;
              break;
            }
          }
        }
      }
// end-if okay to proceed
    
}// end-if post vars array exists
    
return $ret;
  }

  function
prepareApplyFile( $fd )
  {
    global
$arr, $table, $cols, $numCols, $primaryKey;

    
insertInto( $fd, $arr["page"], HEADER );

    
/**--------------------------------------------------------------------**/
    
$s = "
<h1>"
.$arr["page"]." : Modify/Delete Record</h1>
<?php
  global \$PHP_SELF;"
;
    
/**--------------------------------------------------------------------**/

    
fputs( $fd, $s, MAX_LINE_SZ );/**/

    /**--------------------------------------------------------------------**/
    
$s = "
  if( ! file_exists(\"connect.php\") ) {
    echo \"<em><font color=\\\"red\\\">
     Error: Database connect parameters not available. </font></em><br><br>\";
     exit;
  }
  require( \"connect.php\" );
  checkDBStatus( );\n"
;
    
/**--------------------------------------------------------------------**/

    
fputs( $fd, $s, MAX_LINE_SZ );/**/

    /**--------------------------------------------------------------------**/
    
$s = "
  \$okay = true; // loop control
  \$mstring = \"\"; // modify string
  \$istring = \"\"; // insert string
  \$vstring = \"\"; // value string
  while( \$okay && (list(\$key, \$val) =  each(\$HTTP_POST_VARS)) ) {
    // we should get the phpgen-* keys first; do parsing in two bits
    switch( \$key ) {
      case \"phpgen-option\":
        \$opt = \$val;
        if( strcmp(\$val, \"delete\")!= 0 && strcmp(\$val, \"modify\") != 0 &&
         strcmp(\$val, \"insert\") !=0 ) {
          echo \"<font color=\\\"red\\\">
           Note: Nothing to be done..</font><br>\";
          \$okay = false;
        }
        break;
      case \"phpgen-table\":
        \$tab = \$val;
        if( strlen(\$val) <= 0 ) {
          echo \"Programmer goof-up .. exitting <br>\";
          \$okay = false;
        }
        break;
      case \"phpgen-pkey\":
        \$pkey = \$val;
        if( strlen(\$pkey) <= 0 ) {
          echo \"Programmer goof-up .. exitting <br>\";
          \$okay = false;
        }
        break;
      default: // the rest of the POST-vars are column:data pairs
        if( strlen(\$val) <= 0 && strcmp(\$key, \$pkey) != 0 ) {
          echo \"<font color=\\\"red\\\">
           Error: Value required for column: <em>\"
           .\$key. \"</em></font><br>\";
           \$okay = false;
        }
        if( \$okay && strcmp(\$opt, \"delete\") != 0 &&
         strcmp(\$key, \$pkey) != 0 ) {
          // don't try to mod/ins prim. key
          if( strcmp(\$opt, \"modify\") == 0 ) {
            if( strlen(\$mstring) > 0 ) {
              \$mstring = \$mstring . \", \";
            }
            \$mstring = \$mstring .\$key. \"=\\\"\" .\$val. \"\\\"\";
          }
          else {
            if( strlen(\$vstring) > 0 ) {
              \$vstring = \$vstring . \", \";
            }
            \$vstring = \$vstring .\$key;
            if( strlen(\$istring) > 0 ) {
              \$istring = \$istring . \", \";
            }
            \$istring = \$istring . \"\\\"\" .\$val. \"\\\"\";
          }
        }
        break;
    }
  }
  if( \$argv ) { // then called with row # argument
    \$arg = \$argv[0];
  }"
;
  
/**----------------------------------------------------------------------**/

    
fputs( $fd, $s, MAX_LINE_SZ );/**/

    /**--------------------------------------------------------------------**/
    
$s = "
  if( \$okay && (\$link=mysql_pconnect(\$DB_HOST, \$DB_USER, \$DB_PW)) ) {
    if( \$opt == \"delete\" ) {
      \$q = \"delete from \" .\$tab. \" where \" .\$pkey. \" = \" .\$arg;
      if( \$res=mysql_db_query(\$DB_NAME, \$q, \$link) ) {
        echo \"<font color=\\\"green\\\">Delete successful for \" .\$pkey.
         \" = \" .\$arg. \"</font>\";
      }
      else {
        printError( \"DELETE\", \$link );
      }
    }
    else if( \$opt == \"modify\" ) {
      \$q = \"update \" .\$tab. \" set \" .\$mstring. \" where \" .\$pkey.
       \" = \" .\$arg;
      if( \$res=mysql_db_query(\$DB_NAME, \$q, \$link) ) {
        echo \"<font color=\\\"green\\\">Modification successful for \"
         .\$pkey. \" = \" .\$arg. \"</font>\";
      }
      else {
        printError( \"UPDATE\", \$link );
      }
    }
    else if( \$opt == \"insert\" ) {
      \$q = \"insert into \" .\$tab. \" (\" .\$vstring. \") values (\"
       .\$istring. \")\";
      if( \$res=mysql_db_query(\$DB_NAME, \$q, \$link) ) {
        echo \"<font color=\\\"green\\\">Insertion successful.</font>\";
      }
      else {
        printError( \"INSERT\", \$link );
      }
    }
  }
?>"
;
    
/**--------------------------------------------------------------------**/

    
fputs( $fd, $s, MAX_LINE_SZ );/**/

    
insertInto( $fd, $arr["contact"], TRAILER );
    
fclose( $fd );
  }

  function
prepareRowFile( $fd )
  {
    global
$arr, $table, $cols, $numCols, $primaryKey;

    
insertInto( $fd, $arr["page"], HEADER );

    
/**--------------------------------------------------------------------**/
    
$s = "
<h1>"
.$arr["page"]." : Browse Record</h1>
<?php
  global \$PHP_SELF;
  function showForm( \$data, \$arg )
  {
    echo \"
    <table width=300 cols=2>
    <tr>
      <td nowrap><em>Full record:</em>&nbsp;&nbsp;&nbsp;
      <font size=-1><input type=\\\"reset\\\"></font></td>
      <td nowrap>
      <font color=\\\"red\\\" size=-1><select name=\\\"phpgen-option\\\">
      <option value=\\\"-\\\">-\";
      if( \$arg ) { // then called with argument
        echo \"    <option value=\\\"modify\\\">Apply Modification
        <option value=\\\"delete\\\">Delete This Entry\";
      }
      else {
        echo \"    <option value=\\\"insert\\\">Insert this Entry into DB\";
      }\n"
;
      
/**------------------------------------------------------------------**/

    
fputs( $fd, $s, MAX_LINE_SZ );/**/

      /**------------------------------------------------------------------**/
    
$s = "    echo \"
      </select><input type=\\\"submit\\\" name=\\\"$arg\\\" value=\\\"Go\\\">
      </font></td>
    </tr>\";\n"
;
    
/**--------------------------------------------------------------------**/

    
fputs( $fd, $s, MAX_LINE_SZ );/**/
    /** now we need to loop thru the column defs and add them **/
    
for( $j = 0; $j < $numCols; $j++ ) {
      if(
strcmp($cols[$j]["edit"], "text") == 0 ) {

        
/**----------------------------------------------------------------**/
        
$s = "    echo \"    <tr><td nowrap>" . $cols[$j]["title"] .
         
"</td>\n    <td nowrap>" .
         
"<input name=\\\"" .$cols[$j]["name"]. "\\\" value=\\\"\" .
         \$data["
.$j. "].\"\\\" size=" . $cols[$j]["prec"].
         
"></td></tr>\n\";\n";
        
/**----------------------------------------------------------------**/

      
}
      else if(
strcmp($cols[$j]["edit"], "select") == 0 ) {
        
/** parse out select options here **/
        
$optsArr = explode(",", $cols[$j]["opt"]);  
        
$tmpstr = "\n    \$arr_$j = array(";
        while( list(
$k, $v) = each($optsArr) ) {
          
$tmpstr = $tmpstr . "\"$v\",";
        }
        
$tmpstr = substr( $tmpstr, 0, strlen($tmpstr)-1 );
        
$tmpstr = $tmpstr . ");\n";

        
/**----------------------------------------------------------------**/
        
$s = $tmpstr . "    echo \"    <tr><td nowrap>" . $cols[$j]["title"] .
         
"</td>\n    <td nowrap>" .
         
"<select name=\\\"" .$cols[$j]["name"]. "\\\">\";
         for( \$k=0; \$k < sizeof(\$arr_$j); \$k++ ) {
           \$this = \$arr_$j"
."["."\$k"."];
           echo \"<option value=\\\"\$this\\\"\";
           if( strcmp(\$this, \$data["
.$j. "] ) == 0 ) {
             echo \" selected>\$this\";
           }
           else {
             echo \">\$this\";
           }
         }
         echo \"</select></td></tr>\n\";\n"
;

        
/**----------------------------------------------------------------**/

      
}
      else if(
strcmp($cols[$j]["edit"], "textarea") == 0 ) {

        
/**----------------------------------------------------------------**/
        
$s = "    echo \"    <tr><td nowrap colspan=2>" . $cols[$j]["title"] .
         
"<BR><textarea name=\\\"". $cols[$j]["name"] .
         
"\\\" rows=10 cols=80>\" .\$data[" . $j .
         
"]. \"</textarea></td></tr>\";\n";
        
/**----------------------------------------------------------------**/

      
}
      
fputs( $fd, $s, MAX_LINE_SZ );
    }

    
/**--------------------------------------------------------------------**/
    
$s = "echo \"
    </table>\";
  }// end of function showForm()"
;
    
/**--------------------------------------------------------------------**/

    
fputs( $fd, $s, MAX_LINE_SZ );/**/

    /**--------------------------------------------------------------------**/
    
$s = "
  if( ! file_exists(\"connect.php\") ) {
    echo \"<em><font color=\\\"red\\\">
     Error: Database connect parameters not available. </font></em><br><br>\";
     exit;
  }
  require( \"connect.php\" );
  checkDBStatus( );\n"
;
    
/**--------------------------------------------------------------------**/

    
fputs( $fd, $s, MAX_LINE_SZ );/**/

    /**--------------------------------------------------------------------**/
    
$s = "
  if( \$argv ) { // then called with row # argument(s).
    \$arg = \$argv[0];
    if( (\$link=mysql_pconnect(\$DB_HOST, \$DB_USER, \$DB_PW)) ) {
      \$q = \"select * from "
. $table . " where " . $cols[0]["name"] .
       
" = \" . \$arg;
      //echo \"DBG: q is \" . \$q . \"<BR>\";
      if( \$res=mysql_db_query(\$DB_NAME, \$q, \$link) ) {
        if( \$row = mysql_fetch_array(\$res, MYSQL_NUM) ) {
          echo \"<form action=\\\"apply.php?\$arg\\\"
           method=\\\"post\\\">\";
          echo \"<input type=\\\"hidden\\\" name=\\\"phpgen-table\\\"
           value=\\\""
.$arr["table"]. "\\\">\";
          echo \"<input type=\\\"hidden\\\" name=\\\"phpgen-pkey\\\"
           value=\\\""
.$cols[$primaryKey]["name"]. "\\\">\";
          showForm( \$row, \$arg );
          echo \"</form>\";
        }
        else {
          printError( \"Fetch\", \$link );
        }
      }
      else {
        printError( \"Query\", \$link );
      }
    }
    else {
      printf( \"Connect: %s: %s<BR>\n\", mysql_errno(), mysql_error() );
    }
  }\n"
;
  
/**----------------------------------------------------------------------**/

    
fputs( $fd, $s, MAX_LINE_SZ );

    
/**--------------------------------------------------------------------**/
    
$s = "
  else { // then need default values for the INSERT option
    echo \"<form action=\\\"apply.php\\\" method=\\\"post\\\">\";
    echo \"<input type=\\\"hidden\\\" name=\\\"phpgen-table\\\"
     value=\\\""
.$arr["table"]. "\\\">\";
    echo \"<input type=\\\"hidden\\\" name=\\\"phpgen-pkey\\\"
     value=\\\""
.$cols[$primaryKey]["name"]. "\\\">\";\n";
    
/**--------------------------------------------------------------------**/

    
$defArr = "     \$arr_default = array(";
    for(
$i = 0; $i < $numCols; $i++ ) {
      
$defArr  = $defArr . "\"" .$cols[$i]["default"]. "\",";
    }
    
$defArr = substr( $defArr, 0, strlen($defArr)-1 );
    
$defArr = $defArr . ");\n";

    
/**--------------------------------------------------------------------**/
    
$s = $s . $defArr . "
    \$row = \$arr_default;

    showForm( \$row, \$arg );
    echo \"</form>\";
  }\n

?>\n"
;
  
/**----------------------------------------------------------------------**/

    
fputs( $fd, $s, MAX_LINE_SZ );

    
insertInto( $fd, $arr["contact"], TRAILER );
    
fclose( $fd );
  }

  
/********************************************************************/
  /** main begins .. to ease the pain, exit on error conditions asap **/
  
if( ! checkPost() ) {
    exit;
  }
  
/** okay, lets generate some PHP into files.. **/
  
$dir = buildTmpPath( );  // generate tmp pathname; top of download dir.

  
$f0 = $dir . "/" . USER_INPUT;
  if( (
strlen($f0) <= 0) || ($phpfd = fopen($f0, "w+")) == false ) {
    echo
"<font color=\"red\">Error: cannot open .basename($f0).
    for writing.</font><br>"
;
    exit;
  }
  
  
reset( $names );
  while( list(
$k, $v) = each($names) ) {
    
$s = $v. ": " .$arr[$v]. "\n";
    
fputs( $phpfd, $s, MAX_LINE_SZ );
  }
  
$s = "numCols: " .$numCols. "\n\n";
  
fputs( $phpfd, $s, MAX_LINE_SZ );

  for(
$i = 0; $i < $numCols; $i++ ) {
    
reset( $fields );
    while( list(
$k, $v) = each($fields) ) {
      
$s = $v. ": " .$cols[$i][$v]. "\n";
      
fputs( $phpfd, $s, MAX_LINE_SZ );
    }
    
fputs( $phpfd, "\n", MAX_LINE_SZ );
  }
  
fclose( $phpfd );

  
$f1 = $dir . "/" . PROC_ROW;
  if( (
strlen($f1) <= 0) || ($phpfd = fopen($f1, "w+")) == false ) {
    echo
"<font color=\"red\">Error: cannot open .basename($f1).
    for writing.</font><br>"
;
    exit;
  }
  
prepareRowFile( $phpfd );

  
$f2 = $dir . "/" . APPLY_ROW;
  if( (
strlen($f2) <= 0) || ($phpfd = fopen($f2, "w+")) == false ) {
    echo
"<font color=\"red\">Error: cannot open .basename($f2).
    for writing.</font><br>"
;
    exit;
  }
  
prepareApplyFile( $phpfd );

  
$f4 = $dir . "/" . $arr["table"] . ".phps";
  if( (
strlen($f4) <= 0) || ($phpfd = fopen($f4, "w+")) == false ) {
    echo
"<font color=\"red\">Error: cannot open " .$f4. " for writing.</font>
    <br>"
;
    exit;
  }

  
array_push( $files, basename($f4) );
  
insertInto( $phpfd, $arr["page"], HEADER );

  
/** okay, the fun begins .. start filling buffer to insert into php file **/
  /**----------------------------------------------------------------------**/
  
$s = "
<h1>"
.$arr["page"]."</h1>
<?php
  global \$PHP_SELF;

  if( ! file_exists(\"connect.php\") ) {
    echo \"<em><font color=\\\"red\\\">
     Error: Database connect parameters not available. </font></em><br><br>\";
     exit;
  }
  require( \"connect.php\" );
  checkDBStatus( );"
;
  
/**----------------------------------------------------------------------**/

  
fputs( $phpfd, $s, MAX_LINE_SZ );/**/

  /**----------------------------------------------------------------------**/
  
$s = "
  if( is_array(\$HTTP_POST_VARS) ) { // then sort or search argument(s).
    \$i = 0;
    while( list(\$key, \$val) = each(\$HTTP_POST_VARS) ) {
      //echo \"DBG: key:val is \" .\$key. \":\" .\$val. \"<BR>\";
      // process each POST variable argument
      if( \$key == \"sort\" && strlen(\$val) > 0 ) {
        \$sortkey = \$val;
      }
      if( \$key == \"search\" && strlen(\$val) > 0 ) {
        \$searchkey = \$val;
      }
      \$i++;
    }
  }"
;
  
/**----------------------------------------------------------------------**/

  
fputs( $phpfd, $s, MAX_LINE_SZ );/**/

  /**----------------------------------------------------------------------**/
  
$s = "
  if( (\$link=mysql_pconnect(\$DB_HOST, \$DB_USER, \$DB_PW)) ) {
    \$q = \"select * from "
. $table . "\";

    if( (is_string(\$searchkey)) && strlen(\$searchkey) > 0 ) {
      // then add a keyword search clause
      \$q = \$q . \" where \" . \$searchColumn . \" like \\\"%\" .
       \$searchkey .\"%\\\"\";
    }
    if( (is_string(\$sortkey)) && strlen(\$sortkey) > 0 ) {
      // then add the required sort specification
      \$q = \$q . \" order by \" . \$sortkey;
    }

    \$n = 0;
    //echo \"q is \" . \$q . \"<BR>\";
    if( \$res=mysql_db_query(\$DB_NAME, \$q, \$link) ) {
      \$nrows = mysql_num_rows(\$res);
      while( (\$row=mysql_fetch_array(\$res, MYSQL_NUM)) ) {
        \$data[\$n] = \$row;
        \$n++;
      }
      mysql_free_result( \$res );
    }
    else {
      printError( \"QUERY\", \$link );
      exit;
    }
  }
  else {
    echo mysql_errno().\": \".mysql_error().\"<BR>\";
    exit;
  }"
;
  
/**----------------------------------------------------------------------**/

  
fputs( $phpfd, $s, MAX_LINE_SZ );/**/

  /**----------------------------------------------------------------------**/
  
$s = "
  if( \$n <= 0 ) {
    echo \"<em><font color=\\\"red\\\">No more rows or no qualifying rows
    </font></em><br><br>\";
  }
  else {
    echo \"<form action=\\\"\$PHP_SELF\\\" method=\\\"post\\\">\";\n"
;
  
/**----------------------------------------------------------------------**/

  
fputs( $phpfd, $s, MAX_LINE_SZ );/**/

  /** we need to know which columns are visible .. store it in an array **/
  
$numColsVisible = 0;
  for(
$i = 0; $i < $numCols; $i++ ) {
    if(
$cols[$i]["view"]) {
      
$visible[$numColsVisible] = $i;
      
$numColsVisible++;
    }
  }
  
$numColsSearchable = 0; // which columns are searchable
  
$searchArr = "    \$arr_search = array(";
  for(
$i = 0; $i < $numCols; $i++ ) {
    if(
$cols[$i]["search"]) {
      
$searchable[$numColsSearchable] = $i;
      
$searchArr = $searchArr . "\"" .$cols[$i]["name"]. "\",";
      
$numColsSearchable++;
    }
  }
  
$searchArr = substr( $searchArr, 0, strlen($searchArr)-1 );
  
$searchArr = $searchArr . ");\n";

  
$numColsSortable = 0; // which columns are sortable
  
for( $i = 0; $i < $numCols; $i++ ) {
    if(
$cols[$i]["sort"]) {
      
$sortable[$numColsSortable] = $i;
      
$numColsSortable++;
    }
  }

  if(
$numColsSearchable > 0 ) {

    
/**--------------------------------------------------------------------**/
    
$s = "
    echo \"<font size=-1>Search:
    <input type=\\\"input\\\" size=30 name=\\\"search\\\"\";
    if( strlen(\$searchkey) > 0 ) {
      echo \" value=\\\"\$searchkey\\\" \";
    }
    echo \">
    <select name=\\\"searchColumn\\\">\";\n"
;
    
/**--------------------------------------------------------------------**/

    
fputs( $phpfd, $s, MAX_LINE_SZ );/**/

    //echo "DBG: search arr is " .$searchArr. "<BR>";

    /**--------------------------------------------------------------------**/
    
$s = $searchArr . "
    for( \$k=0; \$k < sizeof(\$arr_search); \$k++ ) {
      \$this = \$arr_search[\$k];
      echo \"<option value=\\\"\$this\\\"\";
      if( strlen(\$searchkey) > 0 &&
       strcmp(\$searchColumn, \$this ) == 0 ) {
        echo \" selected>\$this\";
      }
      else {
        echo \">\$this\";
      }
    }
    echo \"</select></font>\";\n"
;
    
/**--------------------------------------------------------------------**/

    
fputs( $phpfd, $s, MAX_LINE_SZ );
  }

  
/**----------------------------------------------------------------------**/
  
$s = "    echo \"<table border=1><tr>
    <th width=30 nowrap>#</th>\";\n"
;
  
/**----------------------------------------------------------------------**/

  
fputs( $phpfd, $s, MAX_LINE_SZ );/**/
  
for( $j = 0; $j < $numColsVisible; $j++ ) {
    if(
$cols[$visible[$j]]["sort"]) {

      
/**------------------------------------------------------------------**/
      
$s = "    echo \"<th width=" . ($cols[$visible[$j]]["prec"])*6 .
      
" nowrap><input type =\\\"submit\\\" name=\\\"sort\\\" value=\\\"" .
      
$cols[$visible[$j]]["name"]. "\\\"></th>\";\n";
      
/**------------------------------------------------------------------**/

    
}
    else {

      
/**------------------------------------------------------------------**/
      
$s = "    echo \"<th width=" . ($cols[$visible[$j]]["prec"])*6 .
      
" nowrap>" . $cols[$visible[$j]]["title"]. "</th>\";\n";
      
/**------------------------------------------------------------------**/

    
}
    
fputs( $phpfd, $s, MAX_LINE_SZ );
  }

  
/**----------------------------------------------------------------------**/
  
$s = "    echo \"  </tr></table></form>\n  <table border=1>\";";
  
/**----------------------------------------------------------------------**/

  
fputs( $phpfd, $s, MAX_LINE_SZ );/**/

  /**----------------------------------------------------------------------**/
  
$s = "
    for( \$i = 0; \$i < \$n; \$i++ ) {
      // first column is just the row number
      \$row = \$data[\$i][0];
      echo \"    <tr><td width=30 align=\\\"right\\\" nowrap><font size=-1>\"
       . \"<A target=\\\"_blank\\\" HREF=\\\"process_row.php?\$row\\\">\"
       . (\$i+1) . \"</A></font></td>\n\";\n"
;
  
/**----------------------------------------------------------------------**/

  
fputs( $phpfd, $s, MAX_LINE_SZ );/**/
  
for( $j = 0; $j < $numCols; $j++ ) {
    if(
$cols[$j]["view"]) {
      
$arrString = "\$data[\$i][".$j."]";

      
/**------------------------------------------------------------------**/
      
$s = "    echo \"    <td width=" . ($cols[$j]["prec"])*6 .
       
" nowrap><font size=-1>\" .$arrString. \"</font></td>\";\n";
      
/**------------------------------------------------------------------**/

      
fputs( $phpfd, $s, MAX_LINE_SZ );
    }
  }

  
/**----------------------------------------------------------------------**/
  
$s = "    echo \"</tr>\n\";
    }\n"
;
  
/**----------------------------------------------------------------------**/

  
fputs( $phpfd, $s, MAX_LINE_SZ );

  
/**----------------------------------------------------------------------**/
  
$s = "    echo \"
    </table>
    <form target=\\\"_blank\\\"
    action=\\\"process_row.php\\\" method=\\\"post\\\">
    <input type=\\\"submit\\\" name=\\\"insert\\\"
     VALUE=\\\"Insert New Data...\\\">
    </form>\";
  }
?>"
;
  
/**----------------------------------------------------------------------**/

  
fputs( $phpfd, $s, MAX_LINE_SZ );

  
insertInto( $phpfd, $arr["contact"], TRAILER );
  
fclose( $phpfd );

  
/** next prep files for user upload **/

  // convert web-path to user-accessible format:
  
$access = DOWNLOADS. "/" .basename(dirname($f4));

  
$f0 = $access . "/" . basename($f0);
  
$f1 = $access . "/" . basename($f1);
  
$f2 = $access . "/" . basename($f2);
  
$f4 = $access . "/" . basename($f4);
  
$f5 = $access. "/" .CSS_TEMPLATE;
  
$f6 = $access. "/" .CONNECT;


  
// copy style sheet:
  
if( ! copy(CSS_TEMPLATE, $dir. "/" .CSS_TEMPLATE) ) {
    echo
"<font color=\"red\">Error: cannot copy " .CSS_TEMPLATE.
     
" for download.</font>";
    exit;
  }
  
$r = CONNECT_TEMPLATE;
  
$w = $dir . "/" . CONNECT;
  if( !(
$src = fopen($r, "r")) || !($dest = fopen($w, "w+")) ) {
    echo
"<font color=\"red\">Error: cannot open " .CONNECT_TEMPLATE.
     
" for copying.</font>";
    exit;
  }
  while(
$s = fgets($src, MAX_LINE_SZ) ) { // for each line
    
if( ereg("^.DB_HOST", $s) ) {
      
$s = "\$DB_HOST = \"" .$arr["host"]. "\";\n";
    }
    else if(
ereg("^.DB_USER", $s) ) {
      
$s = "\$DB_USER = \"" .$arr["user"]. "\";\n";
    }
    else if(
ereg("^.DB_PW", $s) ) {
      
$s = "\$DB_PW = \"" .$arr["passwd"]. "\";\n";
    }
    else if(
ereg("^.DB_NAME", $s) ) {
      
$s = "\$DB_NAME = \"" .$arr["db"]. "\";\n";
    }
    
fwrite( $dest, $s, MAX_LINE_SZ );
  }
  
fclose( $src );
  
fclose( $dest );

  echo
"
  <div class=\"note\">
  <p>The files are presented individually for viewing.
  The best way to save them is to download the tarball.
  <p> Generated files are removed from the server very frequently
  (about every 5 minutes..)
  <p>
  </div>
  <table border=1 align=\"center\" width=95% cols=2>
  <tr>
  <th width=30%><font color=\"red\">File Name</font></th>
  <th width=60%><font color=\"red\">Purpose</font></th>
  </tr>
  <tr><td><A HREF=\"$f0\">"
.basename($f0)."</A></td>
  <td>A Record of your input for this table (perhaps to be used for upload
   for subsequent versions of this PHP generator).</td></tr>
  <tr><td><A HREF=\"$f5\">"
.basename($f5). "</A></td>
  <td>The style sheet referenced in the PHP code.  You can edit it to
   customize the appearance of your web forms.</td></tr>
  <tr><td><A HREF=\"$f6\">"
.basename($f6). "</A></td>
  <td>The PHP Database connect data that you must rename to
   <em>connect.php</em> (and probably edit).</td></tr>
  <tr><td><A HREF=\"$f4\">"
.basename($f4). "</A></td>
  <td>The main PHP code</td></tr>
  <tr><td><A HREF=\"$f1\">"
.basename($f1). "</A></td>
  <td>PHP script for processing a single table row.
   It is referenced by the main PHP script above.</td></tr>
  <tr><td><A HREF=\"$f2\">"
.basename($f2). "</A></td>
  <td>PHP script for modifying or deleting a table row.
   It is referenced by the row-processing file 'process_row.php'.</td></tr>
  "
;

  
$f7 = $dir . "/" . TAR_BALL;
  if(
chdir($dir) ) {
    
reset( $files );
    
$okay = true;
    
$cmdline = "";
    while(
$okay && (list($k, $v) = each($files)) ) {
      
chmod( $v, 0644 );
      if(
ereg(".phps$", $v) ) {
        
$symfile = substr( $v, 0, strlen($v)-1 );
        
$cmdline = $cmdline . " " . $symfile;
        if( !
symlink($v, $symfile) ) {
          
$okay = $false;
        }
      }
      else {
        
$cmdline = $cmdline . " " . $v;
      }
    }
    if(
$okay ) {
      
/** fork the 'tar' command to provide compressed tarball **/
      
$cmd = EscapeShellCmd( "/bin/tar -zhcf $f7 " . $cmdline );
      if(
exec($cmd, $ar, $ret) != 0 ) {
        
$okay = false;
      }
    }
  }
  if(
$okay ) { // then add the tarball as the last table row
    
$f7 = $access. "/" .TAR_BALL;
    echo
"
    <tr bgcolor=\"yellow\">
    <td><b><A HREF=\"$f7\">"
.basename($f7). "</A></b></td>
    <td>All the above files as a GNU-zipped tar file</td></tr>"
;
  }
  echo
"
  </table>\n"
;

?>

  <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 17:40:12 MEST 2000
<!-- hhmts end -->
  </div>
</body>
</html>