From 12688c653dee1510f10b8c3ac3546c03b34948f5 Mon Sep 17 00:00:00 2001
From: Brian Osborne <bosborne@stargroup1.com>
Date: Thu, 17 Mar 2011 13:02:01 -0400
Subject: [PATCH] Issue #1062402 by bkosborne: Dropped sessions, replaced with session cookies

---
 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

