function to_refkey($token)
{
    $binConverter = new WPAM_Util_BinConverter();
    $testRefKey = new WPAM_Tracking_TrackingKey();
    $testRefKey->unpack($token);
    $refkey = $binConverter->binToString($testRefKey->getAffiliateRefKey());
    var_dump($testRefKey);
    var_dump($refkey);
    var_dump($binConverter->binToString($binConverter->binToString($testRefKey->getAffiliateRefKey())));
    return $refkey;
}
function wpam_generate_refkey_from_affiliate_id($aff_id)
{
    $db = new WPAM_Data_DataAccess();
    $affiliateRepos1 = $db->getAffiliateRepository();
    $wpam_refkey = NULL;
    $affiliate = $affiliateRepos1->loadBy(array('affiliateId' => $aff_id, 'status' => 'active'));
    if ($affiliate === NULL) {
        //affiliate with this ID does not exist
        WPAM_Logger::log_debug("generate_refkey_from_affiliate_id function - affiliate ID " . $aff_id . " does not exist");
    } else {
        $default_creative_id = get_option(WPAM_PluginConfig::$DefaultCreativeId);
        if (!empty($default_creative_id)) {
            $creative = $db->getCreativesRepository()->load($default_creative_id);
            $linkBuilder = new WPAM_Tracking_TrackingLinkBuilder($affiliate, $creative);
            $strRefKey = $linkBuilder->getTrackingKey()->pack();
            $refKey = new WPAM_Tracking_TrackingKey();
            $refKey->unpack($strRefKey);
            $idGenerator = new WPAM_Tracking_UniqueIdGenerator();
            $trackTokenModel = new WPAM_Data_Models_TrackingTokenModel();
            $trackTokenModel->dateCreated = time();
            $trackTokenModel->sourceAffiliateId = $aff_id;
            $trackTokenModel->sourceCreativeId = $refKey->getCreativeId();
            $trackTokenModel->trackingKey = $idGenerator->generateId();
            $trackTokenModel->referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : NULL;
            /* add a new visit so it doesn't fail while awarding commission */
            $db->getTrackingTokenRepository()->insert($trackTokenModel);
            $db->getEventRepository()->quickInsert(time(), $trackTokenModel->trackingKey, 'visit');
            /* */
            $binConverter = new WPAM_Util_BinConverter();
            $wpam_refkey = $binConverter->binToString($trackTokenModel->trackingKey);
        }
    }
    return $wpam_refkey;
}
 public function __toString()
 {
     $binConverter = new WPAM_Util_BinConverter();
     $key = $binConverter->binToString($this->affiliateRefKey);
     $object = "\naffiliateRefKey = {$key}\ncreativeId = {$this->creativeId}\nreserved = {$this->reserved}\n";
     return $object;
 }
 public function quickInsert($dateCreated, $trackingKey, $actionKey)
 {
     $query = "\n\t\t\tINSERT INTO `{$this->tableName}`\n\t\t\tSET\n\t\t\t\tdateCreated = %s,\n\t\t\t\ttrackingTokenId = (\n\t\t\t\t\tselect trackingTokenId\n\t\t\t\t\tfrom " . $this->db->prefix . WPAM_Data_DataAccess::TABLE_TRACKING_TOKENS . "\n\t\t\t\t\twhere `trackingKey` = %s\n\t\t\t\t),\n\t\t\t\tactionId = (\n\t\t\t\t\tselect actionId\n\t\t\t\t\tfrom " . $this->db->prefix . WPAM_Data_DataAccess::TABLE_ACTIONS . "\n\t\t\t\t\twhere `name` = %s\n\t\t\t\t)";
     $binConverter = new WPAM_Util_BinConverter();
     $query = $this->db->prepare($query, date("Y-m-d H:i:s", $dateCreated), $binConverter->binToString($trackingKey), $actionKey);
     $this->db->query($query);
 }
 function toRow()
 {
     $row = new stdClass();
     $binConverter = new WPAM_Util_BinConverter();
     $modelMapper = new WPAM_Data_Models_ModelMapper();
     $modelMapper->map($this, $row, false);
     $row->dateCreated = date('Y-m-d H:i:s', $row->dateCreated);
     $row->trackingKey = $binConverter->binToString($row->trackingKey);
     return $row;
 }
 public function toRow()
 {
     $row = new stdClass();
     $modelMapper = new WPAM_Data_Models_ModelMapper();
     $modelMapper->map($this, $row, false);
     $binConverter = new WPAM_Util_BinConverter();
     $row->dateCreated = date('Y-m-d H:i:s', $row->dateCreated);
     $row->uniqueRefKey = $binConverter->binToString($this->uniqueRefKey);
     $row->userData = serialize($this->userData);
     return $row;
 }
 public function getAffiliateIdFromRefKey($refkey)
 {
     $binConverter = new WPAM_Util_BinConverter();
     $query = "\n\t\t\tselect affiliateId\n\t\t\tfrom `{$this->tableName}`\n\t\t\twhere\n\t\t\t\tuniqueRefKey=%s\n\t\t";
     $value = $binConverter->binToString($refkey);
     //this output will prevent wp_redirect from working
     //if ( WPAM_DEBUG )
     //	echo "\n\nQuery: {$query}\nValue: {$value}\n\n";
     $query = $this->db->prepare($query, $value);
     $affId = $this->db->get_var($query);
     return $affId != NULL ? (int) $affId : $affId;
 }
 public function handleIncomingReferral($request)
 {
     $strRefKey = NULL;
     if (isset($request[WPAM_PluginConfig::$RefKey])) {
         $strRefKey = $request[WPAM_PluginConfig::$RefKey];
     } else {
         throw new Exception(__('no refkey in request.', 'wpam'));
     }
     if (!array_key_exists(WPAM_PluginConfig::$RefKey, $_COOKIE)) {
         if (is_email($strRefKey)) {
             $db1 = new WPAM_Data_DataAccess();
             $affiliateRepos1 = $db1->getAffiliateRepository();
             $affiliate = $affiliateRepos1->loadBy(array('email' => $strRefKey, 'status' => 'active'));
             if ($affiliate === NULL) {
                 //affiliate with this email does not exist
             } else {
                 $default_creative_id = get_option(WPAM_PluginConfig::$DefaultCreativeId);
                 if (!empty($default_creative_id)) {
                     $creative = $db1->getCreativesRepository()->load($default_creative_id);
                     $linkBuilder = new WPAM_Tracking_TrackingLinkBuilder($affiliate, $creative);
                     $strRefKey = $linkBuilder->getTrackingKey()->pack();
                 }
             }
         } else {
             if (is_numeric($strRefKey)) {
                 $db1 = new WPAM_Data_DataAccess();
                 $affiliateRepos1 = $db1->getAffiliateRepository();
                 $affiliate = $affiliateRepos1->loadBy(array('affiliateId' => $strRefKey, 'status' => 'active'));
                 if ($affiliate === NULL) {
                     //affiliate with this ID does not exist
                 } else {
                     $default_creative_id = get_option(WPAM_PluginConfig::$DefaultCreativeId);
                     if (!empty($default_creative_id)) {
                         $creative = $db1->getCreativesRepository()->load($default_creative_id);
                         $linkBuilder = new WPAM_Tracking_TrackingLinkBuilder($affiliate, $creative);
                         $strRefKey = $linkBuilder->getTrackingKey()->pack();
                     }
                 }
             }
         }
         $refKey = new WPAM_Tracking_TrackingKey();
         $refKey->unpack($strRefKey);
         $db = new WPAM_Data_DataAccess();
         $affiliateRepos = $db->getAffiliateRepository();
         $affiliateId = $affiliateRepos->getAffiliateIdFromRefKey($refKey->getAffiliateRefKey());
         if ($affiliateId === NULL) {
             echo '<pre>Affiliate ID: ';
             var_export($affiliateId);
             echo "\n\n";
             echo $refKey;
             throw new Exception(__('invalid refkey data.', 'wpam'));
         }
         //#37 make sure the affiliate is active before tracking stats
         $affiliate = $affiliateRepos->load($affiliateId);
         if (!$affiliate->isActive()) {
             return;
         }
         $idGenerator = new WPAM_Tracking_UniqueIdGenerator();
         $trackTokenModel = new WPAM_Data_Models_TrackingTokenModel();
         $trackTokenModel->dateCreated = time();
         $trackTokenModel->sourceAffiliateId = $affiliateId;
         $trackTokenModel->sourceCreativeId = $refKey->getCreativeId();
         $trackTokenModel->trackingKey = $idGenerator->generateId();
         $trackTokenModel->referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : NULL;
         if (isset($request['wpam_affiliateSubCode'])) {
             $trackTokenModel->affiliateSubCode = $request['wpam_affiliateSubCode'];
         }
         $db->getTrackingTokenRepository()->insert($trackTokenModel);
         $db->getEventRepository()->quickInsert(time(), $trackTokenModel->trackingKey, 'visit');
         $binConverter = new WPAM_Util_BinConverter();
         // store the tracking key in a cookie so we can monitor their activities
         $return = setcookie(WPAM_PluginConfig::$RefKey, $binConverter->binToString($trackTokenModel->trackingKey), $this->getExpireTime(), COOKIEPATH);
     }
 }
