<?   
    /*************************************************************************************  
     */tSQLAdmin  v2.0  -  An  SQL  Administration  User  Interface  for  the  Web *   
     */t    Copyright  (C)  1997-98    Alessandro  Vernet  <avernet@scdi.org>      *   
     *************************************************************************************   
     *      This  library  is  free  software;  you  can  redistribute  it  and/or       *   
     *      modify  it  under  the  terms  of  the  GNU  Library  General  Public/t*   
     *      License  as  published  by  the  Free  Software  Foundation;  either/t *   
     *      version  2  of  the  License,  or  (at  your  option)  any  later  version.  *   
     */t/t/t/t/t/t/t/t/t/t   *   
     *      This  library  is  distributed  in  the  hope  that  it  will  be  useful,   *   
     *      but  WITHOUT  ANY  WARRANTY;  without  even  the  implied  warranty  of      *   
     *      MERCHANTABILITY  or  FITNESS  FOR  A  PARTICULAR  PURPOSE.    See  the  GNU  *   
     *      Library  General  Public  License  for  more  details./t/t       *   
     */t/t/t/t/t/t/t/t/t/t   *   
     *      You  should  have  received  a  copy  of  the  GNU  Library  General  Public *   
     *      License  along  with  this  library;  if  not,  write  to  the/t       *   
     *      Free  Software  Foundation,  Inc.,  59  Temple  Place  -  Suite  330,/t*   
     *      Boston,  MA    02111-1307,  USA./t/t/t/t/t     *   
     *************************************************************************************/   
    /*  TODO:   
      *  -  Add  sort  order.   
      *  -  Add  simple  view.   
      *  -  Add  some  documentation.   
      */   
    /*  LIMITATIONS:   
      *  -  Works  only  with  mSQL.   
      */   
    /*  HISTORY:   
      *  -  97-11-05  (avernet)  Corrected  a  bug  with  quote.   
      *  -  98-01-01  (avernet)  Added  a  sortColumn  parameter  to   
      *      administrationTable  function.   
      *  -  98-03-14  (avernet)  Added  function  addTable  to  enable  users  to   
      *      add  (but  not  modify)  en  entry  to  the  database.   
      *  -  98-05-19  (avernet)  Submitted  to  PX.   
      *  -  98-10-11  (avernet)  Now  SQLAdmin  works  with  PHP3.  The  PHP2  version   
      *      will  not  be  mainteained  anymore.   
      *  -  98-10-11  (avernet)  SQLAdmin  is  now  distributed  under  the  LGPL   
      *      instead  of  MPL.   
      */   
    function  escapeforhtml  ($string)   
    {   
/t$result  =  $string;   
/t//$result  =  ereg_replace  ("/"",  """,  $result);   
/t$result  =  ereg_replace  ("<",  "<",  $result);   
/t$result  =  ereg_replace  (">",  ">",  $result);   
/treturn  $result;   
    }   
    function  displayTuple  ($fieldsNumber,  $fieldNames,   
/t/t/t/t/t/t  $fieldLengths,  $values,  $mode)   
    {   
/t$result  =  "";   
/t$result  .=  "<FORM  METHOD=/"post/"><TABLE  BORDER><TR>"  .   
/t    "<TD  BGCOLOR=/"#CCCCFF/">";   
/t$result  .=  "<TABLE  CELLSPACING=/"0/"  CELLPADDING=/"0/">";   
/t$fieldIndex  =  0;   
/twhile  ($fieldIndex  <  $fieldsNumber)   
/t{   
/t    $result  .=  "<TR><TD>"  .  $fieldNames  [$fieldIndex]  .  "</TD><TD>";   
/t    if  ($fieldLengths  [$fieldIndex]  <=  128)   
/t    {   
/t/t$result  .=  "<INPUT  TYPE=/"text/"  NAME=/""  .   
/t/t    $fieldNames  [$fieldIndex]  .  "/"  VALUE=/""  .   
/t/t    $values  [$fieldIndex]  .  "/"  SIZE=/"64/">";   
/t    }   
/t    else   
/t    {   
/t/t$result  .=  "<TEXTAREA  NAME=/""  .   
/t/t    $fieldNames  [$fieldIndex]  .  "/""  .   
/t/t    "  COLS=/"64/"  ROWS=/"10/"  WRAP=/"virtual/">"  .   
/t/t    escapeforhtml  ($values  [$fieldIndex])  .  "</TEXTAREA>";   
/t    }   
/t    $result  .=    "<INPUT  TYPE=/"hidden/"  NAME=/"old-"  .   
/t/t$fieldNames  [$fieldIndex]  .   
/t/t"/"  VALUE=/""  .  escapeforhtml  ($values  [$fieldIndex])  .  "/">"  .   
/t/t"</TD></TR>";   
/t    $fieldIndex++;   
/t}   
/t$result  .=  "<TR><TD  ALIGN=/"center/"  COLSPAN=/"2/">";   
/tif  ($mode  ==  "modify")   
/t{   
/t    $result  .=  "<INPUT  TYPE=/"submit/"  NAME=/"remove/"  VALUE=/"Remove/">";   
/t    $result  .=  "<INPUT  TYPE=/"submit/"  NAME=/"update/"  VALUE=/"Update/">";   
/t}   
/telse   
/t    {  $result  .=  "<INPUT  TYPE=/"submit/"  NAME=/"add/"  VALUE=/"Add/">";  }   
/t$result  .=  "</TABLE></TD></TR></TABLE></FORM>";   
/treturn  $result;   
    }   
    function  fieldFromType  ($text,  $type)   
    {   
/tif  ($type  ==  "int"  ||  $type  ==  "uint"  ||  $type  == "real")   
/t    {  $result  =  $text;  }   
/telse   
/t    {  $result  =  "'"  .  AddSlashes  ($text)  .  "'";  }   
/treturn  $result;   
    }   
    function  executeMsql  ($database,  $command)   
    {   
/t/*echo  "<TT>"  .  $command  .  "</TT><HR>";*/   
/tmsql  ($database,  $command);   
    }   
    function  handleRemove  ($database,  $table,  $fieldsNumber,   
/t/t/t/t/t/t  $fieldNames,  $fieldLengths,  $fieldTypes)   
    {   
/tglobal  $remove;   
/tif  ($remove  !=  "")   
/t{   
/t    $command  =  "DELETE  FROM  "  .  $table  .  "  WHERE  ";   
/t    $fieldIndex  =  0;   
/t    while  ($fieldIndex  <  $fieldsNumber)   
/t    {   
/t/t$fieldName  =  "old-"  .  $fieldNames  [$fieldIndex];   
/t/tglobal  $$fieldName;   
/t/t$command  .=  $fieldNames  [$fieldIndex]  .  "="  .   
/t/t    fieldFromType  ($$fieldName,  $fieldTypes  [$fieldIndex]);   
/t/tif  ($fieldIndex  !=  $fieldsNumber  -  1)   
/t/t    {  $command  .=  "  AND  ";  }   
/t/t$fieldIndex++;   
/t    }/t/t  
/t    executeMsql  ($database,  $command);   
/t}   
    }   
    function  handleUpdate  ($database,  $table,  $fieldsNumber,   
/t/t/t/t/t/t  $fieldNames,  $fieldLengths,  $fieldTypes)   
    {   
/tglobal  $update;   
/tif  ($update  !=  "")   
/t{   
/t    $command  =  "UPDATE  "  .  $table  .  "  SET  ";   
/t    $fieldIndex  =  0;   
/t    while  ($fieldIndex  <  $fieldsNumber)   
/t    {   
/t/t$fieldName  =  $fieldNames  [$fieldIndex];   
/t/tglobal  $$fieldName;   
/t/t$command  .=  $fieldName  .  "="  .   
/t/t    fieldFromType  ($$fieldName,  $fieldTypes  [$fieldIndex]);   
/t/tif  ($fieldIndex  !=  $fieldsNumber  -  1)   
/t/t    {  $command  .=  ",  ";  }   
/t/t$fieldIndex++;   
/t    }   
/t    $command  .=  "  WHERE  ";   
/t    $fieldIndex  =  0;   
/t    while  ($fieldIndex  <  $fieldsNumber)   
/t    {   
/t/t$fieldName  =  "old-"  .  $fieldNames  [$fieldIndex];   
/t/tglobal  $$fieldName;   
/t/t$command  .=  $fieldNames  [$fieldIndex]  .  "="  .   
/t/t    fieldFromType  ($$fieldName,  $fieldTypes  [$fieldIndex]);   
/t/tif  ($fieldIndex  !=  $fieldsNumber  -  1)   
/t/t    {  $command  .=  "  AND  ";  }   
/t/t$fieldIndex++;   
/t    }   
/t    executeMsql  ($database,  $command);   
/t}   
    }   
    function  handleAdd  ($database,  $table,  $fieldsNumber,   
/t/t/t/t/t    $fieldNames,  $fieldLengths,  $fieldTypes)   
    {   
/tglobal  $add;   
/tif  ($add  !=  "")   
/t{   
/t    $command  =  "INSERT  INTO  "  .  $table  .  "  (";   
/t    $fieldIndex  =  0;   
/t    while  ($fieldIndex  <  $fieldsNumber)   
/t    {   
/t/t$command  .=  $fieldNames  [$fieldIndex];   
/t/tif  ($fieldIndex  !=  $fieldsNumber  -  1)   
/t/t    {  $command  .=  ",  ";  }   
/t/t$fieldIndex++;   
/t    }   
/t    $command  .=  ")  VALUES  (";   
/t    $fieldIndex  =  0;   
/t    while  ($fieldIndex  <  $fieldsNumber)   
/t    {   
/t/t$fieldName  =  $fieldNames  [$fieldIndex];   
/t/tglobal  $$fieldName;   
/t/t$command  .=  fieldFromType  ($$fieldName,  $fieldTypes  [$fieldIndex]);   
/t/tif  ($fieldIndex  !=  $fieldsNumber  -  1)   
/t/t    {  $command  .=  ",  ";  }   
/t/t$fieldIndex++;   
/t    }   
/t    $command  .=  ")";   
/t    executeMsql  ($database,  $command);   
/t}   
    }   
    function  displayRemoveUpdate  ($database,  $table,  $sortColumn,   
/t/t/t/t/t/t/t/t$fieldsNumber,  $fieldNames,  $fieldLengths)   
    {   
/t$result  =  "";   
/tif  ($sortColumn  !=  "")   
/t    {  $sortColumn  =  "  ORDER  BY  "  .  $sortColumn;  }   
/t$msqlresult  =  msql  ($database,  "SELECT  *  FROM  "  .  $table  .  $sortColumn);   
/t$tuplesNumber  =  msql_numrows  ($msqlresult);   
/t$tupleIndex  =  0;   
/twhile  ($tupleIndex  <  $tuplesNumber)   
/t{   
/t    $fieldIndex  =  0;   
/t    while  ($fieldIndex  <  $fieldsNumber)   
/t    {   
/t/t$values  [$fieldIndex]  =  msql_result  ($msqlresult,  $tupleIndex,   
/t/t    $fieldNames  [$fieldIndex]);   
/t/t$fieldIndex++;   
/t    }   
/t    $result  .=  displayTuple  ($fieldsNumber,  $fieldNames,   
/t/t$fieldLengths,  $values,  "modify");   
/t    $tupleIndex++;   
/t}   
/treturn  $result;   
    }   
    function  displayAdd  ($fieldsNumber,  $fieldNames,  $fieldLengths)   
    {   
/t$result  =  "";   
/t$fieldIndex  =  0;   
/twhile  ($fieldIndex  <  $fieldsNumber)   
/t{   
/t    $values  [$fieldIndex]  =  "";   
/t    $fieldIndex++;   
/t}   
/t$result  .=  displayTuple  ($fieldsNumber,  $fieldNames,   
/t    $fieldLengths,  $values,  "add");   
/tmsql_close  ();   
/treturn  $result;   
    }   
    function  administrationTable  ($database,  $table,  $sortColumn)   
    {   
/t$result  =  "";   
/tmsql_connect  ( "localhost");   
/t$msqlresult  =  msql  ($database,  "SELECT  *  FROM  "  .  $table);   
/t$fieldsNumber  =  msql_numfields  ($msqlresult);    
/t$msqlresult  =  msql_listfields  ($database,  $table);   
/t$fieldIndex  =  0;   
/twhile  ($fieldIndex  <  $fieldsNumber)   
/t{   
/t    $fieldNames  [$fieldIndex]  =  msql_fieldname  ($msqlresult,  $fieldIndex);   
/t    $fieldLengths  [$fieldIndex]  =  msql_fieldlen  ($msqlresult,  $fieldIndex);   
/t    $fieldTypes  [$fieldIndex]  =  msql_fieldtype  ($msqlresult,  $fieldIndex);   
/t    $fieldIndex++;   
/t}   
/thandleRemove  ($database,  $table,  $fieldsNumber,  $fieldNames,  $fieldLengths,  $fieldTypes);   
/thandleUpdate  ($database,  $table,  $fieldsNumber,  $fieldNames,  $fieldLengths,  $fieldTypes);   
/thandleAdd  ($database,  $table,  $fieldsNumber,  $fieldNames,  $fieldLengths,  $fieldTypes);   
/t$result  .=  displayRemoveUpdate  ($database,  $table,  $sortColumn,  $fieldsNumber,  $fieldNames,    
$fieldLengths);   
/t$result  .=  displayAdd  ($fieldsNumber,  $fieldNames,  $fieldLengths);   
/treturn  $result;   
    }   
    function  addTable  ($database,  $table)   
    {   
/t$result  =  "";   
/tmsql_connect  ( "localhost");   
/t$msqlresult  =  msql  ($database,  "SELECT  *  FROM  "  .  $table);   
/t$fieldsNumber  =  msql_numfields  ($msqlresult);    
/t$msqlresult  =  msql_listfields  ($database,  $table);   
/t$fieldIndex  =  0;   
/twhile  ($fieldIndex  <  $fieldsNumber)   
/t{   
/t    $fieldNames  [$fieldIndex]  =  msql_fieldname  ($msqlresult,  $fieldIndex);   
/t    $fieldLengths  [$fieldIndex]  =  msql_fieldlen  ($msqlresult,  $fieldIndex);   
/t    $fieldTypes  [$fieldIndex]  =  msql_fieldtype  ($msqlresult,  $fieldIndex);   
/t    $fieldIndex++;   
/t}   
/thandleAdd  ($database,  $table,  $fieldsNumber,  $fieldNames,  $fieldLengths,  $fieldTypes);   
/t$result  .=  displayAdd  ($fieldsNumber,  $fieldNames,  $fieldLengths);   
/treturn  $result;   
    }   
?>