<?php

/*
    Copyright 2015 Michael Sumner
    Email: michael@namebio.com
    Web: https://namebio.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/>.
*/ 

define("NJ_USER""USER");
define("NJ_PASS""PASS");

// Edit below this line at your own risk. ////////////////////////////////////////////////

$ch my_curl_setopts(curl_init());
log_in($ch);
delete_backorders($ch);

function 
log_in($ch)
{
    
$url 'https://www.namejet.com/Pages/Login.aspx';
    
    
curl_setopt($chCURLOPT_URL$url);
    
$html curl_exec($ch);
    
$asp_vars parse_asp_vars($html);
    
    
$params =     '__EVENTTARGET=' rawurlencode($asp_vars['__EVENTTARGET']) . 
                
'&__EVENTARGUMENT=' rawurlencode($asp_vars['__EVENTARGUMENT']) . 
                
'&__VIEWSTATE=' rawurlencode($asp_vars['__VIEWSTATE']) .
                
'&__VIEWSTATEGENERATOR=' rawurlencode($asp_vars['__VIEWSTATEGENERATOR']) .
                
'&__EVENTVALIDATION=' rawurlencode($asp_vars['__EVENTVALIDATION']) . 
                
'&ctl00%24ContentPlaceHolder1%24txtUsername=' NJ_USER 
                
'&ctl00%24ContentPlaceHolder1%24txtPassword=' NJ_PASS 
                
'&ctl00%24ContentPlaceHolder1%24btnSubmit=Login';    
    
    
curl_setopt($chCURLOPT_POST1);
    
curl_setopt($chCURLOPT_REFERER'https://www.namejet.com/Pages/Login.aspx?');
    
curl_setopt($chCURLOPT_POSTFIELDS$params);
    
curl_setopt($chCURLOPT_URL$url);
    
$html curl_exec($ch);
}

function 
delete_backorders($ch)
{
    
$run true;
    
$url 'http://www.namejet.com/Pages/MyAccount/MyBackordersAll.aspx';
    
curl_setopt($chCURLOPT_URL$url);
    
$html curl_exec($ch);

    if(
stripos($html'<a disabled="disabled">Last</a>') === FALSE){
        
$params '__EVENTTARGET=ctl00%24ContentPlaceHolder1%24TopDomainsDataPager%24ctl02%24ctl01' add_asp_vars($html);
        
curl_setopt($chCURLOPT_POST1);
        
curl_setopt($chCURLOPT_POSTFIELDS$params);
        
$html curl_exec($ch);
    }

    do{
        if(
stripos($html'<a disabled="disabled">Prev</a>') !== FALSE){ $run false; }
        
$backorders parse_backorders($html);
        if(
count($backorders) > 0){ $num_removed remove($html$backorders$ch); }

        
$params '__EVENTTARGET=ctl00%24ContentPlaceHolder1%24TopDomainsDataPager%24ctl00%24ctl01' add_asp_vars($html);
        
curl_setopt($chCURLOPT_POSTFIELDS$params);
        
$html curl_exec($ch);
    }while(
$run);
}

function 
remove($html$backorders$ch)
{
    
$backorders_removed 0;
    
$params '__EVENTTARGET=ctl00%24ContentPlaceHolder1%24btnRemoveDomain' add_asp_vars($html);
    
    for(
$i 0$i count($backorders); $i++){
        if(
$backorders[$i]['type'] == "Wish List"){
            echo 
"Attempting to remove backorder for " $backorders[$i]['domain'] . ".<br />";
            
$params .=     '&ctl00%24ContentPlaceHolder1%24ListView1%24ctrl' $i '%24CheckBox2=on' 
                        
'&ctl00%24ContentPlaceHolder1%24ListView1%24ctrl' $i '%24hdnDomainName=' $backorders[$i]['domain'] . 
                        
'&ctl00%24ContentPlaceHolder1%24ListView1%24ctrl' $i '%24hdnEndDate=' $backorders[$i]['closing_time'] . 
                        
'&ctl00%24ContentPlaceHolder1%24ListView1%24ctrl' $i '%24hdnMinimumBid=' round($backorders[$i]['starting_bid'], 4) . 
                        
'&ctl00%24ContentPlaceHolder1%24ListView1%24ctrl' $i '%24ReservePrice=0' 
                        
'&ctl00%24ContentPlaceHolder1%24ListView1%24ctrl' $i '%24IsReserve=0' 
                        
'&ctl00%24ContentPlaceHolder1%24ListView1%24ctrl' $i '%24DefaultBid=69.0000' 
                        
'&ctl00%24ContentPlaceHolder1%24ListView1%24ctrl' $i '%24hdnLastUpdate=' rawurlencode($backorders[$i]['update']);
            
$backorders_removed++;
        }
    }

    
curl_setopt($chCURLOPT_POST1);
    
curl_setopt($chCURLOPT_POSTFIELDS$params);
    
$html curl_exec($ch);
        
    return 
$backorders_removed;
}