<?php

/**
 * @author John Hargrove
 * 
 * Date: Jul 8, 2010
 * Time: 9:20:49 PM
 */
require_once '../source/Tracking/UniqueIdGenerator.php';
require_once '../source/Util/BinConverter.php';
$idgen = new WPAM_Tracking_UniqueIdGenerator();
$binConverter = new WPAM_Util_BinConverter();
echo $binConverter->binToString($idgen->generateId()) . PHP_EOL;
 function create_wpam_affiliate_record($fields)
 {
     global $wpdb;
     //Do some validation to make sure we have some minimum required info
     if (!isset($fields['email']) || empty($fields['email'])) {
         WPAM_Logger::log_debug("create_wpam_affiliate_record() - Error, email address is missing. Cannot create affiliate record!", 4);
         return;
     }
     if (!isset($fields['userId']) || empty($fields['userId'])) {
         WPAM_Logger::log_debug("create_wpam_affiliate_record() - Error, userId value is missing. Cannot create affiliate record!", 4);
         return;
     }
     //Check and set the default status values
     if (!isset($fields['status']) || empty($fields['status'])) {
         if (get_option(WPAM_PluginConfig::$AutoAffiliateApproveIsEnabledOption) == 1) {
             $fields['status'] = 'active';
         } else {
             $fields['status'] = 'applied';
         }
     }
     //Check and set default dateCreated value
     if (!isset($fields['dateCreated']) || empty($fields['dateCreated'])) {
         $fields['dateCreated'] = current_time('mysql');
         //date("Y-m-d H:i:s");
     }
     //Check and set default dateCreated value
     if (!isset($fields['uniqueRefKey']) || empty($fields['uniqueRefKey'])) {
         $idGenerator = new WPAM_Tracking_UniqueIdGenerator();
         $binConverter = new WPAM_Util_BinConverter();
         $fields['uniqueRefKey'] = $binConverter->binToString($idGenerator->generateId());
     }
     //Check and set default bountyType
     if (!isset($fields['bountyType']) || empty($fields['bountyType'])) {
         $fields['bountyType'] = get_option(WPAM_PluginConfig::$AffBountyType);
     }
     //Check and set default bountyAmount
     if (!isset($fields['bountyAmount']) || empty($fields['bountyAmount'])) {
         $fields['bountyAmount'] = get_option(WPAM_PluginConfig::$AffBountyAmount);
     }
     $fields['userData'] = serialize(array());
     //assign an empty array so $wpdb->insert does not fail
     $wpdb->insert(WPAM_AFFILIATES_TBL, $fields);
 }