bug 1953: removing potential SQL injection in C4::Acquisition::GetParcels
[koha.git] / C4 / Reports.pm
index 0944c52..376196a 100644 (file)
@@ -18,7 +18,7 @@ package C4::Reports;
 # Suite 330, Boston, MA  02111-1307 USA
 
 use strict;
-require Exporter;
+use CGI;
 
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
 use C4::Context;
@@ -28,15 +28,18 @@ use XML::Dumper;
 # use Smart::Comments;
 # use Data::Dumper;
 
-# set the version for version checking
-$VERSION = 0.01;
-
-@ISA = qw(Exporter);
-@EXPORT =
-  qw(get_report_types get_report_areas get_columns build_query get_criteria
-  save_report get_saved_reports execute_query get_saved_report create_compound run_compound
-  get_column_type get_distinct_values save_dictionary get_from_dictionary
-  delete_definition delete_report format_results);
+BEGIN {
+       # set the version for version checking
+       $VERSION = 0.12;
+       require Exporter;
+       @ISA = qw(Exporter);
+       @EXPORT = qw(
+               get_report_types get_report_areas get_columns build_query get_criteria
+               save_report get_saved_reports execute_query get_saved_report create_compound run_compound
+               get_column_type get_distinct_values save_dictionary get_from_dictionary
+               delete_definition delete_report format_results get_sql
+       );
+}
 
 our %table_areas;
 $table_areas{'1'} =
@@ -67,31 +70,24 @@ $keys{'5'} = ['borrowers.borrowernumber=accountlines.borrowernumber'];
 our %criteria;
 $criteria{'1'} = [
     'statistics.type',   'borrowers.categorycode',
-    'statistics.branch', 'biblioitems.itemtype',
+    'statistics.branch',
     'biblioitems.publicationyear|date',
     'items.dateaccessioned|date'
 ];
 $criteria{'2'} =
-  [ 'biblioitems.itemtype', 'items.holdingbranch', 'items.homebranch' ,'items.itemlost'];
+  [ 'items.holdingbranch', 'items.homebranch' ,'items.itemlost', 'items.location', 'items.ccode'];
 $criteria{'3'} = ['borrowers.branchcode'];
 $criteria{'4'} = ['aqorders.datereceived|date'];
 $criteria{'5'} = ['borrowers.branchcode'];
 
