<commit_before><?php

namespace Remp\MailerModule\Models\ContentGenerator\Replace;

use Nette\Http\Url;
use Nette\InvalidArgumentException;
use Remp\MailerModule\Models\ContentGenerator\AllowedDomainManager;
use Remp\MailerModule\Models\ContentGenerator\GeneratorInput;

class TextUrlRtmReplace implements IReplace
{
    use RtmReplaceTrait;

    private AllowedDomainManager $allowedDomainManager;

    public function __construct(AllowedDomainManager $allowedDomainManager)
    {
        $this->allowedDomainManager = $allowedDomainManager;
    }

    public function replace(string $content, GeneratorInput $generatorInput): string
    {
        if ($content !== strip_tags($content)) {
            // This replacer is intended to be used only for text emails. HTML is handled by AnchorRtmReplace.
            return $content;
        }

        $matches = [];
        preg_match_all('/(https?:\/\/.+?)(\s|")/i', $content, $matches);

        if (count($matches) > 0) {
            foreach ($matches[1] as $idx => $hrefUrl) {
                // parse URL
                try {
                    $url = new Url($hrefUrl);
                } catch (InvalidArgumentException $e) {
                    continue;
                }

                // check if the host is whitelisted
                if (!$this->allowedDomainManager->isAllowed($url->getHost())) {
                    continue;
                }

                $finalUrl = $this->replaceUrl($hrefUrl, $generatorInput) . $matches[2][$idx];
                $content = str_replace($matches[0][$idx], $finalUrl, $content);
            }
        }

        return $content;
    }
}
<commit_msg>Revert accidental change of regexp in TextUrlRtmReplace<commit_after><?php

namespace Remp\MailerModule\Models\ContentGenerator\Replace;

use Nette\Http\Url;
use Nette\InvalidArgumentException;
use Remp\MailerModule\Models\ContentGenerator\AllowedDomainManager;
use Remp\MailerModule\Models\ContentGenerator\GeneratorInput;

class TextUrlRtmReplace implements IReplace
{
    use RtmReplaceTrait;

    private AllowedDomainManager $allowedDomainManager;

    public function __construct(AllowedDomainManager $allowedDomainManager)
    {
        $this->allowedDomainManager = $allowedDomainManager;
    }

    public function replace(string $content, GeneratorInput $generatorInput): string
    {
        if ($content !== strip_tags($content)) {
            // This replacer is intended to be used only for text emails. HTML is handled by AnchorRtmReplace.
            return $content;
        }

        $matches = [];
        preg_match_all('/(https?:\/\/.+?)(\s)/i', $content, $matches);

        if (count($matches) > 0) {
            foreach ($matches[1] as $idx => $hrefUrl) {
                // parse URL
                try {
                    $url = new Url($hrefUrl);
                } catch (InvalidArgumentException $e) {
                    continue;
                }

                // check if the host is whitelisted
                if (!$this->allowedDomainManager->isAllowed($url->getHost())) {
                    continue;
                }

                $finalUrl = $this->replaceUrl($hrefUrl, $generatorInput) . $matches[2][$idx];
                $content = str_replace($matches[0][$idx], $finalUrl, $content);
            }
        }

        return $content;
    }
}

{'fraction_non_alphanumeric': 0.11157517899761336, 'fraction_numerical': 0.002386634844868735, 'mean_word_length': 2.1902949571836348, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6276830', 'n_tokens_mistral': 911, 'n_tokens_neox': 875, 'n_words': 196}