Automatically get stats for any affiliate network - DirectTrack, Neverblue, etc

Discussion in 'Programming' started by tguillea, Apr 19, 2009.

  1. #1
    Tonight I finally finished it! The ultimate in automatic reporting! haha well it might not be perfect but it works for me. The script below will log into any network, and then download the report. I currently have it turning the csv into an array and putting that into the database, but that gets a little messy and everything. So here it is, hope you don't get lost!

    The only database reference is to a table called autocsv. This table has the following fields:
    * network (ex: neverblue)
    * url (if it is a DirectTrack site it looks like "partners.copeac.com", otherwise it is the link to download the report)
    * homepage (where you login - ex: h**p://www.neverblue.com/sign-in.html?login=aff&tab=aff)
    * username (for the network)
    * password (for the network)
    * userbox (name of the input where you type in your username)
    * passbox (name of the input where you type in your password)
    * subidbox (the column for the subid in the report starting with 0)
    * namebox (column for name in report)
    * campaignbox (campaign id)
    * creditbox (some networks like neverblue show a subid even if it didn't credit - mark this and any other "box" field as X if it is not provided in the report)
    * revenuebox (revenue for the lead)
    * datebox (date)
    * dt (if it is a direct track site - bool)
    * active (if it is active - bool)

    
    <?php
    session_start();
    ob_start();
    /**
     * @author Tom Guilleaume
     * @copyright 2009
     */
    
    
    //autocsvc.php
    
    if (getenv(HTTP_X_FORWARDED_FOR)) {
        $uploaderip = getenv(HTTP_X_FORWARDED_FOR);
    	}else{
        $uploaderip = getenv(REMOTE_ADDR);
    	}
    
    $adminname = 'AUTO UPLOADER';
    
    require('dirtrack.php');
    require('../../db2.php');
    $result0 = mysql_query("SELECT * FROM autocsv WHERE active='1' AND dt='1' ORDER BY id ASC");
    $num = mysql_num_rows($result0);
    if ($num == '0'){
    	echo 'An error has occured!<br />Code: 4024x4';
    	exit();
    	}
    while ($row = mysql_fetch_array($result0)){
    	
    	$s_box = $row['subidbox'];
    	$n_box = $row['namebox'];
    	$c_box = $row['campaignbox'];
    	$r_box = $row['revenuebox'];
    	$d_box = $row['datebox'];
    	
    	$file = 'csvuploads/'.$row['network'];
    	$timestamp = time();
    	$em = date('n', $timestamp);
    	$ed = date('j', $timestamp);
    	$ey = date('Y', $timestamp);
    	$timestamp2 = $timestamp - 150000;
    	$sm = date('n', $timestamp2);
    	$sd = date('j', $timestamp2);
    	$sy = date('Y', $timestamp2);
    	
    	$cookieDir = "cookies";
    	$dt = new DirectTrack($row['username'],$row['password'],$row['url'],$cookieDir);
    	$dt->setStartDate($sd,$sm,$sy);
    	$dt->setEndDate($ed,$em,$ey);
    	$tom = $dt->AdvancedStats();
    	unset($dt);
    
    	$network = $row['network'];
    	$file = "csvuploads/$network.csv";
    	$sitename = $network;
    	$handle = fopen("$file","r");
    	$max = filesize($file);
    	echo 'Max: '.$max.' ('.$sitename.')<br />';
    	if ($max > 0){
    		$filestring = fread($handle, $max);
    		if (substr_count($filestring,chr(10)) > 1){
    		echo 'Importing leads...<br />';
    		//add leads to database / credit user
    		}else{
    		echo 'No Leads<br />';
    		}
    		}
    	unset($url,$sitename,$file,$ch,$handle,$sitename0);
    	}
    $result4 = mysql_query("SELECT * FROM autocsv WHERE active='1' AND dt='0' ORDER BY id ASC");
    $num4 = mysql_num_rows($result4);
    if ($num4 == '0'){
    	echo 'An error has occured!<br />Code: 4011x4';
    	exit();
    	}
    while ($row4 = mysql_fetch_array($result4)){
    	
    	$s_box = $row4['subidbox'];
    	$n_box = $row4['namebox'];
    	$c_box = $row4['campaignbox'];
    	$r_box = $row4['revenuebox'];
    	$d_box = $row4['datebox'];
    	
    	$pass = str_replace('@','%40',$row4['password']);
    	$user = str_replace('@','%40',$row4['username']);
    	$url = str_replace('{password}',$pass,$row4['url']);
    	$url = str_replace('{username}',$user,$url);
    	$url = str_replace('{em}',$em,$url);
    	$url = str_replace('{ed}',$ed,$url);
    	$url = str_replace('{ey}',$ey,$url);
    	$url = str_replace('{sm}',$sm,$url);
    	$url = str_replace('{sd}',$sd,$url);
    	$url = str_replace('{sy}',$sy,$url);
    	$ch = curl_init();
    	$network = $row4['network'];
    	$file = "csvuploads/$network.csv";
    	$sitename = $network;
    	$handle = fopen("$file","w+");
    	//echo '<br />em: '.$em.'<br />ed: '.$ed.'<br />ey: '.$ey.'<br />sm: '.$sm.'<br />sd: '.$sd.'<br />sy: '.$sy.'<br />url: '.$url.'<br />username: '.$row4['username'].'<br />password: '.$row4['password'].'<br />'.$row4['userbox'].'<br />'.$row4['passbox'].'<br />'.$row4['homepage'].'<br />';
    
    	$ch = curl_init();
    	// SET URL FOR THE POST FORM LOGIN
    	curl_setopt($ch, CURLOPT_URL, $homepage);
    	// ENABLE HTTP POST
    	curl_setopt ($ch, CURLOPT_POST, 1);
    	// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
    	curl_setopt ($ch, CURLOPT_POSTFIELDS, $row4['userbox'].'='.$user.'&'.$row4['passbox'].'='.$pass);
    	// IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES
    	curl_setopt ($ch, CURLOPT_COOKIEJAR, "cookies/cookie".rand(11111111,99999999).".txt");
    	# Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
    	# not to print out the results of its query.
    	# Instead, it will return the results as a string return value
    	# from curl_exec() instead of the usual true/false.
    	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($ch, CURLOPT_FILE, $handle);
    	// EXECUTE 1st REQUEST (FORM LOGIN)
    	$store = curl_exec ($ch);
    	// SET FILE TO DOWNLOAD
    	curl_setopt($ch, CURLOPT_URL, $url);
    	// EXECUTE 2nd REQUEST (FILE DOWNLOAD)
    	$content = curl_exec ($ch);
    	//print_r($content);
    	// CLOSE CURL
    	curl_close ($ch);
    	fclose($handle);
    	unset($handle);
    	$file = "csvuploads/$network.csv";
    	$handle = fopen("$file","r");
    	$max = filesize($file);
    	echo 'Max: '.$max.' ('.$sitename.')<br />';
    	if (($max > 0) && (($max != 165) && ($network != 'elitecommission'))){
    		$filestring = fread($handle, $max);
    		if (substr_count($filestring,chr(10)) > 0){
    		echo 'Importing leads...<br />';
    
    		//add leads to database / credit user accounts
    		
    		}else{
    		echo 'No Leads<br />';
    		}			
    	unset($url,$sitename,$file,$ch,$handle,$max);
    	}else{
    	echo 'No Leads<br />';
    	}
    	}
    echo '<br />Done!';
    $savelog = ob_get_contents();
    $logfolder0 = scandir("log/");
    if (!in_array(date('Y'),$logfolder0)){
    	mkdir("log/".date('Y'));
    	}
    $logfolder1 = scandir("log/".date('Y').'/');
    if (!in_array(date('n'),$logfolder1)){
    	mkdir("log/".date('Y').'/'.date('n'));
    	}
    $logfolder2 = scandir("log/".date('Y').'/'.date('n').'/');
    if (!in_array(date('j'),$logfolder2)){
    	mkdir("log/".date('Y').'/'.date('n').'/'.date('j'));
    	}
    $filename = "log/".date('Y').'/'.date('n').'/'.date('j').'/'.time().".html";
    $handle = fopen($filename,"w+");
    fwrite($handle,$savelog);
    fclose($handle);
    ?>
    
    PHP:
    
    <?
    //dirtrack.php
    class DirectTrack {
    
    private $loggedin;
    private $login;
    private $password;
    private $site;
    private $cookiefile;
    private $start_day;
    private $start_month;
    private $start_year;
    private $end_day;
    private $end_month;
    private $end_year;
    
    /**
    * logs you into the dt system of your choice
    *
    * @param string $login - login for directtrack
    * @param string $password - password for directtrack
    * @param string $site - i.e. affiliates.copeac.com or monetizeit.net
    * @param string $cookie - cookie location
    */
    public function __construct($login,$password,$site,$cookie){
    
    $this->loggedin = false;
    $this->login = null;
    $this->password  = null;
    $this->site = null;
    $this->cookiefile = null;
    $this->start_day = null;
    $this->start_month = null;
    $this->start_year = null;
    $this->end_day = null;
    $this->end_month = null;
    $this->end_year = null;
    
    $this->setLogin($login);
    $this->setPassword($password);
    $this->setSite($site);
    $this->setCookieLocation($cookie);
    $this->DTLogin();
    
    }
    /**
    * deletes the cookie file on destroying the object remember to unset your object
    *
    */
    public function __destruct(){
    if (file_exists($this->cookiefile)){
    	unlink($this->cookiefile);
    	}
    }
    
    /**
    * login setter, url encodes
    *
    * @param string $login
    */
    public function setLogin($login){ $this->login = urlencode($login); }
    /**
    * password setter, url encodes
    *
    * @param string $password
    */
    public function setPassword($password){ $this->password = urlencode($password);}
    /**
    * Directtrack site domain setter
    *
    * @param string $site - i.e. affiliates.copeac.com or monetizeit.net
    */
    public function setSite($site){ $this->site = $site;}
    /**
    * setter for the cookie directory, randomizes the cookie as well incase multi threads are running
    * Please make sure your directory is writable
    *
    * @param string $cookie
    */
    public function setCookieLocation($cookie){
    $this->cookiefile = $cookie."/cookie".rand(11111111,99999999).".txt";
    }
    /**
    * getter for the currently generated cookie file location
    *
    * @return string
    */
    public function getCookieLocation(){return $this->cookiefile;}
    /**
    * Checks the status of being logged In
    *
    * @return array - array('msg'=> ",'status'=>") 0 logged out, 1 logged in.
    */
    public function LoggedInStatus(){
    if($this->loggedin){
    return array("msg"=>"Logged In!","status"=>1);
    }else{
    return array("msg"=>"Not logged in!","status"=>0);
    }
    }
    /**
    * sets the start date for the report you're looking for
    *
    * @param int $day
    * @param int $month
    * @param int $year
    */
    public function setStartDate($day,$month,$year){
    $this->start_day = $day;
    $this->start_month = $month;
    $this->start_year = $year;
    
    }
    /**
    * sets the ending date for the report you're looking for
    *
    * @param int $day
    * @param int $month
    * @param int $year
    */
    public function setEndDate($day,$month,$year){
    $this->end_day = $day;
    $this->end_month = $month;
    $this->end_year = $year;
    
    }
    /**
    * Combo function which gets the advanced report from direct track that has all the
    * specific sub-id data then parses it into a multi dimensional array
    *
    * @return array - array[line][fields]
    */
    public function AdvancedStats(){
    
    if(!$this->loggedin){
    $this->DTLogin();
    }
    
    $stat_page = $this->DTfetchAdvancedStats();
    return $this->csv2array($stat_page);
    
    }
    /**
    * extends AdvnacedStats, and then inserts the data into the db
    *
    * @return array - advanced stats array incase you want it for something else
    */
    public function AdvancedStats2DB(){
    $stats = $this->AdvancedStats();
    $this->DBconversionInsert($stats);
    return $stats;
    
    }
    /**
    * gets totals based on account for dates set
    *
    */
    public function Totals(){
    $csv = $this->DTfetchTotals();
    return $this->csv2array($csv);
    }
    /**
    * NOT COMPLETE
    * -Needs the regex to work properly
    * -paging for sites that have multiple pages of results
    *
    * @return array
    */
    public function AllOffers(){
    $campaign_page = $this->DTfetchAllCamapigns();
    
    $regex_sections = "/</a></td>.+?program_id=(d*)&.+?'>(.+?)<.+?top'>$*(.+?)/*<*.+?'>(.+?)<.+?'>(.+?)</s";
    preg_match_all($regex_sections,$campaign_page,$matches);
    
    foreach($matches[0] as $section){
    echo "<br><br>$section<br><br>";
    //get section name
    $regex_section = "/Category: (.+?)</";
    preg_match($regex_section,$section,$match);
    $section_name = "<b>".$match[1]."</b>";
    
    $regex_offers = "/program_id=(d*)&/s";
    preg_match_all($regex_offers,$section,$matches);
    var_dump($matches);
    exit;
    
    $offers = array();
    $sets = 0;
    foreach ($matches as $data){
    
    if($sets != 0){
    $i = 0;
    foreach ($data as $offer_data){
    
    $offers[$i][] = $offer_data;
    $i++;
    }
    }
    $sets++;
    exit;
    }
    array_unshift($offers,$section_name);
    var_dump($offers);
    }
    return $offers;
    exit;
    
    }
    //// Private //////
    
    /**
    * the function that logs into your directtrack account
    *
    * @return string - curl output
    */
    private function DTLogin(){
    
    $url = "https://$this->site/index.html";
    $post = "DL_AUTH_USERNAME=$this->login&DL_AUTH_PASSWORD=$this->password";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookiefile);  
    curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookiefile);
    $c_out = curl_exec($ch);
    unset($ch);
    if(1){ //parse for successful login
    $this->loggedin = true;
    }
    return $c_out;
    }
    /**
    * gets the advanced stats once logged in
    *
    * @return string - curl output - csv format
    */
    private function DTfetchAdvancedStats(){
    
    $url = "https://$this->site/partners/monthly_affiliate_stats.html?program_id&affiliate_stats_start_month=$this->start_month&affiliate_stats_start_day=$this->start_day&affiliate_stats_start_year=$this->start_year&affiliate_stats_end_month=$this->end_month&affiliate_stats_end_day=$this->end_day&affiliate_stats_end_year=$this->end_year&breakdown=cumulative&get_lead_info=Download+Optional+Info";
    
    //make file handle
    $sitename0 = explode('.',$this->site);
    $sitename = $sitename0[count($sitename0) - 2];
    if ($sitename == 'directtrack'){
    	$sitename = $sitename0[count($sitename0) - 3];
    	}
    $handle = fopen("csvuploads/$sitename.csv","w+");
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookiefile);  
    curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookiefile);
    curl_setopt($ch, CURLOPT_FILE, $handle);
    $curl_stats = curl_exec($ch);
    unset($ch);
    return $curl_stats;
    }
    /**
    * breaks csv data into a multi-dim array with array[line][fields]
    *
    * @param string $curl_data - csv from advanced page
    * @return array - multi-dim array[line][fields]
    */
    private function csv2array($curl_data){
    
    $return = array();
    //lines to array
    $stats = explode("n",trim($curl_data));
    
    foreach($stats as $line){
    //fields to array
    $fields = explode(",",trim($line));
    $record = array();
    foreach($fields as $field){
    $record[] = trim($field);
    }
    $return[] = $record;
    }
    return $return;
    }
    /**
    * takes the multi-dim array from DTparseAdvancedStats and inserts it into a db
    *
    * @param array $stats
    */
    private function DBconversionInsert($stats){
    
    $count = 0;
    foreach($stats as $row){
    if($count != 0){
    
    //assignments
    $click_id        = trim($row[2]);
    $source         = $this->site;
    $date             = $this->_ChangeDateFormat($row[0]);
    $campaign        = addslashes(trim($row[1]));
    $campaignID     = trim($row[3]);
    $transactionID    = trim($row[4]);
    $lineitemID        = trim($row[5]);
    $commission     = $this->_CleanCommission($row[6]);
    
    $sql = "INSERT INTO `conversions` (`click_id`,`source`,`date`,`campaign`,`campiagnID`,`transactionID`,`lineitemID`,`commission`) VALUES ('$click_id','$source','$date','$campaign','$campaignID','$transactionID','$lineitemID','$commission')";
    
    //echo "$sql<br>";
    if(!strpos($click_id,"blogs"))
    {
    mysql_query($sql);
    }
    //if(mysql_error()){ echo mysql_error()."n"; }
    
    }
    $count ++;
    }
    }
    /**
    * gets the totals for all sales by campaign
    *
    * @return string - curl output
    */
    private function DTfetchTotals(){
    
    $url = "https://$this->site/partners/monthly_affiliate_stats.html?program_id=&affiliate_stats_start_month=$this->start_month&affiliate_stats_start_day=$this->start_day&affiliate_stats_start_year=$this->start_year&affiliate_stats_end_month=$this->end_month&affiliate_stats_end_day=$this->end_day&affiliate_stats_end_year=$this->end_year&breakdown=cumulative&get_csv=1";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookiefile);  
    curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookiefile);
    $curl_stats = curl_exec($ch);
    unset($ch);
    return $curl_stats;
    }
    /**
    * once logged in curl to get the campaigns
    *
    * @return string - curl page
    */
    private function DTfetchAllCamapigns(){
    $url = "https://$this->site/partners/search_program_categories.html";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookiefile);  
    curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookiefile);
    $curl_stats = curl_exec($ch);
    unset($ch);
    return $curl_stats;
    }
    
    // private functions
    private function _ChangeDateFormat($date)
    {
    $parts = explode("/",$date);
    $date = $parts[2]."-".$parts[1]."-".$parts[0];
    //echo $date."<br>";
    return $date;
    }
    private function _CleanCommission($dollar)
    {
    return str_ireplace("$","",$dollar);
    }
    }
    
    // THIS IS HOW YOU USE THE CLASS
    
    //$cookieDir = "cookies";
    //$dt = new DirectTrack('my@email.com','password','copec.com',$cookieDir);
    //$dt->setStartDate(1,6,2007);
    //$dt->setEndDate(18,6,2007);
    //var_dump($dt->LoggedInStatus());
    //echo $dt->getCookieLocation();
    //var_dump($dt->AdvancedStats());
    //var_dump($dt->Stats2DB());
    //var_dump($dt->Totals());
    //var_dump($dt->AllOffers());
    //unset($dt);
    
    ?>
    
    PHP:
     
    tguillea, Apr 19, 2009 IP
  2. tguillea

    tguillea Active Member

    Messages:
    229
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #2
    no one can use this?
    no one wants to get automatic stats downloaded?
     
    tguillea, Apr 19, 2009 IP
  3. vivianflv

    vivianflv Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I want. I will try yours. Hope it is useful for me. Could you please provide more for this? Thanks for this. Good post.
     
    vivianflv, Apr 20, 2009 IP
  4. tguillea

    tguillea Active Member

    Messages:
    229
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #4
    Programming is a great hobby of mine and I'm more than happy to help others while doing what I enjoy.

    Any requests for another script like a mass e-mailer, Auto-Flag system (for suspicious accounts), PayPal Mass Pay file generator, etc?
     
    tguillea, Apr 20, 2009 IP