@@ -82,12 +82,6 @@ class ContactsEncoder
8282 */
8383 protected $ plain_email_pattern ;
8484
85- /**
86- * @var string example: '/\b[_A-Za-z0-9-\.]+@[_A-Za-z0-9-\.]+\.[A-Za-z]{2,}/'
87- * @ToDo Is this regular expression needed? A little different against `$plain_email_pattern`.
88- */
89- protected $ plain_email_pattern_without_capturing ;
90-
9185 /**
9286 * @var string example: '/tel:(\+\d{8,12})/'
9387 * @ToDo Is this regexp is actual and right?
@@ -223,7 +217,6 @@ private function prepareRegularExpressions()
223217 $ this ->global_phones_pattern = '/ ' . implode ('| ' , self ::PHONE_NUMBERS_PATTERNS ) . '/ ' ;
224218 $ this ->global_mailto_pattern = '/mailto\:( ' . self ::EMAIL_PATTERN . ')/ ' ;
225219 $ this ->plain_email_pattern = '/(\b ' . self ::EMAIL_PATTERN . '\b)/ ' ;
226- $ this ->plain_email_pattern_without_capturing = '/\b ' . self ::EMAIL_PATTERN . '/ ' ;
227220 $ this ->global_tel_pattern = '/tel:( ' . self ::PHONE_NUMBER . ')/ ' ;
228221 }
229222
@@ -300,83 +293,41 @@ public function modifyContent($content, $skip_exclusions = false)
300293 */
301294 public function modifyGlobalEmails ($ content )
302295 {
303- $ replacing_result = '' ;
304-
305- if ( version_compare (phpversion (), '7.4.0 ' , '>= ' ) ) {
306- $ replacing_result = preg_replace_callback ($ this ->global_email_pattern , function ($ matches ) use ($ content ) {
307- if ( isset ($ matches [3 ][0 ], $ matches [0 ][0 ]) && in_array (strtolower ($ matches [3 ][0 ]), ['.jpg ' , '.jpeg ' , '.png ' , '.gif ' , '.svg ' , '.webp ' ]) ) {
308- return $ matches [0 ][0 ];
309- }
310-
311- //chek if email is placed in excluded attributes and return unchanged if so
312- if ( isset ($ matches [0 ][0 ]) && $ this ->helper ->hasAttributeExclusions ($ matches [0 ][0 ], $ this ->temp_content ) ) {
313- return $ matches [0 ][0 ];
314- }
315-
316- // skip encoding if the content in script tag
317- if ( isset ($ matches [0 ][0 ]) && $ this ->helper ->isInsideScriptTag ($ matches [0 ][0 ], $ content ) ) {
318- return $ matches [0 ][0 ];
319- }
320-
321- // skip encoding inside select option values/text — breaks form submission
322- if ( isset ($ matches [0 ][0 ]) && $ this ->helper ->isInsideOptionTag ($ matches [0 ][0 ], $ content ) ) {
323- return $ matches [0 ][0 ];
324- }
325-
326- if ( isset ($ matches [0 ][0 ]) && $ this ->helper ->isMailto ($ matches [0 ][0 ]) ) {
327- return $ this ->encodeMailtoLinkV2 ($ matches [0 ], $ content );
328- }
296+ $ replacing_result = preg_replace_callback ($ this ->global_email_pattern , function ($ matches ) {
297+ if ( isset ($ matches [3 ]) && in_array (strtolower ($ matches [3 ]), ['.jpg ' , '.jpeg ' , '.png ' , '.gif ' , '.svg ' , '.webp ' ]) && isset ($ matches [0 ]) ) {
298+ return $ matches [0 ];
299+ }
329300
330- if (
331- isset ($ matches [0 ]) &&
332- is_array ($ matches [0 ]) &&
333- $ this ->helper ->isMailtoAdditionalCopy ($ matches [0 ], $ content )
334- ) {
335- return '' ;
336- }
301+ //chek if email is placed in excluded attributes and return unchanged if so
302+ if ( isset ($ matches [0 ]) && $ this ->helper ->hasAttributeExclusions ($ matches [0 ], $ this ->temp_content ) ) {
303+ return $ matches [0 ];
304+ }
337305
338- if (
339- isset ($ matches [0 ], $ matches [0 ][0 ]) &&
340- is_array ($ matches [0 ]) &&
341- $ this ->helper ->isEmailInLink ($ matches [0 ], $ content )
342- ) {
343- return $ matches [0 ][0 ];
344- }
306+ // skip encoding if the content in script tag
307+ if ( isset ($ matches [0 ]) && $ this ->helper ->isInsideScriptTag ($ matches [0 ], $ this ->temp_content ) ) {
308+ return $ matches [0 ];
309+ }
345310
346- if ( isset ($ matches [0 ][0 ]) ) {
347- return $ this -> encodePlainEmail ( $ matches [0 ][ 0 ]) ;
348- }
311+ if ( isset ($ matches [0 ]) && $ this -> helper -> isInsideOptionTag ( $ matches [0 ], $ this -> temp_content ) ) {
312+ return $ matches [0 ];
313+ }
349314
315+ if (
316+ isset ($ matches [0 ]) && $ this ->helper ->isMailtoAdditionalCopy ($ matches [0 ], $ this ->temp_content )
317+ ) {
350318 return '' ;
351- }, $ content , -1 , $ count , PREG_OFFSET_CAPTURE );
352- }
353-
354- if ( version_compare (phpversion (), '7.4.0 ' , '< ' ) ) {
355- $ replacing_result = preg_replace_callback ($ this ->global_email_pattern , function ($ matches ) {
356- if ( isset ($ matches [3 ]) && in_array (strtolower ($ matches [3 ]), ['.jpg ' , '.jpeg ' , '.png ' , '.gif ' , '.svg ' , '.webp ' ]) && isset ($ matches [0 ]) ) {
357- return $ matches [0 ];
358- }
359-
360- //chek if email is placed in excluded attributes and return unchanged if so
361- if ( isset ($ matches [0 ]) && $ this ->helper ->hasAttributeExclusions ($ matches [0 ], $ this ->temp_content ) ) {
362- return $ matches [0 ];
363- }
364-
365- if ( isset ($ matches [0 ]) && $ this ->helper ->isInsideOptionTag ($ matches [0 ], $ this ->temp_content ) ) {
366- return $ matches [0 ];
367- }
319+ }
368320
369- if ( isset ($ matches [0 ]) && $ this ->helper ->isMailto ($ matches [0 ]) ) {
370- return $ this ->encodeMailtoLink ($ matches [0 ]);
371- }
321+ if ( isset ($ matches [0 ]) && $ this ->helper ->isMailto ($ matches [0 ]) ) {
322+ return $ this ->encodeMailtoLink ($ matches [0 ]);
323+ }
372324
373- if ( isset ($ matches [0 ]) ) {
374- return $ this ->encodePlainEmail ($ matches [0 ]);
375- }
325+ if ( isset ($ matches [0 ]) ) {
326+ return $ this ->encodePlainEmail ($ matches [0 ]);
327+ }
376328
377- return '' ;
378- }, $ content );
379- }
329+ return '' ;
330+ }, $ content );
380331
381332 // modify content to turn back aria-label
382333 $ replacing_result = $ this ->handleAriaLabelContent ($ replacing_result , true );
@@ -395,89 +346,44 @@ public function modifyGlobalEmails($content)
395346 public function modifyGlobalPhoneNumbers ($ content )
396347 {
397348 $ phones_pattern = $ this ->global_phones_pattern ;
398- $ replacing_result = '' ;
399-
400- if ( version_compare (phpversion (), '7.4.0 ' , '>= ' ) ) {
401- $ replacing_result = preg_replace_callback (
402- $ phones_pattern ,
403- function ($ matches ) use ($ content ) {
404- if ( isset ($ matches [0 ]) ) {
405- $ first_group = $ matches [0 ];
406- } else {
407- return '' ;
349+ $ replacing_result = preg_replace_callback (
350+ $ phones_pattern ,
351+ function ($ matches ) {
352+ if ( isset ($ matches [0 ]) ) {
353+ if ( $ this ->helper ->isTelTag ($ matches [0 ]) ) {
354+ return $ this ->encodeTelLink ($ matches [0 ]);
408355 }
409356
410- if ( isset ($ first_group [0 ]) ) {
411- $ second_group = $ first_group [0 ];
412- } else {
413- return '' ;
414- }
357+ // symbols clearance
358+ $ item_length = strlen (str_replace ([' ' , '( ' , ') ' , '- ' , '+ ' , '. ' ], '' , $ matches [0 ]));
415359
416- if (is_array ($ first_group ) && $ this ->helper ->isTelTag ($ second_group ) ) {
417- return $ this ->encodeTelLinkV2 ($ first_group , $ content );
418- }
419- //symbols clearance
420- $ item_length = strlen (str_replace ([' ' , '( ' , ') ' , '- ' , '+ ' , '. ' ], '' , $ second_group ));
421- //check length
360+ // check length
422361 if ( $ item_length > 12 || $ item_length < 8 ) {
423- return $ second_group ;
362+ return $ matches [ 0 ] ;
424363 }
425- //check attribute exclusions
426- if ( $ this ->helper ->hasAttributeExclusions ($ second_group , $ this ->temp_content ) ) {
427- return $ second_group ;
364+
365+ // check attribute exclusions
366+ if ( $ this ->helper ->hasAttributeExclusions ($ matches [0 ], $ this ->temp_content ) ) {
367+ return $ matches [0 ];
428368 }
429- //check if in script
430- if ( $ this ->helper ->isInsideScriptTag ($ second_group , $ content ) ) {
431- return $ second_group ;
369+
370+ // check if in script
371+ if ( $ this ->helper ->isInsideScriptTag ($ matches [0 ], $ this ->temp_content ) ) {
372+ return $ matches [0 ];
432373 }
433- //do encode
374+
434375 return $ this ->encodeAny (
435- $ second_group ,
376+ $ matches [ 0 ] ,
436377 $ this ->global_obfuscation_mode ,
437378 $ this ->global_replacing_text ,
438379 true
439380 );
440- },
441- $ content ,
442- -1 ,
443- $ count ,
444- PREG_OFFSET_CAPTURE
445- );
446- }
447-
448- if ( version_compare (phpversion (), '7.4.0 ' , '< ' ) ) {
449- $ replacing_result = preg_replace_callback (
450- $ phones_pattern ,
451- function ($ matches ) {
452- if ( isset ($ matches [0 ]) ) {
453- if ( $ this ->helper ->isTelTag ($ matches [0 ]) ) {
454- return $ this ->encodeTelLink ($ matches [0 ]);
455- }
456-
457- $ item_length = strlen (str_replace ([' ' , '( ' , ') ' , '- ' , '+ ' , '. ' ], '' , $ matches [0 ]));
458- if ( $ item_length > 12 || $ item_length < 8 ) {
459- return $ matches [0 ];
460- }
461-
462- if ( $ this ->helper ->hasAttributeExclusions ($ matches [0 ][0 ], $ this ->temp_content ) ) {
463- return $ matches [0 ];
464- }
465- }
466-
467- if ( isset ($ matches [0 ]) ) {
468- return $ this ->encodeAny (
469- $ matches [0 ],
470- $ this ->global_obfuscation_mode ,
471- $ this ->global_replacing_text ,
472- true
473- );
474- }
381+ }
475382
476- return '' ;
477- },
478- $ content
479- );
480- }
383+ return '' ;
384+ },
385+ $ content
386+ );
481387
482388 // modify content to turn back aria-label
483389 $ replacing_result = $ this ->handleAriaLabelContent ($ replacing_result , true );
@@ -550,7 +456,7 @@ protected function encodeAny($string, $mode = Params::OBFUSCATION_MODE_BLUR, $re
550456 }
551457
552458 /**
553- * Method to process mailto: links. For PHP < 7.4
459+ * Method to process mailto: links.
554460 *
555461 * @param string $mailto_link_str
556462 *
@@ -576,40 +482,7 @@ private function encodeMailtoLink($mailto_link_str)
576482 }
577483
578484 /**
579- * Method to process mailto: links. Use this only for PHP 7.4+
580- *
581- * @param $match array
582- * @param $content string
583- *
584- * @return string
585- */
586- private function encodeMailtoLinkV2 ($ match , $ content )
587- {
588- $ position = $ match [1 ];
589- $ q_position = $ position + strcspn ($ content , '\'" ' , $ position );
590- $ mailto_link_str = substr ($ content , $ position , $ q_position - $ position );
591- // Get inner tag text and place it in $matches[1]
592- preg_match ($ this ->global_mailto_pattern , $ mailto_link_str , $ matches );
593- if ( isset ($ matches [1 ]) ) {
594- $ mailto_inner_text = preg_replace_callback ($ this ->plain_email_pattern_without_capturing , function ($ matches ) {
595- if ( isset ($ matches [0 ]) ) {
596- return $ this ->getObfuscatedEmailString ($ matches [0 ]);
597- }
598-
599- return '' ;
600- }, $ matches [1 ]);
601- }
602-
603- $ mailto_link_str = str_replace ('mailto: ' , '' , $ mailto_link_str );
604- $ encoded = $ this ->encoder ->encodeString ($ mailto_link_str );
605-
606- $ text = isset ($ mailto_inner_text ) ? $ mailto_inner_text : $ mailto_link_str ;
607-
608- return 'mailto: ' . $ text . '" data-original-string=" ' . $ encoded . '" title=" ' . htmlspecialchars ($ this ->getTooltip (), ENT_QUOTES , 'UTF-8 ' );
609- }
610-
611- /**
612- * Method to process tel: links. For PHP < 7.4
485+ * Method to process tel: links.
613486 *
614487 * @param string $tel_link_str
615488 *
@@ -635,42 +508,6 @@ private function encodeTelLink($tel_link_str)
635508 return 'tel: ' . $ text . '" data-original-string=" ' . $ encoded . '" title=" ' . htmlspecialchars ($ this ->getTooltip (), ENT_QUOTES , 'UTF-8 ' );
636509 }
637510
638- /**
639- * Method to process tel: links. Use this only for PHP 7.4+
640- *
641- * @param array $match
642- * @param string $content
643- *
644- * @return string
645- */
646- private function encodeTelLinkV2 ($ match , $ content )
647- {
648- $ position = !empty ($ match [1 ]) ? (int )$ match [1 ] : null ;
649- if (null === $ position ) {
650- return $ content ;
651- }
652- $ q_position = $ position + strcspn ($ content , '\'" ' , $ position );
653- $ tel_link_string = substr ($ content , $ position , $ q_position - $ position );
654- // Get inner tag text and place it in $matches[1]
655- preg_match ($ this ->global_tel_pattern , $ tel_link_string , $ matches );
656- if ( isset ($ matches [1 ]) ) {
657- $ tel_inner_text = preg_replace_callback ('/ ' . self ::PHONE_NUMBER . '/ ' , function ($ matches ) {
658- if ( isset ($ matches [0 ]) ) {
659- $ obfuscator = new Obfuscator ();
660- return $ obfuscator ->processPhone ($ matches [0 ]);
661- }
662- return '' ;
663- }, $ matches [1 ]);
664- }
665-
666- $ tel_link_string = str_replace ('tel: ' , '' , $ tel_link_string );
667- $ encoded = $ this ->encoder ->encodeString ($ tel_link_string );
668-
669- $ text = isset ($ tel_inner_text ) ? $ tel_inner_text : $ tel_link_string ;
670-
671- return 'tel: ' . $ text . '" data-original-string=" ' . $ encoded . '" title=" ' . htmlspecialchars ($ this ->getTooltip (), ENT_QUOTES , 'UTF-8 ' );
672- }
673-
674511 /**
675512 * @param string $email_str
676513 *
0 commit comments