<?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/>.
*/ 

session_start();

$user "YOUR_DYNADOT_USERNAME_GOES_HERE";
$pass "YOUR_DYNADOT_PASSWORD_GOES_HERE";
$file "drops.txt";

// MODIFY BELOW THIS LINE AT YOUR OWN RISK //////////////////////////////////////////////////////

$result FALSE;
$domains explode("\n"str_replace(array("\r\n""\r"), "\n"file_get_contents($file)));

$ch curl_init();
set_opts($ch);
curl_setopt($chCURLOPT_HTTPGETTRUE);
curl_setopt($chCURLOPT_URL'http://www.dynadot.com/?p6ZJ8oEy9V9U');
curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
set_opts($ch);

if(!isset(
$_SESSION['logged_in']))
{
    
log_in($ch$user$pass);
}
    
while(
count($domains) != 0)
{
    
$domain trim(array_shift($domains));
    
$count++;
    if(!
is_avail_safe($domain))
    {
        
$domains[] = $domain;
    }
    else
    {
        
$result is_avail($ch$domain);
            
        if(
$result != FALSE)
        {
            
add_to_cart($chgrab_input_name($domain$result), grab_input_value($domain$result));
            
check_out($ch);
            echo 
"Attempted to register $domain.<br />";
        }
        else
        {
            echo 
"Failed to register $domain.<br />";
        }
    }
}

function 
set_opts(&$ch)
{
    
$user_agent "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
    
curl_setopt($chCURLOPT_POST1);
    
curl_setopt($chCURLOPT_SSL_VERIFYHOST2);
    
curl_setopt($chCURLOPT_USERAGENT$user_agent);
    
curl_setopt($chCURLOPT_SSL_VERIFYPEERFALSE);
    
curl_setopt($chCURLOPT_COOKIEJARsession_name() . '=' session_id());
    
curl_setopt($chCURLOPT_COOKIEFILEsession_name() . '=' session_id());
    
curl_setopt($chCURLOPT_RETURNTRANSFER1);
}

function 
log_in($ch$user$pass)
{
    
$url 'https://www.dynadot.com/account/signin.html';
    
$params 'signin_username=' $user '&signin_password=' $pass '&signin=Sign%20In';    
    
curl_setopt($chCURLOPT_POSTFIELDS$params);
    
curl_setopt($chCURLOPT_URL$url);
    
curl_exec($ch);
    
$_SESSION['logged_in'] = 1;
}

function 
is_avail_safe($domain)
{    
    
$pieces explode("."$domain);
    
$ext = (count($pieces) > 2) ? $pieces[count($pieces) - 2] . "." $pieces[count($pieces) - 1] : $pieces[1];
    
$server "$ext.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') !== FALSE) || (strtolower($result) == "not found\n")) ? true false;
}

function 
is_avail($ch$domain)
{
    
$url 'https://www.dynadot.com/account/search/search.html';
    
$params 'domain=' $domain '&search=Search';    
    
curl_setopt($chCURLOPT_POSTFIELDS$params);
    
curl_setopt($chCURLOPT_URL$url);
    
$result curl_exec($ch);
        
    
$result strstr($result'<td class="subjectTitle">Search Results</td>');
    
$piece strstr($result$domain);
    
$piece strstr($piece'<td valign="top">');
    
$status substr($piece17strpos($piece'</td>') - 17);

    return (
$status == "Available") ? $result FALSE;
}

function 
grab_input_name($domain, &$result)
{
    
$result substr($result0strpos($result$domain));
    
$result substr($resultstrrpos($result'domain_'));
    return 
substr($result0strpos($result'"'));
}

function 
grab_input_value($domain, &$result)
{
    
$result substr($resultstrrpos($result'value=') + 7);
    return 
substr($result0strpos($result'"'));
}

function 
add_to_cart($ch$name$value)
{
    
$url 'https://www.dynadot.com/account/search/search.html';
    
$params $name '=' $value '&add_submit=Add%20and%20Checkout';
    
curl_setopt($chCURLOPT_POSTFIELDS$params);
    
curl_setopt($chCURLOPT_URL$url);
    
curl_exec($ch);
}

function 
check_out($ch)
{
    
$url 'https://www.dynadot.com/order/submit.html';
    
$params 'submit=Submit%20my%20Order';
    
curl_setopt($chCURLOPT_POSTFIELDS$params);
    
curl_setopt($chCURLOPT_URL$url);
    
curl_exec($ch);
}

?>