[aur-dev] [PATCH] Make external links in comments clickable (FS#20137).

Lukas Fleischer archlinux at cryptocrack.de
Sun Oct 3 15:51:45 EDT 2010


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.

---
 web/lib/aur.inc               |   24 ++++++++++++++++++++++++
 web/template/pkg_comments.php |    2 +-
 2 files changed, 25 insertions(+), 1 deletions(-)

diff --git a/web/lib/aur.inc b/web/lib/aur.inc
index bd69c4c..a6292ca 100644
--- a/web/lib/aur.inc
+++ b/web/lib/aur.inc
@@ -494,3 +494,27 @@ function salted_hash($passwd, $salt)
 	}
 	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;
+}
diff --git a/web/template/pkg_comments.php b/web/template/pkg_comments.php
index 02171a0..2ca9bf0 100644
--- a/web/template/pkg_comments.php
+++ b/web/template/pkg_comments.php
@@ -20,7 +20,7 @@ while (list($indx, $carr) = each($comments)) { ?>
 ?></div>
 	<blockquote class="comment-body">
 	<div>
-<?php echo nl2br(htmlspecialchars($carr['Comments'])) ?>
+<?php echo parse_comment($carr['Comments']) ?>
 	</div>
 	</blockquote>
 <?php
-- 
1.7.3.1



More information about the aur-dev mailing list