Possible fix for UID not a number + some small optimizations

Phliplip - October 9, 2009 - 06:40
Project:User Stats
Version:6.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:needs review
Description

Hi,

Added a fix for the UID not a number if UID = null or UID = 0 (Anonymous).
Also changed some syntax where the same function was called twice right after eachother.

I have made a patch with WinMerge on the latest user_stats.module in the 6.1 CVS-rep.

And my patch removes some previous patches, because i think my fix is doing the same job.

Try it out, give feedback

@@ -83,17 +83,11 @@
  *   counted types) then 'n/a' is returned.
  */
function user_stats_get_stats($type, $uid) {
-  // Bug [#479394] - Anonymous User Comment preview causes 'UID is not a number'
-  // errors. User Stats always expects UIDs to be numeric. Since this is a bit
-  // of a hack/workaround, we only fix the specific case of an anonymous user
-  // comment preview, everything should (and will) generate an error. These can
-  // be dealt with on a case-by-case basis.
-  $item = menu_get_item();
-  if ($uid == NULL && $item['path'] == 'comment/reply/%') {
-    $uid = 0;
-  }
   // Dirt simple error checking.
-  if (!is_numeric($uid)) {
+  if($uid == 0) {
+    return;
+  }
+  else if (!is_numeric($uid)) {
     trigger_error('UID is not a number', E_USER_WARNING);
     return;
   }
@@ -105,13 +99,15 @@
       return FALSE;
     }
     // Check cache.
-    if (user_stats_cache_get($type, $uid) === FALSE) {
+    $user_stats_cache_check = user_stats_cache_get($type, $uid);
+    if ($user_stats_cache_check === FALSE) {
       $query = db_query("SELECT ip_address
         FROM {user_stats_ips} WHERE uid = %d
         ORDER BY first_seen_timestamp LIMIT 1", $uid);
       user_stats_cache_set($type, $uid, db_result($query));
+      $user_stats_cache_check = user_stats_cache_get($type, $uid);
     }
-    return user_stats_cache_get($type, $uid);
+    return $user_stats_cache_check;
   }

   // Everything else is under the 'View statistics' permission.
@@ -120,8 +116,9 @@
   }

   // Check cache first.
-  if (user_stats_cache_get($type, $uid) !== FALSE) {
-    return user_stats_cache_get($type, $uid);
+  $user_stats_cache_check = user_stats_cache_get($type, $uid);
+  if ($user_stats_cache_check !== FALSE) {
+    return $user_stats_cache_check;
   }

   switch ($type) {
@@ -171,8 +168,8 @@
       $data = floor((time() - $user_access) / 86400);
       break;
     case 'online':
-      $user_access = db_result(db_query("SELECT timestamp FROM {sessions} WHERE uid = %d", $uid));
-      $data = ((time() - $user_access) < variable_get('user_block_seconds_online', 900) ? TRUE : FALSE);
+      $user_access = db_result(db_query("SELECT access FROM {users} WHERE uid = %d", $uid));
+      $data = (round((time() - $user_access) / 60) < 15 ? TRUE : FALSE);
       break;
     default: 
       // Raise an error if the statistic doesn't exist.
@@ -204,7 +201,10 @@
  */
function user_stats_cache_get($type, $uid) {
   // Dirt simple error checking.
-  if (!is_numeric($uid)) {
+  if($uid == 0) {
+    return;
+  }
+  else if (!is_numeric($uid)) {
     trigger_error('UID is not a number', E_USER_WARNING);
     return;
   }
@@ -245,13 +245,17 @@
  * @see user_stats_cache_get().
  */
function user_stats_cache_set($type = NULL, $uid = 0, $data = NULL) {
+  static $user_stats_cache = array();
+
   // Dirt simple error checking.
-  if (!is_numeric($uid)) {
+  if($uid == 0) {
+    return $user_stats_cache;
+  }
+  else if (!is_numeric($uid)) {
     trigger_error('UID is not a number', E_USER_WARNING);
     return;
   }

-  static $user_stats_cache = array();
   // Flush entire cache.
   if ($uid == -1 && $type == 'reset') {
     unset($user_stats_cache);
@@ -349,11 +353,6 @@
   // check to see if comments should be counted at all.
   if (variable_get('user_stats_count_comments', TRUE)) {
     $comment = (object)$a1;
-    // Bug [#479394] - Anonymous User Comment causes 'UID is not a number'
-    // errors. User Stats always expects UIDs to be numeric.
-    if ($comment->uid == NULL) {
-      $comment->uid = 0;
-    }
     $post_count_content_types = variable_get('user_stats_included_content_types', array());
     $node = node_load(array('nid' => $comment->nid));

@@ -644,8 +643,11 @@
  */
function user_stats_isset($statistic, $uid) {
   // Dirt simple error checking.
-  if (!is_numeric($uid)) {
-    trigger_error('UID is not a number', E_USER_WARNING);
+  if($uid == 0) {
+    return FALSE;
+  }
+  else if (!is_numeric($uid)) {
+    trigger_error('UID is not a number.', E_USER_WARNING);
     return;
   }

@@ -673,7 +675,10 @@
  */
function user_stats_login_count_update($op, $uid) {
   // Dirt simple error checking.
-  if (!is_numeric($uid)) {
+  if($uid == 0) {
+    return;
+  }
+  else if (!is_numeric($uid)) {
     trigger_error('UID is not a number', E_USER_WARNING);
     return;
   }
@@ -732,7 +737,10 @@
  */
function user_stats_post_count_update($op, $uid) {
   // Dirt simple error checking.
-  if (!is_numeric($uid)) {
+  if($uid == 0) {
+    return;
+  }
+  else if (!is_numeric($uid)) {
     trigger_error('UID is not a number', E_USER_WARNING);
     return;
   }
@@ -816,16 +824,15 @@
  */
function user_stats_ip_address_update($uid, $ip_address) {
   // Dirt simple error checking.
-  if (!is_numeric($uid)) {
-    trigger_error('UID is not a number', E_USER_WARNING);
-    return;
-  }
-
   // Don't bother recording IPs of anonymous users, and don't record any
   // addresses if the config form tells us not to.
   if ($uid == 0 || !variable_get('user_stats_track_ips', TRUE)) {
     return;
   }
+  else if (!is_numeric($uid)) {
+    trigger_error('UID is not a number', E_USER_WARNING);
+    return;
+  }

   $query = db_query_range("SELECT ip_address
     FROM {user_stats_ips} WHERE uid = %d
@@ -850,3 +857,4 @@
function user_stats_reset_counts($statistic) {
   db_query("DELETE FROM {user_stats_values} WHERE name = '%s'", $statistic);
}
+

AttachmentSize
user_stats-uid-not-a-number.patch5.47 KB
 
 

Drupal is a registered trademark of Dries Buytaert.