From 5bdb8bb3da8837240d7f677e40bf578d632e62e5 Mon Sep 17 00:00:00 2001
From: Brian Osborne <bosborne@stargroup1.com>
Date: Thu, 17 Mar 2011 17:00:43 -0400
Subject: [PATCH 1/2] applied session patch

---
 uc_affiliate2.admin.inc |   14 ++----------
 uc_affiliate2.ca.inc    |    2 +-
 uc_affiliate2.module    |   50 ++++++++++++++++++----------------------------
 3 files changed, 24 insertions(+), 42 deletions(-)

diff --git a/uc_affiliate2.admin.inc b/uc_affiliate2.admin.inc
index a1d5dea..1aca73d 100644
--- a/uc_affiliate2.admin.inc
+++ b/uc_affiliate2.admin.inc
@@ -358,18 +358,11 @@ function uc_affiliate2_admin_settings() {
     '#collapsible' => TRUE,
     '#collapsed' => FALSE,
   );
-  
-  $form['affiliate_cookie_tracking']['affiliate_set_cookie'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Set the affiliate cookie for the visiting user.'),
-    '#description' => t('By setting the cookie, the affiliate gets credited even when no affiliate id is present in links visited.'),
-    '#default_value' => variable_get('affiliate_set_cookie', 0),
-  );
   $form['affiliate_cookie_tracking']['affiliate_cookie_lifetime'] = array(
     '#type' => 'textfield',
     '#title' => t('Cookie Lifetime'),
-    '#description' => t('How long should the cookie be valid. Strtotime syntax.'),
-    '#default_value' => variable_get('affiliate_cookie_lifetime', '1 year'),
+    '#description' => t('Leave blank to use session cookie (not remembered on browser close). Otherwise, use PHP strtotime syntax to indicate the length of the cookie life.'),
+    '#default_value' => variable_get('affiliate_cookie_lifetime', ''),
   );
   $form['affiliate_cookie_tracking']['affiliate_subsequent_order_commissions'] = array(
     '#type' => 'radios',
@@ -393,8 +386,7 @@ function uc_affiliate2_admin_settings_validate($form, &$form_state) {
   if (!empty($url) && ($url != check_url($url) || strpos($url, '://') !== FALSE)) {
     form_set_error('affiliate_invalid_redirect', t('Please enter a valid Drupal path, without the http and domain parts.'));
   }
-
-  if (!strtotime($form_state['values']['affiliate_cookie_lifetime'])) {
+  if (strtotime($form_state['values']['affiliate_cookie_lifetime']) === FALSE && !empty($form_state['values']['affiliate_cookie_lifetime'])) {
     form_set_error('affiliate_cookie_lifetime', t('Invalid strtotime syntax.'));
   }
 }
diff --git a/uc_affiliate2.ca.inc b/uc_affiliate2.ca.inc
index ec677a6..8399320 100644
--- a/uc_affiliate2.ca.inc
+++ b/uc_affiliate2.ca.inc
@@ -189,7 +189,7 @@ function uc_affiliate2_ca_trigger() {
 function uc_affiliate2_condition_has_affiliate($order, $settings) {
   // check session variable and the database for affiliate for the user making
   // the purchase.
-  if ($_SESSION['affiliate'] > 0) {
+  if ($_COOKIE['affiliate']['uid'] > 0) {
     return TRUE;
   }
   if (_uc_affiliate2_get_user($uid) > 0) {
diff --git a/uc_affiliate2.module b/uc_affiliate2.module
index 192d161..97475ef 100644
--- a/uc_affiliate2.module
+++ b/uc_affiliate2.module
@@ -465,7 +465,7 @@ function uc_affiliate2_user($op, &$edit, &$account, $category = NULL) {
   switch ($op) {
     case 'insert':
       // update user count for affiliate and associate user with affiliate
-      $aff = $_SESSION['affiliate'];
+      $aff = $_COOKIE['affiliate']['uid'];
       if ($aff) {
         db_query('INSERT INTO {uc_affiliate2_users} (aid, uid) VALUES (%d, %d)', $aff, $account->uid);
 
@@ -523,7 +523,7 @@ function uc_affiliate2_user($op, &$edit, &$account, $category = NULL) {
           '#type' => 'textfield',
           '#title' => t('Commission'),
           '#default_value' => $edit['commission'],
-          '#description' => t("The commission for this user. For a fixed commission in the store's currency, just enter a number. For a percentage commission, put a '%' symbol after the number. Leave blank to use either product or global commissions.")
+          '#description' => t('The commission percentage for this user (number only). Leave blank to use either product or global commissions.')
         );
       }
       return $form;
@@ -542,8 +542,8 @@ function uc_affiliate2_user($op, &$edit, &$account, $category = NULL) {
  * Add tracking of which store each product comes from
  */
 function uc_affiliate2_add_to_cart_data($form_values) {
-  if (!empty($_SESSION['affiliate'])) {
-    return array('affiliate' => $_SESSION['affiliate']);
+  if (!empty($_COOKIE['affiliate']['uid'])) {
+    return array('affiliate' => $_COOKIE['affiliate']['uid']);
   }
 }
 
@@ -622,8 +622,8 @@ function uc_affiliate2_order($op, $order, $status) {
     $affiliate = 0;
 
     // If this is the user's session, and an affiliate is stored, use that.
-    if ($user->uid == $order->uid && !empty($_SESSION['affiliate'])) {
-      $affiliate = $_SESSION['affiliate'];
+    if ($user->uid == $order->uid && !empty($_COOKIE['affiliate']['uid'])) {
+      $affiliate = $_COOKIE['affiliate']['uid'];
     }
 
     switch (variable_get('affiliate_subsequent_order_commissions', 'none')) {
@@ -1149,12 +1149,6 @@ function uc_affiliate2_click_callback($affiliate_uid) {
  * Called from uc_affiliate2_init()
  */
 function _uc_affiliate2_handle_cookie() {
-  if (!empty($_COOKIE['affiliate']) && empty($_SESSION['affiliate'])) { // repopulate the session from the cookie
-    $_SESSION['affiliate'] = $_COOKIE['affiliate']['uid'];
-    $_SESSION['affiliate_cookie_timestamp'] = $_COOKIE['affiliate']['timestamp'];
-    $_SESSION['affiliate_rid'] = $_COOKIE['affiliate']['rid'];
-  }
-
   if (!empty($_COOKIE['affiliate']['update_click'])) {
     // The update_click cookie was set to the click_id so we can know exactly which click to update.
     db_query('UPDATE {uc_affiliate2_clicks} SET cookie_status = 2 WHERE click_id = %d', $_COOKIE['affiliate']['update_click']);
@@ -1193,30 +1187,26 @@ function _uc_affiliate2_register_click($account, $target_path) {
   if ($account->uid <= 1 || !user_access('act as affiliate', $account)) {
     return FALSE;
   }
-
-  // Do we already have an affiliate in session? Do nothing
-  if ($_SESSION['affiliate']) {
-    return TRUE;
-  }
-
+  
   // Get the affiliate role (only the last one)
   $rid = _uc_affiliate2_get_user_rid($account->roles);
 
   // Do we need to set the cookie? For how long?
-  $set_cookie = variable_get('affiliate_set_cookie', 0);
+  $cookie_lifetime = variable_get('affiliate_cookie_lifetime', '');
   $time = time();
 
-  if ($set_cookie) {
-    $cookie_lifetime = variable_get('affiliate_cookie_lifetime', '1 year');
+  // set real cookie?
+  if (!empty($cookie_lifetime)) {
+    // convert string to real time
     $cookie_lifetime = strtotime('+ '. $cookie_lifetime);
-
-    setcookie('affiliate[uid]', $account->uid, $cookie_lifetime, '/');
-    setcookie('affiliate[rid]', $rid, $cookie_lifetime, '/');
-    setcookie('affiliate[timestamp]', $time, $cookie_lifetime, '/');
+  // or just session cookie...?
+  } else {
+    $cookie_lifetime = 0;
   }
 
-  // store affiliate id in session and increment click count
-  $_SESSION['affiliate'] = $account->uid;
+  setcookie('affiliate[uid]', $account->uid, $cookie_lifetime, '/');
+  setcookie('affiliate[rid]', $rid, $cookie_lifetime, '/');
+  setcookie('affiliate[timestamp]', $time, $cookie_lifetime, '/');
 
   $target_path = ($target_path == '<front>') ? '' : $target_path;
 
@@ -1225,7 +1215,7 @@ function _uc_affiliate2_register_click($account, $target_path) {
                 $account->uid, $time, $set_cookie, $_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_REFERER'], $target_path);
 
   // If we're setting cookies, also set one so the cookie status for the click can be updated.
-  if ($set_cookie) {
+  if (!empty($cookie_lifetime)) {
     // By setting this to the click_id, this exact click can be updated when the cookie comes back.
     $click_id = db_last_insert_id('uc_affiliate2_clicks', 'click_id');
     setcookie('affiliate[update_click]', $click_id, $cookie_lifetime, '/');
@@ -1424,8 +1414,8 @@ function _uc_affiliate2_get_commission($aff_id, $level = 1, $nid = 0, $model = '
   }
   $default_structure = $product_commission->commission_structure;
 
-  if (!empty($_SESSION['affiliate_rid'])) {
-    $structure = variable_get('affiliate_commission_structure_'. $_SESSION['affiliate_rid'], array('5%', '3%', '2%', '2%', '1%'));
+  if (!empty($_COOKIE['affiliate']['rid'])) {
+    $structure = variable_get('affiliate_commission_structure_'. $_COOKIE['affiliate']['rid'], array('5%', '3%', '2%', '2%', '1%'));
   }
   else {
     $structure = $default_structure;
-- 
1.7.3.3


From 7052fd1895080c8548a21b4862bcc941ea0f6a90 Mon Sep 17 00:00:00 2001
From: Brian Osborne <bosborne@stargroup1.com>
Date: Thu, 17 Mar 2011 17:07:42 -0400
Subject: [PATCH 2/2] Issue #1096426: allow affiliates to have their own cookie life

---
 uc_affiliate2.module |   35 ++++++++++++++++++++++++++++-------
 1 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/uc_affiliate2.module b/uc_affiliate2.module
index 97475ef..9b99331 100644
--- a/uc_affiliate2.module
+++ b/uc_affiliate2.module
@@ -507,24 +507,38 @@ function uc_affiliate2_user($op, &$edit, &$account, $category = NULL) {
             '#value' => t('Manage your business ') . l(t('here'), "user/{$account->uid}/affiliate"),
           ),
         );
-
       }
       break;
     case 'form':
       if (user_access('adjust user commissions') && $category == 'account') {
         $form['commission_settings'] = array(
           '#type' => 'fieldset',
-          '#title' => t('Affiliate Commission Settings'),
+          '#title' => t('Affiliate Settings'),
           '#collapsible' => TRUE,
           '#weight' => 10,
         );
-
         $form['commission_settings']['commission'] = array(
           '#type' => 'textfield',
           '#title' => t('Commission'),
           '#default_value' => $edit['commission'],
           '#description' => t('The commission percentage for this user (number only). Leave blank to use either product or global commissions.')
         );
+        $currentglobal = variable_get('affiliate_cookie_lifetime','');
+        if (empty($currentglobal)) {
+          $currentglobal = 'None';
+        }
+        $form['commission_settings']['affiliate_cookie_usecustom'] = array(
+          '#type' => 'checkbox',
+          '#title' => t('Use custom cookie lifetime.'),
+          '#default_value' => $edit['affiliate_cookie_usecustom'],
+          '#description' => t('If set to yes, override the global cookie lifetime with the value below. <b>The current global cookie lifetime is: @currentglobal</b>',array('@currentglobal'=>$currentglobal)),
+        );
+        $form['commission_settings']['affiliate_cookie_lifetime'] = array(
+          '#type' => 'textfield',
+          '#title' => t('Cookie Lifetime'),
+          '#default_value' => $edit['affiliate_cookie_lifetime'],
+          '#description' => t('Only valid if above option is checked. Leave blank to use session cookie (not remembered on browser close). Otherwise, use PHP strtotime syntax to indicate the length of the cookie life.'),
+        );
       }
       return $form;
     case 'validate':
@@ -532,6 +546,9 @@ function uc_affiliate2_user($op, &$edit, &$account, $category = NULL) {
         if (isset($edit['commission']) && !preg_match('/^([0-9]*(\.[0-9]+)?|1?[0-9]{0,2}(\.[0-9]+)?%)$/', $edit['commission'])) {
           form_set_error('commission', t('Invalid commission value.'));
         }
+        if (strtotime($edit['affiliate_cookie_lifetime']) === FALSE && !empty($edit['affiliate_cookie_lifetime'])) {
+          form_set_error('affiliate_cookie_lifetime', t('Invalid strtotime syntax.'));
+        }
       }
       break;
   }
@@ -1191,15 +1208,19 @@ function _uc_affiliate2_register_click($account, $target_path) {
   // Get the affiliate role (only the last one)
   $rid = _uc_affiliate2_get_user_rid($account->roles);
 
-  // Do we need to set the cookie? For how long?
-  $cookie_lifetime = variable_get('affiliate_cookie_lifetime', '');
+  // Obtain the cookie life from global settings or user settings
+  $userdata = unserialize($account->data);
+  if (!empty($userdata['affiliate_cookie_usecustom'])) {
+    $cookie_lifetime = $userdata['affiliate_cookie_lifetime'];
+  } else {
+    $cookie_lifetime = variable_get('affiliate_cookie_lifetime', '');
+  }
   $time = time();
 
-  // set real cookie?
+  // set real cookie? if cookie_lifetime is empty, we use a life of 0 for a session cookie
   if (!empty($cookie_lifetime)) {
     // convert string to real time
     $cookie_lifetime = strtotime('+ '. $cookie_lifetime);
-  // or just session cookie...?
   } else {
     $cookie_lifetime = 0;
   }
-- 
1.7.3.3

