Merge pull request #1360 from kinglozzer/1359-link-tracking-regex

FIX: preg_quote() anchors in SiteTreeLinkTracking (fixes #1359)
This commit is contained in:
Damian Mooyman 2016-01-12 11:03:32 +13:00
commit b1387641bf
2 changed files with 10 additions and 5 deletions

View File

@ -217,14 +217,18 @@ class SiteTreeLinkTracking_Parser {
$matches = array();
if(preg_match('/\[sitetree_link(?:\s*|%20|,)?id=([0-9]+)\](#(.*))?/i', $href, $matches)) {
$page = DataObject::get_by_id('SiteTree', $matches[1]);
$broken = false;
if (!$page) {
// Page doesn't exist.
$broken = true;
} else if (!empty($matches[3]) && !preg_match("/(name|id)=\"{$matches[3]}\"/", $page->Content)) {
// Broken anchor on the target page.
$broken = true;
} else {
$broken = false;
} else if (!empty($matches[3])) {
$anchor = preg_quote($matches[3], '/');
if (!preg_match("/(name|id)=\"{$anchor}\"/", $page->Content)) {
// Broken anchor on the target page.
$broken = true;
}
}
$results[] = array(

View File

@ -32,6 +32,7 @@ class SiteTreeLinkTrackingTest extends SapphireTest {
$this->assertFalse($this->isBroken("<a href=\"[sitetree_link,id=$page->ID]#yes-name-anchor\">link</a>"));
$this->assertFalse($this->isBroken("<a href=\"[sitetree_link,id=$page->ID]#yes-id-anchor\">link</a>"));
$this->assertFalse($this->isBroken("<a href=\"[file_link,id=$file->ID]\">link</a>"));
$this->assertTrue($this->isBroken("<a href=\"[sitetree_link,id=$page->ID]#http://invalid-anchor.com\"></a>"));
}
function highlight($content) {