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
::Images
;
40 my ($template, $loggedinuser, $cookie)
41 = get_template_and_user
({template_name
=> "tools/picture-upload.tt",
45 flagsrequired
=> { tools
=> 'batch_upload_patron_images'},
49 our $filetype = $input->param('filetype') || '';
50 my $cardnumber = $input->param('cardnumber');
51 our $uploadfilename = $input->param('uploadfile') || '';
52 my $uploadfile = $input->upload('uploadfile');
53 my $borrowernumber = $input->param('borrowernumber');
54 my $op = $input->param('op') || '';
56 #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.
57 # Other parts of this code could be optimized as well, I think. Perhaps the file upload could be done with YUI's upload
60 $debug and warn "Params are: filetype=$filetype, cardnumber=$cardnumber, borrowernumber=$borrowernumber, uploadfile=$uploadfilename";
64 picture-upload.pl - Script for handling uploading of both single and bulk patronimages and importing them into the database.
72 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.
73 Files greater than 100K will be refused. Images should be 140x200 pixels. If they are larger they will be auto-resized to comply.
77 $debug and warn "Operation requested: $op";
79 my ( $total, $handled, $tempfile, $tfh );
83 # Case is important in these operational values as the template must use case to be visually pleasing!
84 if ( ( $op eq 'Upload' ) && $uploadfile ) {
86 output_and_exit
( $input, $cookie, $template, 'wrong_csrf_token' )
87 unless Koha
::Token
->new->check_csrf({
88 session_id
=> scalar $input->cookie('CGISESSID'),
89 token
=> scalar $input->param('csrf_token'),
92 my $dirname = File
::Temp
::tempdir
( CLEANUP
=> 1 );
93 $debug and warn "dirname = $dirname";
95 if ( $uploadfilename =~ m/(\..+)$/i ) {
99 File
::Temp
::tempfile
( SUFFIX
=> $filesuffix, UNLINK
=> 1 );
100 $debug and warn "tempfile = $tempfile";
101 my ( @directories, $results );
103 $errors{'NOTZIP'} = 1
104 if ( $uploadfilename !~ /\.zip$/i && $filetype =~ m/zip/i );
105 $errors{'NOWRITETEMP'} = 1 unless ( -w
$dirname );
106 $errors{'EMPTYUPLOAD'} = 1 unless ( length($uploadfile) > 0 );
109 $template->param( ERRORS
=> [ \
%errors ] );
110 output_html_with_http_headers
$input, $cookie, $template->output;
113 while (<$uploadfile>) {
117 if ( $filetype eq 'zip' ) {
118 qx/unzip $tempfile -d $dirname/;
120 unless ( $exit_code == 0 ) {
121 $errors{'UZIPFAIL'} = $uploadfilename;
122 $template->param( ERRORS
=> [ \
%errors ] );
123 # This error is fatal to the import, so bail out here
124 output_html_with_http_headers
$input, $cookie, $template->output;
127 push @directories, "$dirname";
128 foreach my $recursive_dir (@directories) {
129 opendir RECDIR
, $recursive_dir;
130 while ( my $entry = readdir RECDIR
) {
131 push @directories, "$recursive_dir/$entry"
132 if ( -d
"$recursive_dir/$entry" and $entry !~ /^\
./ );
133 $debug and warn "$recursive_dir/$entry";
137 foreach my $dir (@directories) {
138 $results = handle_dir
( $dir, $filesuffix, $template );
139 $handled++ if $results == 1;
141 $total = scalar @directories;
144 #if ($filetype eq 'zip' )
145 $results = handle_dir
( $dirname, $filesuffix, $template, $cardnumber,
147 $handled++ if $results == 1;
151 if ( $results!=1 || %errors ) {
152 $template->param( ERRORS
=> [$results] );
156 map { $filecount += $_->{count
} } @counts;
157 $debug and warn "Total directories processed: $total";
158 $debug and warn "Total files processed: $filecount";
163 TCOUNTS
=> ( $filecount > 0 ?
$filecount : undef ),
165 $template->param( borrowernumber
=> $borrowernumber )
169 elsif ( ( $op eq 'Upload' ) && !$uploadfile ) {
170 warn "Problem uploading file or no file uploaded.";
171 $template->param( cardnumber
=> $cardnumber );
172 $template->param( filetype
=> $filetype );
174 elsif ( $op eq 'Delete' ) {
175 output_and_exit
( $input, $cookie, $template, 'wrong_csrf_token' )
176 unless Koha
::Token
->new->check_csrf({
177 session_id
=> scalar $input->cookie('CGISESSID'),
178 token
=> scalar $input->param('csrf_token'),
182 Koha
::Patron
::Images
->find( $borrowernumber )->delete;
184 if ( $@
or not $deleted ) {
185 warn "Image for patron '$borrowernumber' has not been deleted";
188 if ( $borrowernumber && !%errors && !$template->param('ERRORS') ) {
189 print $input->redirect(
190 "/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber");
194 csrf_token
=> Koha
::Token
->new->generate_csrf({
195 session_id
=> scalar $input->cookie('CGISESSID'),
198 output_html_with_http_headers
$input, $cookie, $template->output;
202 my ( $dir, $suffix, $template, $cardnumber, $source ) = @_;
203 my ( %counts, %direrrors );
204 $debug and warn "Entering sub handle_dir; passed \$dir=$dir, \$suffix=$suffix";
205 if ( $suffix =~ m/zip/i ) {
206 # If we were sent a zip file, process any included data/idlink.txt files
207 my ( $file, $filename );
209 $debug and warn "Passed a zip file.";
211 while ( my $filename = readdir DIR
) {
212 $file = "$dir/$filename"
213 if ( $filename =~ m/datalink\.txt/i
214 || $filename =~ m/idlink\.txt/i );
216 unless ( open( FILE
, $file ) ) {
217 warn "Opening $dir/$file failed!";
218 $direrrors{'OPNLINK'} = $file;
219 # This error is fatal to the import of this directory contents
220 # so bail and return the error to the caller
224 while ( my $line = <FILE
> ) {
225 $debug and warn "Reading contents of $file";
227 $debug and warn "Examining line: $line";
228 my $delim = ( $line =~ /\t/ ) ?
"\t" : ( $line =~ /,/ ) ?
"," : "";
229 $debug and warn "Delimeter is \'$delim\'";
230 unless ( $delim eq "," || $delim eq "\t" ) {
231 warn "Unrecognized or missing field delimeter. Please verify that you are using either a ',' or a 'tab'";
232 $direrrors{'DELERR'} = 1;
233 # This error is fatal to the import of this directory contents
234 # so bail and return the error to the caller
237 ( $cardnumber, $filename ) = split $delim, $line;
238 $cardnumber =~ s/[\"\r\n]//g; # remove offensive characters
239 $filename =~ s/[\"\r\n\s]//g;
240 $debug and warn "Cardnumber: $cardnumber Filename: $filename";
241 $source = "$dir/$filename";
242 %counts = handle_file
( $cardnumber, $source, $template, %counts );
248 %counts = handle_file
( $cardnumber, $source, $template, %counts );
250 push @counts, \
%counts;
255 my ( $cardnumber, $source, $template, %count ) = @_;
256 $debug and warn "Entering sub handle_file; passed \$cardnumber=$cardnumber, \$source=$source";
257 $count{filenames
} = () if !$count{filenames
};
258 $count{source
} = $source if !$count{source
};
259 $count{count
} = 0 unless exists $count{count
};
262 if ( $filetype eq 'image' ) {
263 $filename = $uploadfilename;
266 $filename = $1 if ( $source && $source =~ /\/([^\
/]+)$/ );
268 if ( $cardnumber && $source ) {
269 # Now process any imagefiles
270 $debug and warn "Source: $source";
271 my $size = ( stat($source) )[7];
272 if ( $size > 550000 ) {
273 # This check is necessary even with image resizing to avoid possible security/performance issues...
274 $filerrors{'OVRSIZ'} = 1;
275 push my @filerrors, \
%filerrors;
276 push @
{ $count{filenames
} },
278 filerrors
=> \
@filerrors,
280 cardnumber
=> $cardnumber
282 $template->param( ERRORS
=> 1 );
283 # this one is fatal so bail here...
286 my ( $srcimage, $image );
287 if ( open( IMG
, "$source" ) ) {
288 $srcimage = GD
::Image
->new(*IMG
);
290 if ( defined $srcimage ) {
292 my $mimetype = 'image/png';
293 # GD autodetects three basic image formats: PNG, JPEG, XPM
294 # we will convert all to PNG which is lossless...
295 # Check the pixel size of the image we are about to import...
296 my ( $width, $height ) = $srcimage->getBounds();
297 $debug and warn "$filename is $width pix X $height pix.";
298 if ( $width > 200 || $height > 300 ) {
299 # MAX pixel dims are 200 X 300...
300 $debug and warn "$filename exceeds the maximum pixel dimensions of 200 X 300. Resizing...";
301 # Percent we will reduce the image dimensions by...
303 if ( $width > 200 ) {
304 # If the width is oversize, scale based on width overage...
305 $percent_reduce = sprintf( "%.5f", ( 140 / $width ) );
308 # otherwise scale based on height overage.
309 $percent_reduce = sprintf( "%.5f", ( 200 / $height ) );
312 sprintf( "%.0f", ( $width * $percent_reduce ) );
314 sprintf( "%.0f", ( $height * $percent_reduce ) );
316 and warn "Reducing $filename by "
317 . ( $percent_reduce * 100 )
318 . "\% or to $width_reduce pix X $height_reduce pix";
319 #'1' creates true color image...
320 $image = GD
::Image
->new( $width_reduce, $height_reduce, 1 );
321 $image->copyResampled( $srcimage, 0, 0, 0, 0, $width_reduce,
322 $height_reduce, $width, $height );
323 $imgfile = $image->png();
325 and warn "$filename is "
327 . " bytes after resizing.";
329 undef $srcimage; # This object can get big...
333 $imgfile = $image->png();
335 and warn "$filename is " . length($imgfile) . " bytes.";
337 undef $srcimage; # This object can get big...
339 $debug and warn "Image is of mimetype $mimetype";
342 my $patron = Koha
::Patrons
->find({ cardnumber
=> $cardnumber });
344 my $image = $patron->image;
345 $image ||= Koha
::Patron
::Image
->new({ borrowernumber
=> $patron->borrowernumber });
347 mimetype
=> $mimetype,
348 imagefile
=> $imgfile,
350 eval { $image->store };
352 # Errors from here on are fatal only to the import of a particular image
353 #so don't bail, just note the error and keep going
354 warn "Database returned error: $@";
355 $filerrors{'DBERR'} = 1;
356 push my @filerrors, \
%filerrors;
357 push @
{ $count{filenames
} },
359 filerrors
=> \
@filerrors,
361 cardnumber
=> $cardnumber
363 $template->param( ERRORS
=> 1 );
366 push @
{ $count{filenames
} },
367 { source
=> $filename, cardnumber
=> $cardnumber };
370 warn "Patron with the cardnumber '$cardnumber' does not exist";
371 $filerrors{'CARDNUMBER_DOES_NOT_EXIST'} = 1;
372 push my @filerrors, \
%filerrors;
373 push @
{ $count{filenames
} },
375 filerrors
=> \
@filerrors,
377 cardnumber
=> $cardnumber
379 $template->param( ERRORS
=> 1 );
383 warn "Unable to determine mime type of $filename. Please verify mimetype.";
384 $filerrors{'MIMERR'} = 1;
385 push my @filerrors, \
%filerrors;
386 push @
{ $count{filenames
} },
388 filerrors
=> \
@filerrors,
390 cardnumber
=> $cardnumber
392 $template->param( ERRORS
=> 1 );
396 warn "Contents of $filename corrupted!";
398 $filerrors{'CORERR'} = 1;
399 push my @filerrors, \
%filerrors;
400 push @
{ $count{filenames
} },
402 filerrors
=> \
@filerrors,
404 cardnumber
=> $cardnumber
406 $template->param( ERRORS
=> 1 );
410 warn "Opening $source failed!";
411 $filerrors{'OPNERR'} = 1;
412 push my @filerrors, \
%filerrors;
413 push @
{ $count{filenames
} },
415 filerrors
=> \
@filerrors,
417 cardnumber
=> $cardnumber
419 $template->param( ERRORS
=> 1 );
423 # The need for this seems a bit unlikely, however, to maximize error trapping it is included
428 : ( $filename ?
"cardnumber" : "cardnumber and filename" )
430 $filerrors{'CRDFIL'} = (
433 : ( $filename ?
"cardnumber" : "cardnumber and filename" )
435 push my @filerrors, \
%filerrors;
436 push @
{ $count{filenames
} },
438 filerrors
=> \
@filerrors,
440 cardnumber
=> $cardnumber
442 $template->param( ERRORS
=> 1 );
449 Original contributor(s) undocumented
451 Database storage, single patronimage upload option, and extensive error trapping contributed by Chris Nighswonger cnighswonger <at> foundations <dot> edu
452 Image scaling/resizing contributed by the same.