mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-04-18 09:52:17 +09:00
Implement URL::modifyURL()
This commit is contained in:
parent
de0d49b9f3
commit
13a67f3496
2 changed files with 55 additions and 10 deletions
|
|
@ -21,19 +21,15 @@ class URL
|
|||
$proto = \RX_SSL ? 'https://' : 'http://';
|
||||
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost';
|
||||
$local = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/';
|
||||
$url = $proto . $host . $local;
|
||||
if (count($changes))
|
||||
{
|
||||
if (($qpos = strpos($local, '?')) !== false)
|
||||
{
|
||||
$querystring = substr($local, $qpos + 1);
|
||||
$local = substr($local, 0, $qpos);
|
||||
parse_str($querystring, $args);
|
||||
$changes = array_merge($args, $changes);
|
||||
}
|
||||
$changes = array_filter($changes, function($val) { return $val !== null; });
|
||||
$local = $local . '?' . http_build_query($changes);
|
||||
return self::modifyURL($url, $changes);
|
||||
}
|
||||
else
|
||||
{
|
||||
return self::getCanonicalURL($url);
|
||||
}
|
||||
return self::getCanonicalURL($proto . $host) . $local;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -102,6 +98,33 @@ class URL
|
|||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify a URL.
|
||||
*
|
||||
* If $changes are given, they will be appended to the current URL as a query string.
|
||||
* To delete an existing query string, set its value to null.
|
||||
*
|
||||
* @param string $url
|
||||
* @param array $changes
|
||||
* @return string
|
||||
*/
|
||||
public static function modifyURL($url, array $changes = array())
|
||||
{
|
||||
$url = parse_url(self::getCanonicalURL($url));
|
||||
$prefix = sprintf('%s://%s%s%s', $url['scheme'], $url['host'], ($url['port'] ? (':' . $url['port']) : ''), $url['path']);
|
||||
parse_str($url['query'], $args);
|
||||
$changes = array_merge($args, $changes);
|
||||
$changes = array_filter($changes, function($val) { return $val !== null; });
|
||||
if (count($changes))
|
||||
{
|
||||
return $prefix . '?' . http_build_query($changes);
|
||||
}
|
||||
else
|
||||
{
|
||||
return $prefix;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode UTF-8 domain into IDNA (punycode)
|
||||
*
|
||||
|
|
|
|||
|
|
@ -18,6 +18,9 @@ class URLTest extends \Codeception\TestCase\Test
|
|||
// Removing item from the query string
|
||||
$this->assertEquals($protocol . $_SERVER['HTTP_HOST'] . '/index.php?xe=sucks', Rhymix\Framework\URL::getCurrentURL(array('foo' => null)));
|
||||
|
||||
// Removing all items from the query string
|
||||
$this->assertEquals($protocol . $_SERVER['HTTP_HOST'] . '/index.php', Rhymix\Framework\URL::getCurrentURL(array('foo' => null, 'xe' => null)));
|
||||
|
||||
// Adding and removing parameters at the same time
|
||||
$this->assertEquals($protocol . $_SERVER['HTTP_HOST'] . '/index.php?xe=sucks&l=ko', Rhymix\Framework\URL::getCurrentURL(array('l' => 'ko', 'foo' => null)));
|
||||
}
|
||||
|
|
@ -55,6 +58,25 @@ class URLTest extends \Codeception\TestCase\Test
|
|||
}
|
||||
}
|
||||
|
||||
public function testModifyURL()
|
||||
{
|
||||
$protocol = \RX_SSL ? 'https://' : 'http://';
|
||||
$_SERVER['HTTP_HOST'] = 'www.rhymix.org';
|
||||
$url = $protocol . $_SERVER['HTTP_HOST'] . \RX_BASEURL . 'index.php?foo=bar';
|
||||
|
||||
// Conversion to absolute
|
||||
$this->assertEquals($url, Rhymix\Framework\URL::modifyURL('./index.php?foo=bar'));
|
||||
|
||||
// Adding items to the query string
|
||||
$this->assertEquals($url . '&var=1&arr%5B0%5D=2&arr%5B1%5D=3', Rhymix\Framework\URL::modifyURL($url, array('var' => '1', 'arr' => array(2, 3))));
|
||||
|
||||
// Removing item from the query string
|
||||
$this->assertEquals($protocol . $_SERVER['HTTP_HOST'] . \RX_BASEURL . 'index.php', Rhymix\Framework\URL::modifyURL($url, array('foo' => null)));
|
||||
|
||||
// Adding and removing parameters at the same time
|
||||
$this->assertEquals($protocol . $_SERVER['HTTP_HOST'] . \RX_BASEURL . 'index.php?l=ko', Rhymix\Framework\URL::modifyURL($url, array('l' => 'ko', 'foo' => null)));
|
||||
}
|
||||
|
||||
public function testIsInternalURL()
|
||||
{
|
||||
// This function is checked in Security::checkCSRF()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue