Better handling of params with more complex Urls - #859

This commit is contained in:
Andy Miller
2016-05-30 16:41:07 -06:00
parent 1c12bb5fc1
commit 7fc2f20f1b
3 changed files with 34 additions and 25 deletions

View File

@@ -10,6 +10,7 @@
* Fix for saving fields in expert mode that have no `Validation::typeX()` methods [#626](https://github.com/getgrav/grav-plugin-admin/issues/626)
* Detect if user really meant to extend parent blueprint, not another one (fixes old page type blueprints)
* Fixed a bug in `Page::relativePagePath()` when `Page::$name` is not defined
* Fix for poor handling of params + query element in `Uri::processParams()` [#859](https://github.com/getgrav/grav/issues/859)
# v1.1.0-beta.5
## 05/23/2016

View File

@@ -12,6 +12,7 @@ use Grav\Common\Page\Page;
class Uri
{
const HOSTNAME_REGEX = '/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/';
const PARAMS_REGEX = '/\/([^\:\#\/\?]*:[^\:\#\/\?]*)/';
public $url;
@@ -349,22 +350,17 @@ class Uri
private function processParams($uri, $delimiter = ':')
{
if (strpos($uri, $delimiter) !== false) {
$bits = explode('/', $uri);
$path = [];
foreach ($bits as $bit) {
if (strpos($bit, $delimiter) !== false) {
$param = explode($delimiter, $bit);
if (count($param) == 2) {
$plain_var = filter_var(rawurldecode($param[1]), FILTER_SANITIZE_STRING);
$this->params[$param[0]] = $plain_var;
}
} else {
$path[] = $bit;
preg_match_all(Uri::PARAMS_REGEX, $uri, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$param = explode($delimiter, $match[1]);
if (count($param) == 2) {
$plain_var = filter_var(rawurldecode($param[1]), FILTER_SANITIZE_STRING);
$this->params[$param[0]] = $plain_var;
$uri = str_replace($match[0], '', $uri);
}
}
$uri = '/' . ltrim(implode('/', $path), '/');
}
return $uri;
}
@@ -941,20 +937,16 @@ class Uri
$params = [];
if (strpos($uri, $delimiter) !== false) {
$bits = explode('/', $uri);
$path = [];
foreach ($bits as $bit) {
if (strpos($bit, $delimiter) !== false) {
$param = explode($delimiter, $bit);
if (count($param) == 2) {
$plain_var = filter_var(rawurldecode($param[1]), FILTER_SANITIZE_STRING);
$params[$param[0]] = $plain_var;
}
} else {
$path[] = $bit;
preg_match_all(Uri::PARAMS_REGEX, $uri, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$param = explode($delimiter, $match[1]);
if (count($param) == 2) {
$plain_var = filter_var(rawurldecode($param[1]), FILTER_SANITIZE_STRING);
$params[$param[0]] = $plain_var;
$uri = str_replace($match[0], '', $uri);
}
}
$uri = '/' . ltrim(implode('/', $path), '/');
}
return [$uri, $params];

View File

@@ -148,6 +148,18 @@ class UriTest extends \Codeception\TestCase\Test
$this->assertSame('/ueper:xxx/test:yyy', $this->uri->params());
$this->assertSame('/ueper:xxx', $this->uri->params('ueper'));
$this->assertSame('/test:yyy', $this->uri->params('test'));
$this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper:xxx++/test:yyy')->init();
$this->assertSame('/ueper:xxx++/test:yyy', $this->uri->params());
$this->assertSame('/ueper:xxx++', $this->uri->params('ueper'));
$this->assertSame('/test:yyy', $this->uri->params('test'));
$this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper:xxx++/test:yyy#something')->init();
$this->assertSame('/ueper:xxx++/test:yyy', $this->uri->params());
$this->assertSame('/ueper:xxx++', $this->uri->params('ueper'));
$this->assertSame('/test:yyy', $this->uri->params('test'));
$this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper:xxx++/test:yyy?foo=bar')->init();
$this->assertSame('/ueper:xxx++/test:yyy', $this->uri->params());
$this->assertSame('/ueper:xxx++', $this->uri->params('ueper'));
$this->assertSame('/test:yyy', $this->uri->params('test'));
$this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper?test=x')->init();
$this->assertSame(null, $this->uri->params());
$this->assertSame(null, $this->uri->params('ueper'));
@@ -175,6 +187,10 @@ class UriTest extends \Codeception\TestCase\Test
$this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper:xxx/test:yyy')->init();
$this->assertSame('xxx', $this->uri->param('ueper'));
$this->assertSame('yyy', $this->uri->param('test'));
$this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper:xxx++/test:yy%20y/foo:bar_baz-bank')->init();
$this->assertSame('xxx++', $this->uri->param('ueper'));
$this->assertSame('yy y', $this->uri->param('test'));
$this->assertSame('bar_baz-bank', $this->uri->param('foo'));
}
public function testFragment()