PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/email-address-encoder/email-address-encoder.php

https://github.com/pica-design/nextstepmaine
PHP | 135 lines | 51 code | 20 blank | 64 comment | 10 complexity | 0ba3a18d01ec043111fc0bc6403160c7 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-3.0, GPL-3.0
  1. <?php
  2. /*
  3. Plugin Name: Email Address Encoder
  4. Plugin URI: http://tillkruess.com/project/email-address-encoder/
  5. Description: A lightweight plugin to protect email addresses from email-harvesting robots by encoding them into decimal and hexadecimal entities.
  6. Version: 1.0.4
  7. Author: Till Kr端ss
  8. Author URI: http://tillkruess.com/
  9. License: GPLv3
  10. License URI: http://www.gnu.org/licenses/gpl-3.0.html
  11. */
  12. /**
  13. * Copyright 2013 Till Kr端ss (www.tillkruess.com)
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation, either version 3 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. * @package Email Address Encoder
  29. * @copyright 2013 Till Kr端ss
  30. */
  31. /**
  32. * Define plugin constants that can be overridden, generally in wp-config.php.
  33. */
  34. if (!defined('EAE_FILTER_PRIORITY'))
  35. define('EAE_FILTER_PRIORITY', 1000);
  36. /**
  37. * Register filters to encode exposed email addresses in
  38. * posts, pages, excerpts, comments and widgets.
  39. */
  40. foreach (array('the_content', 'the_excerpt', 'widget_text', 'comment_text', 'comment_excerpt') as $filter) {
  41. add_filter($filter, 'eae_encode_emails', EAE_FILTER_PRIORITY);
  42. }
  43. /**
  44. * Searches for plain email addresses in given $string and
  45. * encodes them (by default) with the help of eae_encode_str().
  46. *
  47. * Regular expression is based on based on John Gruber's Markdown.
  48. * http://daringfireball.net/projects/markdown/
  49. *
  50. * @param string $string Text with email addresses to encode
  51. * @return string $string Given text with encoded email addresses
  52. */
  53. function eae_encode_emails($string) {
  54. // abort if $string doesn't contain a @-sign
  55. if (apply_filters('eae_at_sign_check', true)) {
  56. if (strpos($string, '@') === false) return $string;
  57. }
  58. // override encoding function with the 'eae_method' filter
  59. $method = apply_filters('eae_method', 'eae_encode_str');
  60. // override regex pattern with the 'eae_regexp' filter
  61. $regexp = apply_filters(
  62. 'eae_regexp',
  63. '{
  64. (?:mailto:)?
  65. (?:
  66. [-!#$%&*+/=?^_`.{|}~\w\x80-\xFF]+
  67. |
  68. ".*?"
  69. )
  70. \@
  71. (?:
  72. [-a-z0-9\x80-\xFF]+(\.[-a-z0-9\x80-\xFF]+)*\.[a-z]+
  73. |
  74. \[[\d.a-fA-F:]+\]
  75. )
  76. }xi'
  77. );
  78. return preg_replace_callback(
  79. $regexp,
  80. create_function(
  81. '$matches',
  82. 'return '.$method.'($matches[0]);'
  83. ),
  84. $string
  85. );
  86. }
  87. /**
  88. * Encodes each character of the given string as either a decimal
  89. * or hexadecimal entity, in the hopes of foiling most email address
  90. * harvesting bots.
  91. *
  92. * Based on Michel Fortin's PHP Markdown:
  93. * http://michelf.com/projects/php-markdown/
  94. * Which is based on John Gruber's original Markdown:
  95. * http://daringfireball.net/projects/markdown/
  96. * Whose code is based on a filter by Matthew Wickline, posted to
  97. * the BBEdit-Talk with some optimizations by Milian Wolff.
  98. *
  99. * @param string $string Text with email addresses to encode
  100. * @return string $string Given text with encoded email addresses
  101. */
  102. function eae_encode_str($string) {
  103. $chars = str_split($string);
  104. $seed = mt_rand(0, (int) abs(crc32($string) / strlen($string)));
  105. foreach ($chars as $key => $char) {
  106. $ord = ord($char);
  107. if ($ord < 128) { // ignore non-ascii chars
  108. $r = ($seed * (1 + $key)) % 100; // pseudo "random function"
  109. if ($r > 60 && $char != '@') ; // plain character (not encoded), if not @-sign
  110. else if ($r < 45) $chars[$key] = '&#x'.dechex($ord).';'; // hexadecimal
  111. else $chars[$key] = '&#'.$ord.';'; // decimal (ascii)
  112. }
  113. }
  114. return implode('', $chars);
  115. }