Skip to content

Commit

Permalink
Homepage|Build Repository: Fixed URL rewriting in commit messages
Browse files Browse the repository at this point in the history
Replaced the commit message URL rewriting rules with a set derived
from Søren Løvborg's UrlLinker, cheers! (See: http://www.kwi.dk/projects/php/UrlLinker/)
  • Loading branch information
danij-deng committed Sep 4, 2012
1 parent d7fd3d1 commit 6057de2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
14 changes: 9 additions & 5 deletions web/includes/utilities.inc.php
Expand Up @@ -230,7 +230,7 @@ function json_encode_clean(&$array, $flags=0, $indent_level=0)
return $result;
}

function generateHyperlinkHTML($uri, $maxLength=40, $cssClass=NULL)
function generateHyperlinkHTML($uri, $attributes = array(), $maxLength = 40)
{
if($uri instanceof Url)
{
Expand All @@ -257,11 +257,15 @@ function generateHyperlinkHTML($uri, $maxLength=40, $cssClass=NULL)
else
$shortUri = $uri;

$html = '<a';
if(strlen($cssClass) > 0)
$attribs = '';
if(is_array($attributes))
{
$html .= " class={$cssClass}";
foreach($attributes as $attribute => $value)
{
$attribs .= " {$attribute}=\"{$value}\"";
}
}
$html .= " href=\"{$uri}\">". htmlspecialchars($shortUri) .'</a>';

$html = "<a href=\"{$uri}\"{$attribs}>". htmlspecialchars($shortUri) .'</a>';
return $html;
}
16 changes: 14 additions & 2 deletions web/plugins/buildrepository/commitutils.php
Expand Up @@ -79,7 +79,7 @@ function make_pretty_hyperlink($matches)
$uri->setScheme()->setHost();
}

return generateHyperlinkHTML($uri, 40, $isExternal? 'link-external' : NULL);
return generateHyperlinkHTML($uri, $isExternal? array('class'=>'link-external') : NULL, 40);
}

function formatCommitHTML($msg)
Expand All @@ -89,7 +89,19 @@ function formatCommitHTML($msg)
// Process the commit message, replacing web URIs with clickable links.
htmlspecialchars($msg);

$msg = preg_replace_callback('/((?:[\w\d]+\:\/\/)?(?:[\w\-\d]+\.)+[\w\-\d]+(?:\/[\w\-\d]+)*(?:\/|\.[\w\-\d]+)?(?:\?[\w\-\d]+\=[\w\-\d]+\&?)?(?:\#[\w\-\d]*)?)/',
// Rules derived from Søren Løvborg's UrlLinker (see: http://www.kwi.dk/projects/php/UrlLinker/)
$rexProtocol = '((http|https|ftp)\:\/\/)';
$rexDomain = '(?:[-a-zA-Z0-9]{1,63}\.)+[a-zA-Z][-a-zA-Z0-9]{1,62}';
$rexIp = '(?:[1-9][0-9]{0,2}\.|0\.){3}(?:[1-9][0-9]{0,2}|0)';
$rexPort = '(:[0-9]{1,5})?';
$rexPath = '(/[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]*?)?';
$rexQuery = '(\?[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]+?)?';
$rexFragment = '(#[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]+?)?';
$rexUsername = '[^]\\\\\x00-\x20\"(),:-<>[\x7f-\xff]{1,64}';
$rexPassword = $rexUsername; // allow the same characters as in the username
$rexUrl = "$rexProtocol(?:($rexUsername)(:$rexPassword)?@)?($rexDomain|$rexIp)($rexPort$rexPath$rexQuery$rexFragment)";

$msg = preg_replace_callback("&\\b$rexUrl(?=[?.!,;:\"]?(\s|$))&",
"make_pretty_hyperlink", $msg);

return $msg;
Expand Down

0 comments on commit 6057de2

Please sign in to comment.