Make external links in comments clickable (FS#20137).

Comments are now split at link boundaries and links are converted
separately. I find this to be a much cleaner way than re-converting
comments that have already been converted using htmlspecialchars(). This
also doesn't require any callback procedure.

Signed-off-by: Lukas Fleischer <archlinux@cryptocrack.de>
This commit is contained in:
Lukas Fleischer 2010-10-03 21:51:45 +02:00
parent a417224988
commit 60edcd04e5
2 changed files with 25 additions and 1 deletions

View file

@ -494,3 +494,27 @@ function salted_hash($passwd, $salt)
} }
return md5($salt . $passwd); return md5($salt . $passwd);
} }
function parse_comment($comment)
{
$url_pattern = '/(\b(?:https?|ftp):\/\/[\w\/\#~:.?+=&%@!\-;,]+?' .
'(?=[.:?\-;,]*(?:[^\w\/\#~:.?+=&%@!\-;,]|$)))/iS';
$matches = preg_split($url_pattern, $comment, -1,
PREG_SPLIT_DELIM_CAPTURE);
$html = '';
for ($i = 0; $i < count($matches); $i++) {
if ($i % 2) {
# convert links
$html .= '<a href="' . htmlspecialchars($matches[$i]) .
'">' . htmlspecialchars($matches[$i]) . '</a>';
}
else {
# convert everything else
$html .= nl2br(htmlspecialchars($matches[$i]));
}
}
return $html;
}

View file

@ -25,7 +25,7 @@ while (list($indx, $carr) = each($comments)) { ?>
?></div> ?></div>
<blockquote class="comment-body"> <blockquote class="comment-body">
<div> <div>
<?php echo nl2br(htmlspecialchars($carr['Comments'])) ?> <?php echo parse_comment($carr['Comments']) ?>
</div> </div>
</blockquote> </blockquote>
<?php <?php