home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / sqladm.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  10.4 KB  |  287 lines

  1. SQLAdmin for PHP3 
  2.  
  3. An SQL Administration User Interface for the Web. 
  4.  
  5.  
  6. <? 
  7.    /*************************************************************************** 
  8.    *    SQLAdmin v2.0 - An SQL Administration User Interface for the Web     * 
  9.    *      Copyright (C) 1997-98  Alessandro Vernet <avernet@scdi.org>        * 
  10.    *************************************************************************** 
  11.    *   This library is free software; you can redistribute it and/or         * 
  12.    *   modify it under the terms of the GNU Library General Public           * 
  13.    *   License as published by the Free Software Foundation; either          * 
  14.    *   version 2 of the License, or (at your option) any later version.      * 
  15.    *                                                                         * 
  16.    *   This library is distributed in the hope that it will be useful,       * 
  17.    *   but WITHOUT ANY WARRANTY; without even the implied warranty of        * 
  18.    *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     * 
  19.    *   Library General Public License for more details.                      * 
  20.    *                                                                         * 
  21.    *   You should have received a copy of the GNU Library General Public     * 
  22.    *   License along with this library; if not, write to the                 * 
  23.    *   Free Software Foundation, Inc., 59 Temple Place - Suite 330,          * 
  24.    *   Boston, MA  02111-1307, USA.                                          * 
  25.    ***************************************************************************/ 
  26.  
  27.  
  28.    /* TODO: 
  29.    * - Add sort order. 
  30.    * - Add simple view. 
  31.    * - Add some documentation. 
  32.    */ 
  33.  
  34.    /* LIMITATIONS: 
  35.    * - Works only with mSQL. 
  36.    */ 
  37.  
  38.    /* HISTORY: 
  39.    * - 97-11-05 (avernet) Corrected a bug with quote. 
  40.    * - 98-01-01 (avernet) Added a sortColumn parameter to 
  41.    *   administrationTable function. 
  42.    * - 98-03-14 (avernet) Added function addTable to enable users to 
  43.    *   add (but not modify) en entry to the database. 
  44.    * - 98-05-19 (avernet) Submitted to PX. 
  45.    * - 98-10-11 (avernet) Now SQLAdmin works with PHP3. The PHP2 version 
  46.    *   will not be mainteained anymore. 
  47.    * - 98-10-11 (avernet) SQLAdmin is now distributed under the LGPL 
  48.    *   instead of MPL. 
  49.    */ 
  50.  
  51.   function escapeforhtml ($string) 
  52.   { 
  53.     $result = $string; 
  54.     $result = ereg_replace ( "\"",  """, $result); 
  55.     $result = ereg_replace ( "<",  "<", $result); 
  56.     $result = ereg_replace ( ">",  ">", $result); 
  57.     return $result; 
  58.   } 
  59.  
  60.   function displayTuple ($fieldsNumber, $fieldNames, 
  61.                          $fieldLengths, $values, $mode) 
  62.   { 
  63.     $result =  ""; 
  64.     $result .=  "<FORM METHOD=\"post\"><TABLE BORDER><TR>" . 
  65.        "<TD BGCOLOR=\"#CCCCFF\">"; 
  66.     $result .=  "<TABLE CELLSPACING=\"0\" CELLPADDING=\"0\">"; 
  67.     $fieldIndex = 0; 
  68.     while ($fieldIndex < $fieldsNumber) 
  69.     { 
  70.       $result .=  "<TR><TD>" . $fieldNames [$fieldIndex] .  "</TD><TD>"; 
  71.       if ($fieldLengths [$fieldIndex] <= 128) 
  72.       { 
  73.         $result .=  "<INPUT TYPE=\"text\" NAME=\"" . 
  74.           $fieldNames [$fieldIndex] .  "\" VALUE=\"" . 
  75.           $values [$fieldIndex] .  "\" SIZE=\"64\">"; 
  76.       } 
  77.       else 
  78.       { 
  79.         $result .=  "<TEXTAREA NAME=\"" . 
  80.           $fieldNames [$fieldIndex] .  "\"" . 
  81.            " COLS=\"64\" ROWS=\"10\" WRAP=\"virtual\">" . 
  82.           escapeforhtml ($values [$fieldIndex]) .  "</TEXTAREA>"; 
  83.       } 
  84.       $result .=   "<INPUT TYPE=\"hidden\" NAME=\"old-" . 
  85.         $fieldNames [$fieldIndex] . 
  86.          "\" VALUE=\"" . escapeforhtml ($values [$fieldIndex]) .  "\">" . 
  87.          "</TD></TR>"; 
  88.       $fieldIndex++; 
  89.     } 
  90.     $result .=  "<TR><TD ALIGN=\"center\" COLSPAN=\"2\">"; 
  91.     if ($mode ==  "modify") 
  92.     { 
  93.       $result .=  "<INPUT TYPE=\"submit\" NAME=\"remove\" VALUE=\"Remove\">"; 
  94.       $result .=  "<INPUT TYPE=\"submit\" NAME=\"update\" VALUE=\"Update\">"; 
  95.     } 
  96.     else 
  97.       { $result .=  "<INPUT TYPE=\"submit\" NAME=\"add\" VALUE=\"Add\">"; } 
  98.     $result .=  "</TABLE></TD></TR></TABLE></FORM>"; 
  99.     return $result; 
  100.   } 
  101.  
  102.   function fieldFromType ($text, $type) 
  103.   { 
  104.     if ($type ==  "int" || $type ==  "uint" || $type == "real") 
  105.       { $result = $text; } 
  106.     else 
  107.       { $result =  "'" . AddSlashes ($text) .  "'"; } 
  108.     return $result; 
  109.   } 
  110.  
  111.   function executeMsql ($database, $command) 
  112.   { 
  113.      /*echo "<TT>" . $command . "</TT><HR>";*/ 
  114.     msql ($database, $command); 
  115.   } 
  116.  
  117.   function handleRemove ($database, $table, $fieldsNumber, 
  118.                          $fieldNames, $fieldLengths, $fieldTypes) 
  119.   { 
  120.     global $remove; 
  121.     if ($remove !=  "") 
  122.     { 
  123.       $command =  "DELETE FROM " . $table .  " WHERE "; 
  124.       $fieldIndex = 0; 
  125.       while ($fieldIndex < $fieldsNumber) 
  126.       { 
  127.         $fieldName =  "old-" . $fieldNames [$fieldIndex]; 
  128.         global $$fieldName; 
  129.         $command .= $fieldNames [$fieldIndex] .  "=" . 
  130.           fieldFromType ($$fieldName, $fieldTypes [$fieldIndex]); 
  131.         if ($fieldIndex != $fieldsNumber - 1) 
  132.           { $command .=  " AND "; } 
  133.         $fieldIndex++; 
  134.       }         
  135.       executeMsql ($database, $command); 
  136.     } 
  137.   } 
  138.  
  139.   function handleUpdate ($database, $table, $fieldsNumber, 
  140.                          $fieldNames, $fieldLengths, $fieldTypes) 
  141.   { 
  142.     global $update; 
  143.     if ($update !=  "") 
  144.     { 
  145.       $command =  "UPDATE " . $table .  " SET "; 
  146.       $fieldIndex = 0; 
  147.       while ($fieldIndex < $fieldsNumber) 
  148.       { 
  149.         $fieldName = $fieldNames [$fieldIndex]; 
  150.         global $$fieldName; 
  151.         $command .= $fieldName .  "=" . 
  152.           fieldFromType ($$fieldName, $fieldTypes [$fieldIndex]); 
  153.         if ($fieldIndex != $fieldsNumber - 1) 
  154.           { $command .=  ", "; } 
  155.         $fieldIndex++; 
  156.       } 
  157.       $command .=  " WHERE "; 
  158.       $fieldIndex = 0; 
  159.       while ($fieldIndex < $fieldsNumber) 
  160.       { 
  161.         $fieldName =  "old-" . $fieldNames [$fieldIndex]; 
  162.         global $$fieldName; 
  163.         $command .= $fieldNames [$fieldIndex] .  "=" . 
  164.           fieldFromType ($$fieldName, $fieldTypes [$fieldIndex]); 
  165.         if ($fieldIndex != $fieldsNumber - 1) 
  166.           { $command .=  " AND "; } 
  167.         $fieldIndex++; 
  168.       } 
  169.       executeMsql ($database, $command); 
  170.     } 
  171.   } 
  172.  
  173.   function handleAdd ($database, $table, $fieldsNumber, 
  174.                       $fieldNames, $fieldLengths, $fieldTypes) 
  175.   { 
  176.     global $add; 
  177.     if ($add !=  "") 
  178.     { 
  179.       $command =  "INSERT INTO " . $table .  " ("; 
  180.       $fieldIndex = 0; 
  181.       while ($fieldIndex < $fieldsNumber) 
  182.       { 
  183.         $command .= $fieldNames [$fieldIndex]; 
  184.         if ($fieldIndex != $fieldsNumber - 1) 
  185.           { $command .=  ", "; } 
  186.         $fieldIndex++; 
  187.       } 
  188.       $command .=  ") VALUES ("; 
  189.       $fieldIndex = 0; 
  190.       while ($fieldIndex < $fieldsNumber) 
  191.       { 
  192.         $fieldName = $fieldNames [$fieldIndex]; 
  193.         global $$fieldName; 
  194.         $command .= fieldFromType ($$fieldName, $fieldTypes [$fieldIndex]); 
  195.         if ($fieldIndex != $fieldsNumber - 1) 
  196.           { $command .=  ", "; } 
  197.         $fieldIndex++; 
  198.       } 
  199.       $command .=  ")"; 
  200.       executeMsql ($database, $command); 
  201.     } 
  202.   } 
  203.  
  204.   function displayRemoveUpdate ($database, $table, $sortColumn, 
  205.                                 $fieldsNumber, $fieldNames, $fieldLengths) 
  206.   { 
  207.     $result =  ""; 
  208.     if ($sortColumn !=  "") 
  209.       { $sortColumn =  " ORDER BY " . $sortColumn; } 
  210.     $msqlresult = msql ($database,  "SELECT * FROM " . $table . $sortColumn); 
  211.     $tuplesNumber = msql_numrows ($msqlresult); 
  212.     $tupleIndex = 0; 
  213.     while ($tupleIndex < $tuplesNumber) 
  214.     { 
  215.       $fieldIndex = 0; 
  216.       while ($fieldIndex < $fieldsNumber) 
  217.       { 
  218.         $values [$fieldIndex] = msql_result ($msqlresult, $tupleIndex, 
  219.           $fieldNames [$fieldIndex]); 
  220.         $fieldIndex++; 
  221.       } 
  222.       $result .= displayTuple ($fieldsNumber, $fieldNames, 
  223.         $fieldLengths, $values,  "modify"); 
  224.       $tupleIndex++; 
  225.     } 
  226.     return $result; 
  227.   } 
  228.  
  229.   function displayAdd ($fieldsNumber, $fieldNames, $fieldLengths) 
  230.   { 
  231.     $result =  ""; 
  232.     $fieldIndex = 0; 
  233.     while ($fieldIndex < $fieldsNumber) 
  234.     { 
  235.       $values [$fieldIndex] =  ""; 
  236.       $fieldIndex++; 
  237.     } 
  238.     $result .= displayTuple ($fieldsNumber, $fieldNames, 
  239.       $fieldLengths, $values,  "add"); 
  240.     msql_close (); 
  241.     return $result; 
  242.   } 
  243.  
  244.   function administrationTable ($database, $table, $sortColumn) 
  245.   { 
  246.     $result =  ""; 
  247.     msql_connect ( "localhost"); 
  248.     $msqlresult = msql ($database,  "SELECT * FROM " . $table); 
  249.     $fieldsNumber = msql_numfields ($msqlresult);  
  250.     $msqlresult = msql_listfields ($database, $table); 
  251.     $fieldIndex = 0; 
  252.     while ($fieldIndex < $fieldsNumber) 
  253.     { 
  254.       $fieldNames [$fieldIndex] = msql_fieldname ($msqlresult, $fieldIndex); 
  255.       $fieldLengths [$fieldIndex] = msql_fieldlen ($msqlresult, $fieldIndex); 
  256.       $fieldTypes [$fieldIndex] = msql_fieldtype ($msqlresult, $fieldIndex); 
  257.       $fieldIndex++; 
  258.     } 
  259.     handleRemove ($database, $table, $fieldsNumber, $fieldNames, $fieldLengths, $fieldTypes); 
  260.     handleUpdate ($database, $table, $fieldsNumber, $fieldNames, $fieldLengths, $fieldTypes); 
  261.     handleAdd ($database, $table, $fieldsNumber, $fieldNames, $fieldLengths, $fieldTypes); 
  262.     $result .= displayRemoveUpdate ($database, $table, $sortColumn, $fieldsNumber, $fieldNames, $fieldLengths); 
  263.     $result .= displayAdd ($fieldsNumber, $fieldNames, $fieldLengths); 
  264.     return $result; 
  265.   } 
  266.  
  267.   function addTable ($database, $table) 
  268.   { 
  269.     $result =  ""; 
  270.     msql_connect ( "localhost"); 
  271.     $msqlresult = msql ($database,  "SELECT * FROM " . $table); 
  272.     $fieldsNumber = msql_numfields ($msqlresult);  
  273.     $msqlresult = msql_listfields ($database, $table); 
  274.     $fieldIndex = 0; 
  275.     while ($fieldIndex < $fieldsNumber) 
  276.     { 
  277.       $fieldNames [$fieldIndex] = msql_fieldname ($msqlresult, $fieldIndex); 
  278.       $fieldLengths [$fieldIndex] = msql_fieldlen ($msqlresult, $fieldIndex); 
  279.       $fieldTypes [$fieldIndex] = msql_fieldtype ($msqlresult, $fieldIndex); 
  280.       $fieldIndex++; 
  281.     } 
  282.     handleAdd ($database, $table, $fieldsNumber, $fieldNames, $fieldLengths, $fieldTypes); 
  283.     $result .= displayAdd ($fieldsNumber, $fieldNames, $fieldLengths); 
  284.     return $result; 
  285.   } 
  286. ?> 
  287.