This code will modify multiple rec in hercules (db1) and make the same changes in master (db2). ****************************************************************************************** db.cgi ****************************************************************************************** sub modify_record { # -------------------------------------------------------- # This routine does the actual modification of a record. It expects # to find in %in a record that is already in the database, and will # rewrite the database with the new entry. First it checks to make # sure that the modified record is ok with validate record. # It then goes through the database looking for the right record to # modify, if found, it prints out the modified record, and returns # the user to a success page. Otherwise the user is returned to an error # page with a reason why. my ($status, $line, @lines, @data, $output, $found, $restricted); my (%rec) = @_; $status = &validate_record(%in); # Check to make sure the modifications are ok! if ($status eq "ok") { 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); } @lines = ; # Slurp the database into @lines.. close DB; ($restricted = 1) if ($auth_modify_own and !$per_admin); $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); ($output .= "$line\n" and next LINE) if ($restricted and ($db_userid ne $data[$auth_user_field])); if ($data[$db_key_pos] eq $in{$db_key}) { # If we have userid's and this is not an admin, then we force the record to keep it's own # userid. if ($auth_user_field >= 0 and (!$per_admin or !$in{$db_cols[$auth_user_field]})) { $in{$db_cols[$auth_user_field]} = $data[$auth_user_field]; } $output .= &join_encode(%in); $found = 1; } else { $output .= $line . "\n"; # else print regular line. } } if ($found) { 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 &auth_logging("modified record: $in{$db_key}") if ($auth_logging); if ($db_after_modify_record) { $after = &after_modify_record; } &html_modify_success; } else { &html_modify_failure("$in{$db_key} (can't find requested record)"); } } else { &html_modify_failure($status); # Validation Error } } 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); ### new line 20151219 ########################################################## my (%modify_list2); ######################################################### # 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); ### new line 20151219 %modify_list2 = %modify_list; #### 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 ########## new lines 20151219 ############################################################# if ($db_after_modify_record) { my ($db2_file_name) = "$db_after_add_db"; ## i don't understand this but if it works for adding ## it should work here my ($output2); open (DB, "<$db2_file_name") or &cgierr("error in modify_records. unable to open db file: $db2_file_name.\nReason: $!"); if ($db_use_flock) { flock(DB, 1); } LINE: while () { (/^#/) and ($output2 .= $_ and next LINE); (/^\s*$/) and next LINE; chomp; @data = &split_decode($_); $key = $data[$db_key_pos]; ##### 20151219 change this if key pos not same in db and db2!!! # 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_list2{$key}) { # not validating records in db2 $output2 .= &join_encode(%{$modify_rec{$key}}); $modify_list2{$key} = 0; } else { $output2 .= "$_\n"; } } close DB; # Reprint out the database. open (DB, ">$db2_file_name") or &cgierr("error in modify_records. unable to open db file: $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 $output2; close DB; # automatically removes file lock } #################################################### &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 ****************************************************************************************** # Full path to db2 databace. $db_after_add_db = $db_script_path . "/../db/hercules.db"; # This is the name of the 2nd DB. # Call for ad to master. $db_after_add_record = 1; # Call for modify to master. $db_after_modify_record = 1; This code will modify multiple rec in master (db2) and make the same changes in hercules (db1). But will only write the record to hercules if the record is already exist. ****************************************************************************************** db.cgi ****************************************************************************************** sub modify_record { # -------------------------------------------------------- # This routine does the actual modification of a record. It expects # to find in %in a record that is already in the database, and will # rewrite the database with the new entry. First it checks to make # sure that the modified record is ok with validate record. # It then goes through the database looking for the right record to # modify, if found, it prints out the modified record, and returns # the user to a success page. Otherwise the user is returned to an error # page with a reason why. my ($status, $line, @lines, @data, $output, $found, $restricted); my (%rec) = @_; $status = &validate_record(%in); # Check to make sure the modifications are ok! if ($status eq "ok") { 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); } @lines = ; # Slurp the database into @lines.. close DB; ($restricted = 1) if ($auth_modify_own and !$per_admin); $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); ($output .= "$line\n" and next LINE) if ($restricted and ($db_userid ne $data[$auth_user_field])); if ($data[$db_key_pos] eq $in{$db_key}) { # If we have userid's and this is not an admin, then we force the record to keep it's own # userid. if ($auth_user_field >= 0 and (!$per_admin or !$in{$db_cols[$auth_user_field]})) { $in{$db_cols[$auth_user_field]} = $data[$auth_user_field]; } $output .= &join_encode(%in); $found = 1; } else { $output .= $line . "\n"; # else print regular line. } } if ($found) { 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 &auth_logging("modified record: $in{$db_key}") if ($auth_logging); if ($db_after_modify_record) { #Added this line $after = &after_modify_record; #Added this line } #Added this line 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); ### new line 20151219 ########################################################## my (%modify_list2); ######################################################### # 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); ### new line 20151219 %modify_list2 = %modify_list; #### 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 ########## new lines 20151219 ############################################################# if ($db_after_modify_record) { my ($db2_file_name) = "$db_after_add_db"; ## i don't understand this but if it works for adding ## it should work here my ($output2); open (DB, "<$db2_file_name") or &cgierr("error in modify_records. unable to open db file: $db2_file_name.\nReason: $!"); if ($db_use_flock) { flock(DB, 1); } LINE: while () { (/^#/) and ($output2 .= $_ and next LINE); (/^\s*$/) and next LINE; chomp; @data = &split_decode($_); $key = $data[$db_key_pos]; ##### 20151219 change this if key pos not same in db and db2!!! # 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_list2{$key}) { # not validating records in db2 $output2 .= &join_encode(%{$modify_rec{$key}}); $modify_list2{$key} = 0; } else { $output2 .= "$_\n"; } } close DB; # Reprint out the database. open (DB, ">$db2_file_name") or &cgierr("error in modify_records. unable to open db file: $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 $output2; close DB; # automatically removes file lock } &html_modify_mult_results($succstr, $errstr); } ****************************************************************************************** html.pl file ****************************************************************************************** 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! if it is then make the changes, if not don't add the record. 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 ****************************************************************************************** # Full path to db2 databace. $db_after_add_db = $db_script_path . "/../db/hercules.db"; # This is the name of the 2nd DB. # Call for modify to hercules. $db_after_modify_record = 1;