Index: tagadelic.module
===================================================================
--- tagadelic.module	(revision 259)
+++ tagadelic.module	(working copy)
@@ -72,6 +72,12 @@
     '#default_value' => variable_get('tagadelic_sort_order', 'title,asc'),
     '#description' => t('Determines the sort order of the tags on the freetagging page.'),
   );
+  $form['tagadelic_check_permission'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Check permissions on tags'),
+    '#default_value' => variable_get('tagadelic_check_permission',0),
+    '#description' => t('Before showing each term, check to see if user has permission to view at least one item of content tagged with that term. Terms will be sized according to the number of items the user has permission to see.')
+  );
   return $form;
 }
 
@@ -80,6 +86,7 @@
  */
 function tagadelic_page_chunk() {
   $vocs = arg(2);
+  global $user;
 
   if (is_numeric($vocs)) {
     $vocs = array($vocs);
@@ -93,7 +100,7 @@
     }
   }
 
-  $output = theme('tagadelic_weighted',tagadelic_get_weighted_tags($vocs));
+  $output = theme('tagadelic_weighted',tagadelic_get_weighted_tags($vocs,6,60,$user->uid));
 
   if (!$output) {
     return drupal_not_found();
@@ -109,7 +116,8 @@
  */
 function tagadelic_page_list() {
   $vocs = arg(2);
-
+  global $user;
+  
   if (is_numeric($vocs)) {
     $vocs = array($vocs);
   }
@@ -125,7 +133,7 @@
     if ($vocabulary->description) {
       $output .= theme("box", NULL, $vocabulary->description);
     }
-    $output .= theme('box', $vocabulary->name, theme('tagadelic_weighted', tagadelic_get_weighted_tags(array($vocabulary->vid))));
+    $output .= theme('box', $vocabulary->name, theme('tagadelic_weighted', tagadelic_get_weighted_tags(array($vocabulary->vid),6,60,$user->uid)));
   }
 
   if (!$output) {
@@ -177,7 +185,7 @@
  * API that returns an array with weighted tags
  * This is the hard part. People with better ideas are very very welcome to send these to ber@webschuur.com. Distribution is one thing that needs attention.
  */
-function tagadelic_get_weighted_tags($vids, $steps = 6, $size = 60) {
+function tagadelic_get_weighted_tags($vids, $steps = 6, $size = 60, $uid = NULL) {
   if (!is_array($vids) || count($vids) == 0) {
     return array();
   }
@@ -190,11 +198,13 @@
   $min = 1e9;
   $max = -1e9;
   while ($tag = db_fetch_object($result)) {
-    $tag->number_of_posts = $tag->count;
-    $tag->count = log($tag->count);
-    $min = min($min, $tag->count);
-    $max = max($max, $tag->count);
-    $tags[$tag->tid] = $tag;
+    if ($tag = tagadelic_check_permission($tag, $uid)) {
+      $tag->number_of_posts = $tag->count;
+      $tag->count = log($tag->count);
+      $min = min($min, $tag->count);
+      $max = max($max, $tag->count);
+      $tags[$tag->tid] = $tag;
+    }
   }
   // Note: we need to ensure the range is slightly too large to make sure even
   // the largest element is rounded down.
@@ -223,6 +233,34 @@
 }
 
 /**
+ * Check to see if user has permission to see a particular tag 
+ */
+function tagadelic_check_permission($tag, $uid) {
+  if (variable_get('tagadelic_check_permission',0)) {
+    $result = db_query('SELECT nid FROM {term_node} n INNER JOIN {term_data} d ON d.tid = n.tid WHERE d.name = "' . $tag->name .'" GROUP BY nid');
+
+    $allowed_count = 0;
+    while ($node = db_fetch_object($result)) {
+      $node = node_load($node);
+      if (node_access("view", $node, $uid)) {
+        $allowed = TRUE;
+        $allowed_count++;
+      }
+    }
+  
+    if ($allowed) {
+      $tag->count = $allowed_count;
+      return $tag;
+    }
+    else {
+      return FALSE;
+    }
+  } else {
+    return $tag;
+  }
+}
+
+/**
  * callback for usort, sort by count
  */
 function _tagadelic_sort_by_title($a, $b) {
