Skip to content

Commit

Permalink
Fix #878
Browse files Browse the repository at this point in the history
  • Loading branch information
sjinks committed Jul 22, 2013
1 parent a9c199d commit 499cd04
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 14 deletions.
70 changes: 58 additions & 12 deletions ext/http/request.c
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ PHP_METHOD(Phalcon_Http_Request, hasFiles){
RETURN_LONG(nfiles);
}

static void phalcon_http_request_getuploadedfiles_helper(zval **return_value, zval *name, zval *type, zval *tmp_name, zval *error, zval *size, int only_successful TSRMLS_DC)
static void phalcon_http_request_getuploadedfiles_helper(zval **return_value, zval *name, zval *type, zval *tmp_name, zval *error, zval *size, int only_successful, smart_str *prefix TSRMLS_DC)
{
if (
Z_TYPE_P(name) == IS_ARRAY && Z_TYPE_P(type) == IS_ARRAY
Expand All @@ -1218,7 +1218,8 @@ static void phalcon_http_request_getuploadedfiles_helper(zval **return_value, zv
) {
HashPosition pos_name, pos_type, pos_tmp, pos_error, pos_size;
zval **dname, **dtype, **dtmp, **derror, **dsize;
zval *arr, *file;
zval *arr, *file, *key;
size_t prefix_len = prefix->len;

zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(name), &pos_name);
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(type), &pos_type);
Expand All @@ -1233,6 +1234,15 @@ static void phalcon_http_request_getuploadedfiles_helper(zval **return_value, zv
&& zend_hash_get_current_data_ex(Z_ARRVAL_P(error), (void**)&derror, &pos_error) == SUCCESS
&& zend_hash_get_current_data_ex(Z_ARRVAL_P(size), (void**)&dsize, &pos_size) == SUCCESS
) {
zval index = phalcon_get_current_key_w(Z_ARRVAL_P(name), &pos_name);

if (Z_TYPE(index) == IS_STRING) {
smart_str_appendl(prefix, Z_STRVAL(index), Z_STRLEN(index));
}
else {
smart_str_append_long(prefix, Z_LVAL(index));
}

if (Z_TYPE_PP(derror) < IS_ARRAY) {
if (!zend_is_true(*derror) || !only_successful) {
Z_ADDREF_PP(dname);
Expand All @@ -1249,16 +1259,31 @@ static void phalcon_http_request_getuploadedfiles_helper(zval **return_value, zv
add_assoc_zval_ex(arr, SS("error"), *derror);
add_assoc_zval_ex(arr, SS("size"), *dsize);

ALLOC_INIT_ZVAL(key);
ZVAL_STRINGL(key, prefix->c, prefix->len, 1);
prefix->len = prefix_len;

ALLOC_INIT_ZVAL(file);
object_init_ex(file, phalcon_http_request_file_ce);
phalcon_call_method_p1_noret(file, "__construct", arr);

zval* params[2] = { arr, key };
int res = phalcon_call_method_params_w(NULL, file, SL("__construct"), 2, params, 0, 0 TSRMLS_CC);

zval_ptr_dtor(&arr);
zval_ptr_dtor(&key);

add_next_index_zval(*return_value, file);
if (res != FAILURE) {
add_next_index_zval(*return_value, file);
}
else {
break;
}
}
}
else if (Z_TYPE_PP(derror) == IS_ARRAY) {
phalcon_http_request_getuploadedfiles_helper(return_value, *dname, *dtype, *dtmp, *derror, *dsize, only_successful TSRMLS_CC);
smart_str_appendc(prefix, '.');
phalcon_http_request_getuploadedfiles_helper(return_value, *dname, *dtype, *dtmp, *derror, *dsize, only_successful, prefix TSRMLS_CC);
prefix->len = prefix_len;
}

zend_hash_move_forward_ex(Z_ARRVAL_P(name), &pos_name);
Expand All @@ -1279,39 +1304,44 @@ static void phalcon_http_request_getuploadedfiles_helper(zval **return_value, zv
PHP_METHOD(Phalcon_Http_Request, getUploadedFiles){

zval *name = NULL, *type = NULL, *tmp_name = NULL, *error = NULL, *size = NULL;
zval *not_errored = NULL, *_FILES, *request_file = NULL;
zval *not_errored = NULL, *_FILES, *request_file = NULL, *key = NULL;
HashTable *ah0;
HashPosition hp0;
zval **hd;
int only_successful;
smart_str prefix = { NULL, 0, 0 };

PHALCON_MM_GROW();

phalcon_fetch_params(1, 0, 1, &not_errored);
phalcon_fetch_params(0, 0, 1, &not_errored);

only_successful = not_errored ? phalcon_get_intval(not_errored) : 1;

array_init(return_value);

phalcon_get_global(&_FILES, SS("_FILES") TSRMLS_CC);
if (Z_TYPE_P(_FILES) != IS_ARRAY || !zend_hash_num_elements(Z_ARRVAL_P(_FILES))) {
RETURN_MM();
return;
}

PHALCON_MM_GROW();
phalcon_is_iterable(_FILES, &ah0, &hp0, 0, 0);

while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {

if (phalcon_array_isset_string(*hd, SS("error"))) {
PHALCON_OBS_NVAR(error);
phalcon_array_fetch_string(&error, *hd, SL("error"), PH_NOISY);
zval index = phalcon_get_current_key_w(ah0, &hp0);

if (Z_TYPE_P(error) < IS_ARRAY) {
if (!zend_is_true(error) || !only_successful) {
PHALCON_INIT_NVAR(request_file);
object_init_ex(request_file, phalcon_http_request_file_ce);

PHALCON_INIT_NVAR(key);
ZVAL_STRINGL(key, Z_STRVAL(index), Z_STRLEN(index), 1);

Z_ADDREF_PP(hd);
phalcon_call_method_p1_noret(request_file, "__construct", *hd);
phalcon_call_method_p2_noret(request_file, "__construct", *hd, key);

phalcon_array_append(&return_value, request_file, 0);
}
Expand All @@ -1326,13 +1356,29 @@ PHP_METHOD(Phalcon_Http_Request, getUploadedFiles){
phalcon_array_fetch_string(&tmp_name, *hd, SL("tmp_name"), PH_NOISY);
phalcon_array_fetch_string(&size, *hd, SL("size"), PH_NOISY);

phalcon_http_request_getuploadedfiles_helper(&return_value, name, type, tmp_name, error, size, only_successful TSRMLS_CC);
if (prefix.len) {
prefix.len = 0;
}

if (likely(Z_TYPE(index) == IS_STRING)) {
smart_str_appendl(&prefix, Z_STRVAL(index), Z_STRLEN(index));
}
else {
smart_str_append_long(&prefix, Z_LVAL(index));
}

smart_str_appendc(&prefix, '.');
phalcon_http_request_getuploadedfiles_helper(&return_value, name, type, tmp_name, error, size, only_successful, &prefix TSRMLS_CC);
}
}

zend_hash_move_forward_ex(ah0, &hp0);
}

if (prefix.c) {
smart_str_free(&prefix);
}

RETURN_MM();
}

Expand Down
42 changes: 40 additions & 2 deletions ext/http/request/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "Zend/zend_exceptions.h"
#include "Zend/zend_interfaces.h"

#include "main/SAPI.h"

#include "kernel/main.h"
#include "kernel/memory.h"

Expand Down Expand Up @@ -74,6 +76,7 @@ PHALCON_INIT_CLASS(Phalcon_Http_Request_File){
zend_declare_property_null(phalcon_http_request_file_ce, SL("_size"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_http_request_file_ce, SL("_type"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_http_request_file_ce, SL("_error"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_http_request_file_ce, SL("_key"), ZEND_ACC_PROTECTED TSRMLS_CC);

zend_class_implements(phalcon_http_request_file_ce TSRMLS_CC, 1, phalcon_http_request_fileinterface_ce);

Expand All @@ -87,11 +90,11 @@ PHALCON_INIT_CLASS(Phalcon_Http_Request_File){
*/
PHP_METHOD(Phalcon_Http_Request_File, __construct){

zval *file, *name, *temp_name, *size, *type, *error;
zval *file, *name, *temp_name, *size, *type, *error, *key = NULL;

PHALCON_MM_GROW();

phalcon_fetch_params(1, 1, 0, &file);
phalcon_fetch_params(1, 1, 1, &file, &key);

if (Z_TYPE_P(file) != IS_ARRAY) {
PHALCON_THROW_EXCEPTION_STR(phalcon_http_request_exception_ce, "Phalcon\\Http\\Request\\File requires a valid uploaded file");
Expand Down Expand Up @@ -127,6 +130,10 @@ PHP_METHOD(Phalcon_Http_Request_File, __construct){
phalcon_update_property_this(this_ptr, SL("_error"), error TSRMLS_CC);
}

if (key) {
phalcon_update_property_this(this_ptr, SL("_key"), key TSRMLS_CC);
}

PHALCON_MM_RESTORE();
}

Expand Down Expand Up @@ -198,6 +205,37 @@ PHP_METHOD(Phalcon_Http_Request_File, getError){
RETURN_MEMBER(this_ptr, "_error");
}

/**
* Returns the file key
*
* @return string
*/
PHP_METHOD(Phalcon_Http_Request_File, getKey){

RETURN_MEMBER(this_ptr, "_key");
}

PHP_METHOD(Phalcon_Http_Request_File, isUploadedFile) {

zval *tmp_name;

if (!SG(rfc1867_uploaded_files)) {
RETURN_FALSE;
}

PHALCON_ALLOC_ZVAL(tmp_name);
if (phalcon_call_method_params_w(tmp_name, getThis(), SL("gettempname"), 0, NULL, 0, 0 TSRMLS_CC) == SUCCESS) {
if (Z_TYPE_P(tmp_name) == IS_STRING && zend_hash_exists(SG(rfc1867_uploaded_files), Z_STRVAL_P(tmp_name), Z_STRLEN_P(tmp_name) + 1)) {
RETVAL_TRUE;
}
else {
RETVAL_FALSE;
}
}

zval_ptr_dtor(&tmp_name);
}

/**
* Moves the temporary file to a destination within the application
*
Expand Down
5 changes: 5 additions & 0 deletions ext/http/request/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ PHP_METHOD(Phalcon_Http_Request_File, getTempName);
PHP_METHOD(Phalcon_Http_Request_File, getType);
PHP_METHOD(Phalcon_Http_Request_File, getRealType);
PHP_METHOD(Phalcon_Http_Request_File, getError);
PHP_METHOD(Phalcon_Http_Request_File, getKey);
PHP_METHOD(Phalcon_Http_Request_File, isUploadedFile);
PHP_METHOD(Phalcon_Http_Request_File, moveTo);
PHP_METHOD(Phalcon_Http_Request_File, __set_state);

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_http_request_file___construct, 0, 0, 1)
ZEND_ARG_INFO(0, file)
ZEND_ARG_INFO(0, key)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_http_request_file_moveto, 0, 0, 1)
Expand All @@ -47,6 +50,8 @@ PHALCON_INIT_FUNCS(phalcon_http_request_file_method_entry){
PHP_ME(Phalcon_Http_Request_File, getType, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Http_Request_File, getRealType, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Http_Request_File, getError, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Http_Request_File, getKey, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Http_Request_File, isUploadedFile, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Http_Request_File, moveTo, arginfo_phalcon_http_request_file_moveto, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Config, __set_state, arginfo_phalcon_http_request_file___construct, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_FE_END
Expand Down

0 comments on commit 499cd04

Please sign in to comment.