Skip to content

Commit

Permalink
fix for XOOPS#1468
Browse files Browse the repository at this point in the history
  • Loading branch information
mambax7 committed Jun 9, 2024
1 parent df37d48 commit 91c2360
Showing 1 changed file with 53 additions and 14 deletions.
67 changes: 53 additions & 14 deletions htdocs/class/module.textsanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,13 @@ public function smiley($message)
/**
* Callback to process email address match
*
* @param array $match array of matched elements
* @param array $matches array of matched elements
*
* @return string
*/
protected function makeClickableCallbackEmailAddress($match)
protected function makeClickableCallbackEmailAddress($matches)
{
$email = trim($match[0]);
return '<a href="mailto:' . $email . '" title="' . $email . '">' . $email . '</a>';
return $matches[1] . '<a href="mailto:' . $matches[2] . '">' . $matches[2] . '</a>';
}

/**
Expand All @@ -315,20 +314,60 @@ protected function makeClickableCallbackEmailAddress($match)
*/
public function makeClickable($text)
{
$pattern = "/(^|\s)([-_a-z0-9\'+*$^&%=~!?{}]+(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@[-a-z0-9.]+\.[a-z]{2,6})/i";
$text = preg_replace_callback($pattern, function ($matches) { return $matches[1] . $this->makeClickableCallbackEmailAddress([$matches[2]]); }, $text);

$pattern = '/(?:\s+|^)(https?:\/\/)([-A-Z0-9.\_*?&:;=#\/\[\]\%@]+)/i';
$replacement = '<a href="$1$2" target="_blank" rel="external noopener nofollow">$1$2</a>';
$text = preg_replace($pattern, $replacement, $text);

$pattern = '%(?:\s+|^)(s?ftp://)([-A-Z0-9./_*?&:;=#\[\]\%@]+)%i';
$replacement = '<a href="$1$2" target="_blank" rel="external">$1$2</a>';
$text = preg_replace($pattern, $replacement, $text);
// Convert email addresses into clickable mailto links
$pattern = "/(^|[\s\n])([-_a-z0-9\'+*$^&%=~!?{}]+(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@[-a-z0-9.]+\.[a-z]{2,6})/i";
$text = preg_replace_callback($pattern, [$this, 'makeClickableCallbackEmailAddress'], $text);

// Convert http/https URLs into clickable links
$pattern = "/(^|[\s\n]|[\(\[\{])((https?:\/\/[^\s<>\(\)\[\]]+[^\s<>\(\)\[\]\.,!\"'\(\)\[\]{}<>]))/i";
$text = preg_replace_callback(
$pattern,
function ($matches) {
return $matches[1] . '<a href="' . $matches[2] . '" target="_blank" rel="external noopener nofollow">' . $matches[2] . '</a>';
},
$text
);

// Convert URLs starting with www. into clickable links
$pattern = "/(^|[\s\n]|[\(\[\{])((www\.[^\s<>\(\)\[\]]+[^\s<>\(\)\[\]\.,!\"'\(\)\[\]{}<>]))/i";
$text = preg_replace_callback(
$pattern,
function ($matches) {
return $matches[1] . '<a href="http://' . $matches[2] . '" target="_blank" rel="external noopener nofollow">http://' . $matches[2] . '</a>';
},
$text
);

// Convert ftp URLs into clickable links
$pattern = "/(^|[\s\n]|[\(\[\{])((s?ftp:\/\/[^\s<>\(\)\[\]]+[^\s<>\(\)\[\]\.,!\"'\(\)\[\]{}<>]))/i";
$text = preg_replace_callback(
$pattern,
function ($matches) {
return $matches[1] . '<a href="' . $matches[2] . '" target="_blank" rel="external">' . $matches[2] . '</a>';
},
$text
);

// Convert URLs within angular brackets into clickable links
$pattern = "/(<)(https?:\/\/[^\s>]+)(>)/i";
$text = preg_replace_callback(
$pattern,
function ($matches) {
return $matches[1] . '<a href="' . $matches[2] . '" target="_blank" rel="external noopener nofollow">' . $matches[2] . '</a>' . $matches[3];
},
$text
);

// Ensure consistent handling of newlines by converting them to <br /> tags
$text = nl2br($text);

// Clean up extra newlines
$text = preg_replace('/(<br \/>|<br>)[\n\s]*/', '$1', $text);

return $text;
}


/**
* MyTextSanitizer::truncate()
*
Expand Down

0 comments on commit 91c2360

Please sign in to comment.