#!/usr/bin/perl
#
# lookup.pl, v3.0
# Copyright (c) 1996 SurfUtah.Com
# written by Rus Berrett
# edited by Tadao Mochizuki webmaster@creative-japan.org
# check domain name availability (any top level domain)
# 

require "util.pl";

$local = 1;

# @valid_referers = ("INSERT DOMAIN NAME HERE FOR REFERER CHECK");
# &check_referer(1);

# where does everything live
$whois = "/usr/bin/whois";
$iwhois = "iwhois.pl";
$nslookup = "/usr/bin/nslookup";
$domaincodes = "domain-codes";

# check for existence of necessary files
unless (-e $whois) {
  &print_header_info("Domain Name Lookup Error: whois not found");
  print "<h3><u>Domain Name Lookup Error: whois not found</u></h3>\n";
  print "Request failed due to improper script configuration.\n";
  print "Cannot find \"$whois\".  Please check your configuration\n";
  print "settings at the top of the lookup.pl file.\n<p>\n";
  &print_footer_info();
  exit(1);  
}
unless (-e $iwhois) {
  &print_header_info("Domain Name Lookup Error: iwhois not found");
  print "<h3><u>Domain Name Lookup Error: iwhois not found</u></h3>\n";
  print "Request failed due to improper script configuration.\n";
  print "Cannot find \"$iwhois\".  Please check your configuration\n";
  print "settings at the top of the lookup.pl file.\n<p>\n";
  print "iwhois must be installed manually by support.\n<p>\n";
  &print_footer_info();
  exit(1);  
}
unless (-e $nslookup) {
  &print_header_info("Domain Name Lookup Error: nslookup not found");
  print "<h3><u>Domain Name Lookup Error: nslookup not found</u></h3>\n";
  print "Request failed due to improper script configuration.\n";
  print "Cannot find \"$nslookup\".  Please check your configuration\n";
  print "settings at the top of the lookup.pl file.\n<p>\n";
  &print_footer_info();
  exit(1);  
}
unless (-e $domaincodes) {
  &print_header_info("Domain Name Lookup Error: domain codes not found");
  print "<h3><u>Domain Name Lookup Error: domain codes not found</u></h3>\n";
  print "Request failed due to improper script configuration.\n";
  print "Cannot find \"$domaincodes\".  Please check your configuration\n";
  print "settings at the top of the lookup.pl file.\n<p>\n";
  &print_footer_info();
  exit(1);  
}

# get the host name to lookup
&parse_form_data(*array);

# check for name
if ($array{'name'} eq "") {
  &print_header_info("Domain Name Lookup Error: Name Not Provided");
  print "<h3><u>ドメイン名 入力エラー</u></h3>\n";
  print "検索対象ドメイン名が入力されていません。 \n";
  print "前画面に戻りドメイン名を入力してから再度検索してください。\n<p>\n";
  &print_footer_info();
  exit(0);
}

# check for errors in name
if ($array{'name'} =~ /[^A-Za-z0-9\.\-]/) {
  &log_invalid_request("$array{'name'}");
  &print_header_info("Domain Name Lookup Error: Name Not Valid");
  print "<h3><u>ドメイン名 入力エラー</u></h3>\n";
  print "入力文字が正しくありません。\n";
  print "使える文字は半角英数文字と半角ハイホンと半角ピリオドだけです。\n";
  print "前画面に戻りドメイン名を入力してから再度検索してください。\n<p>\n";
  &print_footer_info();
  exit(0);
}

