From 8c9dfa3b1d5ae588affb3937782049ef32f8f214 Mon Sep 17 00:00:00 2001 From: amit kumar laravel Date: Tue, 31 Dec 2024 11:41:45 +0530 Subject: [PATCH 1/3] Fix Pint Issue. --- .../Webkul/Email/src/Helpers/Htmlfilter.php | 186 ++++++------------ 1 file changed, 60 insertions(+), 126 deletions(-) diff --git a/packages/Webkul/Email/src/Helpers/Htmlfilter.php b/packages/Webkul/Email/src/Helpers/Htmlfilter.php index 76404b722..45b277564 100644 --- a/packages/Webkul/Email/src/Helpers/Htmlfilter.php +++ b/packages/Webkul/Email/src/Helpers/Htmlfilter.php @@ -61,7 +61,6 @@ public function tln_skipspace($body, $offset) $offset += $count; } } catch (\Exception $e) { - // Do nothing ... } return $offset; @@ -171,6 +170,7 @@ public function tln_getnxtag($body, $offset) switch (substr($body, $pos, 1)) { case '/': $tagtype = 2; + $pos++; break; @@ -196,12 +196,9 @@ public function tln_getnxtag($body, $offset) break; default: - /** - * Assume tagtype 1 for now. If it's type 3, we'll switch values - * later. - */ $tagtype = 1; break; + } /** @@ -238,11 +235,14 @@ public function tln_getnxtag($body, $offset) $tagtype = 3; } else { $gt = $this->tln_findnxstr($body, $pos, '>'); + $retary = [false, false, false, $lt, $gt]; return $retary; } + break; + //intentional fall-through case '>': return [$tagname, false, $tagtype, $lt, $pos]; @@ -296,6 +296,7 @@ public function tln_getnxtag($body, $offset) if ($matches[2] == '/>') { $tagtype = 3; + $pos++; } @@ -357,6 +358,8 @@ public function tln_getnxtag($body, $offset) return $retary; } + break; + //intentional fall-through case '>': $attary[$attname] = '"yes"'; @@ -427,22 +430,13 @@ public function tln_getnxtag($body, $offset) [$pos, $attval, $match] = $regary; - /** - * If it's ">" it will be caught at the top. - */ $attval = preg_replace('/\"/s', '"', $attval); $attary[$attname] = '"'.$attval.'"'; } } elseif (preg_match('|[\w/>]|', $char)) { - /** - * That was attribute type 4. - */ $attary[$attname] = '"yes"'; } else { - /** - * An illegal character. Find next '>' and return. - */ $gt = $this->tln_findnxstr($body, $pos, '>'); return [false, false, false, $lt, $gt]; @@ -576,18 +570,8 @@ public function tln_fixatts( } } - /** - * Remove any backslashes, entities, or extraneous whitespace. - */ - $oldattvalue = $attvalue; - $this->tln_defang($attvalue); - // if ($attname == 'style' && $attvalue !== $oldattvalue) { - // $attvalue = "idiocy"; - // $attary{$attname} = $attvalue; - // } - $this->tln_unspace($attvalue); /** @@ -600,11 +584,6 @@ public function tln_fixatts( if (preg_match($matchtag, $tagname)) { foreach ($matchattrs as $matchattr => $valary) { if (preg_match($matchattr, $attname)) { - /** - * There are two arrays in valary. - * First is matches. - * Second one is replacements - */ [$valmatch, $valrepl] = $valary; $newvalue = preg_replace($valmatch, $valrepl, $attvalue); @@ -617,20 +596,6 @@ public function tln_fixatts( } } } - - // if ($attname == 'style') { - // if (preg_match('/[\0-\37\200-\377]+/', $attvalue)) { - // $attary{$attname} = '"disallowed character"'; - // } - // preg_match_all("/url\s*\((.+)\)/si", $attvalue, $aMatch); - // if (count($aMatch)) { - // foreach($aMatch[1] as $sMatch) { - // $urlvalue = $sMatch; - // $this->tln_fixurl($attname, $urlvalue, $trans_image_path, $block_external_images); - // $attary{$attname} = str_replace($sMatch, $urlvalue, $attvalue); - // } - // } - // } } /** @@ -677,6 +642,7 @@ public function tln_fixurl($attname, &$attvalue, $trans_image_path, $block_exter default: $attvalue = $sQuote.$trans_image_path.$sQuote; break; + } } else { $aUrl = parse_url($attvalue); @@ -699,12 +665,15 @@ public function tln_fixurl($attname, &$attvalue, $trans_image_path, $block_exter $attvalue = $sQuote.$attvalue.$sQuote; } break; + case 'outbind': $attvalue = $sQuote.$attvalue.$sQuote; break; + case 'cid': $attvalue = $sQuote.$attvalue.$sQuote; break; + default: $attvalue = $sQuote.$trans_image_path.$sQuote; break; @@ -722,9 +691,6 @@ public function tln_fixstyle($body, $pos, $trans_image_path, $block_external_ima { $me = 'tln_fixstyle'; - // workaround for in between comments - $iCurrentPos = $pos; - $content = ''; $sToken = ''; @@ -743,21 +709,29 @@ public function tln_fixstyle($body, $pos, $trans_image_path, $block_external_ima case '/': if ($sToken == '<') { $sToken .= $char; + $bEndTag = true; } else { $content .= $char; } + break; + case '>': if ($bEndTag) { $sToken .= $char; + if (preg_match('/\<\/\s*style\s*\>/i', $sToken, $aMatch)) { $newpos = $i + 1; + $bSucces = true; + break 2; + } else { $content .= $sToken; } + $bEndTag = false; } else { $content .= $char; @@ -765,12 +739,13 @@ public function tln_fixstyle($body, $pos, $trans_image_path, $block_external_ima break; case '!': if ($sToken == '<') { - // possible comment if (isset($body[$i + 2]) && substr($body, $i, 3) == '!--') { $i = strpos($body, '-->', $i + 3); - if ($i === false) { // no end comment + + if (! $i) { $i = strlen($body); } + $sToken = ''; } } else { @@ -787,7 +762,7 @@ public function tln_fixstyle($body, $pos, $trans_image_path, $block_external_ima } } - if ($bSucces == false) { + if (! $bSucces) { return [false, strlen($body)]; } @@ -801,12 +776,6 @@ public function tln_fixstyle($body, $pos, $trans_image_path, $block_external_ima $trans_image_path = $trans_image_path; - /** - * Fix url('blah') declarations. - */ - // $content = preg_replace("|url\s*\(\s*([\'\"])\s*\S+script\s*:.*?([\'\"])\s*\)|si", - // "url(\\1$trans_image_path\\2)", $content); - // first check for 8bit sequences and disallowed control characters if (preg_match('/[\16-\37\200-\377]+/', $content)) { $content = ''; @@ -825,7 +794,6 @@ public function tln_fixstyle($body, $pos, $trans_image_path, $block_external_ima $aValue = $aReplace = []; foreach ($aMatch[1] as $sMatch) { - // url value $urlvalue = $sMatch; $this->tln_fixurl('style', $urlvalue, $trans_image_path, $block_external_images); $aValue[] = $sMatch; @@ -868,8 +836,6 @@ public function tln_body2div($attary, $trans_image_path) $divattary = ['class' => "'bodyclass'"]; - $text = '#000000'; - $has_bgc_stl = $has_txt_stl = false; $styledef = ''; @@ -884,21 +850,25 @@ public function tln_body2div($attary, $trans_image_path) case 'background': $styledef .= "background-image: url('$trans_image_path'); "; break; + case 'bgcolor': $has_bgc_stl = true; + $styledef .= "background-color: $attvalue; "; break; + case 'text': $has_txt_stl = true; + $styledef .= "color: $attvalue; "; break; + } } - // Outlook defines a white bgcolor and no text color. This can lead to - // white text on a white bg with certain themes. + // Outlook defines a white bgcolor and no text color. This can lead to white text on a white bg with certain themes. if ($has_bgc_stl && ! $has_txt_stl) { - $styledef .= "color: $text; "; + $styledef .= 'color: #000000; '; } if (strlen($styledef) > 0) { @@ -1003,9 +973,6 @@ public function tln_sanitize( if ($tagname != false) { if ($tagtype == 2) { if ($skip_content == $tagname) { - /** - * Got to the end of tag we needed to remove. - */ $tagname = false; $skip_content = false; @@ -1025,15 +992,9 @@ public function tln_sanitize( } } } else { - /** - * $rm_tags_with_content - */ - if ($skip_content == false) { - /** - * See if this is a self-closing type and change - * tagtype appropriately. - */ - if ($tagtype == 1 + if (! $skip_content) { + if ( + $tagtype == 1 && in_array($tagname, $self_closing_tags) ) { $tagtype = 3; @@ -1043,15 +1004,19 @@ public function tln_sanitize( * See if we should skip this tag and any content * inside it. */ - if ($tagtype == 1 + if ( + $tagtype == 1 && in_array($tagname, $rm_tags_with_content) ) { $skip_content = $tagname; } else { - if (($rm_tags == false - && in_array($tagname, $tag_list)) || - ($rm_tags == true - && ! in_array($tagname, $tag_list)) + if (( + ! $rm_tags + && in_array($tagname, $tag_list)) + || ( + $rm_tags + && ! in_array($tagname, $tag_list) + ) ) { $tagname = false; } else { @@ -1114,9 +1079,14 @@ public function tln_sanitize( return $trusted; } - // - // Use the nifty htmlfilter library - // + /** + * Use the nifty htmlfilter library + * + * @param mixed $body + * @param mixed $trans_image_path + * @param mixed $block_external_images + * @return string + */ public function HTMLFilter($body, $trans_image_path, $block_external_images = false) { $tag_list = [ @@ -1155,12 +1125,10 @@ public function HTMLFilter($body, $trans_image_path, $block_external_images = fa $rm_attnames = [ '/.*/' => [ - // "/target/i", '/^on.*/i', '/^dynsrc/i', '/^data.*/i', '/^lowsrc.*/i', - // "/^style/i", ], ]; @@ -1171,54 +1139,24 @@ public function HTMLFilter($body, $trans_image_path, $block_external_images = fa '/^([\'"])\s*\S+script\s*:.*([\'"])/si', '/^([\'"])\s*mocha\s*:*.*([\'"])/si', '/^([\'"])\s*about\s*:.*([\'"])/si', - ], - [ + ], [ "\\1$trans_image_path\\2", "\\1$trans_image_path\\2", "\\1$trans_image_path\\2", ], ], + '/^href|action/i' => [ [ '/^([\'"])\s*\S+script\s*:.*([\'"])/si', '/^([\'"])\s*mocha\s*:*.*([\'"])/si', '/^([\'"])\s*about\s*:.*([\'"])/si', - ], - [ + ], [ '\\1#\\1', '\\1#\\1', '\\1#\\1', ], ], - // "/^style/i" => - // array( - // array( - // "/\/\*.*\*\//", - // "/expression/i", - // "/binding/i", - // "/behaviou*r/i", - // "/include-source/i", - // '/position\s*:/i', - // '/(\\\\)?u(\\\\)?r(\\\\)?l(\\\\)?/i', - // '/url\s*\(\s*([\'"])\s*\S+script\s*:.*([\'"])\s*\)/si', - // '/url\s*\(\s*([\'"])\s*mocha\s*:.*([\'"])\s*\)/si', - // '/url\s*\(\s*([\'"])\s*about\s*:.*([\'"])\s*\)/si', - // '/(.*)\s*:\s*url\s*\(\s*([\'"]*)\s*\S+script\s*:.*([\'"]*)\s*\)/si' - // ), - // array( - // "", - // "idiocy", - // "idiocy", - // "idiocy", - // "idiocy", - // "idiocy", - // "url", - // "url(\\1#\\1)", - // "url(\\1#\\1)", - // "url(\\1#\\1)", - // "\\1:url(\\2#\\3)" - // ) - // ) ], ]; @@ -1232,14 +1170,6 @@ public function HTMLFilter($body, $trans_image_path, $block_external_images = fa $bad_attvals['/.*/']['/^src|background/i'][1], "\\1$trans_image_path\\1" ); - // array_push( - // $bad_attvals{'/.*/'}{'/^style/i'}[0], - // '/url\(([\'\"])\s*https*:.*([\'\"])\)/si' - // ); - // array_push( - // $bad_attvals{'/.*/'}{'/^style/i'}[1], - // "url(\\1$trans_image_path\\1)" - // ); } $add_attr_to_tag = [ @@ -1268,12 +1198,16 @@ public function AutoLinkUrls($str, $popup = false) if (preg_match_all("#(^|\s|\()((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i", $str, $matches)) { $pop = ($popup == true) ? ' target="_blank" ' : ''; + for ($i = 0; $i < count($matches['0']); $i++) { $period = ''; + if (preg_match("|\.$|", $matches['6'][$i])) { $period = '.'; + $matches['6'][$i] = substr($matches['6'][$i], 0, -1); } + $str = str_replace($matches['0'][$i], $matches['1'][$i].'stream = &$tmp_fp; } else { throw new \Exception( - 'Could not create temporary files for attachments. Your tmp directory may be unwritable by PHP.' + 'Could not create temporary files for attachments. Your tmp directory may be un-writable by PHP.' ); } @@ -161,8 +166,6 @@ public function setText($data) * Parse the Message into parts * * @return void - * - * @private */ private function parse() { @@ -501,7 +504,7 @@ public function getAttachments() && ! strpos($this->getPart('content-type', $part), 'image/') && ! stripos($filename, 'noname') == false ) { - //skip + // skip } else { $attachments[] = new Attachment( $filename, @@ -710,7 +713,7 @@ private function decodeContentTransfer($encodedString, $encodingType) */ private function decodeHeader($input) { - //Sometimes we have 2 label From so we take only the first + // Sometimes we have 2 label From so we take only the first if (is_array($input)) { return $this->decodeSingleHeader($input[0]); } diff --git a/packages/Webkul/Email/src/Repositories/EmailRepository.php b/packages/Webkul/Email/src/Repositories/EmailRepository.php index fadc641bf..c15ba011c 100644 --- a/packages/Webkul/Email/src/Repositories/EmailRepository.php +++ b/packages/Webkul/Email/src/Repositories/EmailRepository.php @@ -153,7 +153,7 @@ public function processInboundParseMail($content) if (! isset($email)) { $email = $this->create(array_merge($headers, [ 'folders' => ['inbox'], - 'reply' => $reply, //$this->htmlFilter->HTMLFilter($reply, ''), + 'reply' => $reply, // $this->htmlFilter->HTMLFilter($reply, ''), 'unique_id' => time().'@'.config('mail.domain'), 'reference_ids' => [$headers['message_id']], 'user_type' => 'person', From 501ac052b949cd5d45fd5a179b441dcf976ec07c Mon Sep 17 00:00:00 2001 From: amit kumar laravel Date: Tue, 31 Dec 2024 11:52:31 +0530 Subject: [PATCH 3/3] Fixed isse. --- packages/Webkul/Email/src/Helpers/Parser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/Webkul/Email/src/Helpers/Parser.php b/packages/Webkul/Email/src/Helpers/Parser.php index 1ec70b744..472ebc9de 100644 --- a/packages/Webkul/Email/src/Helpers/Parser.php +++ b/packages/Webkul/Email/src/Helpers/Parser.php @@ -701,7 +701,7 @@ private function decodeContentTransfer($encodedString, $encodingType) } elseif ($encodingType == 'quoted-printable') { return quoted_printable_decode($encodedString); } else { - return $encodedString; //8bit, 7bit, binary + return $encodedString; // 8bit, 7bit, binary } }