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>.
35 use Koha
::Patron
::Image
;
36 use Koha
::Patron
::Images
;
41 my ($template, $loggedinuser, $cookie)
42 = get_template_and_user
({template_name
=> "tools/picture-upload.tt",
46 flagsrequired
=> { tools
=> 'batch_upload_patron_images'},
50 our $filetype = $input->param('filetype') || '';
51 my $cardnumber = $input->param('cardnumber');
52 our $uploadfilename = $input->param('uploadfile') || '';
53 my $uploadfile = $input->upload('uploadfile');
54 my $borrowernumber = $input->param('borrowernumber');
55 my $op = $input->param('op') || '';
57 #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.
58 # Other parts of this code could be optimized as well, I think. Perhaps the file upload could be done with YUI's upload
61 $debug and warn "Params are: filetype=$filetype, cardnumber=$cardnumber, borrowernumber=$borrowernumber, uploadfile=$uploadfilename";
65 picture-upload.pl - Script for handling uploading of both single and bulk patronimages and importing them into the database.
73 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.
74 Files greater than 100K will be refused. Images should be 140x200 pixels. If they are larger they will be auto-resized to comply.
78 $debug and warn "Operation requested: $op";
80 my ( $total, $handled, $tempfile, $tfh );
84 # Case is important in these operational values as the template must use case to be visually pleasing!
85 if ( ( $op eq 'Upload' ) && $uploadfile ) {
87 die "Wrong CSRF token"
88 unless Koha
::Token
->new->check_csrf({
89 session_id
=> scalar $input->cookie('CGISESSID'),
90 token
=> scalar $input->param('csrf_token'),
93 my $dirname = File
::Temp
::tempdir
( CLEANUP
=> 1 );
94 $debug and warn "dirname = $dirname";
96 if ( $uploadfilename =~ m/(\..+)$/i ) {
100 File
::Temp
::tempfile
( SUFFIX
=> $filesuffix, UNLINK
=> 1 );
101 $debug and warn "tempfile = $tempfile";
102 my ( @directories, $results );
104 $errors{'NOTZIP'} = 1
105 if ( $uploadfilename !~ /\.zip$/i && $filetype =~ m/zip/i );
106 $errors{'NOWRITETEMP'} = 1 unless ( -w
$dirname );
107 $errors{'EMPTYUPLOAD'} = 1 unless ( length($uploadfile) > 0 );
110 $template->param( ERRORS
=> [ \
%errors ] );
111 output_html_with_http_headers
$input, $cookie, $template->output;
114 while (<$uploadfile>) {
118 if ( $filetype eq 'zip' ) {
119 unless ( system( "unzip", $tempfile, '-d', $dirname ) == 0 ) {
120 $errors{'UZIPFAIL'} = $uploadfilename;
121 $template->param( ERRORS
=> [ \
%errors ] );
122 # This error is fatal to the import, so bail out here
123 output_html_with_http_headers
$input, $cookie, $template->output;
126 push @directories, "$dirname";
127 foreach my $recursive_dir (@directories) {
128 opendir RECDIR
, $recursive_dir;
129 while ( my $entry = readdir RECDIR
) {
130 push @directories, "$recursive_dir/$entry"
131 if ( -d
"$recursive_dir/$entry" and $entry !~ /^\
./ );
132 $debug and warn "$recursive_dir/$entry";
136 foreach my $dir (@directories) {
137 $results = handle_dir
( $dir, $filesuffix, $template );
138 $handled++ if $results == 1;
140 $total = scalar @directories;
143 #if ($filetype eq 'zip' )
144 $results = handle_dir
( $dirname, $filesuffix, $template, $cardnumber,
146 $handled++ if $results == 1;
150 if ( $results!=1 || %errors ) {
151 $template->param( ERRORS
=> [$results] );
155 map { $filecount += $_->{count
} } @counts;
156 $debug and warn "Total directories processed: $total";
157 $debug and warn "Total files processed: $filecount";
162 TCOUNTS
=> ( $filecount > 0 ?
$filecount : undef ),
164 $template->param( borrowernumber
=> $borrowernumber )
168 elsif ( ( $op eq 'Upload' ) && !$uploadfile ) {
169 warn "Problem uploading file or no file uploaded.";
170 $template->param( cardnumber
=> $cardnumber );
171 $template->param( filetype
=> $filetype );
173 elsif ( $op eq 'Delete' ) {
174 die "Wrong CSRF token"
175 unless Koha
::Token
->new->check_csrf({
176 session_id
=> scalar $input->cookie('CGISESSID'),
177 token
=> scalar $input->param('csrf_token'),
181 Koha
::Patron
::Images
->find( $borrowernumber )->delete;
183 if ( $@
or not $deleted ) {
184 warn "Image for patron '$borrowernumber' has not been deleted";
187 if ( $borrowernumber && !%errors && !$template->param('ERRORS') ) {
188 print $input->redirect(
189 "/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber");
193 csrf_token
=> Koha
::Token
->new->generate_csrf({
194 session_id
=> scalar $input->cookie('CGISESSID'),
197 output_html_with_http_headers
$input, $cookie, $template->output;
201 my ( $dir, $suffix, $template, $cardnumber, $source ) = @_;
202 my ( %counts, %direrrors );
203 $debug and warn "Entering sub handle_dir; passed \$dir=$dir, \$suffix=$suffix";
204 if ( $suffix =~ m/zip/i ) {
205 # If we were sent a zip file, process any included data/idlink.txt files
206 my ( $file, $filename );
208 $debug and warn "Passed a zip file.";
210 while ( my $filename = readdir DIR
) {
211 $file = "$dir/$filename"
212 if ( $filename =~ m/datalink\.txt/i
213 || $filename =~ m/idlink\.txt/i );
215 unless ( open( FILE
, $file ) ) {
216 warn "Opening $dir/$file failed!";
217 $direrrors{'OPNLINK'} = $file;
218 # This error is fatal to the import of this directory contents
219 # so bail and return the error to the caller
223 while ( my $line = <FILE
> ) {
224 $debug and warn "Reading contents of $file";
226 $debug and warn "Examining line: $line";
227 my $delim = ( $line =~ /\t/ ) ?
"\t" : ( $line =~ /,/ ) ?
"," : "";
228 $debug and warn "Delimeter is \'$delim\'";
229 unless ( $delim eq "," || $delim eq "\t" ) {
230 warn "Unrecognized or missing field delimeter. Please verify that you are using either a ',' or a 'tab'";
231 $direrrors{'DELERR'} = 1;
232 # This error is fatal to the import of this directory contents
233 # so bail and return the error to the caller
236 ( $cardnumber, $filename ) = split $delim, $line;
237 $cardnumber =~ s/[\"\r\n]//g; # remove offensive characters
238 $filename =~ s/[\"\r\n\s]//g;
239 $debug and warn "Cardnumber: $cardnumber Filename: $filename";
240 $source = "$dir/$filename";
241 %counts = handle_file
( $cardnumber, $source, $template, %counts );
247 %counts = handle_file
( $cardnumber, $source, $template, %counts );
249 push @counts, \
%counts;
254 my ( $cardnumber, $source, $template, %count ) = @_;
255 $debug and warn "Entering sub handle_file; passed \$cardnumber=$cardnumber, \$source=$source";
256 $count{filenames
} = () if !$count{filenames
};
257 $count{source
} = $source if !$count{source
};
258 $count{count
} = 0 unless exists $count{count
};
261 if ( $filetype eq 'image' ) {
262 $filename = $uploadfilename;
265 $filename = $1 if ( $source && $source =~ /\/([^\
/]+)$/ );
267 if ( $cardnumber && $source ) {
268 # Now process any imagefiles
269 $debug and warn "Source: $source";
270 my $size = ( stat($source) )[7];
271 if ( $size > 550000 ) {
272 # This check is necessary even with image resizing to avoid possible security/performance issues...
273 $filerrors{'OVRSIZ'} = 1;
274 push my @filerrors, \
%filerrors;
275 push @
{ $count{filenames
} },
277 filerrors
=> \
@filerrors,
279 cardnumber
=> $cardnumber
281 $template->param( ERRORS
=> 1 );
282 # this one is fatal so bail here...
285 my ( $srcimage, $image );
286 if ( open( IMG
, "$source" ) ) {
287 $srcimage = GD
::Image
->new(*IMG
);
289 if ( defined $srcimage ) {
291 my $mimetype = 'image/png';
292 # GD autodetects three basic image formats: PNG, JPEG, XPM
293 # we will convert all to PNG which is lossless...
294 # Check the pixel size of the image we are about to import...
295 my ( $width, $height ) = $srcimage->getBounds();
296 $debug and warn "$filename is $width pix X $height pix.";
297 if ( $width > 200 || $height > 300 ) {
298 # MAX pixel dims are 200 X 300...
299 $debug and warn "$filename exceeds the maximum pixel dimensions of 200 X 300. Resizing...";
300 # Percent we will reduce the image dimensions by...
302 if ( $width > 200 ) {
303 # If the width is oversize, scale based on width overage...
304 $percent_reduce = sprintf( "%.5f", ( 140 / $width ) );
307 # otherwise scale based on height overage.
308 $percent_reduce = sprintf( "%.5f", ( 200 / $height ) );
311 sprintf( "%.0f", ( $width * $percent_reduce ) );
313 sprintf( "%.0f", ( $height * $percent_reduce ) );
315 and warn "Reducing $filename by "
316 . ( $percent_reduce * 100 )
317 . "\% or to $width_reduce pix X $height_reduce pix";
318 #'1' creates true color image...
319 $image = GD
::Image
->new( $width_reduce, $height_reduce, 1 );
320 $image->copyResampled( $srcimage, 0, 0, 0, 0, $width_reduce,
321 $height_reduce, $width, $height );
322 $imgfile = $image->png();
324 and warn "$filename is "
326 . " bytes after resizing.";
328 undef $srcimage; # This object can get big...
332 $imgfile = $image->png();
334 and warn "$filename is " . length($imgfile) . " bytes.";
336 undef $srcimage; # This object can get big...
338 $debug and warn "Image is of mimetype $mimetype";
341 my $patron = Koha
::Patrons
->find({ cardnumber
=> $cardnumber });
343 my $image = $patron->image;
344 $image ||= Koha
::Patron
::Image
->new({ borrowernumber
=> $patron->borrowernumber });
346 mimetype
=> $mimetype,
347 imagefile
=> $imgfile,
349 eval { $image->store };
351 # Errors from here on are fatal only to the import of a particular image
352 #so don't bail, just note the error and keep going
353 warn "Database returned error: $@";
354 $filerrors{'DBERR'} = 1;
355 push my @filerrors, \
%filerrors;
356 push @
{ $count{filenames
} },
358 filerrors
=> \
@filerrors,
360 cardnumber
=> $cardnumber
362 $template->param( ERRORS
=> 1 );
365 push @
{ $count{filenames
} },
366 { source
=> $filename, cardnumber
=> $cardnumber };
369 warn "Patron with the cardnumber '$cardnumber' does not exist";
370 $filerrors{'CARDNUMBER_DOES_NOT_EXIST'} = 1;
371 push my @filerrors, \
%filerrors;
372 push @
{ $count{filenames
} },
374 filerrors
=> \
@filerrors,
376 cardnumber
=> $cardnumber
378 $template->param( ERRORS
=> 1 );
382 warn "Unable to determine mime type of $filename. Please verify mimetype.";
383 $filerrors{'MIMERR'} = 1;
384 push my @filerrors, \
%filerrors;
385 push @
{ $count{filenames
} },
387 filerrors
=> \
@filerrors,
389 cardnumber
=> $cardnumber
391 $template->param( ERRORS
=> 1 );
395 warn "Contents of $filename corrupted!";
397 $filerrors{'CORERR'} = 1;
398 push my @filerrors, \
%filerrors;
399 push @
{ $count{filenames
} },
401 filerrors
=> \
@filerrors,
403 cardnumber
=> $cardnumber
405 $template->param( ERRORS
=> 1 );
409 warn "Opening $source failed!";
410 $filerrors{'OPNERR'} = 1;
411 push my @filerrors, \
%filerrors;
412 push @
{ $count{filenames
} },
414 filerrors
=> \
@filerrors,
416 cardnumber
=> $cardnumber
418 $template->param( ERRORS
=> 1 );
422 # The need for this seems a bit unlikely, however, to maximize error trapping it is included
427 : ( $filename ?
"cardnumber" : "cardnumber and filename" )
429 $filerrors{'CRDFIL'} = (
432 : ( $filename ?
"cardnumber" : "cardnumber and filename" )
434 push my @filerrors, \
%filerrors;
435 push @
{ $count{filenames
} },
437 filerrors
=> \
@filerrors,
439 cardnumber
=> $cardnumber
441 $template->param( ERRORS
=> 1 );
448 Original contributor(s) undocumented
450 Database storage, single patronimage upload option, and extensive error trapping contributed by Chris Nighswonger cnighswonger <at> foundations <dot> edu
451 Image scaling/resizing contributed by the same.