function 
parse_backorders($html)
{
    
$backorders = array();
    
    while(
$html strstr($html'<td class="main">'))
    {
        
$html strstr($html'class="sLg">'); $html strstr($html"<");
        
$domain parse_substr($html">""</");
        
$type parse_substr($html"Type:</span>""</div>");
        
$update parse_substr($html"Update:</span>""<input");
        
$bids parse_substr($html"Bids:</span>""</div>");
        
$bid parse_substr($html"Bid:</span>""</div>");
        
$time parse_substr($html"Time:</span>","</div>");
        
        
$backorders[] = array("domain" => $domain"type" => $type"update" => $update"bids" => $bids"starting_bid" => $bid"closing_time" => $time);
    }
    
    return 
$backorders;
}

function 
my_curl_setopts($ch)
{
    
$user_agent "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0";
    
    
curl_setopt($chCURLOPT_USERAGENT$user_agent);
    
curl_setopt($chCURLOPT_RETURNTRANSFER1);
    
curl_setopt($chCURLOPT_FOLLOWLOCATIONTRUE);
    
curl_setopt($chCURLOPT_MAXREDIRS10);
    
curl_setopt($chCURLOPT_SSL_VERIFYPEER0);
    
curl_setopt($chCURLOPT_TIMEOUT90);

    
$cookie dirname(__FILE__) . '/namejet.txt';
    
curl_setopt($chCURLOPT_COOKIEJAR$cookie);
    
curl_setopt($chCURLOPT_COOKIEFILE$cookie);
    
    return 
$ch;
}

function 
parse_asp_vars($html)
{
    
$asp_vars = array(    "__EVENTTARGET" => ""
                        
"__EVENTARGUMENT" => ""
                        
"__VIEWSTATE" => ""
                        
"__VIEWSTATEGENERATOR" => "",
                        
"__VIEWSTATEENCRYPTED" => ""
                        
"__EVENTVALIDATION" => "");
    
    foreach(
$asp_vars as $var => $val){
        if(
stripos($html$var) !== FALSE){
            
$html strstr($html'id="' $var '"');
            
$html strstr($html'value="');
            
$end strpos($html'" />');
            
$asp_vars[$var] = substr($html7$end 7);
        }
    }
    
    return 
$asp_vars;
}

function 
add_asp_vars($html)
{
    
$asp_vars parse_asp_vars($html);
    return     
'&__EVENTARGUMENT=' rawurlencode($asp_vars['__EVENTARGUMENT']) . 
            
'&__VIEWSTATE=' rawurlencode($asp_vars['__VIEWSTATE']) .
            
'&__VIEWSTATEGENERATOR=' rawurlencode($asp_vars['__VIEWSTATEGENERATOR']) . 
            
'&__VIEWSTATEENCRYPTED=' rawurlencode($asp_vars['__VIEWSTATEENCRYPTED']) . 
            
'&__EVENTVALIDATION=' rawurlencode($asp_vars['__EVENTVALIDATION']);
}

function 
parse_substr($html$start$end)
{
    
$len strlen($start);
    
$html strstr($html$start);
    
$pos strpos($html$end);
    return 
trim(substr($html$len$pos $len));
}

?>