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

PyroPeter abi1789 at googlemail.com
Fri Oct 1 10:59:22 EDT 2010


On 09/30/2010 05:22 PM, Lukas Fleischer wrote:
> This is a bit hacky patch to make links in AUR comments clickable (fixes
> FS#20137 [1]). Huge parts of this code are ripped from the DokuWiki
> plugin that is also used in Flyspray.
>
> I didn't have any time to test it extensively so I'd suggest to do some
> more tests if this will be commited.

I fixed the </> bugs and changed the indention to tabs.
I also changed the regex to one that accepts everything
that starts with one of 'http', 'https' or 'ftp' followed by
a colon ":", contains no whitespace and ends with a letter (\w)
or a slash "/".
It also parses hostnames starting with "www." and ending
in a 2 to 5 letters long TLD consisting of only a-z.

I have tested it with the comments at [0].

Regards, PyroPeter

[0] http://aur.keks.selfip.org/packages.php?ID=298
---
diff --git a/web/lib/aur.inc b/web/lib/aur.inc
index bd69c4c..7465fac 100644
--- a/web/lib/aur.inc
+++ b/web/lib/aur.inc
@@ -494,3 +494,24 @@ function salted_hash($passwd, $salt)
  	}
  	return md5($salt . $passwd);
  }
+
+function parse_link($matches)
+{
+	$name = $matches[0];
+	$url = preg_replace('/^www\./', 'http://www.', $matches[0]);
+	return '<a href="' . $url . '">' . $name . '</a>';
+}
+
+function parse_comment($comment)
+{
+	$regex = '/' . str_replace('/', '\/', implode('|',array(
+		'\b(http|https|ftp):[^\s]*[\w\d/]',
+		'\bwww\.[^\s/]+\.[a-z]{2,5}'
+	))) . '/S';
+
+	$comment = htmlspecialchars($comment);
+	$comment = preg_replace_callback($regex, 'parse_link', $comment);
+	$comment = nl2br($comment);
+
+	return $comment;
+}
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

-- 
freenode/pyropeter                          "12:50 - Ich drücke Return."


More information about the aur-dev mailing list