-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
email-address-encoder.php
209 lines (180 loc) · 6.11 KB
/
email-address-encoder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/*
Plugin Name: Email Address Encoder
Plugin URI: https://encoder.till.im/
Description: A lightweight plugin that protects email addresses from email-harvesting robots by encoding them into decimal and hexadecimal entities.
Version: 1.0.24
Author: Till Krüss
Author URI: https://till.im/
Text Domain: email-address-encoder
Domain Path: /languages
License: GPLv3
License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Define filter-priority constant, unless it has already been defined.
*/
if ( ! defined( 'EAE_FILTER_PRIORITY' ) ) {
define(
'EAE_FILTER_PRIORITY',
(integer) get_option( 'eae_filter_priority', 1000 )
);
}
/**
* Define regular expression constant, unless it has already been defined.
*/
if ( ! defined( 'EAE_REGEXP' ) ) {
define(
'EAE_REGEXP',
'{
(?:mailto:)?
(?:
[-!#$%&*+/=?^_`.{|}~\w\x80-\xFF]+
|
".*?"
)
\@
(?:
[-a-z0-9\x80-\xFF]+(\.[-a-z0-9\x80-\xFF]+)*\.[a-z]+
|
\[[\d.a-fA-F:]+\]
)
}xi'
);
}
/**
* Load admin related code.
*/
require_once __DIR__ . '/includes/admin.php';
/**
* Register uninstall and activation hooks.
*/
register_uninstall_hook( __FILE__, 'eae_uninstall_hook' );
register_activation_hook( __FILE__, 'eae_activation_hook' );
/**
* Register filters to encode plain email addresses in posts, pages, excerpts,
* comments, text widgets and nav menus.
*/
if ( get_option( 'eae_search_in', 'filters' ) !== 'void' ) {
foreach ( array( 'the_content', 'the_excerpt', 'widget_text', 'comment_text', 'comment_excerpt' ) as $filter ) {
add_filter( $filter, 'eae_encode_emails', EAE_FILTER_PRIORITY );
}
add_filter( 'walker_nav_menu_start_el', 'eae_encode_emails', EAE_FILTER_PRIORITY );
}
/**
* Attempt to register the shortcode relatively late to avoid conflicts.
*/
add_action( 'init', 'eae_register_shortcode', 1000 );
/**
* Register the [encode] shortcode, if it doesn't exist.
*
* @return void
*/
function eae_register_shortcode() {
if ( ! shortcode_exists( 'encode' ) ) {
add_shortcode( 'encode', 'eae_shortcode' );
}
}
/**
* The [encode] shortcode callback function. Returns encoded shortcode content.
*
* @param array $attributes Shortcode attributes
* @param string $string Shortcode content
*
* @return string Encoded given text
*/
function eae_shortcode( $attributes, $content = '' ) {
$atts = shortcode_atts( array(
'link' => null,
'class' => null,
), $attributes, 'encode' );
// override encoding function with the 'eae_method' filter
$method = apply_filters( 'eae_method', 'eae_encode_str' );
if ( ! empty( $atts[ 'link' ] ) ) {
$link = esc_url( $atts[ 'link' ], null, 'shortcode' );
if ( $link === '' ) {
return $method( $content );
}
if ( empty( $atts[ 'class' ] ) ) {
return sprintf(
'<a href="%s">%s</a>',
$method( $link ),
$method( $content )
);
}
return sprintf(
'<a href="%s" class="%s">%s</a>',
$method( $link ),
esc_attr( $atts[ 'class' ] ),
$method( $content )
);
}
return $method( $content );
}
/**
* Searches for plain email addresses in given $string and
* encodes them (by default) with the help of eae_encode_str().
*
* Regular expression is based on based on John Gruber's Markdown.
* http://daringfireball.net/projects/markdown/
*
* @param string $string Text with email addresses to encode
*
* @return string Given text with encoded email addresses
*/
function eae_encode_emails( $string ) {
// abort if `$string` isn't a string
if ( ! is_string( $string ) ) {
return $string;
}
// abort if `eae_at_sign_check` is true and `$string` doesn't contain a @-sign
if ( apply_filters( 'eae_at_sign_check', true ) && strpos( $string, '@' ) === false ) {
return $string;
}
// override encoding function with the 'eae_method' filter
$method = apply_filters( 'eae_method', 'eae_encode_str' );
// override regular expression with the 'eae_regexp' filter
$regexp = apply_filters( 'eae_regexp', EAE_REGEXP );
$callback = function ( $matches ) use ( $method ) {
return $method( $matches[ 0 ] );
};
// override callback method with the 'eae_email_callback' filter
if ( has_filter( 'eae_email_callback' ) ) {
$callback = apply_filters( 'eae_email_callback', $callback, $method );
return preg_replace_callback( $regexp, $callback, $string );
}
return preg_replace_callback( $regexp, $callback, $string );
}
/**
* Encodes each character of the given string as either a decimal
* or hexadecimal entity, in the hopes of foiling most email address
* harvesting bots.
*
* Based on Michel Fortin's PHP Markdown:
* http://michelf.com/projects/php-markdown/
* Which is based on John Gruber's original Markdown:
* http://daringfireball.net/projects/markdown/
* Whose code is based on a filter by Matthew Wickline, posted to
* the BBEdit-Talk with some optimizations by Milian Wolff.
*
* @param string $string Text to encode
* @param bool $hex Whether to use hex entities as well
*
* @return string Encoded given text
*/
function eae_encode_str( $string, $hex = false ) {
$chars = str_split( $string );
$seed = mt_rand( 0, (int) abs( crc32( $string ) / strlen( $string ) ) );
foreach ( $chars as $key => $char ) {
$ord = ord( $char );
if ( $ord < 128 ) { // ignore non-ascii chars
$r = ( $seed * ( 1 + $key ) ) % 100; // pseudo "random function"
if ( $r > 75 && $char !== '@' && $char !== '.' ); // plain character (not encoded), except @-signs and dots
else if ( $hex && $r < 25 ) $chars[ $key ] = '%' . bin2hex( $char ); // hex
else if ( $r < 45 ) $chars[ $key ] = '&#x' . dechex( $ord ) . ';'; // hexadecimal
else $chars[ $key ] = "&#{$ord};"; // decimal (ascii)
}
}
return implode( '', $chars );
}