--- spam_filter_url.module	2012-01-13 15:24:00.373197080 +0100
+++ spam_filter_url.NEU.module	2012-01-13 15:25:28.062071879 +0100
@@ -10,14 +10,19 @@
 
 function spam_filter_url_spamapi($op, $type = NULL, $content = array(), $fields = array(), $extra = NULL) {
   // Don't both with this hook unless the filter is actually enabled.
-
   switch ($op) {
     case 'filter':
-      if (!module_invoke('spam', 'filter_enabled', 'spam_filter_url', $type, $content, $fields, $extra)) return;
-      return spam_filter_url_spam_filter($content, $type, $fields, $extra);
+    	if (!module_invoke('spam', 'filter_enabled', 'spam_filter_url', $type, $content, $fields, $extra)) {
+				return;
+			}
+			else {
+  			return spam_filter_url_spam_filter($content, $type, $fields, $extra);
+			}
+		break;
 
     case 'filter_module':
-      return 'spam_filter_url';
+    	return 'spam_filter_url';
+		break;
 
     case 'filter_info':
       return array(
@@ -41,93 +46,100 @@ function spam_filter_url_spamapi($op, $t
       spam_log(SPAM_DEBUG, 'spam_filter_url_spamapi', t('@op', array('@op' => $op)), $type, $extra['id']);
       $fields = spam_invoke_module($type, 'filter_fields', $extra['content']);
       $spam_filter_urls = _spam_filter_url_extract($extra['content'], $type, $fields, $extra);
-      spam_filter_url_update($spam_filter_urls, ($op == 'mark_as_spam' ? TRUE : FALSE), $type, $extra['id']);
+
+	//	H.S. TODO: make this only if bayesian filter is ON
+	//     spam_filter_url_update($spam_filter_urls, ($op == 'mark_as_spam' ? TRUE : FALSE), $type, $extra['id']);
       break;
   }
+
 }
 
 function _spam_filter_url_extract($content, $type, $fields, $extra = array()) {
+	// H.S.
   static $spam_filter_urls = array();
+
   $id = spam_invoke_module($type, 'content_id', $content, $extra);
 
   if (is_object($content)) {
-    $content = (array)$content;
+	$content = (array)$content;
+	
+	if (!isset($spam_filter_urls[$id]["$type-$id"])) {
+		$string = '';
+		if (isset($fields['main']) && is_array($fields['main'])) {
+			foreach ($fields['main'] as $field) {
+				$string .= $content["$field"] .' ';
+			}
+		}
+		if (isset($fields['other']) && is_array($fields['other'])) {
+			foreach ($fields['other'] as $field) {
+				$string .= $content["$field"] .' ';
+			}
+		}
+		
+		$URI = "(http://|https://|ftp://|mailto:|smb://|afp://|file://|gopher://|news://|ssl://|sslv2://|sslv3://|tls://|tcp://|udp://)";
+		
+		preg_match_all('('.$URI.'([a-zA-Z0-9@:%_+*~#?&=.,/;-]*[a-zA-Z0-9@:%_+*~#&=/;-]))', $string, $matches_absolute);
+		preg_match_all('([^'.$URI.']www\.([a-zA-Z0-9@:%_+*~#?&=.,/;-]*[a-zA-Z0-9@:%_+*~#&=/;-]))', $string, $matches_www);
+	
+		$matches = array_merge($matches_absolute[2], $matches_www[0]);
+				
+		$u = array();
+		foreach ($matches as $spam_filter_url) {
+		
+			$spam_filter_url = preg_replace("'$URI'", '', $spam_filter_url);
+			// get full domain (ie www.sample.com)
+			preg_match("/^()?([^\/\"\']+)/i", $spam_filter_url, $domain);
+			// get root domain (ie sample.com)
+			preg_match("/[^\.\/]+\.[^\.\/]+$/", $domain[2], $root);
+			
+			$spam_filter_url = htmlspecialchars(drupal_strtolower($root[0]));
+			_spam_filter_url_count($spam_filter_url, $id);
+			$u[] = $spam_filter_url;
+		}
+		$spam_filter_urls[$id]["$type-$id"] = $u;
+	}
+	
+	return $spam_filter_urls[$id]["$type-$id"];
   }
-
-  if (!isset($spam_filter_urls["$type-$id"])) {
-    $string = '';
-    if (isset($fields['main']) && is_array($fields['main'])) {
-      foreach ($fields['main'] as $field) {
-        $string .= $content["$field"] .' ';
-      }
-    }
-    if (isset($fields['other']) && is_array($fields['other'])) {
-      foreach ($fields['other'] as $field) {
-        $string .= $content["$field"] .' ';
-      }
-    }
-
-    $URI = "(http://|https://|ftp://|mailto:|smb://|afp://|file://|gopher://|news://|ssl://|sslv2://|sslv3://|tls://|tcp://|udp://)";
-    // Find all urls in content. Code based on _filter_url().
-    // Match absolute URLs.
-    preg_match_all("`(<p>|<li>|<br\s*/?>|[ \n\r\t\(])($URI([a-zA-Z0-9@:%_+*~#?&=.,/;-]*[a-zA-Z0-9@:%_+*~#&=/;-]))([.,?!]*?)(?=(</p>|</li>|<br\s*/?>|[ \n\r\t\)]))`i", $string, $matches_absolute);
-    // Match e-mail addresses.
-    preg_match_all("`(<p>|<li>|<br\s*/?>|[ \n\r\t\(])([A-Za-z0-9._-]+@[A-Za-z0-9._+-]+\.[A-Za-z]{2,4})([.,?!]*?)(?=(</p>|</li>|<br\s*/?>|[ \n\r\t\)]))`i", $string, $matches_email);
-    // Match www domains/addresses.
-    preg_match_all("`(<p>|<li>|[ \n\r\t\(])(www\.[a-zA-Z0-9@:%_+*~#?&=.,/;-]*[a-zA-Z0-9@:%_+~#\&=/;-])([.,?!]*?)(?=(</p>|</li>|<br\s*/?>|[ \n\r\t\)]))`i", $string, $matches_www);
-    $matches = array_merge($matches_absolute[2], $matches_email[2], $matches_www[2]);
-    $u = array();
-    foreach ($matches as $spam_filter_url) {
-      $spam_filter_url = preg_replace("'$URI'", '', $spam_filter_url);
-      // get full domain (ie www.sample.com)
-      preg_match("/^()?([^\/\"\']+)/i", $spam_filter_url, $domain);
-      // get root domain (ie sample.com)
-      preg_match("/[^\.\/]+\.[^\.\/]+$/", $domain[2], $root);
-      $spam_filter_url = htmlspecialchars(drupal_strtolower($root[0]));
-      _spam_filter_url_count($spam_filter_url);
-      $u[] = $spam_filter_url;
-    }
-    $spam_filter_urls["$type-$id"] = $u;
+  else {
+		return $spam_filter_urls;
   }
-
-  return $spam_filter_urls["$type-$id"];
 }
 
 /**
  * Search for known spam urls in content.
  */
 function spam_filter_url_spam_filter($content, $type, $fields, $extra = array(), $filter_test = FALSE) {
-  $action = array();
+ 
+	$action = array();
 
   $id = spam_invoke_module($type, 'content_id', $content, $extra);
   $spam = FALSE;
-
   $spam_filter_urls = _spam_filter_url_extract($content, $type, $fields, $extra);
-  
   $probability = variable_get('spam_filter_url_probability', 99);
 
   if (is_array($spam_filter_urls) && !empty($spam_filter_urls)) {
+
     $count = _spam_filter_url_count();
 
     $limit = variable_get('spam_filter_url_limit_total', 10);
-    if ($limit > -1 && $count['total'] > $limit) {
-      spam_log(SPAM_VERBOSE, 'spam_filter_url_spam_filter', t('total urls(@total) > spam_filter_url_limit_total(@limit)', array('@total' => $count['total'], '@limit' => variable_get('spam_filter_url_limit_total', 10))), $type, $id);
+
+    if ($limit > -1 && $count[$id]['total'] > $limit) {
+      spam_log(SPAM_VERBOSE, 'spam_filter_url_spam_filter', t('total urls(@total) > spam_filter_url_limit_total(@limit)', array('@total' => $count[$id]['total'], '@limit' => variable_get('spam_filter_url_limit_total', 10))), $type, $id);
       $action['spam_filter_url'][] = array(
         'limit' => 'total',
-        'total' => $count['total'],
+        'total' => $count[$id]['total'],
       );
       $action['total'] = $probability;
       return $action;
     }
+
     $limit = variable_get('spam_filter_url_limit_repeat', 5);
     if ($limit > -1) {
-      // Sort urls from most repeated to least repeated.
-      asort($count);
+      asort($count[$id]);	      // Sort urls from most repeated to least repeated.
+      array_pop($count[$id]);       // skip count[$id]['total']
+      $max = array_pop($count[$id]);
 
-      // skip count['total']
-      array_pop($count);
-
-      $max = array_pop($count);
       if ($max > $limit) {
         spam_log(SPAM_VERBOSE, 'spam_filter_url_spam_filter', t('repeated urls(@total) > spam_filter_url_limit_repeat(@limit)', array('@total' => $max, '@limit' => variable_get('spam_filter_url_limit_repeat', 5))), $type, $id);
         $action['spam_filter_url'][] = array(
@@ -139,7 +151,7 @@ function spam_filter_url_spam_filter($co
       }
     }
 
-    foreach ($spam_filter_urls as $spam_filter_url) {
+    foreach ($spam_filter_urls[$id] as $spam_filter_url) {
       $p = db_fetch_object(db_query("SELECT probability FROM {spam_filter_bayesian_tokens} WHERE class = 'spam_filter_url' AND token = '%s'", $spam_filter_url));
       $action['spam_filter_url'][] = array(
         'spam_filter_url' => $spam_filter_url,
@@ -161,16 +173,22 @@ function spam_filter_url_spam_filter($co
     $action['total'] = 0;
   }
 
+
   return $action;
 }
 
+
 /**
  * Update url probabilities in database.
  */
+// H.S. TODO: make this only if bayesian filter is ON
 function spam_filter_url_update($spam_filter_urls, $yes, $type, $id) {
   module_invoke('spam_filter_bayesian', 'tokens_update', 'spam_filter_url', $spam_filter_urls, $yes, $type, $id);
 }
 
+
+
+
 /**
  * Keep track of the total number of URLs found in the current content.
  *
@@ -178,23 +196,26 @@ function spam_filter_url_update($spam_fi
  * @return      Array of URLs showing how many times each URL is present, and
  *              the total number of arrays.
  */
-function _spam_filter_url_count($spam_filter_url = NULL) {
+function _spam_filter_url_count($spam_filter_url = NULL, $id = 0) {
   // build up an array of all URLs seen in current content
-  static $spam_filter_urls = array('total' => 0);
+  static $spam_filter_urls;
 
   if ($spam_filter_url != NULL) {
-    if (empty($spam_filter_urls["$spam_filter_url"])) {
-      $spam_filter_urls["$spam_filter_url"] = 1;
+    if (empty($spam_filter_urls[$id]["$spam_filter_url"])) {
+      $spam_filter_urls[$id]["$spam_filter_url"] = 1;
     }
     else {
-      $spam_filter_urls["$spam_filter_url"]++;
+      $spam_filter_urls[$id]["$spam_filter_url"]++;
     }
-    $spam_filter_urls['total']++;
+    $spam_filter_urls[$id]['total']++;
   }
 
   return $spam_filter_urls;
 }
 
+
+
+
 function spam_filter_url_menu() {
   $items = array();
 
