<?php
/*
 * URL Encoder script.
 * Version 1.0 - February 2002
 * Version 1.1 - December 2007 - added transpose2 - a delayed action on decoding
 * Version 1.2 - August 2008 - renamed transpose2 to encodelink_delayed and updated encodelink_delayed to allow it to be used multiple times in a page
 * (c) 2002-2008, 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.
 *
 */

$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
$REQUEST_URI = $_SERVER['REQUEST_URI'];
$PHP_SELF = $_SERVER['PHP_SELF'];

include_once($DOCUMENT_ROOT . '/functions.php');
$href = isset($_GET['href']) ? $_GET['href'] : '';
$text = isset($_GET['text']) ? $_GET['text'] : '';
$func = isset($_GET['func']) ? $_GET['func'] : '';

echo '<p align=right><a href="content.phps">Source code</a></p>';
print "<p align=right><a href=\"encode_link.inc.phps\">PHP includable</a></p>\n";

$encoder_functions = array (
    "encodelink_delayed" => "Delayed Transpose characters / Javascript",
    "encodelink" => "Transpose characters / Javascript",
    "encodehash" => "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.";

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

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

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

  include 'encode_link.inc.php';
  $UserCode = call_user_func($func, $href, $text);
#echo '<pre>';
#var_dump($UserCode);

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

} else {

  if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on') {
    print "<a href='$shome$PHP_SELF'><h4>You can use this form securely.</h4></a><br><br>\n";
  }
  print "<p>Tired of spammer's crawlers picking up your email address and spamming you? The solution is here - Encode your email address.<br></p>\n";
  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
  if (isset($_SERVER['HTTP_REFERER'])
   && preg_match("|http://spamlinks.datapacket.net/spambots.htm|",
    $_SERVER['HTTP_REFERER'])) {
    print "<p>Note: The referring page says '(javascript option fails?)' - it should work just fine. Please let me know if it doesn't work for you.</p>";
  }
}

?>