From 63de49eab1edeaa1c65b01e019f8aedd7f8645f0 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Mon, 5 Jan 2009 04:52:01 +0000 Subject: [PATCH] BUGFIX #3224 ajshort: Get HTTP::setGetVar() working with variables that contain array indexes git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@69700 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/HTTP.php | 17 +++++++---------- tests/HTTPTest.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 tests/HTTPTest.php diff --git a/core/HTTP.php b/core/HTTP.php index 10ef477f0..7e9f06caa 100644 --- a/core/HTTP.php +++ b/core/HTTP.php @@ -73,16 +73,13 @@ class HTTP { return $content; } - static function setGetVar($varname, $varvalue, $currentURL = null) { - $currentURL = $currentURL ? $currentURL : $_SERVER['REQUEST_URI']; - - $scriptbase = $currentURL; - $scriptbase = str_replace('&','&',$scriptbase); - - $scriptbase = ereg_replace("&$varname=[^&]*",'',$scriptbase); - $scriptbase = ereg_replace("\?$varname=[^&]*&",'?',$scriptbase); - $scriptbase = ereg_replace("\?$varname=[^&]*",'',$scriptbase); - + public static function setGetVar($varname, $varvalue, $currentURL = null) { + $scriptbase = $currentURL ? $currentURL : $_SERVER['REQUEST_URI']; + + $scriptbase = str_replace('&', '&', $scriptbase); + $scriptbase = preg_replace('/\?' . quotemeta($varname) . '=([^&]*)&/', '?', $scriptbase); + $scriptbase = preg_replace('/([\?&]+)' . quotemeta($varname) . '=([^&]*)/', null, $scriptbase); + $suffix = ''; if(($hashPos = strpos($scriptbase,'#')) !== false) { $suffix .= substr($scriptbase, $hashPos); diff --git a/tests/HTTPTest.php b/tests/HTTPTest.php new file mode 100644 index 000000000..15b4fbc5f --- /dev/null +++ b/tests/HTTPTest.php @@ -0,0 +1,29 @@ + array('foo', 'bar', '/'), + '/?baz=buz&foo=bar' => array('foo', 'bar', '/?baz=buz'), + '/?buz=baz&foo=baz' => array('foo', 'baz', '/?foo=bar&buz=baz'), + '/?foo=var' => array('foo', 'var', '/?foo=&foo=bar'), + '/?foo[test]=var' => array('foo[test]', 'var', '/?foo[test]=another') + ); + + foreach($expected as $result => $args) { + $this->assertEquals( + call_user_func_array(array('HTTP', 'setGetVar'), $args), str_replace('&', '&', $result) + ); + } + } + +}