Bug 36520: Sanitize input in opac-sendbasket.pl
[koha.git] / acqui / check_uniqueness.pl
1 #!/usr/bin/perl
2
3 # Copyright 2011 BibLibre SARL
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 # This script search in items table if a value for a given field exists.
21 # It is used in check_additem (additem.js)
22 # Parameters are a list of 'field', which must be field names in items table
23 # and a list of 'value', which are the corresponding value to check
24 # Eg. @field = ('barcode', 'barcode', 'stocknumber')
25 #     @value = ('1234', '1235', 'ABC')
26 #     The script will check if there is already an item with barcode '1234',
27 #     then an item with barcode '1235', and finally check if there is an item
28 #     with stocknumber 'ABC'
29 # It returns a JSON string which contains what have been found
30 # Eg. { barcode: ['1234', '1235'], stocknumber: ['ABC'] }
31
32 use Modern::Perl;
33
34 use CGI qw ( -utf8 );
35 use JSON qw( to_json );
36 use C4::Output qw( output_with_http_headers );
37 use C4::Items qw( SearchItems );
38 use C4::Auth   qw( check_cookie_auth );
39
40 my $input = CGI->new;
41 my ($auth_status) =
42     check_cookie_auth( $input->cookie('CGISESSID'), { catalogue => 1 } );
43 if ( $auth_status ne "ok" ) {
44     print $input->header( -type => 'text/plain', -status => '403 Forbidden' );
45     exit 0;
46 }
47
48 my @field = $input->multi_param('field[]');
49 my @value = $input->multi_param('value[]');
50
51 my $r = {};
52 my $i = 0;
53 for ( my $i=0; $i<@field; $i++ ) {
54     my ($items) = C4::Items::SearchItems({ field => $field[$i], query => $value[$i]});
55
56     if ( @$items ) {
57         push @{ $r->{$field[$i]} }, $value[$i];
58     }
59 }
60
61 output_with_http_headers $input, undef, to_json($r), 'json';