2
0

cnam.cgi 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/perl
  2. #
  3. # OpenCNAM front end because they only take 10 digits and can't filter 11 on their side.
  4. #
  5. use Data::Dumper;
  6. use CGI qw/:standard/;
  7. use LWP::UserAgent;
  8. use SDBM_File;
  9. use Fcntl;
  10. my %params = map { $_ => get_data( $_ ) } param;
  11. $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0, timeout => 3 });
  12. sub get_data {
  13. my $name = shift;
  14. my @values = param( $name );
  15. return @values > 1
  16. ? \@values
  17. : $values[0];
  18. }
  19. print "Content-Type: text/plain\n\n";
  20. tie (my %cache, 'SDBM_File', 'cnam.dbm', O_RDWR|O_CREAT, 0640) || die $!;
  21. my $number = $params{number};
  22. if($number =~ m/1?\d{10}/) {
  23. if($number =~ m/^1(\d{10})$/) {
  24. $number = $1;
  25. }
  26. if($cache{"$number"}) {
  27. print $cache{"$number"};
  28. untie %cache;
  29. exit;
  30. }
  31. my $url = "https://api.opencnam.com/v1/phone/$number?format=text";
  32. my $res = $ua->get( $url );
  33. my $code = $res->code;
  34. if ($code eq '200') {
  35. my $content = $res->decoded_content;
  36. $cache{"$number"} = $content;
  37. print $content;
  38. } else {
  39. print "UNKNOWN";
  40. }
  41. }
  42. untie %cache;