class-email-encoder-bundle-ajax.php
6 years ago
class-email-encoder-bundle-helpers.php
6 years ago
class-email-encoder-bundle-run-admin.php
6 years ago
class-email-encoder-bundle-run.php
6 years ago
class-email-encoder-bundle-settings.php
6 years ago
class-email-encoder-bundle-validate.php
6 years ago
index.php
6 years ago
class-email-encoder-bundle-validate.php
926 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class Email_Encoder_Validate |
| 5 | * |
| 6 | * The main validation functionality for the plugin. |
| 7 | * Here is where the logic happens. |
| 8 | * |
| 9 | * @since 2.0.0 |
| 10 | * @package EEB |
| 11 | * @author Ironikus <info@ironikus.com> |
| 12 | */ |
| 13 | |
| 14 | class Email_Encoder_Validate{ |
| 15 | |
| 16 | /** |
| 17 | * The main page name for our admin page |
| 18 | * |
| 19 | * @var string |
| 20 | * @since 2.0.0 |
| 21 | */ |
| 22 | private $page_name; |
| 23 | |
| 24 | /** |
| 25 | * The main page title for our admin page |
| 26 | * |
| 27 | * @var string |
| 28 | * @since 2.0.0 |
| 29 | */ |
| 30 | private $page_title; |
| 31 | |
| 32 | /** |
| 33 | * Our Email_Encoder_Run constructor. |
| 34 | */ |
| 35 | function __construct(){ |
| 36 | $this->page_name = EEB()->settings->get_page_name(); |
| 37 | $this->page_title = EEB()->settings->get_page_title(); |
| 38 | $this->final_outout_buffer_hook = EEB()->settings->get_final_outout_buffer_hook(); |
| 39 | $this->at_identifier = EEB()->settings->get_at_identifier(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * ###################### |
| 44 | * ### |
| 45 | * #### FILTERS |
| 46 | * ### |
| 47 | * ###################### |
| 48 | */ |
| 49 | |
| 50 | /** |
| 51 | * The main page filter function |
| 52 | * |
| 53 | * @param string $content - the content that needs to be filtered |
| 54 | * @param bool $convertPlainEmails - wether plain emails should be preserved or not |
| 55 | * @return string - The filtered content |
| 56 | */ |
| 57 | public function filter_page( $content, $protect_using ){ |
| 58 | |
| 59 | //Added in 2.0.6 |
| 60 | $content = apply_filters( 'eeb/validate/filter_page_content', $content, $protect_using ); |
| 61 | |
| 62 | $content = $this->filter_soft_dom_attributes( $content, 'char_encode' ); |
| 63 | |
| 64 | $htmlSplit = preg_split( '/(<body(([^>]*)>))/is', $content, null, PREG_SPLIT_DELIM_CAPTURE ); |
| 65 | |
| 66 | if ( count( $htmlSplit ) < 4 ) { |
| 67 | return $content; |
| 68 | } |
| 69 | |
| 70 | switch( $protect_using ){ |
| 71 | case 'with_javascript': |
| 72 | case 'without_javascript': |
| 73 | case 'char_encode': |
| 74 | $head_encoding_method = 'char_encode'; |
| 75 | break; |
| 76 | default: |
| 77 | $head_encoding_method = 'default'; |
| 78 | break; |
| 79 | } |
| 80 | |
| 81 | //Filter head area |
| 82 | $filtered_head = $this->filter_plain_emails( $htmlSplit[0], null, $head_encoding_method ); |
| 83 | |
| 84 | //Filter body |
| 85 | //Soft attributes always need to be protected using only the char encode method since otherwise the logic breaks |
| 86 | $filtered_body = $this->filter_soft_attributes( $htmlSplit[4], 'char_encode' ); |
| 87 | $filtered_body = $this->filter_content( $filtered_body, $protect_using ); |
| 88 | |
| 89 | $filtered_content = $filtered_head . $htmlSplit[1] . $filtered_body; |
| 90 | |
| 91 | //Revalidate filtered emails that should not bbe encoded |
| 92 | $filtered_content = $this->temp_encode_at_symbol( $filtered_content, true ); |
| 93 | |
| 94 | return $filtered_content; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Filter content |
| 99 | * |
| 100 | * @param string $content |
| 101 | * @param integer $protect_using |
| 102 | * @return string |
| 103 | */ |
| 104 | public function filter_content( $content, $protect_using ){ |
| 105 | $filtered = $content; |
| 106 | $self = $this; |
| 107 | $encode_mailtos = (bool) EEB()->settings->get_setting( 'encode_mailtos', true, 'filter_body' ); |
| 108 | $convert_plain_to_image = (bool) EEB()->settings->get_setting( 'convert_plain_to_image', true, 'filter_body' ); |
| 109 | |
| 110 | //Added in 2.0.6 |
| 111 | $filtered = apply_filters( 'eeb/validate/filter_content_content', $filtered, $protect_using ); |
| 112 | |
| 113 | //Soft attributes always need to be protected using only the char encode method since otherwise the logic breaks |
| 114 | $filtered = $this->filter_soft_attributes( $filtered, 'char_encode' ); |
| 115 | |
| 116 | switch( $protect_using ){ |
| 117 | case 'char_encode': |
| 118 | $filtered = $this->filter_plain_emails( $filtered, null, 'char_encode' ); |
| 119 | break; |
| 120 | case 'strong_method': |
| 121 | $filtered = $this->filter_plain_emails( $filtered ); |
| 122 | break; |
| 123 | case 'without_javascript': |
| 124 | $filtered = $this->filter_input_fields( $filtered, $protect_using ); |
| 125 | $filtered = $this->filter_mailto_links( $filtered, 'without_javascript' ); |
| 126 | |
| 127 | if( $convert_plain_to_image ){ |
| 128 | $replace_by = 'convert_image'; |
| 129 | } else { |
| 130 | $replace_by = 'use_css'; |
| 131 | } |
| 132 | |
| 133 | if( $encode_mailtos ){ |
| 134 | if( ! ( function_exists( 'et_fb_enabled' ) && et_fb_enabled() ) ){ |
| 135 | $filtered = $this->filter_plain_emails( $filtered, function ( $match ) use ( $self ) { |
| 136 | return $self->create_protected_mailto( $match[0], array( 'href' => 'mailto:' . $match[0] ), 'without_javascript' ); |
| 137 | }, $replace_by); |
| 138 | } else { |
| 139 | $filtered = $this->filter_plain_emails( $filtered, null, $replace_by ); |
| 140 | } |
| 141 | } else { |
| 142 | $filtered = $this->filter_plain_emails( $filtered, null, $replace_by ); |
| 143 | } |
| 144 | |
| 145 | break; |
| 146 | case 'with_javascript': |
| 147 | $filtered = $this->filter_input_fields( $filtered, $protect_using ); |
| 148 | $filtered = $this->filter_mailto_links( $filtered ); |
| 149 | |
| 150 | if( $convert_plain_to_image ){ |
| 151 | $replace_by = 'convert_image'; |
| 152 | } else { |
| 153 | $replace_by = 'use_javascript'; |
| 154 | } |
| 155 | |
| 156 | if( $encode_mailtos ){ |
| 157 | if( ! ( function_exists( 'et_fb_enabled' ) && et_fb_enabled() ) ){ |
| 158 | $filtered = $this->filter_plain_emails( $filtered, function ( $match ) use ( $self ) { |
| 159 | return $self->create_protected_mailto( $match[0], array( 'href' => 'mailto:' . $match[0] ), 'with_javascript' ); |
| 160 | }, $replace_by); |
| 161 | } else { |
| 162 | $filtered = $this->filter_plain_emails( $filtered, null, $replace_by ); |
| 163 | } |
| 164 | } else { |
| 165 | $filtered = $this->filter_plain_emails( $filtered, null, $replace_by ); |
| 166 | } |
| 167 | |
| 168 | break; |
| 169 | } |
| 170 | |
| 171 | //Revalidate filtered emails that should not be encoded |
| 172 | $filtered = $this->temp_encode_at_symbol( $filtered, true ); |
| 173 | |
| 174 | return $filtered; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Emails will be replaced by '*protected email*' |
| 179 | * @param string $content |
| 180 | * @param string|callable $replace_by Optional |
| 181 | * @param string $protection_method Optional |
| 182 | * @param mixed $show_encoded_check Optional |
| 183 | * @return string |
| 184 | */ |
| 185 | public function filter_plain_emails($content, $replace_by = null, $protection_method = 'default', $show_encoded_check = 'default' ){ |
| 186 | |
| 187 | if( $show_encoded_check === 'default' ){ |
| 188 | $show_encoded_check = (bool) EEB()->settings->get_setting( 'show_encoded_check', true ); |
| 189 | } |
| 190 | |
| 191 | if ( $replace_by === null ) { |
| 192 | $replace_by = __( EEB()->settings->get_setting( 'protection_text', true ), 'email-encoder-bundle' ); |
| 193 | } |
| 194 | |
| 195 | $self = $this; |
| 196 | |
| 197 | return preg_replace_callback( EEB()->settings->get_email_regex(), function ( $matches ) use ( $replace_by, $protection_method, $show_encoded_check, $self ) { |
| 198 | // workaround to skip responsive image names containing @ |
| 199 | $extention = strtolower( $matches[4] ); |
| 200 | $excludedList = array('.jpg', '.jpeg', '.png', '.gif'); |
| 201 | |
| 202 | if ( in_array( $extention, $excludedList ) ) { |
| 203 | return $matches[0]; |
| 204 | } |
| 205 | |
| 206 | if ( is_callable( $replace_by ) ) { |
| 207 | return call_user_func( $replace_by, $matches, $protection_method ); |
| 208 | } |
| 209 | |
| 210 | if( $protection_method === 'char_encode' ){ |
| 211 | $protected_return = antispambot( $matches[0] ); |
| 212 | } elseif( $protection_method === 'convert_image' ){ |
| 213 | |
| 214 | $image_link = $self->generate_email_image_url( $matches[0] ); |
| 215 | if( ! empty( $image_link ) ){ |
| 216 | $protected_return = '<img src="' . $image_link . '" />'; |
| 217 | } else { |
| 218 | $protected_return = antispambot( $matches[0] ); |
| 219 | } |
| 220 | |
| 221 | } elseif( $protection_method === 'use_javascript' ){ |
| 222 | $protection_text = __( EEB()->settings->get_setting( 'protection_text', true ), 'email-encoder-bundle' ); |
| 223 | $protected_return = $this->dynamic_js_email_encoding( $matches[0], $protection_text ); |
| 224 | } elseif( $protection_method === 'use_css' ){ |
| 225 | $protection_text = __( EEB()->settings->get_setting( 'protection_text', true ), 'email-encoder-bundle' ); |
| 226 | $protected_return = $this->encode_email_css( $matches[0], $protection_text ); |
| 227 | } elseif( $protection_method === 'no_encoding' ){ |
| 228 | $protected_return = $matches[0]; |
| 229 | } else { |
| 230 | $protected_return = $replace_by; |
| 231 | } |
| 232 | |
| 233 | // mark link as successfully encoded (for admin users) |
| 234 | if ( current_user_can( EEB()->settings->get_admin_cap( 'frontend-display-security-check' ) ) && $show_encoded_check ) { |
| 235 | $protected_return .= '<i class="eeb-encoded dashicons-before dashicons-lock" title="' . __( 'Email encoded successfully!', 'email-encoder-bundle' ) . '"></i>'; |
| 236 | } |
| 237 | |
| 238 | return $protected_return; |
| 239 | |
| 240 | }, $content ); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Filter passed input fields |
| 245 | * |
| 246 | * @param string $content |
| 247 | * @return string |
| 248 | */ |
| 249 | public function filter_input_fields( $content, $encoding_method = 'default' ){ |
| 250 | $self = $this; |
| 251 | $strong_encoding = (bool) EEB()->settings->get_setting( 'input_strong_protection', true, 'filter_body' ); |
| 252 | |
| 253 | $callback_encode_input_fields = function ( $match ) use ( $self, $encoding_method, $strong_encoding ) { |
| 254 | $input = $match[0]; |
| 255 | $email = $match[2]; |
| 256 | |
| 257 | //Only allow strong encoding if javascript is supported |
| 258 | if( $encoding_method === 'without_javascript' ){ |
| 259 | $strong_encoding = false; |
| 260 | } |
| 261 | |
| 262 | return $self->encode_input_field( $input, $email, $strong_encoding ); |
| 263 | }; |
| 264 | |
| 265 | $regexpInputField = '/<input([^>]*)value=["\'][\s+]*' . EEB()->settings->get_email_regex( true ) . '[\s+]*["\']([^>]*)>/is'; |
| 266 | |
| 267 | return preg_replace_callback( $regexpInputField, $callback_encode_input_fields, $content ); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * @param string $content |
| 272 | * @return string |
| 273 | */ |
| 274 | public function filter_mailto_links( $content, $protection_method = null ){ |
| 275 | $self = $this; |
| 276 | |
| 277 | $callbackEncodeMailtoLinks = function ( $match ) use ( $self, $protection_method ) { |
| 278 | $attrs = shortcode_parse_atts( $match[1] ); |
| 279 | return $self->create_protected_mailto( $match[4], $attrs, $protection_method ); |
| 280 | }; |
| 281 | |
| 282 | $regexpMailtoLink = '/<a[\s+]*(([^>]*)href=["\']mailto\:([^>]*)["\'])>(.*?)<\/a[\s+]*>/is'; |
| 283 | |
| 284 | return preg_replace_callback( $regexpMailtoLink, $callbackEncodeMailtoLinks, $content ); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Emails will be replaced by '*protected email*' |
| 289 | * |
| 290 | * @param string $content |
| 291 | * @return string |
| 292 | */ |
| 293 | public function filter_rss( $content, $protection_type ){ |
| 294 | |
| 295 | if( $protection_type === 'strong_method' ) { |
| 296 | $filtered = $this->filter_plain_emails( $content ); |
| 297 | } else { |
| 298 | $filtered = $this->filter_plain_emails( $content, null, 'char_encode' ); |
| 299 | } |
| 300 | |
| 301 | return $filtered; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Filter plain emails using soft attributes |
| 306 | * |
| 307 | * @param string $content - the content that should be soft filtered |
| 308 | * @param string $protection_method - The method (E.g. char_encode) |
| 309 | * @return string |
| 310 | */ |
| 311 | public function filter_soft_attributes( $content, $protection_method ){ |
| 312 | $soft_attributes = EEB()->settings->get_soft_attribute_regex(); |
| 313 | |
| 314 | foreach( $soft_attributes as $ident => $regex ){ |
| 315 | |
| 316 | $array = array(); |
| 317 | preg_match_all( $regex, $content, $array ) ; |
| 318 | |
| 319 | foreach( $array as $single ){ |
| 320 | $content = str_replace( $single, $this->filter_plain_emails( $single, null, $protection_method, false ), $content ); |
| 321 | } |
| 322 | |
| 323 | } |
| 324 | |
| 325 | return $content; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Filter plain emails using soft dom attributes |
| 330 | * |
| 331 | * @param string $content - the content that should be soft filtered |
| 332 | * @param string $protection_method - The method (E.g. char_encode) |
| 333 | * @return string |
| 334 | */ |
| 335 | public function filter_soft_dom_attributes( $content, $protection_method ){ |
| 336 | |
| 337 | $no_script_tags = (bool) EEB()->settings->get_setting( 'no_script_tags', true, 'filter_body' ); |
| 338 | $no_attribute_validation = (bool) EEB()->settings->get_setting( 'no_attribute_validation', true, 'filter_body' ); |
| 339 | |
| 340 | if( class_exists( 'DOMDocument' ) ){ |
| 341 | $dom = new DOMDocument(); |
| 342 | @$dom->loadHTML($content); |
| 343 | |
| 344 | //Filter html attributes |
| 345 | if( ! $no_attribute_validation ){ |
| 346 | $allNodes = $dom->getElementsByTagName('*'); |
| 347 | foreach( $allNodes as $snote ){ |
| 348 | if( $snote->hasAttributes() ) { |
| 349 | foreach( $snote->attributes as $attr ) { |
| 350 | if( $attr->nodeName == 'href' || $attr->nodeName == 'src' ){ |
| 351 | continue; |
| 352 | } |
| 353 | |
| 354 | if( strpos( $attr->nodeValue, '@' ) !== FALSE ){ |
| 355 | $single_tags = array(); |
| 356 | preg_match_all( '/' . $attr->nodeName . '="([^"]*)"/i', $content, $single_tags ); |
| 357 | |
| 358 | foreach( $single_tags as $single ){ |
| 359 | $content = str_replace( $single, $this->filter_plain_emails( $single, null, $protection_method, false ), $content ); |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | //Soft-encode scripts |
| 368 | $script = $dom->getElementsByTagName('script'); |
| 369 | if( ! $no_script_tags ){ |
| 370 | foreach($script as $item){ |
| 371 | $content = str_replace( $item->nodeValue, $this->filter_plain_emails( $item->nodeValue, null, $protection_method, false ), $content ); |
| 372 | } |
| 373 | } else { |
| 374 | foreach($script as $item){ |
| 375 | $content = str_replace( $item->nodeValue, $this->temp_encode_at_symbol( $item->nodeValue ), $content ); |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | return $content; |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * ###################### |
| 385 | * ### |
| 386 | * #### ENCODINGS |
| 387 | * ### |
| 388 | * ###################### |
| 389 | */ |
| 390 | |
| 391 | public function temp_encode_at_symbol( $content, $decode = false ){ |
| 392 | if( $decode ){ |
| 393 | return str_replace( $this->at_identifier, '@', $content ); |
| 394 | } |
| 395 | |
| 396 | return str_replace( '@', $this->at_identifier, $content ); |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * ASCII method |
| 401 | * |
| 402 | * @param string $value |
| 403 | * @param string $protection_text |
| 404 | * @return string |
| 405 | */ |
| 406 | public function encode_ascii($value, $protection_text) { |
| 407 | $mail_link = $value; |
| 408 | |
| 409 | // first encode, so special chars can be supported |
| 410 | $mail_link = EEB()->helpers->encode_uri_components( $mail_link ); |
| 411 | |
| 412 | $mail_letters = ''; |
| 413 | |
| 414 | for ($i = 0; $i < strlen($mail_link); $i ++) { |
| 415 | $l = substr($mail_link, $i, 1); |
| 416 | |
| 417 | if (strpos($mail_letters, $l) === false) { |
| 418 | $p = rand(0, strlen($mail_letters)); |
| 419 | $mail_letters = substr($mail_letters, 0, $p) . |
| 420 | $l . substr($mail_letters, $p, strlen($mail_letters)); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | $mail_letters_enc = str_replace("\\", "\\\\", $mail_letters); |
| 425 | $mail_letters_enc = str_replace("\"", "\\\"", $mail_letters_enc); |
| 426 | |
| 427 | $mail_indices = ''; |
| 428 | for ($i = 0; $i < strlen($mail_link); $i ++) { |
| 429 | $index = strpos($mail_letters, substr($mail_link, $i, 1)); |
| 430 | $index += 48; |
| 431 | $mail_indices .= chr($index); |
| 432 | } |
| 433 | |
| 434 | $mail_indices = str_replace("\\", "\\\\", $mail_indices); |
| 435 | $mail_indices = str_replace("\"", "\\\"", $mail_indices); |
| 436 | |
| 437 | $element_id = 'eeb-' . mt_rand( 0, 1000000 ) . '-' . mt_rand(0, 1000000); |
| 438 | |
| 439 | return '<span id="'. $element_id . '"></span>' |
| 440 | . '<script type="text/javascript">' |
| 441 | . '(function(){' |
| 442 | . 'var ml="'. $mail_letters_enc .'",mi="'. $mail_indices .'",o="";' |
| 443 | . 'for(var j=0,l=mi.length;j<l;j++){' |
| 444 | . 'o+=ml.charAt(mi.charCodeAt(j)-48);' |
| 445 | . '}document.getElementById("' . $element_id . '").innerHTML = decodeURIComponent(o);' // decode at the end, this way special chars can be supported |
| 446 | . '}());' |
| 447 | . '</script><noscript>' |
| 448 | . $protection_text |
| 449 | . '</noscript>'; |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * Escape encoding method |
| 454 | * |
| 455 | * @param string $value |
| 456 | * @param string $protection_text |
| 457 | * @return string |
| 458 | */ |
| 459 | public function encode_escape( $value, $protection_text ) { |
| 460 | $element_id = 'eeb-' . mt_rand( 0, 1000000 ) . '-' . mt_rand( 0, 1000000 ); |
| 461 | $string = '\'' . $value . '\''; |
| 462 | |
| 463 | //Validate escape sequences |
| 464 | $string = preg_replace('/\s+/S', " ", $string); |
| 465 | |
| 466 | // break string into array of characters, we can't use string_split because its php5 only |
| 467 | $split = preg_split( '||', $string ); |
| 468 | $out = '<span id="'. $element_id . '"></span>' |
| 469 | . '<script type="text/javascript">' . 'document.getElementById("' . $element_id . '").innerHTML = ev' . 'al(decodeURIComponent("'; |
| 470 | |
| 471 | foreach( $split as $c ) { |
| 472 | // preg split will return empty first and last characters, check for them and ignore |
| 473 | if( ! empty( $c ) || $c === '0' ) { |
| 474 | $out .= '%' . dechex( ord( $c ) ); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | $out .= '"))' . '</script><noscript>' |
| 479 | . $protection_text |
| 480 | . '</noscript>'; |
| 481 | |
| 482 | return $out; |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * Encode email in input field |
| 487 | * @param string $input |
| 488 | * @param string $email |
| 489 | * @return string |
| 490 | */ |
| 491 | public function encode_input_field( $input, $email, $strongEncoding = false ){ |
| 492 | |
| 493 | $show_encoded_check = (bool) EEB()->settings->get_setting( 'show_encoded_check', true ); |
| 494 | |
| 495 | if ( $strongEncoding === false ) { |
| 496 | // encode email with entities (default wp method) |
| 497 | $sub_return = str_replace( $email, antispambot( $email ), $input ); |
| 498 | |
| 499 | if ( current_user_can( EEB()->settings->get_admin_cap( 'frontend-display-security-check' ) ) && $show_encoded_check ) { |
| 500 | $sub_return .= '<i class="eeb-encoded dashicons-before dashicons-lock" title="' . __( 'Email encoded successfully!', 'email-encoder-bundle' ) . '"></i>'; |
| 501 | } |
| 502 | |
| 503 | return $sub_return; |
| 504 | } |
| 505 | |
| 506 | // add data-enc-email after "<input" |
| 507 | $inputWithDataAttr = substr( $input, 0, 6 ); |
| 508 | $inputWithDataAttr .= ' data-enc-email="' . $this->get_encoded_email( $email ) . '"'; |
| 509 | $inputWithDataAttr .= substr( $input, 6 ); |
| 510 | |
| 511 | // mark link as successfullly encoded (for admin users) |
| 512 | if ( current_user_can( EEB()->settings->get_admin_cap( 'frontend-display-security-check' ) ) && $show_encoded_check ) { |
| 513 | $inputWithDataAttr .= '<i class="eeb-encoded dashicons-before dashicons-lock" title="' . __( 'Email encoded successfully!', 'email-encoder-bundle' ) . '"></i>'; |
| 514 | } |
| 515 | |
| 516 | // remove email from value attribute |
| 517 | $encInput = str_replace( $email, '', $inputWithDataAttr ); |
| 518 | |
| 519 | return $encInput; |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * Get encoded email, used for data-attribute (translate by javascript) |
| 524 | * |
| 525 | * @param string $email |
| 526 | * @return string |
| 527 | */ |
| 528 | public function get_encoded_email( $email ){ |
| 529 | $encEmail = $email; |
| 530 | |
| 531 | // decode entities |
| 532 | $encEmail = html_entity_decode( $encEmail ); |
| 533 | |
| 534 | // rot13 encoding |
| 535 | $encEmail = str_rot13( $encEmail ); |
| 536 | |
| 537 | // replace @ |
| 538 | $encEmail = str_replace( '@', '[at]', $encEmail ); |
| 539 | |
| 540 | return $encEmail; |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * Create a protected email |
| 545 | * |
| 546 | * @param string $display |
| 547 | * @param array $attrs Optional |
| 548 | * @return string |
| 549 | */ |
| 550 | public function create_protected_mailto( $display, $attrs = array(), $protection_method = null ){ |
| 551 | $email = ''; |
| 552 | $class_ori = ( empty( $attrs['class'] ) ) ? '' : $attrs['class']; |
| 553 | $custom_class = (string) EEB()->settings->get_setting( 'class_name', true ); |
| 554 | $show_encoded_check = (string) EEB()->settings->get_setting( 'show_encoded_check', true ); |
| 555 | |
| 556 | // set user-defined class |
| 557 | if ( $custom_class && strpos( $class_ori, $custom_class ) === FALSE ) { |
| 558 | $attrs['class'] = ( empty( $attrs['class'] ) ) ? $custom_class : $attrs['class'] . ' ' . $custom_class; |
| 559 | } |
| 560 | |
| 561 | // check title for email address |
| 562 | if ( ! empty( $attrs['title'] ) ) { |
| 563 | $attrs['title'] = $this->filter_plain_emails( $attrs['title'], '{{email}}' ); // {{email}} will be replaced in javascript |
| 564 | } |
| 565 | |
| 566 | // set ignore to data-attribute to prevent being processed by WPEL plugin |
| 567 | $attrs['data-wpel-link'] = 'ignore'; |
| 568 | |
| 569 | // create element code |
| 570 | $link = '<a '; |
| 571 | |
| 572 | foreach ( $attrs AS $key => $value ) { |
| 573 | if ( strtolower( $key ) == 'href' ) { |
| 574 | if( $protection_method === 'without_javascript' ){ |
| 575 | $link .= $key . '="' . antispambot( $value ) . '" '; |
| 576 | } else { |
| 577 | // get email from href |
| 578 | $email = substr($value, 7); |
| 579 | |
| 580 | $encoded_email = $this->get_encoded_email( $email ); |
| 581 | |
| 582 | // set attrs |
| 583 | $link .= 'href="javascript:;" '; |
| 584 | $link .= 'data-enc-email="' . $encoded_email . '" '; |
| 585 | } |
| 586 | |
| 587 | } else { |
| 588 | $link .= $key . '="' . $value . '" '; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | // remove last space |
| 593 | $link = substr( $link, 0, -1 ); |
| 594 | |
| 595 | $link .= '>'; |
| 596 | |
| 597 | $link .= ( preg_match( EEB()->settings->get_email_regex(), $display) > 0 ) ? $this->get_protected_display( $display, $protection_method ) : $display; |
| 598 | |
| 599 | $link .= '</a>'; |
| 600 | |
| 601 | // filter |
| 602 | $link = apply_filters( 'eeb_mailto', $link, $display, $email, $attrs ); |
| 603 | |
| 604 | // just in case there are still email addresses f.e. within title-tag |
| 605 | $link = $this->filter_plain_emails( $link, null, 'char_encode' ); |
| 606 | |
| 607 | // mark link as successfullly encoded (for admin users) |
| 608 | if ( current_user_can( EEB()->settings->get_admin_cap( 'frontend-display-security-check' ) ) && $show_encoded_check ) { |
| 609 | $link .= '<i class="eeb-encoded dashicons-before dashicons-lock" title="' . __( 'Email encoded successfully!', 'email-encoder-bundle' ) . '"></i>'; |
| 610 | } |
| 611 | |
| 612 | |
| 613 | return $link; |
| 614 | } |
| 615 | |
| 616 | /** |
| 617 | * Create protected display combining these 3 methods: |
| 618 | * - reversing string |
| 619 | * - adding no-display spans with dummy values |
| 620 | * - using the wp antispambot function |
| 621 | * |
| 622 | * @param string|array $display |
| 623 | * @return string Protected display |
| 624 | */ |
| 625 | public function get_protected_display( $display, $protection_method = null ){ |
| 626 | |
| 627 | $convert_plain_to_image = (bool) EEB()->settings->get_setting( 'convert_plain_to_image', true, 'filter_body' ); |
| 628 | $protection_text = __( EEB()->settings->get_setting( 'protection_text', true ), 'email-encoder-bundle' ); |
| 629 | |
| 630 | // get display out of array (result of preg callback) |
| 631 | if ( is_array( $display ) ) { |
| 632 | $display = $display[0]; |
| 633 | } |
| 634 | |
| 635 | if( $convert_plain_to_image ){ |
| 636 | return '<img src="' . $this->generate_email_image_url( $display ) . '" />'; |
| 637 | } |
| 638 | |
| 639 | if( $protection_method !== 'without_javascript' ){ |
| 640 | return $this->dynamic_js_email_encoding( $display, $protection_text ); |
| 641 | } |
| 642 | |
| 643 | return $this->encode_email_css( $display ); |
| 644 | |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * Dynamic email encoding with certain javascript methods |
| 649 | * |
| 650 | * @param string $email |
| 651 | * @param string $protection_text |
| 652 | * @return the encoded email |
| 653 | */ |
| 654 | public function dynamic_js_email_encoding( $email, $protection_text = null ){ |
| 655 | $return = $email; |
| 656 | $rand = apply_filters( 'eeb/validate/random_encoding', rand(0,2), $email, $protection_text ); |
| 657 | |
| 658 | switch( $rand ){ |
| 659 | case 2: |
| 660 | $return = $this->encode_escape( $return, $protection_text ); |
| 661 | break; |
| 662 | case 1: |
| 663 | $return = $this->encode_ascii( $return, $protection_text ); |
| 664 | break; |
| 665 | default: |
| 666 | $return = $this->encode_ascii( $return, $protection_text ); |
| 667 | break; |
| 668 | } |
| 669 | |
| 670 | return $return; |
| 671 | } |
| 672 | |
| 673 | public function encode_email_css( $display ){ |
| 674 | $deactivate_rtl = (bool) EEB()->settings->get_setting( 'deactivate_rtl', true, 'filter_body' ); |
| 675 | |
| 676 | $stripped_display = strip_tags( $display ); |
| 677 | $stripped_display = html_entity_decode( $stripped_display ); |
| 678 | |
| 679 | $length = strlen( $stripped_display ); |
| 680 | $interval = ceil( min( 5, $length / 2 ) ); |
| 681 | $offset = 0; |
| 682 | $dummy_data = time(); |
| 683 | $protected = ''; |
| 684 | $protection_classes = 'eeb'; |
| 685 | |
| 686 | if( $deactivate_rtl ){ |
| 687 | $rev = $stripped_display; |
| 688 | $protection_classes .= ' eeb-nrtl'; |
| 689 | } else { |
| 690 | // reverse string ( will be corrected with CSS ) |
| 691 | $rev = strrev( $stripped_display ); |
| 692 | $protection_classes .= ' eeb-rtl'; |
| 693 | } |
| 694 | |
| 695 | |
| 696 | while ( $offset < $length ) { |
| 697 | $protected .= '<span class="eeb-sd">' . antispambot( substr( $rev, $offset, $interval ) ) . '</span>'; |
| 698 | |
| 699 | // setup dummy content |
| 700 | $protected .= '<span class="eeb-nodis">' . $dummy_data . '</span>'; |
| 701 | $offset += $interval; |
| 702 | } |
| 703 | |
| 704 | $protected = '<span class="' . $protection_classes . '">' . $protected . '</span>'; |
| 705 | |
| 706 | return $protected; |
| 707 | } |
| 708 | |
| 709 | public function email_to_image( $email, $image_string_color = 'default', $image_background_color = 'default', $alpha_string = 0, $alpha_fill = 127, $font_size = 4 ){ |
| 710 | |
| 711 | $setting_image_string_color = (string) EEB()->settings->get_setting( 'image_color', true, 'image_settings' ); |
| 712 | $setting_image_background_color = (string) EEB()->settings->get_setting( 'image_background_color', true, 'image_settings' ); |
| 713 | $image_text_opacity = (int) EEB()->settings->get_setting( 'image_text_opacity', true, 'image_settings' ); |
| 714 | $image_background_opacity = (int) EEB()->settings->get_setting( 'image_background_opacity', true, 'image_settings' ); |
| 715 | $image_font_size = (int) EEB()->settings->get_setting( 'image_font_size', true, 'image_settings' ); |
| 716 | |
| 717 | if( $image_background_color === 'default' ){ |
| 718 | $image_background_color = $setting_image_background_color; |
| 719 | } else { |
| 720 | $image_background_color = '0,0,0'; |
| 721 | } |
| 722 | |
| 723 | $colors = explode( ',', $image_background_color ); |
| 724 | $bg_red = $colors[0]; |
| 725 | $bg_green = $colors[1]; |
| 726 | $bg_blue = $colors[2]; |
| 727 | |
| 728 | if( $image_string_color === 'default' ){ |
| 729 | $image_string_color = $setting_image_string_color; |
| 730 | } else { |
| 731 | $image_string_color = '0,0,0'; |
| 732 | } |
| 733 | |
| 734 | $colors = explode( ',', $image_string_color ); |
| 735 | $string_red = $colors[0]; |
| 736 | $string_green = $colors[1]; |
| 737 | $string_blue = $colors[2]; |
| 738 | |
| 739 | if( ! empty( $image_text_opacity ) && $image_text_opacity >= 0 && $image_text_opacity <= 127 ){ |
| 740 | $alpha_string = intval( $image_text_opacity ); |
| 741 | } |
| 742 | |
| 743 | if( ! empty( $image_background_opacity ) && $image_background_opacity >= 0 && $image_background_opacity <= 127 ){ |
| 744 | $alpha_fill = intval( $image_background_opacity ); |
| 745 | } |
| 746 | |
| 747 | if( ! empty( $image_font_size ) && $image_font_size >= 1 && $image_font_size <= 5 ){ |
| 748 | $font_size = intval( $image_font_size ); |
| 749 | } |
| 750 | |
| 751 | $img = imagecreatetruecolor( imagefontwidth( $font_size ) * strlen( $email ), imagefontheight( $font_size ) ); |
| 752 | imagesavealpha( $img, true ); |
| 753 | imagefill( $img, 0, 0, imagecolorallocatealpha ($img, $bg_red, $bg_green, $bg_blue, $alpha_fill ) ); |
| 754 | imagestring( $img, $font_size, 0, 0, $email, imagecolorallocatealpha( $img, $string_red, $string_green, $string_blue, $alpha_string ) ); |
| 755 | ob_start(); |
| 756 | imagepng( $img ); |
| 757 | imagedestroy( $img ); |
| 758 | |
| 759 | return ob_get_clean (); |
| 760 | } |
| 761 | |
| 762 | public function generate_email_signature( $email, $secret ) { |
| 763 | |
| 764 | if( ! $secret ){ |
| 765 | return false; |
| 766 | } |
| 767 | |
| 768 | $hash_signature = apply_filters( 'eeb/validate/email_signature', 'sha256', $email ); |
| 769 | |
| 770 | return base64_encode( hash_hmac( $hash_signature, $email, $secret, true ) ); |
| 771 | } |
| 772 | |
| 773 | public function generate_email_image_url( $email ) { |
| 774 | |
| 775 | if( empty( $email ) || ! is_email( $email ) ){ |
| 776 | return false; |
| 777 | } |
| 778 | |
| 779 | $secret = EEB()->settings->get_email_image_secret(); |
| 780 | $signature = $this->generate_email_signature( $email, $secret ); |
| 781 | $url = home_url() . '?eeb_mail=' . urlencode( base64_encode( $email ) ) . '&eeb_hash=' . urlencode( $signature ); |
| 782 | |
| 783 | $url = apply_filters( 'eeb/validate/generate_email_image_url', $url, $email ); |
| 784 | |
| 785 | return $url; |
| 786 | } |
| 787 | |
| 788 | /** |
| 789 | * ###################### |
| 790 | * ### |
| 791 | * #### ENCODER FORM |
| 792 | * ### |
| 793 | * ###################### |
| 794 | */ |
| 795 | |
| 796 | /** |
| 797 | * Get the encoder form (to use as a demo, like on the options page) |
| 798 | * @return string |
| 799 | */ |
| 800 | public function get_encoder_form() { |
| 801 | $powered_by_setting = (bool) EEB()->settings->get_setting( 'powered_by', true, 'encoder_form' ); |
| 802 | $display_encoder_form = (bool) EEB()->settings->get_setting( 'display_encoder_form', true, 'encoder_form' ); |
| 803 | |
| 804 | //shorten circle |
| 805 | if( ! $display_encoder_form ){ |
| 806 | return apply_filters('eeb_form_content_inactive', '' ); |
| 807 | } |
| 808 | |
| 809 | $powered_by = ''; |
| 810 | if ($powered_by_setting) { |
| 811 | $powered_by .= '<p class="powered-by">' . __('Powered by free', 'email-encoder-bundle') . ' <a rel="external" href="https://wordpress.org/plugins/email-encoder-bundle/">Email Encoder</a></p>'; |
| 812 | } |
| 813 | |
| 814 | $smethods = array( |
| 815 | 'rot13' => __( 'Rot13 (Javascript)', 'email-encoder-bundle' ), |
| 816 | 'escape' => __( 'Escape (Javascript)', 'email-encoder-bundle' ), |
| 817 | 'encode' => __( 'Encode (HTML)', 'email-encoder-bundle' ), |
| 818 | ); |
| 819 | $method_options = ''; |
| 820 | $selected = false; |
| 821 | foreach( $smethods as $method_name => $name ) { |
| 822 | $method_options .= '<option value="' . $method_name . '"' . ( ($selected === false ) ? ' selected="selected"' : '') . '>' . $name . '</option>'; |
| 823 | $selected = true; |
| 824 | } |
| 825 | |
| 826 | $labels = array( |
| 827 | 'email' => __( 'Email Address:', 'email-encoder-bundle' ), |
| 828 | 'display' => __( 'Display Text:', 'email-encoder-bundle' ), |
| 829 | 'mailto' => __( 'Mailto Link:', 'email-encoder-bundle' ), |
| 830 | 'method' => __( 'Encoding Method:', 'email-encoder-bundle' ), |
| 831 | 'create_link' => __( 'Create Protected Mail Link >>', 'email-encoder-bundle' ), |
| 832 | 'output' => __( 'Protected Mail Link (code):', 'email-encoder-bundle' ), |
| 833 | 'powered_by' => $powered_by, |
| 834 | ); |
| 835 | |
| 836 | extract($labels); |
| 837 | |
| 838 | $form = <<<FORM |
| 839 | <div class="eeb-form"> |
| 840 | <form> |
| 841 | <fieldset> |
| 842 | <div class="input"> |
| 843 | <table> |
| 844 | <tbody> |
| 845 | <tr> |
| 846 | <th><label for="eeb-email">{$email}</label></th> |
| 847 | <td><input type="text" class="regular-text" id="eeb-email" name="eeb-email" /></td> |
| 848 | </tr> |
| 849 | <tr> |
| 850 | <th><label for="eeb-display">{$display}</label></th> |
| 851 | <td><input type="text" class="regular-text" id="eeb-display" name="eeb-display" /></td> |
| 852 | </tr> |
| 853 | <tr> |
| 854 | <th>{$mailto}</th> |
| 855 | <td><span class="eeb-example"></span></td> |
| 856 | </tr> |
| 857 | <tr> |
| 858 | <th><label for="eeb-encode-method">{$method}</label></th> |
| 859 | <td><select id="eeb-encode-method" name="eeb-encode-method" class="postform"> |
| 860 | {$method_options} |
| 861 | </select> |
| 862 | <input type="button" id="eeb-ajax-encode" name="eeb-ajax-encode" value="{$create_link}" /> |
| 863 | </td> |
| 864 | </tr> |
| 865 | </tbody> |
| 866 | </table> |
| 867 | </div> |
| 868 | <div class="eeb-output"> |
| 869 | <table> |
| 870 | <tbody> |
| 871 | <tr> |
| 872 | <th><label for="eeb-encoded-output">{$output}</label></th> |
| 873 | <td><textarea class="large-text node" id="eeb-encoded-output" name="eeb-encoded-output" cols="50" rows="4"></textarea></td> |
| 874 | </tr> |
| 875 | </tbody> |
| 876 | </table> |
| 877 | </div> |
| 878 | {$powered_by} |
| 879 | </fieldset> |
| 880 | </form> |
| 881 | </div> |
| 882 | FORM; |
| 883 | |
| 884 | // apply filters |
| 885 | $form = apply_filters('eeb_form_content', $form, $labels, $powered_by_setting ); |
| 886 | |
| 887 | return $form; |
| 888 | } |
| 889 | |
| 890 | |
| 891 | public function is_post_excluded( $post_id = null ){ |
| 892 | |
| 893 | $return = false; |
| 894 | $skip_posts = (string) EEB()->settings->get_setting( 'skip_posts', true ); |
| 895 | if( ! empty( $skip_posts ) ){ |
| 896 | |
| 897 | if( empty( $post_id ) ){ |
| 898 | global $post; |
| 899 | $post_id = $post->ID; |
| 900 | } else { |
| 901 | $post_id = intval( $post_id ); |
| 902 | } |
| 903 | |
| 904 | $exclude_pages = explode( ',', $skip_posts ); |
| 905 | |
| 906 | if( is_array( $exclude_pages ) ){ |
| 907 | $exclude_pages_validated = array(); |
| 908 | |
| 909 | foreach( $exclude_pages as $spost_id ){ |
| 910 | $spost_id = trim($spost_id); |
| 911 | if( is_numeric( $spost_id ) ){ |
| 912 | $exclude_pages_validated[] = intval( $spost_id ); |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | if ( in_array( $post_id, $exclude_pages_validated ) ) { |
| 917 | $return = true; |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | } |
| 922 | |
| 923 | return apply_filters( 'eeb/validate/is_post_excluded', $return, $post_id, $skip_posts ); |
| 924 | } |
| 925 | } |
| 926 |