<?php

/*
    Copyright 2009 Michael Sumner
    Email: msumner@dnmedia.com
    Web: http://www.DNMedia.com
    
    This program 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 3 of the License, or
    (at your option) any later version.
    
    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
*/ 

if(!isset($_POST['Submit']))
{
    echo 
'<form method="POST" action="' $_SERVER['PHP_SELF'] . '">'
            
'Enter the target TLD with leading "." (.com, .co.uk, etc).<br />'
            
'<input type="text" name="target"><br /><br />'
            
'Enter the TLD to check availability on.<br />'
            
'<input type="text" name="check"><br /><br />'
            
'<input type="submit" name="Submit" value="Submit">';
}
else
{
    if(!(
$fp fopen('top-1m.csv''r'))){ die('Cannot open file top-1m.csv'); }

    while(
$line fgets($fp4096))
    {
        
$line trim($line);
        
        
$pieces explode(','$line);
        
$rank $pieces[0];
        
$domain $pieces[1];
        
$target $_POST['target'];
        
$check $_POST['check'];

        if(
substr($domainstrlen($domain) - strlen($target)) == $target)
        {
            
$pieces explode("."$domain);
            
$domain $pieces[0] . $check;
            
            if(
is_avail($domain))
            {
                echo 
$domain " is available.  Buy at <a href=\"http://affiliates.moniker.com/pub/Affiliates?affiliate_id=2176&landingpage=domaincheck&domain=$domain\" target=\"_blank\">Moniker</a> or "
                                
"<a href=\"http://www.godaddy.com/gdshop/registrar/search.asp?isc=cjchst002&domainToCheck=" $pieces[0] . "&tld=" $check "&checkAvail=1\" target=\"_blank\">Godaddy</a>. "
                                
"Target Alexa rank of $rank.<br/>";
            }
        }
    }
    
    echo 
$count;
    
    
fclose($fp);
}

function 
is_avail($domain)
{    
    
$pieces explode("."$domain);
    
$server $pieces[1] . ".whois-servers.net";
    
$fp fsockopen($server43$errno$errstr10);
    
$result "";
    if(
$fp === FALSE){ return FALSE; }
    
fputs($fp$domain "\r\n");    
    while(!
feof($fp)){ $result .= fgets($fp128); }
    
fclose($fp);
    
    return ((
stristr($result'No match for "' $domain '"') !== FALSE) || ($result == "NOT FOUND\n")) ? TRUE FALSE;
}

?>