? ._invisimail_links_6.x_1.patch
? invisimail_links_6.x_1.patch
? invisimail_newregex.patch
? invisimail_work.patch
Index: invisimail.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/invisimail/invisimail.module,v
retrieving revision 1.3.4.6
diff -u -p -r1.3.4.6 invisimail.module
--- invisimail.module	31 Mar 2009 22:48:38 -0000	1.3.4.6
+++ invisimail.module	27 May 2009 21:25:19 -0000
@@ -37,7 +37,7 @@ function invisimail_menu() {
       'type' => MENU_NORMAL_ITEM,
     );
   }
-  
+
   return $items;
 }
 
@@ -90,7 +90,7 @@ function invisimail_filter($op, $delta =
             '#collapsible' => true, '#collapsed' => true
           );
           $form['invisimail_settings'] += _invisimail_settings($format);
-          
+
           return $form;
       }
       break;
@@ -98,28 +98,55 @@ function invisimail_filter($op, $delta =
 }
 
 function invisimail($string, $format) {
-  $pattern = '!(<p>|<li>|<br />|[\s\(])([A-Za-z0-9._-]+@[A-Za-z0-9._-]+\.[A-Za-z]{2,4})([.,]?)(?=(</p>|</li>|<br />|[\s/)]))!i';  
+  $pattern = "!(<p>|<li>|<br />|[ \n\r\t\(])([A-Za-z0-9._-]+@[A-Za-z0-9._-]+\.[A-Za-z]{2,4})([.,]?)(?=(</p>|</li>|<br />|[ \n\r\t\)]))!i";
   // the callback needs to know what filter we're using
   // however there's no way to hand off that variable
   // so we'll set a global variable
   $GLOBALS['invisimail_format'] = $format;
 
-  return preg_replace_callback($pattern, 'invisimail_callback', $string);
+  // First fix addresses that are already linked
+  $pattern = "!<a([^>]+?)href=[\"']mailto:([^\"']+?)[\"'](.*?)>(.*?)</a>!is";
+  $string = preg_replace_callback($pattern, '_invisimail_replace_mailto', $string);
+
+  $pattern = "!(^|<p>|<td>|<li>|<br />|[ \n\r\t\(])([A-Za-z0-9._-]+@[A-Za-z0-9._-]+\.[A-Za-z]{2,4})([.,]?)(?=(</p>|</td>|</li>|<br />|[ \n\r\t\)]|$))!i";
+  $string = preg_replace_callback($pattern, '_invisimail_replace_email', $string);
+
+  return $string;
 }
 
-function invisimail_callback($matches) {
+/**
+ * Calls the encode function on email addresses and converts them into links
+ */
+function _invisimail_replace_email($matches) {
   $format = $GLOBALS['invisimail_format'];
   $js = variable_get('invisimail_js_'.$format, FALSE);
   $link = variable_get('invisimail_link_'.$format, FALSE);
-  
+
   return $matches[1] . invisimail_ascii_encode($matches[2], $js, $link) . $matches[3];
 }
 
 /**
- * This function does the actual encoding. 
+ * Calls the encode function on existing mailto: links
+ */
+function _invisimail_replace_mailto($matches) {
+  $format = $GLOBALS['invisimail_format'];
+  $js = variable_get('invisimail_js_'.$format, FALSE);
+  $link = variable_get('invisimail_link_'.$format, FALSE);
+  $attribs = $matches[1] . $matches[3];
+  $text = null;
+  // If the text is not the email address we don't want it encoded
+  if ($matches[2] != trim($matches[4])) {
+    $text = $matches[4];
+  }
+
+  return invisimail_ascii_encode($matches[2], $js, $link, $text, $attribs);
+}
+
+/**
+ * This function does the actual encoding.
  *
  * @param $string
- *   A string which contains only an email addres to be encoded. 
+ *   A string which contains only an email addres to be encoded.
  * @param $js
  *   Optional: A boolean which sets whether Javascript is used for encoding.
  * @param $link
@@ -127,21 +154,18 @@ function invisimail_callback($matches) {
  * @param $text
  *   Optional: The text to be used for the link.
  * @return
- *   An ascii encoded email address. 
+ *   An ascii encoded email address.
  */
-function invisimail_ascii_encode($string, $js = FALSE, $link = FALSE, $text = NULL) {
-  if ($text == NULL) {
-    $text = $string;
-  }
-  
+function invisimail_ascii_encode($string, $js = FALSE, $link = FALSE, $text = NULL, $attribs = '') {
+
   if ($js) {
     $output = "<script type='text/javascript'><!--
     document.write('";
   }
-  
-  for ($i=0; $i < strlen($string); $i++)	 {
+
+  for ($i=0; $i < strlen($string); $i++)   {
     $char = substr($string, $i, 1);
-    $encode .= '&#'.ord($char).';';	 	 
+    $encode .= '&#'.ord($char).';';
     if ($js) {
       if (in_array($char, array('.', '@'))) {
       // break strings after ats and dots.
@@ -149,13 +173,16 @@ function invisimail_ascii_encode($string
       }
     }
   }
-  
+
   if ($link && !$js) {
     // ascii in this next line is "mailto:"
-    $output .= "<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;$encode\">$encode</a>";
+    $linkText = $text == null ? $encode : $text;
+    $output .= "<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;$encode\"$attribs>$linkText</a>";
   }
   elseif ($link && $js) {
-    $output .= "<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;'+'$encode'+'\">'+'$encode'+'</a>";
+    $attribs = str_replace("'", "\\'", $attribs);
+    $linkText = $text == null ? $encode : str_replace("'", "\\'", $text);
+    $output .= "<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;'+'$encode'+'\"$attribs>'+'$linkText'+'</a>";
   }
   else {
     $output .= $encode;
@@ -166,7 +193,7 @@ function invisimail_ascii_encode($string
     //-->
     </script>";
   }
-    
+
   return $output;
 }
 
@@ -238,6 +265,6 @@ function _invisimail_settings($format) {
     '#options' => array(FALSE => t('Do not create links.'), TRUE => t('Automatically create links from email addresses.')),
     '#description' => t('Selecting "Automatically create links" will convert email addresses into a clickable "mailto:" link.'),
   );
-  
+
   return $form;
-}
+}
\ No newline at end of file