# check for valid tld
@subd = split(/\./, $array{'name'});
$tld = $subd[$#subd];
$tld =~ tr/A-Z/a-z/;
$ftld = "";
open(DCF, "$domaincodes");
while (<DCF>) {
  tr/A-Z/a-z/;
  $pin = index($_, "\ ");
  $ftld = substr($_, 0, $pin);
  if ($ftld eq $tld) {
    last;
  }
  $ftld = "";
}
close(DCF);
if ($ftld eq "") {
  &print_header_info("Domain Name Lookup Error: Unknown Top Level Domain");
  print "<h3><u>ドメイン名 入力エラー</u></h3>\n";
  print "検索対象以外のドメイン名が入力されています。\n";
  print "間違っているのは $tld, の部分です。\n";
  print "前画面に戻りドメイン名を入力してから再度検索してください。\n<p>\n";
  &print_footer_info();
  exit(0);
}

# get rid of any useless prefix, braindead check for braindead users
if (($array{'name'} =~ /^www\./) || ($array{'name'} =~ /^ftp\./) ||
    ($array{'name'} =~ /^smtp\./) || ($array{'name'} =~ /^pop\./) ||
    ($array{'name'} =~ /^imap\./) || ($array{'name'} =~ /^ns\./)) {
  $pin = index($array{'name'}, "\.");
  $array{'name'} = substr($array{'name'}, $pin);

}
@tadao = split(/\./, $array{'name'});
$origin = $tadao[0];
&core;

if ($result eq "1") {
  print "Content-type: text/html\n\n";
  &header;

  print "<center><table border width = \"90%\" cellpadding=10><tr><td bgcolor=\"#bee37f\">";
  print "<center><h3>";
  print "<font color=\"#1965ac\">$array{'name'}</font>\n";
  print "<font color = \"#aa0000\">  はすでに登録されています。</font>\n";
	unless ($array{'name'} =~ /\.co\.jp/) {
		$new_targetcojp =  "$origin" . ".co.jp";
		&cojp;
	}
	unless ($array{'name'} =~ /\.or\.jp/) {
		$new_targetorjp =  "$origin" . ".or.jp";
		&orjp;
	}
	unless ($array{'name'} =~ /\.ne\.jp/) {
		$new_targetnejp =  "$origin" . ".ne.jp";
		&nejp;
	}
	unless ($array{'name'} =~ /\.gr\.jp/) {
		$new_targetgrjp =  "$origin" . ".gr.jp";
		&grjp;
	}
	if (($array{'name'} =~ /\.co\.jp/) || ($array{'name'} =~ /\.or\.jp/) ||($array{'name'} =~ /\.ne\.jp/) ||($array{'name'} =~ /\.gr\.jp/)) {
		$new_targetjp =  "$origin" . ".jp";
		&jp;
	} elsif ($array{'name'} !~ /\.jp/) {
		$new_targetjp =  "$origin" . ".jp";
		&jp;
	}
	unless ($array{'name'} =~ /\.com/) {
    $new_targetc =  "$origin" . ".com";
    &com;
	}
	unless ($array{'name'} =~ /\.org/) {
    $new_targeto =  "$origin" . ".org";
    &org;
	}
	unless ($array{'name'} =~ /\.net/) {
    $new_targetn =  "$origin" . ".net";
    &net;
	}
	unless ($array{'name'} =~ /\.info/) {
    $new_targetinfo =  "$origin" . ".info";
    &info;
	}
	unless ($array{'name'} =~ /\.biz/) {
    $new_targetbiz =  "$origin" . ".biz";
    &biz;
	}
  if (($flg_c eq "1") || ($flg_o eq "1") || ($flg_n eq "1") || ($flg_info eq "1") || ($flg_biz eq "1") || ($flg_cojp eq "1") || ($flg_orjp eq "1") || ($flg_nejp eq "1") || ($flg_grjp eq "1") || ($flg_jp eq "1")) {
    print "<br><br><font color = \"#aa0000\">ただし、以下のドメイン名は使用可能です。</font></h3>\n";
    print "  <table>\n";
    print "    <tr>\n";
    print "      <td>\n";
    print "  <ul>\n";
    if ($flg_c eq "1") {
      print "  <li>  $new_targetc\n";
    }
    if ($flg_o eq "1") {
      print "  <li>  $new_targeto\n";
    }
		if ($flg_n eq "1") {
			print "  <li>  $new_targetn\n";
		}
		if ($flg_info eq "1") {
			print "  <li>  $new_targetinfo\n";
		}
		if ($flg_biz eq "1") {
			print "  <li>  $new_targetbiz\n";
		}
		if ($flg_cojp eq "1") {
			print "  <li>  $new_targetcojp\n";
		}
		if ($flg_orjp eq "1") {
			print "  <li>  $new_targetorjp\n";
		}
		if ($flg_nejp eq "1") {
			print "  <li>  $new_targetnejp\n";
		}
		if ($flg_grjp eq "1") {
			print "  <li>  $new_targetgrjp\n";
		}
		if ($flg_jp eq "1") {
			print "  <li>  $new_targetjp\n";
		}
		print "  </ul>\n";
		print "  </td>\n";
		print "  </tr>\n";
		print "  </table>\n";
	}
	print "  </td></tr></table></center>\n";
	&footer;
} else {
  print "Content-type: text/html\n\n";
  print "<html><META HTTP-EQUIV=\"Content-type\" CONTENT=\"text/html; charset=x-euc-jp\">";
  print "<body><br><center><table width=\"95%\"><tr><td><center><table border width = \"90%\" cellpadding=10><tr><td bgcolor=\"#bee37f\">";
  print "<center><h3>";
  print "<font color=\"#1965ac\">$array{'name'}</font><font color = \"#aa0000\">  は登録可能です。</font>\n";
  print "  </h3></td></tr></table></center></td></tr></table></center></body></html>\n";
}
##############################################################################
sub core {
  # things seem ok, print out a header
  #&print_header_info("Domain Name Lookup: $array{'name'}");
  #print "<h3><u>Domain Name Lookup: $array{'name'}</u></h3>\n";

  open (TEST,">>temp.txt");
  # do the whois first
  #print "<ul><pre>";
  $command = "$whois \"$array{'name'}\" 2>&1";
  open(SHELL, "$command |");
  while(<SHELL>) {
    #print $_;
    print TEST "$_";
  }
  close(SHELL);
  #print "</pre></ul>\n\n";

  #print "<p><ul><hr></ul><p>\n";

  # do the nslookup second
  $command = "$nslookup -timeout=2 -q=ns $array{'name'} 2>&1";
  open(SHELL, "$command |");
  while(<SHELL>) {
    # looking for authoritative nameserver
    if (/^Authoritative answers/) {
      $_ = <SHELL>;
      $authns = (split(/=/))[1];
      last;
    }
  }
  close(SHELL);
  #print "<ul><pre>";
  $command = "$nslookup -timeout=2 -q=any $array{'name'} $authns 2>&1";
  open(SHELL, "$command |");
  while(<SHELL>) {
    #print $_;
    print TEST "$_";

  }
  close(SHELL);
  #print "</pre></ul>\n\n";

  #print "<p>\n";

  #&print_footer_info();
  #exit(0);
  close(SHELL);
  close(TEST);
  open (TEST,"temp.txt");
  while (<TEST>) {
    tr/A-Z/a-z/;
    if ((/^no match/i) || (/^NOT FOUND/i)) {
      $result = "-1";
      last;
    } else {
      $result = "1";
    }
  }
  close(TEST);
  open (TEST,">temp.txt");
  print TEST "\n";
  close(TEST);
}

sub com {
  undef $array{'name'};
  $array{'name'} = $new_targetc;
  &core;
  if($result eq "1") {
    $flg_c = "0";
  } else {
    $flg_c = "1";
  }
}
sub org {
  undef $array{'name'};
  $array{'name'} = $new_targeto;
  &core;
  if($result eq "1") {
    $flg_o = "0";
  } else {
    $flg_o = "1";
  }
}
sub net {
  undef $array{'name'};
  $array{'name'} = $new_targetn;
  &core;
  if($result eq "1") {
    $flg_n = "0";
  } else {
    $flg_n = "1";
  }
}
sub info {
  undef $array{'name'};
  $array{'name'} = $new_targetinfo;
  &core;
  if($result eq "1") {
    $flg_info = "0";
  } else {
    $flg_info = "1";
  }
}
sub biz {
  undef $array{'name'};
  $array{'name'} = $new_targetbiz;
  &core;
  if($result eq "1") {
    $flg_biz = "0";
  } else {
    $flg_biz = "1";
  }
}
sub cojp {
  undef $array{'name'};
  $array{'name'} = $new_targetcojp;
  &core;
  if($result eq "1") {
    $flg_cojp = "0";
  } else {
    $flg_cojp = "1";
  }
}
sub orjp {
  undef $array{'name'};
  $array{'name'} = $new_targetorjp;
  &core;
  if($result eq "1") {
    $flg_orjp = "0";
  } else {
    $flg_orjp = "1";
  }
}
sub nejp {
  undef $array{'name'};
  $array{'name'} = $new_targetnejp;
  &core;
  if($result eq "1") {
    $flg_nejp = "0";
  } else {
    $flg_nejp = "1";
  }
}
sub grjp {
  undef $array{'name'};
  $array{'name'} = $new_targetgrjp;
  &core;
  if($result eq "1") {
    $flg_grjp = "0";
  } else {
    $flg_grjp = "1";
  }
}
sub jp {
  undef $array{'name'};
  $array{'name'} = $new_targetjp;
  &core;
  if($result eq "1") {
    $flg_jp = "0";
  } else {
    $flg_jp = "1";
  }
}
sub header {
	open (HEADER,"header.txt") || print "cannot find header file\n";
	while (<HEADER>) {
		chomp;
		print "$_\n";
	}
}
sub footer {
	open (FOOTER,"footer.txt") || print "cannot find footer file\n";
	while (<FOOTER>) {
		chomp;
		print "$_\n";
	}
}

##############################################################################
# eof test.pl


