--- /opt/build/drupalmods47/filters/done/urlicon/urlicon.module	2006-10-17 21:54:11.000000000 +0800
+++ /opt/www/drupal-4.7.int.thatsbj.com/modules/urlicon/urlicon.module	2006-10-18 13:43:18.793981560 +0800
@@ -45,13 +45,12 @@
       // check for directory
       $dir = UC_FILE_PATH;
       file_check_directory($dir, 1);
-      
-      $text = preg_replace_callback('/<a(.+?)href=\"(.+?)(\")(.+?)?[^>]*>(.+?)<\/a>/i','urlicon_replace', $text);
+      $text = preg_replace_callback('/<a.+?href=\"(.+?)\"[^>]*>(.+?)<\/a>/i','urlicon_replace', $text);
       
       //save css file
       $nid = arg(1);
       if (!empty($nid)) file_save_data($uc_style, $dir.'/ui_'.check_plain(arg(1)).'.css', FILE_EXISTS_REPLACE);
-      drupal_set_message(arg(1));
+      drupal_set_message("(urlicon) Favicons stored for URLs for node: #".arg(1));
 //       if (file_exists(UC_FILE_PATH.'/ui_'.check_plain(arg(1)).'.css')) drupal_set_html_head('<link rel="stylesheet" type="text/css" href="'.base_path().UC_FILE_PATH.'/ui_'.check_plain(arg(1)).'.css" />');
       
       return $text;
@@ -65,27 +64,30 @@
   global $uc_style;
   $dir = UC_FILE_PATH;
   
-  $url = parse_url($match[2]);
+  $url = parse_url($match[1]);
   $domain = explode('.', $url['host']);
   $domain = $domain[(count($domain)-2)];
-  
+
   //check for favicon on remote site
   $data = drupal_http_request(check_plain('http://'.$url['host'].'/favicon.ico'));
-  
-  //check if favicon exists locally
-  if (!file_exists($dir.'/'.$domain.'.ico')) {
-  
-    //if favicon was found save it
-    if ($data->headers['Content-Type'] == 'image/x-icon') {
-      //save favicon to file
-      file_save_data($data->data, $dir.'/'.$domain.'.ico', FILE_EXISTS_REPLACE);
-    }
+
+  // First verify if the favicon was returned
+  if ( ($data->code == '200') && ($data->headers['Content-Length'] != '0') ) {
+      // then check if favicon exists locally
+      if (!file_exists($dir.'/'.$domain.'.ico')) {
+          // Added text for text/plain Content-Type as that was returned from a test of www.nytimes.com
+          if ( ($data->headers['Content-Type'] == 'image/x-icon') || ($data->headers['Content-Type'] == 'text/plain') ) {
+              //save favicon to file
+              file_save_data($data->data, $dir.'/'.$domain.'.ico', FILE_EXISTS_REPLACE);
+          }
+      }
+      // we only add the CSS stuff if the favicon was found to eliminate links with blank spaces
+      if (stristr($match[0], 'class')) {
+          $match[0] = str_replace('class="', 'class="ui-'.check_plain($domain).' ', $match[0]);
+      } else {
+          $match[0] = str_replace('">', '" class="ui-'.check_plain($domain).'">', $match[0]);
+      }
+      $uc_style .= ".ui-".check_plain($domain)." {background: url(".base_path().$dir."/".check_plain($domain).".ico) no-repeat right;padding-right:1.5em;}\n";
   }
-  
-  if (stristr($match[0], 'class')) $match[0] = str_replace('class="', 'class="ui-'.check_plain($domain).' ', $match[0]);
-  else $match[0] = str_replace('">', '" class="ui-'.check_plain($domain).'">', $match[0]);
-  
-  $uc_style .= ".ui-".check_plain($domain)." {background: url(".base_path().$dir."/".check_plain($domain).".ico) no-repeat;padding-left:1.5em;}\n";
-  
   return $match[0];
 }