-our %columns;
-my $columns_def_file = "columns.def";
-my $htdocs = C4::Context->config('intrahtdocs');                       
-my $section='intranet';
-my ($theme, $lang) = themelanguage($htdocs, $columns_def_file, $section);                                                                                 
-
-my $columns_def_file="$htdocs/$theme/$lang/$columns_def_file";    
-open (COLUMNS,$columns_def_file);
-while (my $input = <COLUMNS>){
-       my @row =split(/\t/,$input);
-       $columns{$row[0]}=$row[1];
+if (C4::Context->preference('item-level_itypes')) {
+    unshift @{ $criteria{'1'} }, 'items.itype';
+    unshift @{ $criteria{'2'} }, 'items.itype';
+} else {
+    unshift @{ $criteria{'1'} }, 'biblioitems.itemtype';
+    unshift @{ $criteria{'2'} }, 'biblioitems.itemtype';
 }
 
-close COLUMNS;
-
 =head1 NAME
    
 C4::Reports - Module for generating reports 
@@ -182,29 +178,33 @@ This will return a list of all columns for a report area
 sub get_columns {
 
     # this calls the internal fucntion _get_columns
-    my ($area) = @_;
+    my ($area,$cgi) = @_;
     my $tables = $table_areas{$area};
     my @allcolumns;
+    my $first = 1;
     foreach my $table (@$tables) {
-        my @columns = _get_columns($table);
+        my @columns = _get_columns($table,$cgi, $first);
+        $first = 0;
         push @allcolumns, @columns;
     }
     return ( \@allcolumns );
 }
 
 sub _get_columns {
-    my ($tablename) = @_;
+    my ($tablename,$cgi, $first) = @_;
     my $dbh         = C4::Context->dbh();
     my $sth         = $dbh->prepare("show columns from $tablename");
     $sth->execute();
     my @columns;
+       my $column_defs = _get_column_defs($cgi);
        my %tablehash;
        $tablehash{'table'}=$tablename;
+    $tablehash{'__first__'} = $first;
        push @columns, \%tablehash;
     while ( my $data = $sth->fetchrow_arrayref() ) {
         my %temphash;
         $temphash{'name'}        = "$tablename.$data->[0]";
-        $temphash{'description'} = $columns{"$tablename.$data->[0]"};
+        $temphash{'description'} = $column_defs->{"$tablename.$data->[0]"};
         push @columns, \%temphash;
     }
     $sth->finish();
@@ -284,16 +284,17 @@ sub _build_query {
     return ($query);
 }
 
-=item get_criteria($area);
+=item get_criteria($area,$cgi);
 
 Returns an arraref to hashrefs suitable for using in a tmpl_loop. With the criteria and available values.
 
 =cut
 
 sub get_criteria {
-    my ($area) = @_;
+    my ($area,$cgi) = @_;
     my $dbh    = C4::Context->dbh();
     my $crit   = $criteria{$area};
+       my $column_defs = _get_column_defs($cgi);
     my @criteria_array;
     foreach my $localcrit (@$crit) {
         my ( $value, $type )   = split( /\|/, $localcrit );
@@ -302,7 +303,7 @@ sub get_criteria {
                        my %temp;
             $temp{'name'}   = $value;
             $temp{'date'}   = 1;
-                       $temp{'description'} = $columns{$value};
+                       $temp{'description'} = $column_defs->{$value};
             push @criteria_array, \%temp;
         }
         else {
@@ -319,7 +320,7 @@ sub get_criteria {
             $sth->finish();
             my %temp;
             $temp{'name'}   = $value;
-                       $temp{'description'} = $columns{$value};
+                       $temp{'description'} = $column_defs->{$value};
             $temp{'values'} = \@values;
             push @criteria_array, \%temp;
         }
@@ -332,10 +333,10 @@ sub execute_query {
     my $dbh = C4::Context->dbh();
 
     # take this line out when in production
-       if ($format eq 'url'){
+       if ($format eq 'csv' or $format eq 'tab'){
                }
        else {
-               $sql .= " LIMIT 10";
+               $sql .= " LIMIT 20";
        }
     my $sth = $dbh->prepare($sql);
     $sth->execute();
@@ -447,8 +448,12 @@ sub format_results {
                $row->{'row'} = $htmlrow;
        }
        $sth->finish;
-       return $perl;
-       
+       $query = "SELECT * FROM saved_sql WHERE id = ?";
+       $sth = $dbh->prepare($query);
+       $sth->execute($id);
+       $data = $sth->fetchrow_hashref();
+    $sth->finish();
+       return ($perl,$data->{'report_name'},$data->{'notes'}); 
 }      
 
 sub delete_report {
@@ -483,7 +488,7 @@ sub get_saved_report {
     $sth->execute($id);
     my $data = $sth->fetchrow_hashref();
     $sth->finish();
-    return ( $data->{'savedsql'}, $data->{'type'} );
+    return ( $data->{'savedsql'}, $data->{'type'}, $data->{'report_name'}, $data->{'notes'} );
 }
 
 =item create_compound($masterID,$subreportID)
@@ -595,10 +600,12 @@ sub get_from_dictionary {
                $sth->execute();
        }
        my @loop;
+       my @reports = ( 'Circulation', 'Catalog', 'Patrons', 'Acquisitions', 'Accounts');
        while (my $data = $sth->fetchrow_hashref()){
+               $data->{'areaname'}=$reports[$data->{'area'}-1];
                push @loop,$data;
                
-               }
+       }
        $sth->finish();
        return (\@loop);
 }
@@ -610,11 +617,42 @@ sub delete_definition {
        my $sth = $dbh->prepare($query);
        $sth->execute($id);
        $sth->finish();
+}
+
+sub get_sql {
+       my ($id) = @_;
+       my $dbh = C4::Context->dbh();
+       my $query = "SELECT * FROM saved_sql WHERE id = ?";
+       my $sth = $dbh->prepare($query);
+       $sth->execute($id);
+       my $data=$sth->fetchrow_hashref();
+       $sth->finish(); 
+       return $data->{'savedsql'};
+}
+
+sub _get_column_defs {
+       my ($cgi) = @_;
+       my %columns;
+       my $columns_def_file = "columns.def";
+       my $htdocs = C4::Context->config('intrahtdocs');                       
+       my $section='intranet';
+       my ($theme, $lang) = themelanguage($htdocs, $columns_def_file, $section,$cgi);
+
+       my $full_path_to_columns_def_file="$htdocs/$theme/$lang/$columns_def_file";    
+       open (COLUMNS,$full_path_to_columns_def_file);
+       while (my $input = <COLUMNS>){
+               my @row =split(/\t/,$input);
+               $columns{$row[0]}=$row[1];
        }
+
+       close COLUMNS;
+       return \%columns;
+}
+1;
+__END__
+
 =head1 AUTHOR
 
 Chris Cormack <crc@liblime.com>
 
 =cut
-
-1;