Index: modules/votingapi/votingapi.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/votingapi/votingapi.module,v
retrieving revision 1.25
diff -u -r1.25 votingapi.module
--- modules/votingapi/votingapi.module	21 Apr 2006 00:59:50 -0000	1.25
+++ modules/votingapi/votingapi.module	21 Apr 2006 01:18:28 -0000
@@ -72,14 +72,34 @@
     if (!isset($vote->tag)) {
       $vote->tag = VOTINGAPI_VALUE_DEFAULT_TAG;
     }
-
-    $result = db_query("SELECT * FROM {votingapi_vote} WHERE content_type='%s' AND content_id=%d AND tag='%s' AND value_type='%s' AND uid=%d", $content_type, $content_id, $vote->tag, $vote->value_type, $uid);
-    while ($vobj = db_fetch_object($result)) {
-      _votingapi_change_vote($vobj, $vote->value);
-      $exists = TRUE;
+    // If the uid is 0, we want to match based on IP address.
+    if ($uid === 0) {
+      if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
+        $hostname = $_SERVER['HTTP_X_FORWARDED_FOR'];
+      }
+      else {
+        $hostname = $_SERVER['REMOTE_ADDR'];
+      }
+      $result = db_query("SELECT * FROM {votingapi_vote} WHERE content_type='%s' AND content_id=%d AND tag='%s' AND value_type='%s' AND hostname='%s'", $content_type, $content_id, $vote->tag, $vote->value_type, $hostname);
+      while ($vobj = db_fetch_object($result)) {
+        if ($vobj->uid === 0) {
+          // this means that an the original vote was cast by an anonymous user, too.
+          // unfortunately, it means that anon votes from IPs that have already received
+          // registered votes will transparently fail. We need to improve this somehow.
+          _votingapi_change_vote($vobj, $vote->value);
+        }
+        $exists = TRUE;
+      }
+    }
+    else {
+      $result = db_query("SELECT * FROM {votingapi_vote} WHERE content_type='%s' AND content_id=%d AND tag='%s' AND value_type='%s' AND uid=%d", $content_type, $content_id, $vote->tag, $vote->value_type, $uid);
+      while ($vobj = db_fetch_object($result)) {
+        _votingapi_change_vote($vobj, $vote->value);
+        $exists = TRUE;
+      }
     }
     if (!$exists) {
-      _votingapi_add_vote($content_type, $content_id, $vote->value, $vote->value_type, $vote->tag);
+      _votingapi_add_vote($content_type, $content_id, $vote->value, $vote->value_type, $vote->tag, $uid);
     }
   }
   
@@ -202,7 +222,7 @@
 /**
  * An internal utility function used to pull raw votes for processing. Undocumented at the moment..
  */
-function _votingapi_get_raw_votes($content_type, $content_id, $value_type = NULL, $tag_list = NULL, $uid = NULL) {
+function _votingapi_get_raw_votes($content_type, $content_id, $value_type = NULL, $tag_list = NULL, $uid = NULL, $hostname = NULL) {
   if ($tag_list) {
     $filter_string .= " AND v.tag IN ('" . implode("','",$tag_list) . "')";
   }
@@ -227,6 +247,10 @@
     $filter_string .= " AND v.value_type = '" . $value_type . "'";
   }
 
+  if (isset($hostname)) {
+    $filter_string .= " AND v.hostname = '" . $hostname . "'";
+  }
+
   $votes = array();
   $result = db_query("SELECT * FROM {votingapi_vote} v WHERE content_type='%s' AND content_id=%d $filter_string", $content_type, $content_id);
   while ($vobj = db_fetch_object($result)) {
@@ -250,12 +274,12 @@
  * @return
  *   An array of matching votingapi_vote records.
  */
-function votingapi_get_user_votes($content_type, $content_id, $uid = NULL) {
+function votingapi_get_user_votes($content_type, $content_id, $uid = NULL, $hostname = NULL) {
   if ($uid == NULL) {
     global $user;
     $uid = $user->uid;
   }
-  return _votingapi_get_raw_votes($content_type, $content_id, NULL, NULL, $uid);
+  return _votingapi_get_raw_votes($content_type, $content_id, NULL, NULL, $uid, $hostname);
 }
 
 
