PluginProbe ʕ •ᴥ•ʔ
Email Encoder – Protect Email Addresses and Phone Numbers / 2.2.0
Email Encoder – Protect Email Addresses and Phone Numbers v2.2.0
2.5.0 2.4.8 trunk 0.10 0.11 0.12 0.20 0.21 0.22 0.30 0.31 0.32 0.40 0.41 0.42 0.50 0.60 0.70 0.71 0.80 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.5 1.5.2 1.51 1.53 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.10 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7
email-encoder-bundle / core / includes / classes / class-email-encoder-bundle-run.php
email-encoder-bundle / core / includes / classes Last commit date
class-email-encoder-bundle-ajax.php 2 years ago class-email-encoder-bundle-helpers.php 2 years ago class-email-encoder-bundle-run-admin.php 2 years ago class-email-encoder-bundle-run.php 2 years ago class-email-encoder-bundle-settings.php 2 years ago class-email-encoder-bundle-validate.php 2 years ago index.php 2 years ago
class-email-encoder-bundle-run.php
700 lines
1 <?php
2
3 /**
4 * Class Email_Encoder_Run
5 *
6 * Thats where we bring the plugin to life
7 *
8 * @since 2.0.0
9 * @package EEB
10 * @author Ironikus <info@ironikus.com>
11 */
12
13 class Email_Encoder_Run{
14
15 /**
16 * The main page name for our admin page
17 *
18 * @var string
19 * @since 2.0.0
20 */
21 private $page_name;
22
23 /**
24 * The main page title for our admin page
25 *
26 * @var string
27 * @since 2.0.0
28 */
29 private $page_title;
30
31 /**
32 * The hook used for the final output buffer
33 *
34 * @var string
35 * @since 2.0.0
36 */
37 private $final_outout_buffer_hook;
38
39 /**
40 * The hook used for the widget callbacks
41 *
42 * @var string
43 * @since 2.0.0
44 */
45 private $widget_callback_hook;
46
47 /**
48 * Our Email_Encoder_Run constructor.
49 */
50 function __construct(){
51 $this->page_name = EEB()->settings->get_page_name();
52 $this->page_title = EEB()->settings->get_page_title();
53 $this->final_outout_buffer_hook = EEB()->settings->get_final_outout_buffer_hook();
54 $this->widget_callback_hook = EEB()->settings->get_widget_callback_hook();
55 $this->add_hooks();
56 }
57
58 /**
59 * Define all of our necessary hooks
60 */
61 private function add_hooks(){
62 $filter_hook = (bool) EEB()->settings->get_setting( 'filter_hook', true, 'filter_body' );
63 if( $filter_hook ){
64 $hook_name = 'init';
65 } else {
66 $hook_name = 'wp';
67 }
68
69 add_action( 'wp', array( $this, 'display_email_image' ), EEB()->settings->get_hook_priorities( 'display_email_image' ) );
70 add_action( 'init', array( $this, 'buffer_final_output' ), EEB()->settings->get_hook_priorities( 'buffer_final_output' ) );
71 add_action( 'init', array( $this, 'add_custom_template_tags' ), EEB()->settings->get_hook_priorities( 'add_custom_template_tags' ) );
72 add_action( $hook_name, array( $this, 'setup_single_filter_hooks' ), EEB()->settings->get_hook_priorities( 'setup_single_filter_hooks' ) );
73 add_action( 'wp_enqueue_scripts', array( $this, 'load_frontend_header_styling' ), EEB()->settings->get_hook_priorities( 'load_frontend_header_styling' ) );
74
75 //Add shortcodes
76 add_shortcode( 'eeb_protect_emails', array( $this, 'protect_content_shortcode' ) );
77 add_shortcode( 'eeb_protect_content', array( $this, 'shortcode_eeb_content' ) );
78 add_shortcode( 'eeb_mailto', array( $this, 'shortcode_eeb_email' ) );
79 add_shortcode( 'eeb_form', array( $this, 'shortcode_email_encoder_form' ) );
80
81 //BAckwards compatibility
82 add_shortcode( 'eeb_content', array( $this, 'shortcode_eeb_content' ) );
83 add_shortcode( 'eeb_email', array( $this, 'shortcode_eeb_email' ) );
84
85 do_action('eeb_ready', array($this, 'eeb_ready_callback_filter'), $this);
86
87 add_action( 'init', array( $this, 'reload_settings_for_integrations' ), 5 );
88 }
89
90 /**
91 * ######################
92 * ###
93 * #### CALLBACK FILTERS
94 * ###
95 * ######################
96 */
97
98 /**
99 * WP filter callback
100 * @param string $content
101 * @return string
102 */
103 public function eeb_ready_callback_filter( $content ) {
104
105 $apply_protection = true;
106
107 if( EEB()->validate->is_query_parameter_excluded() ){
108 $apply_protection = false;
109 }
110
111 if( EEB()->validate->is_post_excluded() ){
112 $apply_protection = false;
113 }
114
115 $apply_protection = apply_filters( 'eeb/frontend/apply_protection', $apply_protection );
116
117 if( ! $apply_protection ){
118 return $content;
119 }
120
121 $protect_using = (string) EEB()->settings->get_setting( 'protect_using', true );
122
123 return EEB()->validate->filter_content( $content, $protect_using );
124 }
125
126 /**
127 * Reload the settings to reflect
128 * Third party and integration changes
129 *
130 * @since 2.1.6
131 * @return void
132 */
133 public function reload_settings_for_integrations(){
134 EEB()->settings->reload_settings();
135 }
136
137 /**
138 * ######################
139 * ###
140 * #### PAGE BUFFERING & WIDGET FILTER
141 * ###
142 * ######################
143 */
144
145 /**
146 * Buffer the final output on the init hook
147 *
148 * @return void
149 */
150 public function buffer_final_output(){
151
152 if(
153 defined( 'WP_CLI' ) //Bail if WP CLI command
154 || defined( 'DOING_CRON' ) //Bail if it is a cron call
155 ) {
156 return;
157 }
158
159 if( wp_doing_ajax() ){
160
161 //Maybe allow filtering for ajax requests
162 $filter_ajax_requests = (int) EEB()->settings->get_setting( 'ajax_requests', true, 'filter_body' );
163 if( $filter_ajax_requests !== 1 ){
164 return;
165 }
166
167 }
168
169 if( is_admin() ){
170
171 //Maybe allow filtering for admin requests
172 $filter_admin_requests = (int) EEB()->settings->get_setting( 'admin_requests', true, 'filter_body' );
173 if( $filter_admin_requests !== 1 ){
174 return;
175 }
176
177 }
178
179 ob_start( array( $this, 'apply_content_filter' ) );
180 }
181
182 /**
183 * Apply the callabla function for ob_start()
184 *
185 * @param string $content
186 * @return string - the filtered content
187 */
188 public function apply_content_filter( $content ){
189 $filteredContent = apply_filters( $this->final_outout_buffer_hook, $content );
190
191 // remove filters after applying to prevent multiple applies
192 remove_all_filters( $this->final_outout_buffer_hook );
193
194 return $filteredContent;
195 }
196
197 /**
198 * Filter for "dynamic_sidebar_params" hook
199 *
200 * @deprecated 2.1.4
201 * @global array $wp_registered_widgets
202 * @param array $params
203 * @return array
204 */
205 public function eeb_dynamic_sidebar_params( $params){
206 global $wp_registered_widgets;
207
208 if ( is_admin() ) {
209 return $params;
210 }
211
212 $widget_id = $params[0]['widget_id'];
213
214 // prevent overwriting when already set by another version of the widget output class
215 if ( isset( $wp_registered_widgets[ $widget_id ]['_wo_original_callback'] ) ) {
216 return $params;
217 }
218
219 $wp_registered_widgets[ $widget_id ]['_wo_original_callback'] = $wp_registered_widgets[ $widget_id ]['callback'];
220 $wp_registered_widgets[ $widget_id ]['callback'] = array( $this, 'call_widget_callback' );
221
222 return $params;
223 }
224
225 /**
226 * The Widget Callback
227 *
228 * @deprecated 2.1.4
229 * @global array $wp_registered_widgets
230 */
231 public function call_widget_callback(){
232 global $wp_registered_widgets;
233
234 $original_callback_params = func_get_args();
235 $original_callback = null;
236
237 $widget_id = $original_callback_params[0]['widget_id'];
238
239 $original_callback = $wp_registered_widgets[ $widget_id ]['_wo_original_callback'];
240 $wp_registered_widgets[ $widget_id ]['callback'] = $original_callback;
241
242 $widget_id_base = ( isset( $wp_registered_widgets[ $widget_id ]['callback'][0]->id_base ) ) ? $wp_registered_widgets[ $widget_id ]['callback'][0]->id_base : 0;
243
244 if ( is_callable( $original_callback ) ) {
245 ob_start();
246 call_user_func_array( $original_callback, $original_callback_params );
247 $widget_output = ob_get_clean();
248
249 echo apply_filters( $this->widget_callback_hook, $widget_output, $widget_id_base, $widget_id );
250
251 // remove filters after applying to prevent multiple applies
252 remove_all_filters( $this->widget_callback_hook );
253 }
254 }
255
256 /**
257 * ######################
258 * ###
259 * #### SCRIPT ENQUEUEMENTS
260 * ###
261 * ######################
262 */
263
264 public function load_frontend_header_styling(){
265
266 $js_version = date( "ymd-Gis", filemtime( EEB_PLUGIN_DIR . 'core/includes/assets/js/custom.js' ));
267 $css_version = date( "ymd-Gis", filemtime( EEB_PLUGIN_DIR . 'core/includes/assets/css/style.css' ));
268 $protect_using = (string) EEB()->settings->get_setting( 'protect_using', true );
269 $footer_scripts = (bool) EEB()->settings->get_setting( 'footer_scripts', true );
270
271 if( $protect_using === 'with_javascript' ){
272 wp_enqueue_script( 'eeb-js-frontend', EEB_PLUGIN_URL . 'core/includes/assets/js/custom.js', array( 'jquery' ), $js_version, $footer_scripts );
273 }
274
275 if(
276 $protect_using === 'with_javascript'
277 || $protect_using === 'without_javascript'
278 ){
279 wp_register_style( 'eeb-css-frontend', EEB_PLUGIN_URL . 'core/includes/assets/css/style.css', false, $css_version );
280 wp_enqueue_style ( 'eeb-css-frontend' );
281 }
282
283 if( (string) EEB()->settings->get_setting( 'show_encoded_check', true ) === '1' ){
284 wp_enqueue_style('dashicons');
285 }
286
287 }
288
289 /**
290 * ######################
291 * ###
292 * #### CORE LOGIC
293 * ###
294 * ######################
295 */
296
297 /**
298 * Register all single filters to protect your content
299 *
300 * @return void
301 */
302 public function setup_single_filter_hooks(){
303
304 if( EEB()->validate->is_query_parameter_excluded() ){
305 return;
306 }
307
308 if( EEB()->validate->is_post_excluded() ){
309 return;
310 }
311
312 $protection_method = (int) EEB()->settings->get_setting( 'protect', true );
313 $filter_rss = (int) EEB()->settings->get_setting( 'filter_rss', true, 'filter_body' );
314 $remove_shortcodes_rss = (int) EEB()->settings->get_setting( 'remove_shortcodes_rss', true, 'filter_body' );
315 $protect_shortcode_tags = (bool) EEB()->settings->get_setting( 'protect_shortcode_tags', true, 'filter_body' );
316 $protect_shortcode_tags_valid = false;
317
318 if ( is_feed() ) {
319
320 if( $filter_rss === 1 ){
321 add_filter( $this->final_outout_buffer_hook, array( $this, 'filter_rss' ), EEB()->settings->get_hook_priorities( 'filter_rss' ) );
322 }
323
324 if ( $remove_shortcodes_rss ) {
325 add_filter( $this->final_outout_buffer_hook, array( $this, 'callback_rss_remove_shortcodes' ), EEB()->settings->get_hook_priorities( 'callback_rss_remove_shortcodes' ) );
326 }
327
328 }
329
330 if ( $protection_method === 2 ) {
331 $protect_shortcode_tags_valid = true;
332
333 $filter_hooks = array(
334 'the_title',
335 'the_content',
336 'the_excerpt',
337 'get_the_excerpt',
338
339 //Comment related
340 'comment_text',
341 'comment_excerpt',
342 'comment_url',
343 'get_comment_author_url',
344 'get_comment_author_url_link',
345
346 //Widgets
347 'widget_title',
348 'widget_text',
349 'widget_content',
350 'widget_output',
351 );
352
353 $filter_hooks = apply_filters( 'eeb/frontend/wordpress_filters', $filter_hooks );
354
355 foreach ( $filter_hooks as $hook ) {
356 add_filter( $hook, array( $this, 'filter_content' ), EEB()->settings->get_hook_priorities( 'filter_content' ) );
357 }
358 } elseif( $protection_method === 1 ){
359 $protect_shortcode_tags_valid = true;
360
361 add_filter( $this->final_outout_buffer_hook, array( $this, 'filter_page' ), EEB()->settings->get_hook_priorities( 'filter_page' ) );
362 }
363
364 if( $protect_shortcode_tags_valid ){
365 if( $protect_shortcode_tags ){
366 add_filter( 'do_shortcode_tag', array( $this, 'filter_content' ), EEB()->settings->get_hook_priorities( 'do_shortcode_tag' ) );
367 }
368 }
369
370 }
371
372 /**
373 * Filter the page itself
374 *
375 * @param string $content
376 * @return string
377 */
378 public function filter_page( $content ){
379 $protect_using = (string) EEB()->settings->get_setting( 'protect_using', true );
380
381 return EEB()->validate->filter_page( $content, $protect_using );
382 }
383
384 /**
385 * Filter the whole content
386 *
387 * @param string $content
388 * @return string
389 */
390 public function filter_content( $content ){
391 $protect_using = (string) EEB()->settings->get_setting( 'protect_using', true );
392 return EEB()->validate->filter_content( $content, $protect_using );
393 }
394
395 /**
396 * Filter the rss content
397 *
398 * @param string $content
399 * @return string
400 */
401 public function filter_rss( $content ){
402 $protection_type = (string) EEB()->settings->get_setting( 'protect_using', true );
403 return EEB()->validate->filter_rss( $content, $protection_type );
404 }
405
406 /**
407 * RSS Callback Remove shortcodes
408 * @param string $content
409 * @return string
410 */
411 public function callback_rss_remove_shortcodes( $content ) {
412 // strip shortcodes like [eeb_content], [eeb_form]
413 $content = strip_shortcodes($content);
414
415 return $content;
416 }
417
418 /**
419 * ######################
420 * ###
421 * #### SHORTCODES
422 * ###
423 * ######################
424 */
425
426 /**
427 * Handle content filter shortcode
428 * @param array $atts
429 * @param string $content
430 */
431 public function protect_content_shortcode( $atts, $content = null ){
432 $protect = (int) EEB()->settings->get_setting( 'protect', true );
433 $protect_using = (string) EEB()->settings->get_setting( 'protect_using', true );
434 $protection_activated = ( $protect === 1 || $protect === 2 ) ? true : false;
435
436 if ( ! $protection_activated ) {
437 return $content;
438 }
439
440 if( isset( $atts['protect_using'] ) ){
441 $protect_using = sanitize_title( $atts['protect_using'] );
442 }
443
444 $content = EEB()->validate->filter_content( $content, $protect_using );
445
446 return $content;
447 }
448
449 /**
450 * Return the email encoder form
451 * @param array $atts
452 * @param string $content
453 */
454 public function shortcode_email_encoder_form( $atts = array(), $content = null ){
455
456 if(
457 EEB()->helpers->is_page( $this->page_name )
458 || (bool) EEB()->settings->get_setting( 'encoder_form_frontend', true, 'encoder_form' )
459 ){
460 return EEB()->validate->get_encoder_form();
461 }
462
463 return '';
464 }
465
466 /**
467 * Return the encoded content
468 * @param array $atts
469 * @param string $content
470 */
471 public function shortcode_eeb_content( $atts = array(), $content = null ){
472
473 $original_content = $content;
474 $show_encoded_check = (string) EEB()->settings->get_setting( 'show_encoded_check', true );
475
476 if( ! isset( $atts['protection_text'] ) ){
477 $protection_text = __( EEB()->settings->get_setting( 'protection_text', true ), 'email-protection-text-eeb-content' );
478 } else {
479 $protection_text = wp_kses_post( $atts['protection_text'] );
480 }
481
482 if( isset( $atts['method'] ) ){
483 $method = sanitize_title( $atts['method'] );
484 } else {
485 $method = 'rot13';
486 }
487
488 if( isset( $atts['do_shortcode'] ) && $atts['do_shortcode'] === 'yes' ){
489 $content = do_shortcode( $content );
490 }
491
492 switch( $method ){
493 case 'enc_ascii':
494 case 'rot13':
495 $content = EEB()->validate->encode_ascii( $content, $protection_text );
496 break;
497 case 'enc_escape':
498 case 'escape':
499 $content = EEB()->validate->encode_escape( $content, $protection_text );
500 break;
501 case 'enc_html':
502 case 'encode':
503 default:
504 $content = antispambot( $content );
505 break;
506 }
507
508 // mark link as successfullly encoded (for admin users)
509 if ( current_user_can( EEB()->settings->get_admin_cap( 'frontend-display-security-check' ) ) && $show_encoded_check ) {
510 $content .= EEB()->validate->get_encoded_email_icon();
511 }
512
513 return apply_filters( 'eeb/frontend/shortcode/eeb_protect_content', $content, $atts, $original_content );
514 }
515
516 /**
517 * Return the encoded email
518 * @param array $atts
519 * @param string $content
520 */
521 public function shortcode_eeb_email( $atts = array(), $content = null ){
522
523 $allowed_attr_html = EEB()->settings->get_safe_html_attr();
524 $show_encoded_check = (bool) EEB()->settings->get_setting( 'show_encoded_check', true );
525 $protection_text = __( EEB()->settings->get_setting( 'protection_text', true ), 'email-encoder-bundle' );
526
527 if( empty( $atts['email'] ) ){
528 return '';
529 } else {
530 $email = sanitize_email( $atts['email'] );
531 }
532
533 if( empty( $atts['extra_attrs'] ) ){
534 $extra_attrs = '';
535 } else {
536 $extra_attrs = $atts['extra_attrs'];
537 }
538
539 if( ! isset( $atts['method'] ) || empty( $atts['method'] ) ){
540 $protect_using = (string) EEB()->settings->get_setting( 'protect_using', true );
541 if( ! empty( $protect_using ) ){
542 $method = $protect_using;
543 } else {
544 $method = 'rot13'; //keep as fallback
545 }
546 } else {
547 $method = sanitize_title( $atts['method'] );
548 }
549
550 $custom_class = (string) EEB()->settings->get_setting( 'class_name', true );
551
552 if( empty( $atts['display'] ) ) {
553 $display = $email;
554 } else {
555 $display = wp_kses( html_entity_decode( $atts['display'] ), $allowed_attr_html );
556 }
557
558 if( empty( $atts['noscript'] ) ) {
559 $noscript = $protection_text;
560 } else {
561 $noscript = wp_kses( html_entity_decode( $atts['noscript'] ), $allowed_attr_html );
562 }
563
564 $class_name = ' ' . EEB()->helpers->sanitize_html_attributes( $extra_attrs );
565 $class_name .= ' class="' . esc_attr( $custom_class ) . '"';
566 $mailto = '<a href="mailto:' . $email . '"'. $class_name . '>' . $display . '</a>';
567
568 switch( $method ){
569 case 'enc_ascii':
570 case 'rot13':
571 $mailto = EEB()->validate->encode_ascii( $mailto, $noscript );
572 break;
573 case 'enc_escape':
574 case 'escape':
575 $mailto = EEB()->validate->encode_escape( $mailto, $noscript );
576 break;
577 case 'with_javascript':
578 $mailto = EEB()->validate->dynamic_js_email_encoding( $mailto, $noscript );
579 break;
580 case 'without_javascript':
581 $mailto = EEB()->validate->encode_email_css( $mailto );
582 break;
583 case 'char_encode':
584 $mailto = EEB()->validate->filter_plain_emails( $mailto, null, 'char_encode' );
585 break;
586 case 'strong_method':
587 $mailto = EEB()->validate->filter_plain_emails( $mailto );
588 break;
589 case 'enc_html':
590 case 'encode':
591 default:
592 $mailto = '<a href="mailto:' . antispambot( $email ) . '"'. $class_name . '>' . antispambot( $display ) . '</a>';
593 break;
594 }
595
596 // mark link as successfullly encoded (for admin users)
597 if ( current_user_can( EEB()->settings->get_admin_cap( 'frontend-display-security-check' ) ) && $show_encoded_check ) {
598 $mailto .= EEB()->validate->get_encoded_email_icon();
599 }
600
601 return apply_filters( 'eeb/frontend/shortcode/eeb_mailto', $mailto );
602 }
603
604 /**
605 * ######################
606 * ###
607 * #### EMAIL IMAGE
608 * ###
609 * ######################
610 */
611
612 public function display_email_image(){
613
614 if( ! isset( $_GET['eeb_mail'] ) ){
615 return;
616 }
617
618 $email = sanitize_email( base64_decode( $_GET['eeb_mail'] ) );
619
620 if( ! is_email( $email ) || ! isset( $_GET['eeb_hash'] ) ){
621 return;
622 }
623
624 $hash = (string) $_GET['eeb_hash'];
625 $secret = EEB()->settings->get_email_image_secret();
626
627 if( EEB()->validate->generate_email_signature( $email, $secret ) !== $hash ){
628 wp_die( __('Your signture is invalid.', 'email-encoder-bundle') );
629 }
630
631 $image = EEB()->validate->email_to_image( $email );
632
633 if( empty( $image ) ){
634 wp_die( __('Your email could not be converted.', 'email-encoder-bundle') );
635 }
636
637 header('Content-type: image/png');
638 echo $image;
639 die();
640
641 }
642
643 /**
644 * ######################
645 * ###
646 * #### TEMPLATE TAGS
647 * ###
648 * ######################
649 */
650
651 public function add_custom_template_tags(){
652 $template_tags = EEB()->settings->get_template_tags();
653
654 foreach( $template_tags as $hook => $callback ){
655
656 //Make sure we only call our own custom template tags
657 if( is_callable( array( $this, $callback ) ) ){
658 apply_filters( $hook, array( $this, $callback ), 10 );
659 }
660
661 }
662 }
663
664 /**
665 * Filter for the eeb_filter template tag
666 *
667 * This function is called dynamically by add_custom_template_tags
668 * using the EEB()->settings->get_template_tags() callback.
669 *
670 * @param string $content - the default content
671 * @return string - the filtered content
672 */
673 public function template_tag_eeb_filter( $content ){
674 $protect_using = (string) EEB()->settings->get_setting( 'protect_using', true );
675 return EEB()->validate->filter_content( $content, $protect_using );
676 }
677
678 /**
679 * Filter for the eeb_filter template tag
680 *
681 * This function is called dynamically by add_custom_template_tags
682 * using the EEB()->settings->get_template_tags() callback.
683 *
684 * @param string $content - the default content
685 * @return string - the filtered content
686 */
687 public function template_tag_eeb_mailto( $email, $display = null, $atts = array() ){
688 if ( is_array( $display ) ) {
689 // backwards compatibility (old params: $display, $attrs = array())
690 $atts = $display;
691 $display = $email;
692 } else {
693 $atts['href'] = 'mailto:'.$email;
694 }
695
696 return EEB()->validate->create_protected_mailto( $display, $atts );
697 }
698
699 }
700