mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENHANCEMENT Added Director::is_absolute_url()
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@71707 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
569447d2ba
commit
74ab33f23b
@ -462,6 +462,20 @@ class Director {
|
|||||||
return preg_match('/^[a-zA-Z]:[\\\\\/]/', $path) == 1;
|
return preg_match('/^[a-zA-Z]:[\\\\\/]/', $path) == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a given URL is absolute (e.g. starts with 'http://' etc.).
|
||||||
|
*
|
||||||
|
* @param string $url
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public static function is_absolute_url($url) {
|
||||||
|
$url = trim($url);
|
||||||
|
// remove all query strings to avoid parse_url choking on URLs like 'test.com?url=http://test.com'
|
||||||
|
$url = preg_replace('/.*(\?.*)/', '', $url);
|
||||||
|
$parsed = parse_url($url);
|
||||||
|
return (isset($parsed['scheme']));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Given a filesystem reference relative to the site root, return the full file-system path.
|
* Given a filesystem reference relative to the site root, return the full file-system path.
|
||||||
*
|
*
|
||||||
|
@ -68,5 +68,16 @@ class DirectorTest extends SapphireTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testIsAbsoluteUrl() {
|
||||||
|
$this->assertTrue(Director::is_absolute_url('http://test.com'));
|
||||||
|
$this->assertTrue(Director::is_absolute_url('https://test.com'));
|
||||||
|
$this->assertTrue(Director::is_absolute_url(' https://test.com/testpage '));
|
||||||
|
$this->assertFalse(Director::is_absolute_url('test.com/testpage'));
|
||||||
|
$this->assertTrue(Director::is_absolute_url('ftp://test.com'));
|
||||||
|
$this->assertFalse(Director::is_absolute_url('/relative'));
|
||||||
|
$this->assertFalse(Director::is_absolute_url('relative'));
|
||||||
|
$this->assertFalse(Director::is_absolute_url('/relative/?url=http://test.com'));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
?>
|
?>
|
Loading…
Reference in New Issue
Block a user