Bug 17900: Fix possible SQL injection in patron cards template editing
[koha.git] / C4 / Creators / Lib.pm
1 package C4::Creators::Lib;
2
3 # Copyright 2009 Foundations Bible College.
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 strict;
21 use warnings;
22
23 use autouse 'Data::Dumper' => qw(Dumper);
24
25 use C4::Context;
26 use C4::Debug;
27
28 BEGIN {
29     use version; our $VERSION = qv('3.07.00.049');
30     use base qw(Exporter);
31     our @EXPORT = qw(get_all_templates
32                      get_all_layouts
33                      get_all_profiles
34                      get_all_image_names
35                      get_batch_summary
36                      get_label_summary
37                      get_card_summary
38                      get_barcode_types
39                      get_label_types
40                      get_font_types
41                      get_text_justification_types
42                      get_output_formats
43                      get_table_names
44                      get_unit_values
45                      html_table
46     );
47 }
48
49 =head1 NAME
50
51 C4::Creators::Lib
52
53 =cut
54
55 =head1 FUNCTIONS
56
57 =cut
58
59 #=head2 C4::Creators::Lib::_SELECT()
60 #
61 #    This function returns a recordset upon success and 1 upon failure. Errors are logged to the Apache log.
62 #
63 #    examples:
64 #
65 #        my $field_value = _SELECT(field_name, table_name, condition);
66 #
67 #=cut
68
69 sub _SELECT {
70     my @params = @_;
71     my $query = "SELECT $params[0] FROM $params[1]";
72     $params[2] ? $query .= " WHERE $params[2];" : $query .= ';';
73     my $sth = C4::Context->dbh->prepare($query);
74 #    $sth->{'TraceLevel'} = 3;
75     $sth->execute();
76     if ($sth->err) {
77         warn sprintf('Database returned the following error: %s', $sth->errstr);
78         return 1;
79     }
80     my $record_set = [];
81     while (my $row = $sth->fetchrow_hashref()) {
82         push(@$record_set, $row);
83     }
84     return $record_set;
85 }
86
87 my $barcode_types = [
88     {type => 'CODE39',          name => 'Code 39',              desc => 'Translates the characters 0-9, A-Z, \'-\', \'*\', \'+\', \'$\', \'%\', \'/\', \'.\' and \' \' to a barcode pattern.',                                  selected => 0},
89     {type => 'CODE39MOD',       name => 'Code 39 + Modulo43',   desc => 'Translates the characters 0-9, A-Z, \'-\', \'*\', \'+\', \'$\', \'%\', \'/\', \'.\' and \' \' to a barcode pattern. Encodes Mod 43 checksum.',         selected => 0},
90     {type => 'CODE39MOD10',     name => 'Code 39 + Modulo10',   desc => 'Translates the characters 0-9, A-Z, \'-\', \'*\', \'+\', \'$\', \'%\', \'/\', \'.\' and \' \' to a barcode pattern. Encodes Mod 10 checksum.',         selected => 0},
91     {type => 'COOP2OF5',        name => 'COOP2of5',             desc => 'Creates COOP2of5 barcodes from a string consisting of the numeric characters 0-9',                                                                     selected => 0},
92     {type => 'EAN13',           name => 'EAN13',                desc => 'Creates EAN13 barcodes from a string of 12 or 13 digits. The check number (the 13:th digit) is calculated if not supplied.',                           selected => 0},
93 #    {type => 'EAN8',            name => 'EAN8',                 desc => 'Translates a string of 7 or 8 digits to EAN8 barcodes. The check number (the 8:th digit) is calculated if not supplied.',                              selected => 0},
94 #    {type => 'IATA2of5',        name => 'IATA2of5',             desc => 'Creates IATA2of5 barcodes from a string consisting of the numeric characters 0-9',                                                                     selected => 0},
95     {type => 'INDUSTRIAL2OF5',  name => 'Industrial2of5',       desc => 'Creates Industrial2of5 barcodes from a string consisting of the numeric characters 0-9',                                                               selected => 0},
96 #    {type => 'ITF',             name => 'Interleaved2of5',      desc => 'Translates the characters 0-9 to a barcodes. These barcodes could also be called 'Interleaved2of5'.',                                                  selected => 0},
97 #    {type => 'MATRIX2OF5',      name => 'Matrix2of5',           desc => 'Creates Matrix2of5 barcodes from a string consisting of the numeric characters 0-9',                                                                   selected => 0},
98 #    {type => 'NW7',             name => 'NW7',                  desc => 'Creates a NW7 barcodes from a string consisting of the numeric characters 0-9',                                                                        selected => 0},
99 #    {type => 'UPCA',            name => 'UPCA',                 desc => 'Translates a string of 11 or 12 digits to UPCA barcodes. The check number (the 12:th digit) is calculated if not supplied.',                           selected => 0},
100 #    {type => 'UPCE',            name => 'UPCE',                 desc => 'Translates a string of 6, 7 or 8 digits to UPCE barcodes. If the string is 6 digits long, '0' is added first in the string. The check number (the 8:th digit) is calculated if not supplied.',                                 selected => 0},
101 ];
102
103 my $label_types = [
104     {type => 'BIB',     name => 'Biblio',               desc => 'Only the bibliographic data is printed.',                              selected => 0},
105     {type => 'BARBIB',  name => 'Barcode/Biblio',       desc => 'Barcode proceeds bibliographic data.',                                 selected => 0},
106     {type => 'BIBBAR',  name => 'Biblio/Barcode',       desc => 'Bibliographic data proceeds barcode.',                                 selected => 0},
107     {type => 'ALT',     name => 'Alternating',          desc => 'Barcode and bibliographic data are printed on alternating labels.',    selected => 0},
108     {type => 'BAR',     name => 'Barcode',              desc => 'Only the barcode is printed.',                                         selected => 0},
109 ];
110
111 my $font_types = [
112     {type => 'TR',      name => 'Times-Roman',                  selected => 0},
113     {type => 'TB',      name => 'Times-Bold',                   selected => 0},
114     {type => 'TI',      name => 'Times-Italic',                 selected => 0},
115     {type => 'TBI',     name => 'Times-Bold-Italic',            selected => 0},
116     {type => 'C',       name => 'Courier',                      selected => 0},
117     {type => 'CB',      name => 'Courier-Bold',                 selected => 0},
118     {type => 'CO',      name => 'Courier-Oblique',              selected => 0},
119     {type => 'CBO',     name => 'Courier-Bold-Oblique',         selected => 0},
120     {type => 'H',       name => 'Helvetica',                    selected => 0},
121     {type => 'HO',      name => 'Helvetica-Oblique',            selected => 0},
122     {type => 'HB',      name => 'Helvetica-Bold',               selected => 0},
123     {type => 'HBO',     name => 'Helvetica-Bold-Oblique',       selected => 0},
124 ];
125
126 my $text_justification_types = [
127     {type => 'L',       name => 'Left',                         selected => 0},
128     {type => 'C',       name => 'Center',                       selected => 0},
129     {type => 'R',       name => 'Right',                        selected => 0},
130 #    {type => 'F',       name => 'Full',                         selected => 0},
131 ];
132
133 my $unit_values = [
134     {type       => 'POINT',      desc    => 'PostScript Points',  value   => 1,                 selected => 0},
135     {type       => 'AGATE',      desc    => 'Adobe Agates',       value   => 5.1428571,         selected => 0},
136     {type       => 'INCH',       desc    => 'US Inches',          value   => 72,                selected => 0},
137     {type       => 'MM',         desc    => 'SI Millimeters',     value   => 2.83464567,        selected => 0},
138     {type       => 'CM',         desc    => 'SI Centimeters',     value   => 28.3464567,        selected => 0},
139 ];
140
141 my $output_formats = [
142     {type       => 'pdf',       desc    => 'PDF File'},
143     {type       => 'csv',       desc    => 'CSV File'},
144 ];
145
146 sub _build_query {
147     my ( $params, $table ) = @_;
148     my @fields = exists $params->{fields} ? @{ $params->{fields} } : ();
149     my $query = "SELECT " . ( @fields ? join(', ', @fields ) : '*' ) . " FROM $table";
150     my @where_args;
151     if ( exists $params->{filters} ) {
152         $query .= ' WHERE 1 ';
153         while ( my ( $field, $values ) = each %{ $params->{filters} } ) {
154             if ( ref( $values ) ) {
155                 $query .= " AND $field IN ( " . ( ('?') x scalar( @$values ) ) . " ) ";
156                 push @where_args, @$values;
157             } else {
158                 $query .= " AND $field = ? ";
159                 push @where_args, $values;
160             }
161         }
162     }
163     $query .= (exists $params->{orderby} ? " ORDER BY $params->{orderby} " : '');
164     return ( $query, @where_args );
165 }
166
167 =head2 C4::Creators::Lib::get_all_templates()
168
169   my $templates = get_all_templates();
170
171 This function returns a reference to a hash containing all templates upon success and 1 upon failure. Errors are logged to the Apache log.
172
173 =cut
174
175 sub get_all_templates {
176     my ( $params ) = @_;
177     my @templates = ();
178     my ( $query, @where_args ) = _build_query( $params, 'creator_templates' );
179     my $sth = C4::Context->dbh->prepare($query);
180     $sth->execute( @where_args );
181     if ($sth->err) {
182         warn sprintf('Database returned the following error: %s', $sth->errstr);
183         return -1;
184     }
185     ADD_TEMPLATES:
186     while (my $template = $sth->fetchrow_hashref) {
187         push(@templates, $template);
188     }
189     return \@templates;
190 }
191
192 =head2 C4::Creators::Lib::get_all_layouts()
193
194   my $layouts = get_all_layouts();
195
196 This function returns a reference to a hash containing all layouts upon success and 1 upon failure. Errors are logged to the Apache log.
197
198 =cut
199
200 sub get_all_layouts {
201     my ( $params ) = @_;
202     my @layouts = ();
203     my ( $query, @where_args ) = _build_query( $params, 'creator_layouts' );
204     my $sth = C4::Context->dbh->prepare($query);
205     $sth->execute( @where_args );
206     if ($sth->err) {
207         warn sprintf('Database returned the following error: %s', $sth->errstr);
208         return -1;
209     }
210     ADD_LAYOUTS:
211     while (my $layout = $sth->fetchrow_hashref) {
212         push(@layouts, $layout);
213     }
214     return \@layouts;
215 }
216
217 =head2 C4::Creators::Lib::get_all_profiles()
218
219   my $profiles = get_all_profiles();
220
221   my $profiles = get_all_profiles({ fields => [@fields], filters => { filters => [$value1, $value2] } });
222
223 This function returns an arrayref whose elements are hashes containing all profiles upon success and 1 upon failure. Errors are logged
224 to the Apache log. Two parameters are accepted. The first limits the field(s) returned. This parameter should be string of comma separted
225 fields. ie. "field_1, field_2, ...field_n" The second limits the records returned based on a string containing a valud SQL 'WHERE' filter.
226
227 NOTE: Do not pass in the keyword 'WHERE.'
228
229 =cut
230
231 sub get_all_profiles {
232     my ( $params ) = @_;
233     my @profiles = ();
234     my ( $query, @where_args ) = _build_query( $params, 'printers_profile' );
235     my $sth = C4::Context->dbh->prepare($query);
236     $sth->execute( @where_args );
237     if ($sth->err) {
238         warn sprintf('Database returned the following error: %s', $sth->errstr);
239         return -1;
240     }
241     ADD_PROFILES:
242     while (my $profile = $sth->fetchrow_hashref) {
243         push(@profiles, $profile);
244     }
245     return \@profiles;
246 }
247
248 =head2 C4::Creators::Lib::get_all_image_names()
249
250 =cut
251
252 sub get_all_image_names {
253     my $image_names = [];
254     my $query = "SELECT image_name FROM creator_images";
255     my $sth = C4::Context->dbh->prepare($query);
256 #    $sth->{'TraceLevel'} = 3 if $debug;
257     $sth->execute();
258     if ($sth->err) {
259         warn sprintf('Database returned the following error: %s', $sth->errstr);
260         return -1;
261     }
262     grep {push @$image_names, {type => $$_[0], name => $$_[0], selected => 0}} @{$sth->fetchall_arrayref([0])};
263     return $image_names;
264 }
265
266 =head2 C4::Creators::Lib::get_batch_summary()
267
268   my $batches = get_batch_summary();
269
270   my $batches = get_batch_summary(filter => filter_string);
271
272 This function returns an arrayref whose elements are hashes containing the batch_ids of current batches along with the item count
273 for each batch upon success and 1 upon failure. Item counts are stored under the key '_item_count' Errors are logged to the Apache log.
274 One parameter is accepted which limits the records returned based on a string containing a valud SQL 'WHERE' filter.
275
276 NOTE: Do not pass in the keyword 'WHERE.'
277
278 =cut
279
280 sub get_batch_summary {
281     my ( $params ) = @_;
282     my @batches = ();
283     $params->{fields} = ['batch_id', 'count(batch_id) as _item_count'];
284     my ( $query, @where_args ) = _build_query( $params, 'creator_batches' );
285     $query .= " GROUP BY batch_id";
286     my $sth = C4::Context->dbh->prepare($query);
287     $sth->execute( @where_args );
288     if ($sth->err) {
289         warn sprintf('Database returned the following error on attempted SELECT: %s', $sth->errstr);
290         return -1;
291     }
292     while (my $batch = $sth->fetchrow_hashref) {
293         push(@batches, $batch);
294     }
295     return \@batches;
296 }
297
298 =head2 C4::Creators::Lib::get_label_summary()
299
300   my $labels = get_label_summary();
301
302   my $labels = get_label_summary(items => @item_list);
303
304 This function returns an arrayref whose elements are hashes containing the label_ids of current labels along with the item count
305 for each label upon success and 1 upon failure. Item counts are stored under the key '_item_count' Errors are logged to the Apache log.
306 One parameter is accepted which limits the records returned based on a string containing a valud SQL 'WHERE' filter.
307
308 NOTE: Do not pass in the keyword 'WHERE.'
309
310 =cut
311
312 sub get_label_summary {
313     my %params = @_;
314     my $label_number = 0;
315     my @label_summaries = ();
316     my $query = "     SELECT b.title, b.author, bi.itemtype, i.barcode, i.biblionumber, i.itype
317                       FROM creator_batches AS c LEFT JOIN items AS i ON (c.item_number=i.itemnumber)
318                       LEFT JOIN biblioitems AS bi ON (i.biblioitemnumber=bi.biblioitemnumber)
319                       LEFT JOIN biblio AS b ON (bi.biblionumber=b.biblionumber)
320                       WHERE itemnumber=? AND batch_id=?;
321                   ";
322     my $sth = C4::Context->dbh->prepare($query);
323     foreach my $item (@{$params{'items'}}) {
324         $label_number++;
325         $sth->execute($item->{'item_number'}, $params{'batch_id'});
326         if ($sth->err) {
327             warn sprintf('Database returned the following error on attempted SELECT: %s', $sth->errstr);
328             return -1;
329         }
330         my $record = $sth->fetchrow_hashref;
331         my $label_summary;
332         $label_summary->{'_label_number'} = $label_number;
333         $record->{'author'} =~ s/[^\.|\w]$// if $record->{'author'};  # strip off ugly trailing chars... but not periods or word chars
334         $record->{'title'} =~ s/\W*$//;  # strip off ugly trailing chars
335         # FIXME contructing staff interface URLs should be done *much* higher up the stack - for the most part, C4 module code
336         # should not know that it's part of a web app
337         $record->{'title'} = '<a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=' . $record->{'biblionumber'} . '"> ' . $record->{'title'} . '</a>';
338         $label_summary->{'_summary'} = $record->{'title'} . " | " . ($record->{'author'} ? $record->{'author'} : 'N/A');
339         $label_summary->{'_item_type'} = C4::Context->preference("item-level_itypes") ? $record->{'itype'} : $record->{'itemtype'};
340         $label_summary->{'_barcode'} = $record->{'barcode'};
341         $label_summary->{'_item_number'} = $item->{'item_number'};
342         $label_summary->{'_label_id'} = $item->{'label_id'};
343         push (@label_summaries, $label_summary);
344     }
345     return \@label_summaries;
346 }
347
348 =head2 C4::Creators::Lib::get_card_summary()
349
350   my $cards = get_card_summary();
351
352   my $cards = get_card_summary(items => @item_list);
353
354 This function returns an arrayref whose elements are hashes containing the label_ids of current cards along with the item count
355 for each card upon success and 1 upon failure. Item counts are stored under the key '_item_count' Errors are logged to the Apache log.
356 One parameter is accepted which limits the records returned based on a string containing a valud SQL 'WHERE' filter.
357
358 NOTE: Do not pass in the keyword 'WHERE.'
359
360 =cut
361
362 sub get_card_summary {
363     my %params = @_;
364     my $card_number = 0;
365     my @card_summaries = ();
366     my $query = "SELECT CONCAT_WS(', ', surname, firstname) AS name, cardnumber FROM borrowers WHERE borrowernumber=?;";
367     my $sth = C4::Context->dbh->prepare($query);
368     foreach my $item (@{$params{'items'}}) {
369         $card_number++;
370         $sth->execute($item->{'borrower_number'});
371         if ($sth->err) {
372             warn sprintf('Database returned the following error on attempted SELECT: %s', $sth->errstr);
373             return -1;
374         }
375         my $record = $sth->fetchrow_hashref;
376         my $card_summary->{'_card_number'} = $card_number;
377         $card_summary->{'_summary'} = $record->{'name'};
378         $card_summary->{'borrowernumber'} = $item->{'borrower_number'};
379         $card_summary->{'_label_id'} = $item->{'label_id'};
380         push (@card_summaries, $card_summary);
381     }
382     return \@card_summaries;
383 }
384
385 =head2 C4::Creators::Lib::get_barcode_types()
386
387   my $barcode_types = get_barcode_types();
388
389 This function returns a reference to an array of hashes containing all barcode types along with their name and description.
390
391 =cut
392
393 sub get_barcode_types {
394     return $barcode_types;
395 }
396
397 =head2 C4::Creators::Lib::get_label_types()
398
399   my $label_types = get_label_types();
400
401 This function returns a reference to an array of hashes containing all label types along with their name and description.
402
403 =cut
404
405 sub get_label_types {
406     return $label_types;
407 }
408
409 =head2 C4::Creators::Lib::get_font_types()
410
411   my $font_types = get_font_types();
412
413 This function returns a reference to an array of hashes containing all font types along with their name and description.
414
415 =cut
416
417 sub get_font_types {
418     return $font_types;
419 }
420
421 =head2 C4::Creators::Lib::get_text_justification_types()
422
423   my $text_justification_types = get_text_justification_types();
424
425 This function returns a reference to an array of hashes containing all text justification types along with their name and description.
426
427 =cut
428
429 sub get_text_justification_types {
430     return $text_justification_types;
431 }
432
433 =head2 C4::Creators::Lib::get_unit_values()
434
435   my $unit_values = get_unit_values();
436
437 This function returns a reference to an array of  hashes containing all unit types along with their description and multiplier.
438 NOTE: All units are relative to a PostScript Point.
439 There are 72 PS points to the inch.
440
441 =cut
442
443 sub get_unit_values {
444     return $unit_values;
445 }
446
447 =head2 C4::Creators::Lib::get_output_formats()
448
449   my $label_output_formats = get_output_formats();
450
451 This function returns a reference to an array of hashes containing all label output formats along with their description.
452
453 =cut
454
455 sub get_output_formats {
456     return $output_formats;
457 }
458
459
460 =head2 C4::Creators::Lib::get_table_names($search_term)
461
462 Return an arrayref of an array containing the table names which contain the supplied search term.
463
464 =cut
465
466 sub get_table_names {
467     my $search_term = shift;
468     my $dbh = C4::Context->dbh();
469     my $table_names = [];
470     my $sth = $dbh->table_info(undef,undef,"%$search_term%");
471     while (my $info = $sth->fetchrow_hashref()){
472         push (@$table_names, $info->{'TABLE_NAME'});
473     }
474     return $table_names;
475 }
476
477 =head2 C4::Creators::Lib::html_table()
478
479 This function returns an arrayref of an array of hashes contianing the supplied data formatted suitably to
480 be passed off as a template parameter and used to build an html table.
481
482    my $table = html_table(header_fields, array_of_row_data);
483    $template->param(
484        table_loop => $table,
485    );
486
487     html example:
488
489         <table>
490             [% FOREACH table_loo IN table_loop %]
491                 [% IF ( table_loo.header_fields ) %]
492                     <tr>
493                         [% FOREACH header_field IN table_loo.header_fields %]
494                             <th>[% header_field.field_label %]</th>
495                         [% END %]
496                     </tr>
497                 [% ELSE %]
498                     <tr>
499                         [% FOREACH text_field IN table_loo.text_fields %]
500                             [% IF ( text_field.select_field ) %]
501                                 <td><input type="checkbox" name="action" value="[% text_field.field_value %]"></td>
502                             [% ELSE %]
503                                 <td>[% text_field.field_value %]</td>
504                             [% END %]
505                         [% END %]
506                     </tr>
507                 [% END %]
508             [% END %]
509         </table>
510
511 =cut
512
513 sub html_table {
514     my $headers = shift;
515     my $data = shift;
516     return undef if scalar(@$data) == 0;      # no need to generate a table if there is not data to display
517     my $table = [];
518     my $fields = [];
519     my @table_columns = ();
520     my ($row_index, $col_index) = (0,0);
521     my $cols = 0;       # number of columns to wrap on
522     my $field_count = 0;
523     my $select_value = undef;
524     my $link_field = undef;
525     POPULATE_HEADER:
526     foreach my $header (@$headers) {
527         my @key = keys %$header;
528         if ($key[0] eq 'select' ) {
529             push (@table_columns, $key[0]);
530             $$fields[$col_index] = {hidden => 0, select_field => 0, field_name => ($key[0]), field_label => $header->{$key[0]}{'label'}};
531             # do special formatting stuff....
532             $select_value = $header->{$key[0]}{'value'};
533         }
534         else {
535             # do special formatting stuff....
536             $link_field->{$key[0]} = ($header->{$key[0]}{'link_field'} == 1 ? 1 : 0);
537             push (@table_columns, $key[0]);
538             $$fields[$col_index] = {hidden => 0, select_field => 0, field_name => ($key[0]), field_label => $header->{$key[0]}{'label'}};
539         }
540         $field_count++;
541         $col_index++;
542     }
543     $$table[$row_index] = {header_fields => $fields};
544     $cols = $col_index;
545     $field_count *= scalar(@$data);     # total fields to be displayed in the table
546     $col_index = 0;
547     $row_index++;
548     $fields = [];
549     POPULATE_TABLE:
550     foreach my $db_row (@$data) {
551         POPULATE_ROW:
552         foreach my $table_column (@table_columns) {
553             if (grep {$table_column eq $_} keys %$db_row) {
554                 $$fields[$col_index] = {hidden => 0, link_field => $link_field->{$table_column}, select_field => 0, field_name => ($table_column . "_tbl"), field_value => $db_row->{$table_column}};
555                 $col_index++;
556                 next POPULATE_ROW;
557             }
558             elsif ($table_column =~ m/^_((.*)_(.*$))/) {   # this a special case
559                 my $table_name = get_table_names($2);
560                 my $record_set = _SELECT($1, @$table_name[0], $2 . "_id = " . $db_row->{$2 . "_id"});
561                 $$fields[$col_index] = {hidden => 0, link_field => $link_field->{$table_column}, select_field => 0, field_name => ($table_column . "_tbl"), field_value => $$record_set[0]{$1}};
562                 $col_index++;
563                 next POPULATE_ROW;
564             }
565             elsif ($table_column eq 'select' ) {
566                 $$fields[$col_index] = {hidden => 0, select_field => 1, field_name => 'select', field_value => $db_row->{$select_value}};
567             }
568         }
569         $$table[$row_index] = {text_fields => $fields};
570         $col_index = 0;
571         $row_index++;
572         $fields = [];
573     }
574     return $table;
575 }
576
577 1;
578 __END__
579
580 =head1 AUTHOR
581
582 Chris Nighswonger <cnighswonger AT foundations DOT edu>
583
584 =cut