Skip to content

Commit

Permalink
adding more UTF8 checks
Browse files Browse the repository at this point in the history
  • Loading branch information
chris263 committed Feb 20, 2025
1 parent c0c00ee commit 289427b
Showing 1 changed file with 44 additions and 31 deletions.
75 changes: 44 additions & 31 deletions lib/SGN/Controller/AJAX/Audit.pm
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,18 @@ sub retrieve_results : Path('/ajax/audit/retrieve_results'){
my $counter = 0;

while (my ($audit_ts, $operation, $username, $logged_in_user, $before, $after, $transactioncode, $primary_key, $is_undo) = $h->fetchrow_array) {
# Ensure UTF-8 encoding for text fields to prevent encoding issues
$before = defined $before ? Encode::decode('UTF-8', $before, Encode::FB_DEFAULT) : "";
$after = defined $after ? Encode::decode('UTF-8', $after, Encode::FB_DEFAULT) : "";

$all_audits[$counter] = [$audit_ts, $operation, $username, $logged_in_user, $before, $after, $transactioncode, $primary_key, $is_undo];
$counter++;
};
}

# Encode result properly before sending to response
my $json_string = encode_json(\@all_audits);
utf8::encode($json_string); # Convert to UTF-8 bytes

my $json_string;
$json_string = encode_json(\@all_audits);
$c->stash->{rest} = {
result => $json_string,
};
Expand All @@ -58,14 +63,14 @@ sub retrieve_table_names : Path('/ajax/audit/retrieve_table_names'){
$h->execute();
my @ids;
while (my ($drop_options) = $h->fetchrow_array) {
push @ids, $drop_options;

};

# Fixing problems with Latin characters
@ids = map { Encode::decode('UTF-8', $_, Encode::FB_DEFAULT) } @ids;
# Ensure UTF-8 decoding
push @ids, Encode::decode('UTF-8', $drop_options, Encode::FB_DEFAULT);
}

# Encode result properly before sending to response
my $json_string = encode_json(\@ids);
utf8::encode($json_string); # Convert to UTF-8 bytes

$c->stash->{rest} = {
result1 => $json_string,
};
Expand All @@ -92,22 +97,24 @@ sub retrieve_stock_audits : Path('/ajax/audit/retrieve_stock_audits'){


my @matches;
for (my $i = 0; $i<$counter; $i++){
for (my $i = 0; $i < $counter; $i++) {
my $operation = $all_audits[$i][1];
my $stock_json_string;
eval {
if ($operation eq "DELETE"){
$stock_json_string = decode_json(Encode::decode('UTF-8', $before[$i]));
} else {
$stock_json_string = decode_json(Encode::decode('UTF-8', $after[$i]));
}
};
if ($@) {
warn "Failed to decode JSON at index $i: $@";

eval {
my $json_text = ($operation eq "DELETE") ? $before[$i] : $after[$i];

# Convert Perl Unicode string to UTF-8 encoded bytes before decoding JSON
$json_text = Encode::encode('UTF-8', $json_text);
$stock_json_string = decode_json($json_text);
};
if ($@) {
warn "Failed to decode JSON at index $i: $@";
next; # Skip this iteration in case of error
}

my $desired_uniquename = $stock_json_string->{'uniquename'};
if($stock_uniquename eq $desired_uniquename){
if ($stock_uniquename eq $desired_uniquename) {
push @matches, $all_audits[$i];
}
}
Expand Down Expand Up @@ -144,27 +151,33 @@ sub retrieve_trial_audits : Path('/ajax/audit/retrieve_trial_audits'){
my @matches;
my $num_matches = 0; #this is to make sure only matched audits go into the matches array

for (my $i = 0; $i<$counter; $i++){
for (my $i = 0; $i < $counter; $i++) {
my $operation = $all_audits[$i][1];
my $json_string;
if($operation eq "DELETE"){
$json_string = decode_json(Encode::decode('UTF-8', $before[$i]));
}else{
$json_string = decode_json(Encode::decode('UTF-8', $after[$i]));

eval {
my $json_text = ($operation eq "DELETE") ? $before[$i] : $after[$i];

# Convert Perl Unicode string to UTF-8 encoded bytes before decoding JSON
$json_text = Encode::encode('UTF-8', $json_text);
$json_string = decode_json($json_text);
};
if ($@) {
warn "Failed to decode JSON at index $i: $@";
next; # Skip this iteration in case of error
}

my $desired_trial_id = $json_string->{'project_id'};

if($trial_id eq $desired_trial_id){


if ($trial_id eq $desired_trial_id) {
$matches[$num_matches] = $all_audits[$i];
$num_matches++;

}
}

my $match_trial_json;
$match_trial_json = encode_json(\@matches);
# Encode result properly before sending to response
my $match_trial_json = encode_json(\@matches);
utf8::encode($match_trial_json); # Convert to UTF-8 bytes

$c->stash->{rest} = {
match_project => $match_trial_json,
Expand Down

0 comments on commit 289427b

Please sign in to comment.