Skip to content

Commit

Permalink
Merge pull request bcit-ci#5531 from tianhe1986/develop_input_array_n…
Browse files Browse the repository at this point in the history
…otation

Input: considering nested array keys in `get_post` and `post_get`
  • Loading branch information
narfbg authored Jul 22, 2018
2 parents d0d4548 + 8258435 commit 29b2ac0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
10 changes: 4 additions & 6 deletions system/core/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,8 @@ public function post($index = NULL, $xss_clean = FALSE)
*/
public function post_get($index, $xss_clean = FALSE)
{
return isset($_POST[$index])
? $this->post($index, $xss_clean)
: $this->get($index, $xss_clean);
$output = $this->post($index, $xss_clean);
return isset($output) ? $output : $this->get($index, $xss_clean);
}

// --------------------------------------------------------------------
Expand All @@ -228,9 +227,8 @@ public function post_get($index, $xss_clean = FALSE)
*/
public function get_post($index, $xss_clean = FALSE)
{
return isset($_GET[$index])
? $this->get($index, $xss_clean)
: $this->post($index, $xss_clean);
$output = $this->get($index, $xss_clean);
return isset($output) ? $output : $this->post($index, $xss_clean);
}

// --------------------------------------------------------------------
Expand Down
34 changes: 34 additions & 0 deletions tests/codeigniter/core/Input_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@ public function test_post_get()

// --------------------------------------------------------------------

public function test_post_get_array_notation()
{
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST['foo']['bar'] = 'baz';
$barArray = array('bar' => 'baz');

$this->assertEquals('baz', $this->input->get_post('foo[bar]'));
$this->assertEquals($barArray, $this->input->get_post('foo[]'));
$this->assertNull($this->input->get_post('foo[baz]'));

$this->assertEquals('baz', $this->input->post_get('foo[bar]'));
$this->assertEquals($barArray, $this->input->post_get('foo[]'));
$this->assertNull($this->input->post_get('foo[baz]'));
}

// --------------------------------------------------------------------

public function test_get_post()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
Expand All @@ -100,6 +117,23 @@ public function test_get_post()

// --------------------------------------------------------------------

public function test_get_post_array_notation()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_GET['foo']['bar'] = 'baz';
$barArray = array('bar' => 'baz');

$this->assertEquals('baz', $this->input->get_post('foo[bar]'));
$this->assertEquals($barArray, $this->input->get_post('foo[]'));
$this->assertNull($this->input->get_post('foo[baz]'));

$this->assertEquals('baz', $this->input->post_get('foo[bar]'));
$this->assertEquals($barArray, $this->input->post_get('foo[]'));
$this->assertNull($this->input->post_get('foo[baz]'));
}

// --------------------------------------------------------------------

public function test_cookie()
{
$_COOKIE['foo'] = 'bar';
Expand Down

0 comments on commit 29b2ac0

Please sign in to comment.