****************************************************************************************** db.cgi ****************************************************************************************** sub modify_mult_record { # -------------------------------------------------------- # This routine will update multiple records at once. It expects # to find in %in a series of records to update. They will be of the # form field_name-key. # my ($key, %modify_list, %modify_rec, $rec_to_modify, @data, $key, $errstr, $succstr, $output, %errors); # First let's pick which records to modify and then separate them and store # them in their own hashes. $rec_to_modify = 0; foreach $key (keys %in) { # Build a hash of keys to modify. if ($in{$key} eq "modify") { $modify_list{$key} = 1; $rec_to_modify = 1; } ($key =~ /^(.*)-(.+)$/) and (${$modify_rec{$2}}{$1} = $in{$key}); } # Choke if we don't have anything to do. $rec_to_modify or (&html_modify_failure("no records specified.") and return); open (DB, "<$db_file_name") or &cgierr("error in modify_records. unable to open db file: $db_file_name.\nReason: $!"); if ($db_use_flock) { flock(DB, 1); } LINE: while () { (/^#/) and ($output .= $_ and next LINE); (/^\s*$/) and next LINE; chomp; @data = &split_decode($_); $key = $data[$db_key_pos]; # Now we check if this record is something we want to modify. If so, then # we make sure the new record is ok, if so we replace it. if ($modify_list{$key}) { $status = &validate_record(%{$modify_rec{$key}}); if ($status eq "ok") { $output .= &join_encode(%{$modify_rec{$key}}); $modify_list{$key} = 0; } else { $errors{$key} = $status; $output .= "$_\n"; } } else { $output .= "$_\n"; } } close DB; # Reprint out the database. open (DB, ">$db_file_name") or &cgierr("error in modify_records. unable to open db file: $db_file_name.\nReason: $!"); if ($db_use_flock) { flock(DB, 2) or &cgierr("unable to get exclusive lock on $db_file_name.\nReason: $!"); } print DB $output; close DB; # automatically removes file lock # Let's display an error message if we were unable to modify a record # for some reason. foreach $key (keys %modify_list) { if ($modify_list{$key}) { ($errors{$key}) ? ($errstr .= "$key: $errors{$key}") : ($errstr .= "$key: not found"); } else { $succstr .= qq~$key,~; } } chop($succstr); # Remove trailing delimeter &html_modify_mult_results($succstr, $errstr); } ****************************************************************************************** html.pl file ****************************************************************************************** sub after_add_record { #--------------------------------------------- # update db2 # first get the new record %rec = &get_record($in{$db_key}); my (%rec2) = %rec; my ($db2_file_name) = "$db_after_add_db"; #make any changes you want to the rec2, especially if record layout is not identical #next, append the rec2 to db2 # use the code in sub add_record as a guide: open (DB, ">>$db2_file_name") or &cgierr("error in add_record. unable to open database: $db2_file_name.\nReason: $!"); if ($db_use_flock) { flock(DB, 2) or &cgierr("unable to get exclusive lock on $db2_file_name.\nReason: $!"); } print DB &join_encode(%rec2); # NOTE: I HAVEN'T TESTED THIS SO BE SURE TO BACKUP FIRST close DB; # automatically removes file lock } ****************************************************************************************** sub after_modify_record { # first get the new record %rec = &get_record($in{$db_key}); my ($db2_file_name) = "$db_after_add_db"; #make any changes you want to the rec2, especially if record layout is not identical #next, slurp db2 open (DBX, "<$db2_file_name") or &cgierr("error in modify_records. unable to open db file: $db2_file_name.\nReason: $!"); if ($db_use_flock) { flock(DBX, 1); } @lines = ; # Slurp the database into @lines.. close DBX; # now look for matching record $found = 0; # Make sure the record is in here! LINE: foreach $line (@lines) { if ($line =~ /^$/) { next LINE; } # Skip and Remove blank lines if ($line =~ /^#/) { $output .= $line; next LINE; } # Comment Line chomp ($line); @data = &split_decode($line); # put your key position below if ($data[$db2_key_pos] eq $in{$db_key}) { %rec2 = %rec; $output .= &join_encode(%rec2); $found = 1; } else { $output .= $line . "\n"; # else print regular line. } } if (!$found) { # if the record was not found, you might want to just add it here $output .= &join_encode(%rec2); } open (DBX, ">$db2_file_name") or &cgierr("error in modify_records. unable to open db file: $db2_file_name.\nReason: $!"); if ($db_use_flock) { flock(DBX, 2) or &cgierr("unable to get exclusive lock on $db2_file_name.\nReason: $!"); } print DBX $output; close DBX; # automatically removes file lock } ****************************************************************************************** Config File ****************************************************************************************** # Call for ad to master. $db_after_add_record = 1; # Call for modify to master. $db_after_modify_record = 1;