public function getTrackingKey()
 {
     $trackingKey = new WPAM_Tracking_TrackingKey();
     $trackingKey->setAffiliateRefKey($this->affiliate->uniqueRefKey);
     $trackingKey->setCreativeId($this->creative->creativeId);
     return $trackingKey;
 }
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 static function record_click()
 {
     if (isset($_REQUEST[WPAM_PluginConfig::$RefKey]) && !empty($_REQUEST[WPAM_PluginConfig::$RefKey])) {
         //this is the old tracking code (deprecated). This will be removed once the new tracking system is functional.
         global $wpdb;
         $strRefKey = trim(strip_tags($_REQUEST[WPAM_PluginConfig::$RefKey]));
         $aff_id = "";
         if (is_numeric($strRefKey)) {
             //wpam_refkey contains affiliate ID. If a record is found save it
             $query = "SELECT * FROM " . WPAM_AFFILIATES_TBL . " WHERE affiliateId = %d";
             $affiliate = $wpdb->get_row($wpdb->prepare($query, $strRefKey));
             if ($affiliate != null && $affiliate->status == "active") {
                 $aff_id = $strRefKey;
             }
         } else {
             if (is_email($strRefKey)) {
                 //wpam_refkey contains email. Find the ID associated with that email and save it
                 $query = "SELECT * FROM " . WPAM_AFFILIATES_TBL . " WHERE email = %s";
                 $affiliate = $wpdb->get_row($wpdb->prepare($query, $strRefKey));
                 if ($affiliate != null && $affiliate->status == "active") {
                     $aff_id = $affiliate->affiliateId;
                 }
             } else {
                 //TODO start - wpam_refkey contains long tracking key. Find affiliate ID from it and save it. This block of code will just be here for backwards compatibilty
                 $refKey = new WPAM_Tracking_TrackingKey();
                 $refKey->unpack($strRefKey);
                 $db = new WPAM_Data_DataAccess();
                 $affiliateRepos = $db->getAffiliateRepository();
                 $affiliateId = $affiliateRepos->getAffiliateIdFromRefKey($refKey->getAffiliateRefKey());
                 if ($affiliateId === NULL) {
                 } else {
                     $aff_id = $affiliateId;
                 }
             }
         }
         //TODO end
         if (!empty($aff_id)) {
             $cookie_life_time = wpam_get_cookie_life_time();
             setcookie('wpam_id', $aff_id, $cookie_life_time, "/", COOKIE_DOMAIN);
         }
     }
     //this will be the new affiliate link. A click will be tracked when wpam_id is present in the URL
     if (isset($_REQUEST[WPAM_PluginConfig::$wpam_id]) && !empty($_REQUEST[WPAM_PluginConfig::$wpam_id])) {
         $aff_id = trim(strip_tags($_REQUEST[WPAM_PluginConfig::$wpam_id]));
         $cookie_life_time = wpam_get_cookie_life_time();
         setcookie('wpam_id', $aff_id, $cookie_life_time, "/", COOKIE_DOMAIN);
         $args = array();
         $args['dateCreated'] = date("Y-m-d H:i:s", time());
         $args['sourceAffiliateId'] = $aff_id;
         $args['trackingKey'] = uniqid();
         //save a unique ID to avoid error
         $args['sourceCreativeId'] = '';
         // remove this column from the click tracking menu in the settings
         $args['referer'] = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
         $args['affiliateSubCode'] = '';
         WPAM_Click_Tracking::insert_click_data($args);
     }
 }
function to_token($uniqueRefKey)
{
    $binConverter = new WPAM_Util_BinConverter();
    $binRefKey = $binConverter->stringToBin($uniqueRefKey);
    $creative_id = 1;
    $reserved = 0;
    $revRefKey = new WPAM_Tracking_TrackingKey();
    $revRefKey->setAffiliateRefKey($binRefKey);
    $revRefKey->setCreativeId($creative_id);
    $revRefKey->setReserved($reserved);
    $token = $revRefKey->pack();
    var_dump($token);
    return $token;
}
 public function handleImpression($request)
 {
     $strRefKey = NULL;
     if (isset($request[WPAM_PluginConfig::$RefKey])) {
         $strRefKey = $request[WPAM_PluginConfig::$RefKey];
     } else {
         throw new Exception(__('no refkey in request.', 'wpam'));
     }
     $refKey = new WPAM_Tracking_TrackingKey();
     $refKey->unpack($strRefKey);
     $db = new WPAM_Data_DataAccess();
     $affiliateRepos = $db->getAffiliateRepository();
     $affiliateId = $affiliateRepos->getAffiliateIdFromRefKey($refKey->getAffiliateRefKey());
     if ($affiliateId === NULL) {
         throw new Exception(__('invalid refkey data: ', 'wpam') . $strRefKey);
     }
     $impressionModel = new WPAM_Data_Models_ImpressionModel();
     $impressionModel->dateCreated = time();
     $impressionModel->sourceAffiliateId = $affiliateId;
     $impressionModel->sourceCreativeId = $refKey->getCreativeId();
     $impressionModel->referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : NULL;
     if (isset($request['wpam_affiliateSubCode'])) {
         $impressionModel->affiliateSubCode = $request['wpam_affiliateSubCode'];
     }
     $db->getImpressionRepository()->insert($impressionModel);
 }