X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=C4%2FInput.pm;h=38042421da1a224feb0c9721058d1cbb5085a8d1;hb=7134663f48e81260204bbc3715d0e06af41590ca;hp=153e282ac4db5f7fb452fc7737650695b57da48f;hpb=cceee9e230b4c30c014013ecc0120ae1a935c95b;p=koha_fer diff --git a/C4/Input.pm b/C4/Input.pm index 153e282ac4..38042421da 100644 --- a/C4/Input.pm +++ b/C4/Input.pm @@ -1,93 +1,122 @@ package C4::Input; #assumes C4/Input + +# Copyright 2000-2002 Katipo Communications +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + use strict; +use warnings; + require Exporter; +use C4::Context; +use CGI; use vars qw($VERSION @ISA @EXPORT); # set the version for version checking -$VERSION = 0.01; +$VERSION = 3.07.00.049; + +=head1 NAME + +C4::Input - Miscellaneous sanity checks + +=head1 SYNOPSIS + + use C4::Input; + +=head1 DESCRIPTION + +This module provides functions to see whether a given library card +number or ISBN is valid. + +=head1 FUNCTIONS + +=over 2 + +=cut @ISA = qw(Exporter); @EXPORT = qw( - &checkflds &checkdigit &checkvalidisbn + &checkdigit ); - -sub checkflds { - my ($env,$reqflds,$data) = @_; - my $numrflds = @$reqflds; - my @probarr; - my $i = 0; - while ($i < $numrflds) { - if ($data->{@$reqflds[$i]} eq "") { - push(@probarr, @$reqflds[$i]); - } - $i++ - } - return (\@probarr); -} - -sub checkdigit { - my ($env,$infl) = @_; - $infl = uc $infl; - my @weightings = (8,4,6,3,5,2,1); - my $sum; - my $i = 1; - my $valid = 0; - # print $infl."
"; - while ($i <8) { - my $temp1 = $weightings[$i-1]; - my $temp2 = substr($infl,$i,1); - $sum = $sum + ($temp1*$temp2); -# print "$sum $temp1 $temp2
"; - $i++; - } - my $rem = ($sum%11); - if ($rem == 10) { - $rem = "X"; - } - #print $rem."
"; - if ($rem eq substr($infl,8,1)) { - $valid = 1; - } - return $valid; + +=item checkdigit + + $valid = &checkdigit($cardnumber $nounique); + +Takes a card number, computes its check digit, and compares it to the +checkdigit at the end of C<$cardnumber>. Returns a true value iff +C<$cardnumber> has a valid check digit. + +=cut + +#' +sub checkdigit ($;$) { + + my ($infl, $nounique) = @_; + $infl = uc $infl; + + # Check to make sure the cardnumber is unique + + #FIXME: We should make the error for a nonunique cardnumber + #different from the one where the checkdigit on the number is + #not correct + + unless ( $nounique ) + { + my $query=qq{SELECT * FROM borrowers WHERE cardnumber=?}; + my $sth=C4::Context->prepare($query); + $sth->execute($infl); + my %results = $sth->fetchrow_hashref(); + if ( $sth->rows != 0 ) + { + return 0; + } + } + if (C4::Context->preference("checkdigit") eq "none") { + return 1; + } + + my @weightings = (8,4,6,3,5,2,1); + my $sum; + foreach my $i (1..7) { + my $temp1 = $weightings[$i-1]; + my $temp2 = substr($infl,$i,1); + $sum += $temp1 * $temp2; + } + my $rem = ($sum%11); + if ($rem == 10) { + $rem = "X"; + } + if ($rem eq substr($infl,8,1)) { + return 1; + } + return 0; } # sub checkdigit -#-------------------------------------- -# Determine if a number is a valid ISBN number, according to length -# of 10 digits and valid checksum -sub checkvalidisbn { - use strict; - my ($q)=@_ ; # Input: ISBN number - - my $isbngood = 0; # Return: true or false - - $q=~s/x$/X/g; # upshift lower case X - $q=~s/[^X\d]//g; - $q=~s/X.//g; - if (length($q)==10) { - my $checksum=substr($q,9,1); - my $isbn=substr($q,0,9); - my $i; - my $c=0; - for ($i=0; $i<9; $i++) { - my $digit=substr($q,$i,1); - $c+=$digit*(10-$i); - } - $c=int(11-($c/11-int($c/11))*11+.1); - ($c==10) && ($c='X'); - if ($c eq $checksum) { - $isbngood=1; - } else { - $isbngood=0; - } - } else { - $isbngood=0; - } # if length good - - return $isbngood; - -} # sub checkvalidisbn - - END { } # module clean-up code here (global destructor) + +1; +__END__ + +=back + +=head1 AUTHOR + +Koha Development Team + +=cut