Skip to content

Commit

Permalink
Merge pull request #1267 from dreamsxin/1265
Browse files Browse the repository at this point in the history
Fix 1265
  • Loading branch information
Phalcon committed Sep 22, 2013
2 parents 2451e80 + 464c40a commit 70e1a01
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 16 deletions.
16 changes: 11 additions & 5 deletions ext/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ PHP_METHOD(Phalcon_Filter, add){
*/
PHP_METHOD(Phalcon_Filter, sanitize){

zval *value, *filters, *new_value = NULL, *filter = NULL, *array_value = NULL;
zval *value, *filters, *norecursive = NULL, *new_value = NULL, *filter = NULL, *array_value = NULL;
zval *item_value = NULL, *item_key = NULL, *filter_value = NULL, *sanizited_value = NULL;
zval *key = NULL;
HashTable *ah0, *ah1, *ah2;
Expand All @@ -118,7 +118,12 @@ PHP_METHOD(Phalcon_Filter, sanitize){

PHALCON_MM_GROW();

phalcon_fetch_params(1, 2, 0, &value, &filters);
phalcon_fetch_params(1, 2, 1, &value, &filters, &norecursive);

if (!norecursive) {
PHALCON_INIT_VAR(norecursive);
ZVAL_FALSE(norecursive);
}

/**
* Apply an array of filters
Expand All @@ -136,7 +141,7 @@ PHP_METHOD(Phalcon_Filter, sanitize){
/**
* If the value to filter is an array we apply the filters recursively
*/
if (Z_TYPE_P(new_value) == IS_ARRAY) {
if (Z_TYPE_P(new_value) == IS_ARRAY && !zend_is_true(norecursive)) {

PHALCON_INIT_NVAR(array_value);
array_init(array_value);
Expand Down Expand Up @@ -173,8 +178,9 @@ PHP_METHOD(Phalcon_Filter, sanitize){
/**
* Apply a single filter value
*/
if (Z_TYPE_P(value) == IS_ARRAY) {

if (Z_TYPE_P(value) == IS_ARRAY && !zend_is_true(norecursive)) {

zend_print_zval(norecursive, 0);
PHALCON_INIT_VAR(sanizited_value);
array_init(sanizited_value);

Expand Down
1 change: 1 addition & 0 deletions ext/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_filter_sanitize, 0, 0, 2)
ZEND_ARG_INFO(0, value)
ZEND_ARG_INFO(0, filters)
ZEND_ARG_INFO(0, norecursive)
ZEND_END_ARG_INFO()

PHALCON_INIT_FUNCS(phalcon_filter_method_entry){
Expand Down
36 changes: 27 additions & 9 deletions ext/http/request.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,18 @@ PHP_METHOD(Phalcon_Http_Request, getDI){
* @param string|array $filters
* @param mixed $defaultValue
* @param boolean $allowEmpty
* @param boolean $noRecursive
* @return mixed
*/
PHP_METHOD(Phalcon_Http_Request, get){

zval *name = NULL, *filters = NULL, *default_value = NULL, *allow_empty = NULL, *request = NULL;
zval *name = NULL, *filters = NULL, *default_value = NULL, *allow_empty = NULL, *norecursive = NULL, *request = NULL;
zval *_REQUEST, *value, *filter = NULL, *dependency_injector;
zval *service;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 0, 4, &name, &filters, &default_value, &allow_empty);
phalcon_fetch_params(1, 0, 5, &name, &filters, &default_value, &allow_empty, &norecursive);

if (!name) {
PHALCON_INIT_VAR(name);
Expand All @@ -153,6 +154,11 @@ PHP_METHOD(Phalcon_Http_Request, get){
ZVAL_TRUE(allow_empty);
}

if (!norecursive) {
PHALCON_INIT_VAR(norecursive);
ZVAL_FALSE(norecursive);
}

phalcon_get_global(&_REQUEST, SS("_REQUEST") TSRMLS_CC);
PHALCON_CPY_WRT(request, _REQUEST);
if (Z_TYPE_P(name) != IS_NULL) {
Expand Down Expand Up @@ -182,7 +188,7 @@ PHP_METHOD(Phalcon_Http_Request, get){
phalcon_update_property_this(this_ptr, SL("_filter"), filter TSRMLS_CC);
}

phalcon_call_method_p2(return_value, filter, "sanitize", value, filters);
phalcon_call_method_p3(return_value, filter, "sanitize", value, filters, norecursive);

if (PHALCON_IS_EMPTY(return_value) && !zend_is_true(allow_empty)) {
RETURN_CCTOR(default_value);
Expand Down Expand Up @@ -220,16 +226,17 @@ PHP_METHOD(Phalcon_Http_Request, get){
* @param string|array $filters
* @param mixed $defaultValue
* @param boolean $allowEmpty
* @param boolean $noRecursive
* @return mixed
*/
PHP_METHOD(Phalcon_Http_Request, getPost){

zval *name = NULL, *filters = NULL, *default_value = NULL, *allow_empty = NULL, *post = NULL, *_POST;
zval *name = NULL, *filters = NULL, *default_value = NULL, *allow_empty = NULL, *norecursive = NULL, *post = NULL, *_POST;
zval *value, *filter = NULL, *dependency_injector, *service;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 0, 4, &name, &filters, &default_value, &allow_empty);
phalcon_fetch_params(1, 0, 5, &name, &filters, &default_value, &allow_empty, &norecursive);

if (!name) {
PHALCON_INIT_VAR(name);
Expand All @@ -248,6 +255,11 @@ PHP_METHOD(Phalcon_Http_Request, getPost){
ZVAL_TRUE(allow_empty);
}

if (!norecursive) {
PHALCON_INIT_VAR(norecursive);
ZVAL_FALSE(norecursive);
}

phalcon_get_global(&_POST, SS("_POST") TSRMLS_CC);
PHALCON_CPY_WRT(post, _POST);
if (Z_TYPE_P(name) != IS_NULL) {
Expand Down Expand Up @@ -277,7 +289,7 @@ PHP_METHOD(Phalcon_Http_Request, getPost){
phalcon_update_property_this(this_ptr, SL("_filter"), filter TSRMLS_CC);
}

phalcon_call_method_p2(return_value, filter, "sanitize", value, filters);
phalcon_call_method_p3(return_value, filter, "sanitize", value, filters, norecursive);

if (PHALCON_IS_EMPTY(return_value) && !zend_is_true(allow_empty)) {
RETURN_CCTOR(default_value);
Expand Down Expand Up @@ -318,16 +330,17 @@ PHP_METHOD(Phalcon_Http_Request, getPost){
* @param string|array $filters
* @param mixed $defaultValue
* @param boolean $allowEmpty
* @param boolean $noRecursive
* @return mixed
*/
PHP_METHOD(Phalcon_Http_Request, getQuery){

zval *name = NULL, *filters = NULL, *default_value = NULL, *allow_empty = NULL, *get = NULL, *_GET;
zval *name = NULL, *filters = NULL, *default_value = NULL, *allow_empty = NULL, *norecursive = NULL, *get = NULL, *_GET;
zval *value, *filter = NULL, *dependency_injector, *service;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 0, 4, &name, &filters, &default_value, &allow_empty);
phalcon_fetch_params(1, 0, 5, &name, &filters, &default_value, &allow_empty, &norecursive);

if (!name) {
PHALCON_INIT_VAR(name);
Expand All @@ -346,6 +359,11 @@ PHP_METHOD(Phalcon_Http_Request, getQuery){
ZVAL_TRUE(allow_empty);
}

if (!norecursive) {
PHALCON_INIT_VAR(norecursive);
ZVAL_FALSE(norecursive);
}

phalcon_get_global(&_GET, SS("_GET") TSRMLS_CC);
PHALCON_CPY_WRT(get, _GET);
if (Z_TYPE_P(name) != IS_NULL) {
Expand Down Expand Up @@ -375,7 +393,7 @@ PHP_METHOD(Phalcon_Http_Request, getQuery){
phalcon_update_property_this(this_ptr, SL("_filter"), filter TSRMLS_CC);
}

phalcon_call_method_p2(return_value, filter, "sanitize", value, filters);
phalcon_call_method_p3(return_value, filter, "sanitize", value, filters, norecursive);

if (PHALCON_IS_EMPTY(return_value) && !zend_is_true(allow_empty)) {
RETURN_CCTOR(default_value);
Expand Down
3 changes: 3 additions & 0 deletions ext/http/request.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,23 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_http_request_get, 0, 0, 0)
ZEND_ARG_INFO(0, filters)
ZEND_ARG_INFO(0, defaultValue)
ZEND_ARG_INFO(0, allowEmpty)
ZEND_ARG_INFO(0, noRecursive)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_http_request_getpost, 0, 0, 0)
ZEND_ARG_INFO(0, name)
ZEND_ARG_INFO(0, filters)
ZEND_ARG_INFO(0, defaultValue)
ZEND_ARG_INFO(0, allowEmpty)
ZEND_ARG_INFO(0, noRecursive)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_http_request_getquery, 0, 0, 0)
ZEND_ARG_INFO(0, name)
ZEND_ARG_INFO(0, filters)
ZEND_ARG_INFO(0, defaultValue)
ZEND_ARG_INFO(0, allowEmpty)
ZEND_ARG_INFO(0, noRecursive)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_http_request_getserver, 0, 0, 1)
Expand Down
38 changes: 36 additions & 2 deletions unit-tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@ public function testIssues1226()
);

// get
$this->assertEquals($request->getQuery('id', 'int', 100), 1);
$this->assertEquals($request->get('id', 'int', 100), 1);

$this->assertEquals($request->getQuery('num', 'int', 100), 1);
$this->assertEquals($request->get('num', 'int', 100), 1);

$age = $request->get('age', 'int', 100);
$this->assertTrue(empty($age));
Expand Down Expand Up @@ -292,5 +292,39 @@ public function testIssues1226()
$this->assertTrue(empty($phone));
$this->assertEquals($request->getPost('phone', 'int', 100, FALSE), 100);
}

public function testIssues1265()
{
$di = new Phalcon\DI\FactoryDefault();

$request = new \Phalcon\Http\Request();
$request->setDI($di);

$_REQUEST = $_GET = $_POST = array(
'string' => 'hello',
'array' => array('string' => 'world')
);

// get
$this->assertEquals($request->get('string', 'string'), 'hello');
$this->assertEquals($request->get('string', 'string', NULL, TRUE, TRUE), 'hello');

$this->assertEquals($request->get('array', 'string'), array('string' => 'world'));
$this->assertEquals($request->get('array', 'string', NULL, TRUE, TRUE), NULL);

// getQuery
$this->assertEquals($request->getQuery('string', 'string'), 'hello');
$this->assertEquals($request->getQuery('string', 'string', NULL, TRUE, TRUE), 'hello');

$this->assertEquals($request->getQuery('array', 'string'), array('string' => 'world'));
$this->assertEquals($request->getQuery('array', 'string', NULL, TRUE, TRUE), NULL);

// getPost
$this->assertEquals($request->getPost('string', 'string'), 'hello');
$this->assertEquals($request->getPost('string', 'string', NULL, TRUE, TRUE), 'hello');

$this->assertEquals($request->getPost('array', 'string'), array('string' => 'world'));
$this->assertEquals($request->getPost('array', 'string', NULL, TRUE, TRUE), NULL);
}
}

0 comments on commit 70e1a01

Please sign in to comment.