<?php
/*
 * URL Encoder script.
 * Version 1.0 - February 2002
 * (c) 2002, Paul Gregg <pgregg@pgregg.com>
 * http://www.pgregg.com
 *
 * Function: Take an href link and the visible text and make an obfuscated
 * link to prevent search engines (or spam harvesters) from picking it up.
 *
 * Open Source Code:   If you use this code on your site for public
 * access (i.e. on the Internet) then you must attribute the author and
 * source web site: http://www.pgregg.com/projects/
 * You must also make this original source code available for download
 * unmodified or provide a link to the source.  Additionally you must provide
 * the source to any modified or translated versions or derivatives.
 *
 */


$PHP_SELF $_SERVER['PHP_SELF'];
$href = isset($_GET['href']) ? $_GET['href'] : '';
$text = isset($_GET['text']) ? $_GET['text'] : '';
$func = isset($_GET['func']) ? $_GET['func'] : '';

printf("<p align=right><a href=\"%ss\">Source code</a></p>\n"$PHP_SELF);

$encoder_functions = array (
    
"transpose" => "Transpose characters / Javscript",
    
"hash" => "Hashes characters / No Javscript"
    
);
$valid_functions array_keys($encoder_functions);


if (
$href != "") {
  
# Encode the $href string according to the selected function

  
if ($text == "")
    
$text "No text specified.";

  Function 
escapeencode ($str) {
    
$ret "";
    
$arr unpack("C*"$str);
    foreach (
$arr as $char)
      
$ret .= sprintf("%%%X"$char);
    return 
$ret;
  }

  Function 
transpose($str) {
    
# function takes the string and swaps the order of each group of 2 chars
    
$len strlen($str);
    
$ret "";
    for (
$i=0$i<$len$i=$i+2) {
      if (
$i+== $len)
        
$ret .= substr($str$i1);
      else
        
$ret .= sprintf("%s%s"substr($str$i+11), substr($str$i1));
    }
    return 
$ret;
  }

  
# replace wierd chars in the href
  
$href preg_replace("/[;,]/"""$href);

  if (! isset(
$func) ) {
    
$func "transpose";
  } else {
    if (! 
in_array($func$valid_functions) )
      
$func "transpose";  # prevent bogus functions
  
}

  
printf("<p>Encoding link <b>%s</b> using function <b>%s</b></p>\n",
    
htmlspecialchars($href), $func);

  if (
$func == "transpose") {

    
$code sprintf("var s='%s';var r='';for(var i=0;i<s.length;i++,i++){r=r+s.substring(i+1,i+2)+s.substring(i,i+1)}document.write('<a href=\"'+r+'\">%s</a>');"transpose($href), $text);
    
$UserCode sprintf("%s%s%s",
    
"<SCRIPT language=\"javascript\">eval(unescape('",
    
escapeencode($code),
    
"'))</SCRIPT>");

  } elseif (
$func == "hash") {

    
$prepend "";
    if (
preg_match("/^mailto:/"$href)) {
      
$href preg_replace("/^mailto:/"""$href);
      
$prepend "mailto:";
    }
    if (
preg_match("/^http:\/\//"$href)) {
      
$href preg_replace("/^http:\/\//"""$href);
      list(
$server,$url) = split("/"$href2);
      
$href $url;
      
$prepend "http://$server/";
    }
    
$UserCode sprintf("<a href=\"%s%s\">%s</a>",
        
$prepend,
    
escapeencode($href), $text);

  }

  
printf("<p>Cut and paste the following code into your page where you want the link to be:</p><pre>\n%s\n</pre>\n<hr>\n%s</p>\n",
    
htmlspecialchars($UserCode), $UserCode);

} else {

  print 
"Add encoded mailto: (or not) links into webpages preventing spammers robots from harvesting them.\n";
  print 
"<form method=GET action=\"$PHP_SELF\">";
?>
  <table>
   <tr><td>Link/href</td>
   <td><input type=text name=href value="mailto:" size=30></td>
   </tr>
   <tr><td>Displayed text</td>
   <td><input type=text name=text value="" size=30></td>
   </tr>
   <tr><td>Encoding Function</td>
   <td><select name=func>
<?php
   
foreach ($encoder_functions as $key => $value)
     
printf("<option value=\"%s\">%s</option>"$key$value);
?>
       </select></td>
   </tr>
   <tr><td></td><td><input type=submit value="Encode"></td></tr>
  </table>
  </form>

<?php
}

?>