[56] | 1 | # ipdb/cgi-bin/CustIDCK.pm
|
---|
| 2 | # External Customer ID checker stub
|
---|
| 3 | ###
|
---|
| 4 | # SVN revision info
|
---|
| 5 | # $Date: 2005-02-07 17:00:47 +0000 (Mon, 07 Feb 2005) $
|
---|
| 6 | # SVN revision $Rev: 156 $
|
---|
| 7 | # Last update by $Author: kdeugau $
|
---|
| 8 | ###
|
---|
| 9 |
|
---|
| 10 | package CustIDCK;
|
---|
| 11 |
|
---|
| 12 | use strict;
|
---|
| 13 | use warnings;
|
---|
| 14 | use Exporter;
|
---|
| 15 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
|
---|
| 16 | use DBI;
|
---|
| 17 |
|
---|
| 18 | $VERSION = 1.00;
|
---|
| 19 | @ISA = qw(Exporter);
|
---|
| 20 | @EXPORT = ();
|
---|
| 21 | @EXPORT_OK = qw ( &custid_exist );
|
---|
| 22 |
|
---|
| 23 | # this is really an example stub, and should be replaced by
|
---|
| 24 | # the local admin on installation
|
---|
| 25 | sub custid_exist {
|
---|
| 26 | my $custid = shift;
|
---|
| 27 |
|
---|
| 28 | return 1 if $custid =~ /^STAFF$/;
|
---|
| 29 | return 1 if $custid =~ /^6750400$/; # just in case some later change might block this
|
---|
| 30 | return 1 if $custid =~ /^\d{7}$/;
|
---|
| 31 | return 1 if $custid =~ /^\d{10}$/;
|
---|
| 32 |
|
---|
| 33 | # some example code for a database check
|
---|
[113] | 34 | # Try to catch failures to connect. If the remote server is up but
|
---|
| 35 | # not responding (this has HAPPENED) we need to break out rather than hanging.
|
---|
[114] | 36 | my $dbh;
|
---|
[113] | 37 | eval {
|
---|
[156] | 38 | local $SIG{ALRM} = sub { die "failed connection to newbilling!!" };
|
---|
[113] | 39 | alarm 3; # 3-second timeout. This may be too aggressive.
|
---|
[151] | 40 |
|
---|
| 41 | eval {
|
---|
[156] | 42 | $dbh = DBI->connect ("DBI:Pg:host=newbilling;dbname=custids", "cidcheck", "c1dch4ck");
|
---|
| 43 | die "failed connection to newbilling!!" if !$dbh;
|
---|
[151] | 44 | # Not certain if this is needed here. It doesn't seem to be.
|
---|
| 45 | # $dbh->ping; # Gotta do this to "force" a "failure". NRGH.
|
---|
| 46 | };
|
---|
[113] | 47 | alarm 0; # cancel the alarm
|
---|
[151] | 48 | $dbh->ping; # Gotta do this to "force" a "failure". NRGH.
|
---|
[113] | 49 | };
|
---|
| 50 | alarm 0; # avoid race conditions. May not be needed here. (Hah!)
|
---|
[156] | 51 | if ($@ && $@ !~ /failed connection to newbilling!!/) {
|
---|
[113] | 52 | $CustIDCK::Error = 1;
|
---|
[156] | 53 | $CustIDCK::ErrMsg = "Failed connection to newbilling DB host! Unable to verify CustIDs.";
|
---|
[113] | 54 | return 0;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[151] | 57 | # We should have a valid DB connection by now.
|
---|
| 58 | my $sth = $dbh->prepare("SELECT custid FROM custid WHERE custid = '$custid'");
|
---|
[56] | 59 | $sth->execute;
|
---|
| 60 | if ($dbh->err) {
|
---|
| 61 | $CustIDCK::Error = 1;
|
---|
| 62 | $CustIDCK::ErrMsg = $dbh->errstr();
|
---|
| 63 | $sth->finish;
|
---|
| 64 | $dbh->disconnect;
|
---|
| 65 | return 0;
|
---|
| 66 | }
|
---|
| 67 | my $hr = $sth->fetchrow_hashref();
|
---|
| 68 | my $status = 0;
|
---|
| 69 | $status = 1 if ( $hr->{custid} );
|
---|
| 70 | $sth->finish;
|
---|
| 71 | $dbh->disconnect;
|
---|
| 72 | return $status;
|
---|
| 73 |
|
---|
| 74 | return 0;
|
---|
| 75 | # Stubs for error messages
|
---|
| 76 | $CustIDCK::Error = 1 if 1 == 0;
|
---|
| 77 | $CustIDCK::ErrMsg = "bad foo-frob: 1 == 0";
|
---|
| 78 | }
|
---|