4 # This file is part of Koha.
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
36 my ($template, $loggedinuser, $cookie)
37 = get_template_and_user({template_name => "tools/picture-upload.tt",
41 flagsrequired => { tools => 'batch_upload_patron_images'},
45 our $filetype = $input->param('filetype') || '';
46 my $cardnumber = $input->param('cardnumber');
47 our $uploadfilename = $input->param('uploadfile') || '';
48 my $uploadfile = $input->upload('uploadfile');
49 my $borrowernumber = $input->param('borrowernumber');
50 my $op = $input->param('op') || '';
52 #FIXME: This code is really in the rough. The variables need to be re-scoped as the two subs depend on global vars to operate.
53 # Other parts of this code could be optimized as well, I think. Perhaps the file upload could be done with YUI's upload
56 $debug and warn "Params are: filetype=$filetype, cardnumber=$cardnumber, borrowernumber=$borrowernumber, uploadfile=$uploadfilename";
60 picture-upload.pl - Script for handling uploading of both single and bulk patronimages and importing them into the database.
68 This script is called and presents the user with an interface allowing him/her to upload a single patron image or bulk patron images via a zip file.
69 Files greater than 100K will be refused. Images should be 140x200 pixels. If they are larger they will be auto-resized to comply.
73 $debug and warn "Operation requested: $op";
75 my ( $total, $handled, $tempfile, $tfh );
79 # Case is important in these operational values as the template must use case to be visually pleasing!
80 if ( ( $op eq 'Upload' ) && $uploadfile ) {
81 my $dirname = File::Temp::tempdir( CLEANUP => 1 );
82 $debug and warn "dirname = $dirname";
84 if ( $uploadfilename =~ m/(\..+)$/i ) {
88 File::Temp::tempfile( SUFFIX => $filesuffix, UNLINK => 1 );
89 $debug and warn "tempfile = $tempfile";
90 my ( @directories, $results );
93 if ( $uploadfilename !~ /\.zip$/i && $filetype =~ m/zip/i );
94 $errors{'NOWRITETEMP'} = 1 unless ( -w $dirname );
95 $errors{'EMPTYUPLOAD'} = 1 unless ( length($uploadfile) > 0 );
98 $template->param( ERRORS => [ \%errors ] );
99 output_html_with_http_headers $input, $cookie, $template->output;
102 while (<$uploadfile>) {
106 if ( $filetype eq 'zip' ) {
107 unless ( system( "unzip", $tempfile, '-d', $dirname ) == 0 ) {
108 $errors{'UZIPFAIL'} = $uploadfilename;
109 $template->param( ERRORS => [ \%errors ] );
110 # This error is fatal to the import, so bail out here
111 output_html_with_http_headers $input, $cookie, $template->output;
114 push @directories, "$dirname";
115 foreach my $recursive_dir (@directories) {
116 opendir RECDIR, $recursive_dir;
117 while ( my $entry = readdir RECDIR ) {
118 push @directories, "$recursive_dir/$entry"
119 if ( -d "$recursive_dir/$entry" and $entry !~ /^\./ );
120 $debug and warn "$recursive_dir/$entry";
124 foreach my $dir (@directories) {
125 $results = handle_dir( $dir, $filesuffix, $template );
126 $handled++ if $results == 1;
128 $total = scalar @directories;
131 #if ($filetype eq 'zip' )
132 $results = handle_dir( $dirname, $filesuffix, $template, $cardnumber,
134 $handled++ if $results == 1;
138 if ( $results!=1 || %errors ) {
139 $template->param( ERRORS => [$results] );
143 map { $filecount += $_->{count} } @counts;
144 $debug and warn "Total directories processed: $total";
145 $debug and warn "Total files processed: $filecount";
150 TCOUNTS => ( $filecount > 0 ? $filecount : undef ),
152 $template->param( borrowernumber => $borrowernumber )
156 elsif ( ( $op eq 'Upload' ) && !$uploadfile ) {
157 warn "Problem uploading file or no file uploaded.";
158 $template->param( cardnumber => $cardnumber );
159 $template->param( filetype => $filetype );
161 elsif ( $op eq 'Delete' ) {
162 my $dberror = RmPatronImage($borrowernumber);
163 $debug and warn "Patron image deleted for $borrowernumber";
164 warn "Database returned $dberror" if $dberror;
166 if ( $borrowernumber && !%errors && !$template->param('ERRORS') ) {
167 print $input->redirect(
168 "/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber");
171 output_html_with_http_headers $input, $cookie, $template->output;
175 my ( $dir, $suffix, $template, $cardnumber, $source ) = @_;
176 my ( %counts, %direrrors );
177 $debug and warn "Entering sub handle_dir; passed \$dir=$dir, \$suffix=$suffix";
178 if ( $suffix =~ m/zip/i ) {
179 # If we were sent a zip file, process any included data/idlink.txt files
180 my ( $file, $filename );
182 $debug and warn "Passed a zip file.";
184 while ( my $filename = readdir DIR ) {
185 $file = "$dir/$filename"
186 if ( $filename =~ m/datalink\.txt/i
187 || $filename =~ m/idlink\.txt/i );
189 unless ( open( FILE, $file ) ) {
190 warn "Opening $dir/$file failed!";
191 $direrrors{'OPNLINK'} = $file;
192 # This error is fatal to the import of this directory contents
193 # so bail and return the error to the caller
197 while ( my $line = <FILE> ) {
198 $debug and warn "Reading contents of $file";
200 $debug and warn "Examining line: $line";
201 my $delim = ( $line =~ /\t/ ) ? "\t" : ( $line =~ /,/ ) ? "," : "";
202 $debug and warn "Delimeter is \'$delim\'";
203 unless ( $delim eq "," || $delim eq "\t" ) {
204 warn "Unrecognized or missing field delimeter. Please verify that you are using either a ',' or a 'tab'";
205 $direrrors{'DELERR'} = 1;
206 # This error is fatal to the import of this directory contents
207 # so bail and return the error to the caller
210 ( $cardnumber, $filename ) = split $delim, $line;
211 $cardnumber =~ s/[\"\r\n]//g; # remove offensive characters
212 $filename =~ s/[\"\r\n\s]//g;
213 $debug and warn "Cardnumber: $cardnumber Filename: $filename";
214 $source = "$dir/$filename";
215 %counts = handle_file( $cardnumber, $source, $template, %counts );
221 %counts = handle_file( $cardnumber, $source, $template, %counts );
223 push @counts, \%counts;
228 my ( $cardnumber, $source, $template, %count ) = @_;
229 $debug and warn "Entering sub handle_file; passed \$cardnumber=$cardnumber, \$source=$source";
230 $count{filenames} = () if !$count{filenames};
231 $count{source} = $source if !$count{source};
232 $count{count} = 0 unless exists $count{count};
235 if ( $filetype eq 'image' ) {
236 $filename = $uploadfilename;
239 $filename = $1 if ( $source && $source =~ /\/([^\/]+)$/ );
241 if ( $cardnumber && $source ) {
242 # Now process any imagefiles
243 $debug and warn "Source: $source";
244 my $size = ( stat($source) )[7];
245 if ( $size > 550000 ) {
246 # This check is necessary even with image resizing to avoid possible security/performance issues...
247 $filerrors{'OVRSIZ'} = 1;
248 push my @filerrors, \%filerrors;
249 push @{ $count{filenames} },
251 filerrors => \@filerrors,
253 cardnumber => $cardnumber
255 $template->param( ERRORS => 1 );
256 # this one is fatal so bail here...
259 my ( $srcimage, $image );
260 if ( open( IMG, "$source" ) ) {
261 $srcimage = GD::Image->new(*IMG);
263 if ( defined $srcimage ) {
265 my $mimetype = 'image/png';
266 # GD autodetects three basic image formats: PNG, JPEG, XPM
267 # we will convert all to PNG which is lossless...
268 # Check the pixel size of the image we are about to import...
269 my ( $width, $height ) = $srcimage->getBounds();
270 $debug and warn "$filename is $width pix X $height pix.";
271 if ( $width > 200 || $height > 300 ) {
272 # MAX pixel dims are 200 X 300...
273 $debug and warn "$filename exceeds the maximum pixel dimensions of 200 X 300. Resizing...";
274 # Percent we will reduce the image dimensions by...
276 if ( $width > 200 ) {
277 # If the width is oversize, scale based on width overage...
278 $percent_reduce = sprintf( "%.5f", ( 140 / $width ) );
281 # otherwise scale based on height overage.
282 $percent_reduce = sprintf( "%.5f", ( 200 / $height ) );
285 sprintf( "%.0f", ( $width * $percent_reduce ) );
287 sprintf( "%.0f", ( $height * $percent_reduce ) );
289 and warn "Reducing $filename by "
290 . ( $percent_reduce * 100 )
291 . "\% or to $width_reduce pix X $height_reduce pix";
292 #'1' creates true color image...
293 $image = GD::Image->new( $width_reduce, $height_reduce, 1 );
294 $image->copyResampled( $srcimage, 0, 0, 0, 0, $width_reduce,
295 $height_reduce, $width, $height );
296 $imgfile = $image->png();
298 and warn "$filename is "
300 . " bytes after resizing.";
302 undef $srcimage; # This object can get big...
306 $imgfile = $image->png();
308 and warn "$filename is " . length($imgfile) . " bytes.";
310 undef $srcimage; # This object can get big...
312 $debug and warn "Image is of mimetype $mimetype";
316 PutPatronImage( $cardnumber, $mimetype, $imgfile );
318 if ( !$dberror && $mimetype ) {
319 # Errors from here on are fatal only to the import of a particular image
320 #so don't bail, just note the error and keep going
322 push @{ $count{filenames} },
323 { source => $filename, cardnumber => $cardnumber };
326 warn "Database returned error: $dberror";
327 ( $dberror =~ /patronimage_fk1/ )
328 ? $filerrors{'IMGEXISTS'} = 1
329 : $filerrors{'DBERR'} = 1;
330 push my @filerrors, \%filerrors;
331 push @{ $count{filenames} },
333 filerrors => \@filerrors,
335 cardnumber => $cardnumber
337 $template->param( ERRORS => 1 );
339 elsif ( !$mimetype ) {
340 warn "Unable to determine mime type of $filename. Please verify mimetype.";
341 $filerrors{'MIMERR'} = 1;
342 push my @filerrors, \%filerrors;
343 push @{ $count{filenames} },
345 filerrors => \@filerrors,
347 cardnumber => $cardnumber
349 $template->param( ERRORS => 1 );
353 warn "Contents of $filename corrupted!";
355 $filerrors{'CORERR'} = 1;
356 push my @filerrors, \%filerrors;
357 push @{ $count{filenames} },
359 filerrors => \@filerrors,
361 cardnumber => $cardnumber
363 $template->param( ERRORS => 1 );
367 warn "Opening $source failed!";
368 $filerrors{'OPNERR'} = 1;
369 push my @filerrors, \%filerrors;
370 push @{ $count{filenames} },
372 filerrors => \@filerrors,
374 cardnumber => $cardnumber
376 $template->param( ERRORS => 1 );
380 # The need for this seems a bit unlikely, however, to maximize error trapping it is included
385 : ( $filename ? "cardnumber" : "cardnumber and filename" )
387 $filerrors{'CRDFIL'} = (
390 : ( $filename ? "cardnumber" : "cardnumber and filename" )
392 push my @filerrors, \%filerrors;
393 push @{ $count{filenames} },
395 filerrors => \@filerrors,
397 cardnumber => $cardnumber
399 $template->param( ERRORS => 1 );
406 Original contributor(s) undocumented
408 Database storage, single patronimage upload option, and extensive error trapping contributed by Chris Nighswonger cnighswonger <at> foundations <dot> edu
409 Image scaling/resizing contributed by the same.