Bug 27715: Sanitize order by DT params
[koha.git] / C4 / Utils / DataTables.pm
1 package C4::Utils::DataTables;
2
3 # Copyright 2011 BibLibre
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 use Modern::Perl;
21 require Exporter;
22
23 use vars qw(@ISA @EXPORT);
24
25 BEGIN {
26
27     @ISA        = qw(Exporter);
28     @EXPORT     = qw(dt_build_orderby dt_get_params);
29 }
30
31 =head1 NAME
32
33 C4::Utils::DataTables - Utility subs for building query when DataTables source is AJAX
34
35 =head1 SYNOPSYS
36
37     use CGI qw ( -utf8 );
38     use C4::Context;
39     use C4::Utils::DataTables;
40
41     my $input = new CGI;
42     my $vars = $input->Vars;
43
44     my $query = qq{
45         SELECT surname, firstname
46         FROM borrowers
47         WHERE borrowernumber = ?
48     };
49     $query .= dt_build_orderby($vars);
50     $query .= " LIMIT ?,? ";
51
52     my $dbh = C4::Context->dbh;
53     my $sth = $dbh->prepare($query);
54     $sth->execute(
55         $vars->{'borrowernumber'},
56         @$having_params,
57         $vars->{'iDisplayStart'},
58         $vars->{'iDisplayLength'}
59     );
60     ...
61
62 =head1 DESCRIPTION
63
64     This module provide two utility functions to build a part of the SQL query,
65     depending on DataTables parameters.
66     One function build the 'ORDER BY' part, and the other the 'HAVING' part.
67
68 =head1 FUNCTIONS
69
70 =head2 dt_build_orderby
71
72     my $orderby = dt_build_orderby($dt_param);
73     This function takes a reference to a hash containing DataTables parameters
74     and build the corresponding 'ORDER BY' clause.
75     This hash must contains the following keys:
76         iSortCol_N, where N is a number from 0 to the number of columns to sort on minus 1
77         sSortDir_N is the sorting order ('asc' or 'desc) for the corresponding column
78         mDataProp_N is a mapping between the column index, and the name of a SQL field
79
80 =cut
81
82 sub dt_build_orderby {
83     my $param = shift;
84
85     my $i = 0;
86     my $orderby;
87     my @orderbys;
88     while(exists $param->{'iSortCol_'.$i}){
89         my $iSortCol = $param->{'iSortCol_'.$i};
90         my $sSortDir = $param->{'sSortDir_'.$i};
91         my $mDataProp = $param->{'mDataProp_'.$iSortCol};
92         my @sort_fields = $param->{$mDataProp.'_sorton'}
93             ? split(' ', $param->{$mDataProp.'_sorton'})
94             : ();
95         if(@sort_fields > 0) {
96             push @orderbys, "$_ $sSortDir" foreach (@sort_fields);
97         } else {
98             push @orderbys, "$mDataProp $sSortDir";
99         }
100         $i++;
101     }
102
103     return unless @orderbys;
104
105     # Must be "branches.branchname asc", "borrowers.firstname desc", etc.
106     @orderbys = grep { /^\w+\.\w+\s(asc|desc)$/ } @orderbys;
107
108     $orderby = " ORDER BY " . join(',', @orderbys) . " " if @orderbys;
109     return $orderby;
110 }
111
112 =head2 dt_get_params
113
114     my %dtparam = = dt_get_params( $input )
115     This function takes a reference to a new CGI object.
116     It prepares a hash containing Datatable parameters.
117
118 =cut
119 sub dt_get_params {
120     my $input = shift;
121     my %dtparam;
122     my $vars = $input->Vars;
123
124     foreach(qw/ iDisplayStart iDisplayLength iColumns sSearch bRegex iSortingCols sEcho /) {
125         $dtparam{$_} = $input->param($_);
126     }
127     foreach(grep /(?:_sorton|_filteron)$/, keys %$vars) {
128         $dtparam{$_} = $vars->{$_};
129     }
130     for(my $i=0; $i<$dtparam{'iColumns'}; $i++) {
131         foreach(qw/ bSearchable sSearch bRegex bSortable iSortCol mDataProp sSortDir /) {
132             my $key = $_ . '_' . $i;
133             $dtparam{$key} = $input->param($key) if defined $input->param($key);
134         }
135     }
136     return %dtparam;
137 }
138
139 1;