From bc5f71afc81ec0473368759dfba9e9b46811c831 Mon Sep 17 00:00:00 2001 From: bermiferrer Date: Mon, 15 Oct 2007 18:58:38 +0000 Subject: [PATCH] Fixing file upload params ($_FILES) normalization. Closes #75 git-svn-id: http://svn.akelos.org/trunk@405 a2fa5c27-f921-0410-a72c-bf682d381be0 --- lib/AkRequest.php | 51 ++++++------- test/unit/lib/AkRequest.php | 102 ++++++++++++++++++-------- test/unit/lib/utils/_Ak_object_inspection.php | 2 +- 3 files changed, 94 insertions(+), 61 deletions(-) diff --git a/lib/AkRequest.php b/lib/AkRequest.php index d897378..a0b8b69 100644 --- a/lib/AkRequest.php +++ b/lib/AkRequest.php @@ -599,34 +599,28 @@ class AkRequest extends AkObject foreach ($params as $name=>$details){ if(is_array($details) && !empty($details['name']) && !empty($details['tmp_name']) && !empty($details['size'])){ - - if( is_array($details['tmp_name']) && - ( - count($details['tmp_name']) == 1 && - !is_array(array_shift(array_values($details['tmp_name'])))) - ){ - - foreach (array_keys($details['tmp_name']) as $k){ - if(UPLOAD_ERR_NO_FILE != $details['error'][$k]){ - $result[$name][$k] = array( - 'name'=>$details['name'][$k], - 'tmp_name'=>$details['tmp_name'][$k], - 'size'=>$details['size'][$k], - 'type'=>$details['type'][$k], - 'error'=>$details['error'][$k], - ); - } - } - }elseif(is_array($details['tmp_name'])){ + if(is_array($details['tmp_name'])){ foreach ($details['tmp_name'] as $item=>$item_details){ - foreach (array_keys($item_details) as $k){ - if(UPLOAD_ERR_NO_FILE != $details['error'][$item][$k]){ - $result[$name][$item][$k] = array( - 'name'=>$details['name'][$item][$k], - 'tmp_name'=>$details['tmp_name'][$item][$k], - 'size'=>$details['size'][$item][$k], - 'type'=>$details['type'][$item][$k], - 'error'=>$details['error'][$item][$k], + if(is_array($item_details)){ + foreach (array_keys($item_details) as $k){ + if(UPLOAD_ERR_NO_FILE != $details['error'][$item][$k]){ + $result[$name][$item][$k] = array( + 'name'=>$details['name'][$item][$k], + 'tmp_name'=>$details['tmp_name'][$item][$k], + 'size'=>$details['size'][$item][$k], + 'type'=>$details['type'][$item][$k], + 'error'=>$details['error'][$item][$k], + ); + } + } + }else{ + if(UPLOAD_ERR_NO_FILE != $details['error'][$item]){ + $result[$name][$item] = array( + 'name'=>$details['name'][$item], + 'tmp_name'=>$details['tmp_name'][$item], + 'size'=>$details['size'][$item], + 'type'=>$details['type'][$item], + 'error'=>$details['error'][$item], ); } } @@ -648,8 +642,7 @@ class AkRequest extends AkObject return $result; } - - + // {{{ _addParams() /** diff --git a/test/unit/lib/AkRequest.php b/test/unit/lib/AkRequest.php index 4fa5a67..3945f9a 100755 --- a/test/unit/lib/AkRequest.php +++ b/test/unit/lib/AkRequest.php @@ -6,7 +6,6 @@ require_once(AK_LIB_DIR.DS.'AkRequest.php'); class Test_of_AkRequest_Class extends UnitTestCase { - var $test_request; var $test_request2; var $test_request3; @@ -58,18 +57,18 @@ class Test_of_AkRequest_Class extends UnitTestCase 'session_param'=>'cmd', 'general_param'=>'cmd' ); - - - + + + $this->_testRequestInstance =& new AkRequest(); $this->_testRequestInstance->init(); - + } function tearDown() { unset($this->_testRequestInstance); - + //We reset the original values $_SESSION['request'] = $this->_original_values[0]; $_COOKIE = $this->_original_values[1]; @@ -82,7 +81,7 @@ class Test_of_AkRequest_Class extends UnitTestCase function Test_mergeRequest() { $gpc_param = get_magic_quotes_gpc() ? "Isn't it ironic" : 'Isn\\\'t it ironic'; - + $expected = array( 'cmd_param'=>'cmd', 'get_param'=>'get', @@ -93,9 +92,9 @@ class Test_of_AkRequest_Class extends UnitTestCase 'ak'=>'/session_controller/session_action', 'gpc_param'=>$gpc_param, ); - + $this->_testRequestInstance->_mergeRequest(); - + $this->assertEqual($this->_testRequestInstance->_request,$expected,'Comparing request precedence'); } @@ -103,80 +102,121 @@ class Test_of_AkRequest_Class extends UnitTestCase function Test_parseAkRequestString() { $expected_values = array('user','list','100'); - + $this->assertEqual($this->_testRequestInstance->_parseAkRequestString('/user/list/100'), $expected_values); $this->assertEqual($this->_testRequestInstance->_parseAkRequestString('/user/list/100/'), $expected_values); $this->assertEqual($this->_testRequestInstance->_parseAkRequestString('user/list/100/'), $expected_values); $this->assertEqual($this->_testRequestInstance->_parseAkRequestString('user/list/100'), $expected_values); - + $expected_keys = array('controller','action','id'); $this->assertEqual($this->_testRequestInstance->_parseAkRequestString('/:controller/:action/:id','/:'), $expected_keys); $this->assertEqual($this->_testRequestInstance->_parseAkRequestString('/:controller/:action/:id/:','/:'), $expected_keys); $this->assertEqual($this->_testRequestInstance->_parseAkRequestString('controller/:action/:id/:','/:'), $expected_keys); $this->assertEqual($this->_testRequestInstance->_parseAkRequestString('controller/:action/:id','/:'), $expected_keys); } - - - + + + function test_for_getRemoteIp() { $Request = new AkRequest(); - + $Request->env = array('HTTP_CLIENT_IP'=>'64.68.15.10'); $this->assertEqual($Request->getRemoteIp(),'64.68.15.10'); - + $Request->env = array('HTTP_X_FORWARDED_FOR'=>'64.68.15.11'); $this->assertEqual($Request->getRemoteIp(),'64.68.15.11'); - + $Request->env = array('HTTP_X_FORWARDED_FOR'=>'364.68.15.11'); $this->assertEqual($Request->getRemoteIp(),''); - + $Request->env = array('HTTP_X_FORWARDED_FOR'=>'3e4f:123f:c12a:5566:888e:9975:aaff:2344'); $this->assertEqual($Request->getRemoteIp(),'3e4f:123f:c12a:5566:888e:9975:aaff:2344'); - + $Request->env = array('HTTP_X_FORWARDED_FOR'=>'3e4f:123f:c12a:5566:888e:9975:aafg:2344'); $this->assertEqual($Request->getRemoteIp(),''); $Request->env = array('HTTP_X_FORWARDED_FOR'=>'364.68.15.11,64.68.15.11'); $this->assertEqual($Request->getRemoteIp(),'64.68.15.11'); - + $Request->env = array('REMOTE_ADDR'=>'64.68.15.11'); $this->assertEqual($Request->getRemoteIp(),'64.68.15.11'); } - + function test_for_getDomain() { $Request = new AkRequest(); - + $env_backup = $Request->env; - + $Request->env['SERVER_NAME'] = 'localhost'; $Request->env['SERVER_ADDR'] = '127.0.0.1'; - + $this->assertEqual($Request->getDomain(), 'localhost'); - + $Request->_host = 'www.dev.akelos.com'; $this->assertEqual($Request->getDomain(),'akelos.com'); $this->assertEqual($Request->getDomain(2),'dev.akelos.com'); - + $Request->env = $env_backup; } - + function test_for_getSubDomains() { $Request = new AkRequest(); - + $env_backup = $Request->env; - + $Request->_host = 'www.dev.akelos.com'; - + $this->assertEqual($Request->getSubdomains(), array('www','dev')); $this->assertEqual($Request->getSubdomains(2),array('www')); - + $Request->env = $env_backup; } + + function test_should_normalize_single_level_file_uploads() + { + $Request = new AkRequest(); + $_FILES = array ( + 'image0' => array ( 'name' => 'mod_rewrite_cheat_sheet.pdf', 'type' => 'application/pdf', 'tmp_name' => '/tmp/php0JvZ0p', 'error' => 0, 'size' => 332133 ), + 'image1' => array ( 'name' => 'microformats_cheat_sheet.pdf', 'type' => 'application/pdf', 'tmp_name' => '/tmp/phpKry1xs', 'error' => 0, 'size' => 427735 ), + 'image2' => array ( 'name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0 ) ); + $normalized = array ( + 'image0' => array ( 'name' => 'mod_rewrite_cheat_sheet.pdf', 'type' => 'application/pdf', 'tmp_name' => '/tmp/php0JvZ0p', 'error' => 0, 'size' => 332133 ), + 'image1' => array ( 'name' => 'microformats_cheat_sheet.pdf', 'type' => 'application/pdf', 'tmp_name' => '/tmp/phpKry1xs', 'error' => 0, 'size' => 427735 )); + + $this->assertEqual($Request->_getNormalizedFilesArray(), $normalized); + } + + function test_should_normalize_multi_level_but_flat_file_uploads() + { + $Request = new AkRequest(); + $_FILES = array ( 'image' => array ( 'name' => array ( 'file' => array ( 'a' => 'mod_rewrite_cheat_sheet.pdf', 'b' => 'microformats_cheat_sheet.pdf', 'c' => '', ), ), 'type' => array ( 'file' => array ( 'a' => 'application/pdf', 'b' => 'application/pdf', 'c' => '', ), ), 'tmp_name' => array ( 'file' => array ( 'a' => '/tmp/phporGMwx', 'b' => '/tmp/phpGycyd6', 'c' => '', ), ), 'error' => array ( 'file' => array ( 'a' => 0, 'b' => 0, 'c' => 4, ), ), 'size' => array ( 'file' => array ( 'a' => 332133, 'b' => 427735, 'c' => 0, ), ), ), ); + $normalized = array ( 'image' => array ( 'file' => array ( 'a' => array ( 'name' => 'mod_rewrite_cheat_sheet.pdf', 'tmp_name' => '/tmp/phporGMwx', 'size' => 332133, 'type' => 'application/pdf', 'error' => 0, ), 'b' => array ( 'name' => 'microformats_cheat_sheet.pdf', 'tmp_name' => '/tmp/phpGycyd6', 'size' => 427735, 'type' => 'application/pdf', 'error' => 0, ), ), ), ); + + $this->assertEqual($Request->_getNormalizedFilesArray(), $normalized); + } + + function test_should_normalize_multi_level_as_array_file_uploads() + { + $Request = new AkRequest(); + $_FILES = array ( 'image' => array ( 'name' => array ( 'file' => array ( 0 => 'mod_rewrite_cheat_sheet.pdf', 1 => 'microformats_cheat_sheet.pdf', 2 => '', ), ), 'type' => array ( 'file' => array ( 0 => 'application/pdf', 1 => 'application/pdf', 2 => '', ), ), 'tmp_name' => array ( 'file' => array ( 0 => '/tmp/phpoOcNXs', 1 => '/tmp/php4xVEbv', 2 => '', ), ), 'error' => array ( 'file' => array ( 0 => 0, 1 => 0, 2 => 4, ), ), 'size' => array ( 'file' => array ( 0 => 332133, 1 => 427735, 2 => 0, ), ), ), ); + $normalized = array ( 'image' => array ( 'file' => array ( 0 => array ( 'name' => 'mod_rewrite_cheat_sheet.pdf', 'tmp_name' => '/tmp/phpoOcNXs', 'size' => 332133, 'type' => 'application/pdf', 'error' => 0, ), 1 => array ( 'name' => 'microformats_cheat_sheet.pdf', 'tmp_name' => '/tmp/php4xVEbv', 'size' => 427735, 'type' => 'application/pdf', 'error' => 0, ), ), ), ); + + $this->assertEqual($Request->_getNormalizedFilesArray(), $normalized); + } + + function test_should_normalize_simple_level_as_array_file_uploads() + { + $Request = new AkRequest(); + $_FILES = array ( 'image' => array ( 'name' => array ( 0 => 'mod_rewrite_cheat_sheet.pdf', 1 => 'microformats_cheat_sheet.pdf', 2 => '', ), 'type' => array ( 0 => 'application/pdf', 1 => 'application/pdf', 2 => '', ), 'tmp_name' => array ( 0 => '/tmp/phpXpfUKA', 1 => '/tmp/phpkB6MnX', 2 => '', ), 'error' => array ( 0 => 0, 1 => 0, 2 => 4, ), 'size' => array ( 0 => 332133, 1 => 427735, 2 => 0, ), ), ); + $normalized = array ('image' => array (0 => array ('name' => 'mod_rewrite_cheat_sheet.pdf','tmp_name' => '/tmp/phpXpfUKA','size' => 332133,'type' => 'application/pdf','error' => 0),1 => array ('name' => 'microformats_cheat_sheet.pdf','tmp_name' => '/tmp/phpkB6MnX','size' => 427735,'type' => 'application/pdf','error' => 0))); + $this->assertEqual($Request->_getNormalizedFilesArray(), $normalized); + } + } ak_test('Test_of_AkRequest_Class'); diff --git a/test/unit/lib/utils/_Ak_object_inspection.php b/test/unit/lib/utils/_Ak_object_inspection.php index d61b2ef..f570ef1 100755 --- a/test/unit/lib/utils/_Ak_object_inspection.php +++ b/test/unit/lib/utils/_Ak_object_inspection.php @@ -123,7 +123,7 @@ class test_of_Ak_object_inspection extends UnitTestCase $this->assertEqual('2005-12-25 00:00:00', Ak::getDate(Ak::getTimestamp('2005-12-25'))); $this->assertEqual('1592-10-09 00:00:00', Ak::getDate(Ak::getTimestamp('1592-10-09'))); $this->assertEqual('2192-10-09 00:00:00', Ak::getDate(Ak::getTimestamp('2192-10-09'))); - $this->assertEqual('2192-10-09 01:02:03', Ak::getDate(Ak::getTimestamp('2192-10-9 1:2:3'))); + $this->assertEqual('2192-10-09 01:02:03', Ak::getDate(Ak::getTimestamp('2192-10-9 01:02:03'))); } function Test_for_getDate() -- 2.11.4.GIT