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

  1. Simple DBM database 
  2.  
  3. Handles insert/delete/update to a dbm file. Can specify file and field list. No search/sort. A quick 'n dirty script. Possibly to be used as a starting point for DBM related coding 
  4.  
  5.  
  6.  
  7. <?php 
  8. /* 
  9.  * A simple script to manage a dbm file database 
  10.  *   No query/sort facility 
  11.  * Just a quick dirty hack I put together to make something get 
  12.  * going real fast. Code to handle field separator in data is not 
  13.  * good... General indentation and style is way off too :-) 
  14.  * Still, if it is useful... 
  15.  * 
  16.  * V.Satheesh Babu, vsbabu@csoft.net http://csoft.net/~vsbabu 
  17.  */ 
  18.  
  19. // specify the database file here 
  20. $dbm = dbmopen( "tst.db",  "c"); 
  21. // The directory where this file exists should be 
  22. // writably by httpd user... 
  23.  
  24. // specify field separator and field headings 
  25. $field_separator =  ":" ; 
  26. $fields = array (  "field1", "field2", "field3"); 
  27.  
  28. function print_one_record($w_mykey,$w_fields) { 
  29.     global $fields; 
  30.     if( $w_mykey)print( "<H3>Modify Record</H3>\n"); 
  31.     print( "<FORM NAME=entdetails ACTION=\"$PHP_SELF\" METHOD=\"POST\">\n"); 
  32.     print( "<TABLE BORDER=\"0\" CELLSPACING=\"1\" CELLPADDING=\"0\">\n"); 
  33.      /* we must make mykey a field, only if it is NOT defined */ 
  34.     if ( $w_mykey ) 
  35.         print( "<TR><TH><B>Key</B></TH>\n<TH>". 
  36.          "<INPUT NAME=\"mykey\" TYPE=\"HIDDEN\" VALUE=\"$w_mykey\"><B>". 
  37.          "$w_mykey</B></TH></TR>\n"); 
  38.     else 
  39.         print( "<TR><TH><B>Key</B></TH>\n<TD>". 
  40.          "<INPUT NAME=\"mykey\" TYPE=\"TEXT\"  VALUE=\"\" SIZE=35>". 
  41.          "</TD>\n</TR>\n"); 
  42.     for ($i=0;$i < count($fields) ; $i++) { 
  43.         print( "<TR>\n". 
  44.                  "<TH>$fields[$i]</TH>\n". 
  45.                  "<TD>". 
  46.                  "<INPUT NAME=\"$fields[$i]\" TYPE=\"TEXT\" VALUE=\""); 
  47.                 if ($w_fields[$i] !=  "") 
  48.                     print ( "$w_fields[$i]"); 
  49.                 else 
  50.                     print ( ""); 
  51.                 print( "\" SIZE=35></TD>\n". 
  52.                  "</TR>\n"); 
  53.     } 
  54.         print( "<TR><TD COLSPAN=\"2\">"); 
  55.         print( "<INPUT TYPE=\"RESET\" NAME=\"reset\" VALUE=\"Clear\">\n"); 
  56.         if ( !$w_mykey ) { 
  57.             print( "<INPUT TYPE=\"SUBMIT\" NAME=\"sub\" VALUE=\"Add\">\n"); 
  58.         } else { 
  59.             print( "<INPUT TYPE=\"SUBMIT\" NAME=\"del\" VALUE=\"Delete\">\n"); 
  60.             print( "<INPUT TYPE=\"SUBMIT\" NAME=\"sub\" VALUE=\"Update\">\n"); 
  61.         } 
  62.         print ( "</TD></TR></TABLE></FORM>\n"); 
  63.         return; 
  64.     } 
  65. function verify_data(&$w_mykey,&$w_fields){ 
  66.      /* for any error, show the error and ask user to go back */ 
  67.     global  $field_separator; 
  68.     global  $fields; 
  69.     trim($w_mykey); ereg_replace( "\n", "",$w_mykey); 
  70.     ereg_replace($field_separator, " ",$w_mykey); 
  71.     for ($i=0;$i < count($fields) ; $i++)  { 
  72.     trim($w_fields[$i]);ereg_replace( "\n", "",$w_fields[$i]); 
  73.     ereg_replace($field_separator, " ",$w_fields[$i]); 
  74.     } 
  75.      // add other checks as you need 
  76.     if ( $w_mykey ==  "") { 
  77.         $err = 1; 
  78.         echo  "ERROR : Key cannot be null\n"; 
  79.     } 
  80.   return $err; 
  81. /* 
  82.  * when we start, read the file and show all records 
  83.  * Each mykey has a link, which will lead to update/delete 
  84.  * At the bottom, there is a add new record form 
  85.  */ 
  86. function print_front_page($mydbm) { 
  87.      /* may be some header here? */ 
  88.     global $field_separator; 
  89.     global $fields; 
  90.     print( "<small><i>Click on # to  edit/delete.</i></small>\n"); 
  91.     print( "<TABLE WIDTH=\"100%\" BORDER=\"0\" CELLSPACING=\"1\"". 
  92.          "CELLPADDING=\"0\">\n"); 
  93.     print( "<TR><TH><B>#</B></TH><TH><B>Key</B></TH>\n"); 
  94.     for ($i=0;$i < count($fields) ; $i++) 
  95.         print( "<TH><B>$fields[$i]</B></TH>\n"); 
  96.     print( "</TR>\n"); 
  97.     $mykey = dbmfirstkey($mydbm); 
  98.     $recno=0; 
  99.     while ($mykey) { 
  100.         $record[$mykey]=dbmfetch($mydbm,$mykey); 
  101.         $mykey = dbmnextkey($mydbm,$mykey); 
  102.     } 
  103.     if ( count($record) > 0 ) { 
  104.     asort($record); 
  105.     for(reset($record);$mykey=key($record); next($record)){ 
  106.         $recno++; 
  107.         $w_fields= explode($field_separator,$record[$mykey]); 
  108.         print( "<TR><TH><A HREF=\"$PHP_SELF?mykey=$mykey\"><B>$recno</B></A></TH>\n"); 
  109.         print( "<TD><B>$mykey</B></TH>\n"); 
  110.       for ($i=0;$i < count($fields) ; $i++) 
  111.             print( "<TD> $w_fields[$i] </TD>\n"); 
  112.         print( "</TR>\n"); 
  113.     } 
  114.     } 
  115.     print( "</TABLE>\n"); 
  116.     print( "<A NAME=\"addit\"></A><BR><BR><BR><H3>Add Record</H3>\n"); 
  117.     print_one_record( "", ""); 
  118.     return; 
  119. // Main 
  120. // force page from outside cache 
  121. header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
  122. header( "Last-Modified: " . gmdate( "D, d M Y H:i:s") .  "GMT"); 
  123. header( "Cache-Control: no-cache, must-revalidate"); 
  124. header( "Pragma: no-cache"); 
  125.  
  126. /* 
  127.  * if no mykey is there, read the database, print the add form 
  128.  * and exit 
  129.  * if mykey is there and action is none, fetch and show the form 
  130.  * if the mykey is there and action is update, 
  131.  *     update - then go to the front page 
  132.  * if the mykey is there and action is delete, 
  133.  *     delete - then go to the front page 
  134.  * if the mykey is there and action is add, check and add, front page 
  135.  */ 
  136.     $to_print_frontpage = 1; 
  137.     if ( $del ) $action =  "delete"; 
  138.     if ( $sub ==  "Add" ) $action =  "add"; 
  139.     if ( $sub ==  "Update" ) $action =  "update"; 
  140.     if( $mykey )  { 
  141.       for ($i=0;$i < count($fields) ; $i++) 
  142.             $w_fields[$i] = ${$fields[$i]}; 
  143.         if( $action== "update") { 
  144.              /* update database */ 
  145.             if (0==verify_data($mykey,$w_fields)) { 
  146.                 $record=implode($field_separator,$w_fields); 
  147.                 dbmreplace($dbm,$mykey,$record); 
  148.             } else { 
  149.                 print_one_record($mykey,$w_fields); 
  150.                 $to_print_frontpage=0; 
  151.             } 
  152.         } elseif ( $action== "delete") { 
  153.              /* delete from database */ 
  154.             if (dbmdelete($dbm,$mykey)){ 
  155.                 print( "<B>Record for $mykey could not be deleted</B>\n"); 
  156.             } 
  157.         } elseif ( $action== "add") { 
  158.              /* insert into database */ 
  159.              /* we better verify things here first*/ 
  160.             if (0==verify_data($mykey,$w_fields)) { 
  161.                 $record=implode($field_separator,$w_fields); 
  162.                 if (1==dbminsert($dbm,$mykey,$record)){ 
  163.                     print( "<B>Key $mykey already exists with these values</B>\n"); 
  164.                     $record=dbmfetch($dbm,$mykey); 
  165.                     $w_fields= explode($field_separator,$record); 
  166.                     print_one_record($mykey,$w_fields); 
  167.                     $to_print_frontpage=0; 
  168.                 } 
  169.             } else { 
  170.                 print_one_record($mykey,$w_fields); 
  171.                 $to_print_frontpage=0; 
  172.             } 
  173.         } else { 
  174.              /* no action, just do a fetch and display */ 
  175.             $record=dbmfetch($dbm,$mykey); 
  176.             $w_fields= explode($field_separator,$record); 
  177.             print_one_record($mykey,$w_fields); 
  178.             $to_print_frontpage=0; 
  179.         } 
  180.     } 
  181.     if( $to_print_frontpage==1 )print_front_page($dbm); 
  182.     dbmclose($dbm); 
  183. ?>
  184.