/encode/index.php

https://github.com/pornel/hCardValidator · PHP · 73 lines · 68 code · 3 blank · 2 comment · 1 complexity · 1feaceca087cbd9c1b6d7fd27ebad432 MD5 · raw file

  1. <!DOCTYPE html>
  2. <html>
  3. <link rel=stylesheet href="/i/style.css">
  4. <meta charset=UTF-8>
  5. <meta name=robots content="index,nofollow">
  6. <title>e-mail address obfuscator</title>
  7. <h1>hCard-friendly e-mail address obfuscator</h1>
  8. <p>It encodes e-mail addresses using random mix of <code>urlencode</code>, <abbr>HTML</abbr> entities and then generates markup that's as tricky as possible, while remaining <strong>valid and parseable by browsers</strong> and <abbr>XML</abbr>-compliant parsers.
  9. <form id=email-encoder>
  10. <div><label for=email>e-mail address:</label> <input id=email type=email name=addr required> <small>(<em>obviously</em>, these e-mails are not collected)</small></div>
  11. <p><input type=submit value=Encode></p>
  12. </form>
  13. <?php
  14. /**
  15. * obfuscates e-mail address
  16. * @param in_attribute - if true, will not put HTML comments in it (result will suitable for use in href)
  17. */
  18. function html_encode_email_address($m,$in_attribute=true)
  19. {
  20. $o='';
  21. if ($in_attribute)
  22. {
  23. for($i=0;$i<strlen($m);$i++)
  24. {
  25. // apply url-encoding at random just to be more confusing
  26. $o .= (mt_rand(0,100) > 60 || !ctype_alnum($m[$i]))?sprintf('%%%02x',ord($m[$i])):$m[$i];
  27. }
  28. $m = 'mailto:%20'.$o.'?'; $o=''; // query string is allowed in mailto:, even if empty
  29. }
  30. for($i=0;$i<strlen($m);$i++)
  31. {
  32. if (!$in_attribute && $i==strlen($m)>>1) $o .= '<!--
  33. mailto:abuse@hotmail.com
  34. </a>
  35. -->&shy;';
  36. // random characters are encoded + few special characters for added trickyness.
  37. // <>& are encoded to protect encoder against XSS.
  38. if (mt_rand(0,100) > 40 || false !== strpos(" .:<>&",$m[$i]))
  39. {
  40. // mix of decimal and hexadecimal entities
  41. $format = (mt_rand(0,100) > 66) ? '&#%d;' :
  42. '&#x%'.((mt_rand()&4)?'X':'x').';';
  43. $o .= sprintf($format, ord($m[$i]));
  44. }
  45. else
  46. {
  47. $o .= $m[$i];
  48. }
  49. }
  50. return $o;
  51. }
  52. if (isset($_GET['addr']))
  53. {
  54. $addr = trim($_GET['addr']);
  55. // that's class attribute containing newlines and attribute-like syntax.
  56. // should be enough to confuse regex-based extractors
  57. $out = "<a\nclass='email\nhref=\"mailto:x@y\"\n'\nhref\n =\t'\t\n&#x20;" .
  58. html_encode_email_address($addr) .
  59. "\n'>" .
  60. html_encode_email_address($addr,false) .
  61. '</a>';
  62. echo '<pre style="padding:2em;border:1px dashed #eee"><code>'.htmlspecialchars($out).'</code></pre>';
  63. echo '<p>Test: '.$out.'</p>';
  64. }
  65. ?>
  66. <hr>
  67. <p><a href="/">Return to the hCard Validator</a>.