cvs: pecl /valkyrie config.m4 php_valkyrie.h valkyrie.c

From: Date: Thu, 30 Oct 2003 23:22:06 +0000
Subject: cvs: pecl /valkyrie config.m4 php_valkyrie.h valkyrie.c
Groups: php.pecl.cvs 
Request: Send a blank email to pecl-cvs+get-515@lists.php.net to get a copy of this message
xnoguer Thu Oct 30 18:22:06 2003 EDT Modified files: /pecl/valkyrie config.m4 php_valkyrie.h valkyrie.c Log: adding the strict attribute and general clean up

Index: pecl/valkyrie/config.m4 diff -u pecl/valkyrie/config.m4:1.1 pecl/valkyrie/config.m4:1.2 --- pecl/valkyrie/config.m4:1.1 Thu Oct 30 06:59:13 2003 +++ pecl/valkyrie/config.m4 Thu Oct 30 18:22:06 2003 @@ -1,4 +1,4 @@ -dnl $Id: config.m4,v 1.1 2003/10/30 11:59:13 xnoguer Exp $ +dnl $Id: config.m4,v 1.2 2003/10/30 23:22:06 xnoguer Exp $ dnl config.m4 for extension valkyrie dnl Comments in this file start with the string 'dnl'. @@ -55,14 +55,15 @@ VALKYRIE_LIBNAME=xml fi - XML2_CONFIG=$VALKYRIE_DIR/bin/xml2-config +dnl XML2_CONFIG=$VALKYRIE_DIR/bin/xml2-config - if test -x $XML2_CONFIG; then - VALKYRIE_LIBS=`$XML2_CONFIG --libs` - PHP_EVAL_LIBLINE($VALKYRIE_LIBS, VALKYRIE_SHARED_LIBADD) - else +dnl if test -x $XML2_CONFIG; then +dnl VALKYRIE_LIBS=`$XML2_CONFIG --libs` +dnl PHP_EVAL_LIBLINE($VALKYRIE_LIBS, VALKYRIE_SHARED_LIBADD) +dnl else + PHP_SUBST(VALKYRIE_SHARED_LIBADD) PHP_ADD_LIBRARY_WITH_PATH($VALKYRIE_LIBNAME, $VALKYRIE_DIR/lib, VALKYRIE_SHARED_LIBADD) - fi +dnl fi dnl # --with-valkyrie -> add include path PHP_ADD_INCLUDE($VALKYRIE_DIR/include$VALKYRIE_DIR_ADD) Index: pecl/valkyrie/php_valkyrie.h diff -u pecl/valkyrie/php_valkyrie.h:1.1 pecl/valkyrie/php_valkyrie.h:1.2 --- pecl/valkyrie/php_valkyrie.h:1.1 Thu Oct 30 06:59:13 2003 +++ pecl/valkyrie/php_valkyrie.h Thu Oct 30 18:22:06 2003 @@ -15,7 +15,7 @@ | Author: Xavier Noguer <xnoguer@php.net> | +----------------------------------------------------------------------+ - $Id: php_valkyrie.h,v 1.1 2003/10/30 11:59:13 xnoguer Exp $ + $Id: php_valkyrie.h,v 1.2 2003/10/30 23:22:06 xnoguer Exp $ */ #ifndef PHP_VALKYRIE_H @@ -71,6 +71,7 @@ typedef struct _valkyrie_param_to_receive { char *name; zend_uchar received; + struct _valkyrie_param_to_receive *next; } valkyrie_param_to_receive; #ifdef ZTS Index: pecl/valkyrie/valkyrie.c diff -u pecl/valkyrie/valkyrie.c:1.1 pecl/valkyrie/valkyrie.c:1.2 --- pecl/valkyrie/valkyrie.c:1.1 Thu Oct 30 06:59:13 2003 +++ pecl/valkyrie/valkyrie.c Thu Oct 30 18:22:06 2003 @@ -15,7 +15,12 @@ | Author: Xavier Noguer <xnoguer@php.net> | +----------------------------------------------------------------------+ - $Id: valkyrie.c,v 1.1 2003/10/30 11:59:13 xnoguer Exp $ + $Id: valkyrie.c,v 1.2 2003/10/30 23:22:06 xnoguer Exp $ + + TODO: add an +array valkyrie_errors(), function to access all errors found during validation +instead of dying on valkyrie_validate(). Will need to maintain a global array +of errors. */ #ifdef HAVE_CONFIG_H @@ -93,7 +98,8 @@ { REGISTER_INI_ENTRIES(); ZEND_INIT_MODULE_GLOBALS(valkyrie, php_valkyrie_init_globals, NULL); - /* TODO: can't access php.ini values here. Find out why */ + /* TODO: can't access php.ini values here, so we can't parse the config + file. Find out why */ return SUCCESS; } @@ -106,7 +112,6 @@ UNREGISTER_INI_ENTRIES(); /* free the whole XML document (config file) */ xmlFreeDoc(VALKYRIE_G(doc_ptr)); - return SUCCESS; } /* }}} */ @@ -184,7 +189,9 @@ strcpy(VALKYRIE_G(application_path), ""); } else { - /* change here for CLI testing */ + /* uncomment here for CLI testing */ + /*VALKYRIE_G(application_path) = (char *) emalloc(strlen(path) + 1); + strcpy(VALKYRIE_G(application_path), "");*/ VALKYRIE_G(application_path) = (char *) emalloc(1 + strlen(path) + 1); strcpy(VALKYRIE_G(application_path), "/"); strcat(VALKYRIE_G(application_path), path); @@ -231,6 +238,7 @@ return FAILURE; } zval_dtor(zval_validationfuncs_path); + FREE_ZVAL(zval_validationfuncs_path); if (zend_hash_find(EG(class_table), "valkyrie_validate", strlen("valkyrie_validate") + 1, (void **)&VALKYRIE_G(zce)) == FAILURE) { return FAILURE; @@ -283,43 +291,95 @@ } /* }}} */ -/* {{{ get_input_params +/* {{{ valkyrie_flag_param + flag the received param in the GET or POST list */ +void valkyrie_flag_param(valkyrie_param_to_receive **param_list, + char *param_name) +{ + valkyrie_param_to_receive *param_ptr; + + param_ptr = *param_list; + while (param_ptr != NULL) { + if (strcmp(param_ptr->name, param_name) == 0) { + param_ptr->received = 1; + break; + } + param_ptr = param_ptr->next; + } +} +/* }}} */ + +/* {{{ valkyrie_get_input_params Get GET and POST params and create arrays for "strict" validation if needed Returns FAILURE or SUCCESS */ -int get_input_params(zval **get_data, zval **post_data TSRMLS_DC) +int valkyrie_get_input_params(zval ***get_data, + valkyrie_param_to_receive **get_received, + zval ***post_data, + valkyrie_param_to_receive **post_received + TSRMLS_DC) { zval *tmp; char *string_key; ulong num_key; - int key_type; - valkyrie_param_to_receive **params; + int key_type, total_elements, i; + valkyrie_param_to_receive *param_ptr = NULL; - if (zend_hash_find(&EG(symbol_table), "_GET", sizeof("_GET"), (void **) &get_data) != SUCCESS || Z_TYPE_PP(get_data) != IS_ARRAY) { - return FAILURE; /* php_error(E_ERROR, "_GET array not found"); */ + if (zend_hash_find(&EG(symbol_table), "_GET", sizeof("_GET"), (void **) get_data) != SUCCESS || Z_TYPE_PP(*get_data) != IS_ARRAY) { + return FAILURE; /* _GET array not found */ } - if (zend_hash_find(&EG(symbol_table), "_POST", sizeof("_POST"), (void **) &post_data) != SUCCESS || Z_TYPE_PP(post_data) != IS_ARRAY) { - return FAILURE; /* php_error(E_ERROR, "_POST array not found"); */ + if (zend_hash_find(&EG(symbol_table), "_POST", sizeof("_POST"), (void **) post_data) != SUCCESS || Z_TYPE_PP(*post_data) != IS_ARRAY) { + return FAILURE; /* _POST array not found */ } -#if 0 if (VALKYRIE_G(strict) == 1) { - if (zend_hash_num_elements(Z_ARRVAL_PP(get_data))) { - zend_hash_internal_pointer_reset(Z_ARRVAL_PP(get_data)); - do { - valkyrie_param_to_receive param; - - key_type = zend_hash_get_current_key(Z_ARRVAL_PP(get_data), &string_key, &num_key, 0); + total_elements = zend_hash_num_elements(Z_ARRVAL_PP(*get_data)); + if (total_elements) { + zend_hash_internal_pointer_reset(Z_ARRVAL_PP(*get_data)); + for (i = 0; i < total_elements; i++, zend_hash_move_forward(Z_ARRVAL_PP(*get_data))) { + key_type = zend_hash_get_current_key(Z_ARRVAL_PP(*get_data), &string_key, &num_key, 0); if (key_type == HASH_KEY_IS_LONG) { return FAILURE;/* something bad happened */ } - - param.name = (char *)emalloc(strlen(string_key) + 1); - strcpy(param.name, string_key); - param.received = 0; - } while (zend_hash_move_forward(Z_ARRVAL_PP(get_data)) == SUCCESS); + /* if it's the first param */ + if (param_ptr == NULL) { + param_ptr = (valkyrie_param_to_receive *)emalloc(sizeof(valkyrie_param_to_receive)); + *get_received = param_ptr; + } + else { + param_ptr->next = (valkyrie_param_to_receive *)emalloc(sizeof(valkyrie_param_to_receive)); + param_ptr = param_ptr->next; + } + param_ptr->name = (char *)emalloc(strlen(string_key) + 1); + strcpy(param_ptr->name, string_key); + param_ptr->received = 0; + param_ptr->next = NULL; + } + } + total_elements = zend_hash_num_elements(Z_ARRVAL_PP(*post_data)); + if (total_elements) { + param_ptr = NULL; /* re-initialize pointer */ + zend_hash_internal_pointer_reset(Z_ARRVAL_PP(*post_data)); + for (i = 0; i < total_elements; i++, zend_hash_move_forward(Z_ARRVAL_PP(*post_data))) { + key_type = zend_hash_get_current_key(Z_ARRVAL_PP(*post_data), &string_key, &num_key, 0); + if (key_type == HASH_KEY_IS_LONG) { + return FAILURE;/* something bad happened */ + } + /* if it's the first param */ + if (param_ptr == NULL) { + param_ptr = (valkyrie_param_to_receive *)emalloc(sizeof(valkyrie_param_to_receive)); + *post_received = param_ptr; + } + else { + param_ptr->next = (valkyrie_param_to_receive *)emalloc(sizeof(valkyrie_param_to_receive)); + param_ptr = param_ptr->next; + } + param_ptr->name = (char *)emalloc(strlen(string_key) + 1); + strcpy(param_ptr->name, string_key); + param_ptr->received = 0; + param_ptr->next = NULL; + } } } -#endif return SUCCESS; } @@ -331,8 +391,11 @@ { int i, j; int result, file_found = 0; - zval *zvalidate_func, *retval; + zval *zvalidate_func, *zretval; zval **post_data, **get_data, **zscript_name, **tmp; + valkyrie_param_to_receive *get_received = NULL; + valkyrie_param_to_receive *post_received = NULL; + valkyrie_param_to_receive *aux_ptr; zval ***argv; char *script_name, *file; xmlNodeSet *params; @@ -409,11 +472,7 @@ /* compare file names from config file and SCRIPT_NAME */ if (strcmp(file, file_name) == 0) { file_found = 1; - /* if there are no params defined just return */ params = get_elements_by_tagname(file_node, "param", NULL); - if (params == NULL) { - return 1; - } /* free mem and leave modules/files loops */ efree(module_path); efree(temp_path); @@ -429,115 +488,151 @@ efree(temp_path); } if (file_found == 0) { - php_error(E_ERROR, "File %s is not defined", script_name); + php_error(E_ERROR, "File %s is not defined in the configuration file", script_name); return 0; } /* get REQUEST parameters (TODO: move this to get_input_params()) */ - if (zend_hash_find(&EG(symbol_table), "_POST", sizeof("_POST"), (void **) &post_data) != SUCCESS || Z_TYPE_PP(post_data) != IS_ARRAY) { + /*if (zend_hash_find(&EG(symbol_table), "_POST", sizeof("_POST"), (void **) &post_data) != SUCCESS || Z_TYPE_PP(post_data) != IS_ARRAY) { php_error(E_ERROR, "_POST array not found"); return 0; } if (zend_hash_find(&EG(symbol_table), "_GET", sizeof("_GET"), (void **) &get_data) != SUCCESS || Z_TYPE_PP(get_data) != IS_ARRAY) { php_error(E_ERROR, "_GET array not found"); return 0; - } - /*if (get_input_params(get_data, post_data) == FAILURE) { - php_error(E_ERROR, "Error getting GET and POST params"); - return 0; }*/ + if (valkyrie_get_input_params(&get_data, &get_received, &post_data, &post_received TSRMLS_CC) == FAILURE) { + php_error(E_ERROR, "Error getting GET and POST params"); + return 0; + } MAKE_STD_ZVAL(zvalidate_func); - MAKE_STD_ZVAL(retval); + MAKE_STD_ZVAL(zretval); /* loop through params defined in config file */ - for (i = 0; i < params->nodeNr; i++) { - xmlNodePtr param_node = params->nodeTab[i]; - char *is_optional, *param_name, *param_type, *param_method; - char *validate_func; - zval **param_received; - - /* get attributes for param element */ - param_name = xmlGetProp(param_node, "name"); - if (param_name == NULL) { - php_error(E_ERROR, "Param element missing its name attribute for file %s\n", file); - return 0; - } - param_type = xmlGetProp(param_node, "type"); - if (param_type == NULL) { - php_error(E_ERROR, "Param %s missing its type attribute for file %s\n", param_name, file); - return 0; - } - param_method = xmlGetProp(param_node, "method"); - /* method attribute is obligatory. This may change in the future */ - /* if it changes get the _REQUEST array too */ - if (param_method == NULL) { - php_error(E_ERROR, "Param %s missing its method attribute for file %s\n", param_name, file); - return 0; - } - is_optional = xmlGetProp(param_node, "optional"); + if (params != NULL) { + for (i = 0; i < params->nodeNr; i++) { + xmlNodePtr param_node = params->nodeTab[i]; + char *is_optional, *param_name, *param_type, *param_method; + char *validate_func; + zval **param_received; + + /* get attributes for param element */ + param_name = xmlGetProp(param_node, "name"); + if (param_name == NULL) { + php_error(E_ERROR, "Param element missing its name attribute for file %s\n", file); + return 0; + } + param_type = xmlGetProp(param_node, "type"); + if (param_type == NULL) { + php_error(E_ERROR, "Param %s missing its type attribute for file %s\n", param_name, file); + return 0; + } + param_method = xmlGetProp(param_node, "method"); + /* method attribute is obligatory. This may change in the future */ + /* if it changes get the _REQUEST array too */ + if (param_method == NULL) { + php_error(E_ERROR, "Param %s missing its method attribute for file %s\n", param_name, file); + return 0; + } + is_optional = xmlGetProp(param_node, "optional"); - /* find the parameter if it's post */ - if (strcmp(param_method, "post") == 0) { - if (zend_hash_find(Z_ARRVAL_PP(post_data), param_name, strlen(param_name) + 1, (void **) &param_received) != SUCCESS) { - if ((is_optional == NULL) || (strcmp(is_optional, "no") == 0)) { - php_error(E_ERROR, "Post param %s not received\n", param_name); - return 0; + /* find the parameter if it's post */ + /* TODO: more optimal search if "strict" is set */ + if (strcmp(param_method, "post") == 0) { + if (zend_hash_find(Z_ARRVAL_PP(post_data), param_name, strlen(param_name) + 1, (void **) &param_received) != SUCCESS) { + if ((is_optional == NULL) || (strcmp(is_optional, "no") == 0)) { + php_error(E_ERROR, "Post param %s not received\n", param_name); + return 0; + } + else { /* if it's optional continue */ + continue; + } } - else { /* if it's optional continue */ - continue; + /* if "strict" is set, flag as received */ + else if(VALKYRIE_G(strict) == 1) { + valkyrie_flag_param(&post_received, param_name); } } - } - /* find the parameter if it's get */ - else if (strcmp(param_method, "get") == 0) { - if (zend_hash_find(Z_ARRVAL_PP(get_data), param_name, strlen(param_name) + 1, (void **) &param_received) != SUCCESS) { - if ((is_optional == NULL) || (strcmp(is_optional, "no") == 0)) { - php_error(E_ERROR, "Get param %s not received\n", param_name); - return 0; + /* find the parameter if it's get */ + else if (strcmp(param_method, "get") == 0) { + if (zend_hash_find(Z_ARRVAL_PP(get_data), param_name, strlen(param_name) + 1, (void **) &param_received) != SUCCESS) { + if ((is_optional == NULL) || (strcmp(is_optional, "no") == 0)) { + php_error(E_ERROR, "Get param %s not received\n", param_name); + return 0; + } + else { /* if it's optional continue */ + continue; + } } - else { /* if it's optional continue */ - continue; + /* if "strict" is set, flag as received */ + else if(VALKYRIE_G(strict) == 1) { + valkyrie_flag_param(&get_received, param_name); } } + /* TODO: search on _COOKIES too ? */ + /* param was not received */ + if (param_received == NULL) { + php_error(E_ERROR, "Param %s not received\n", param_name); + return 0; + } + + validate_func = (char *)emalloc(8 + strlen(param_type) + 1); + strcpy(validate_func, "validate"); + strcat(validate_func, param_type); + /*printf("validate_func = %s\n", validate_func);*/ + + /* call validation function */ + ZVAL_STRING(zvalidate_func, validate_func, 1); + ZVAL_FALSE(zretval); + argv = emalloc(sizeof(zval **)); + argv[0] = &param_received[0]; + result = call_user_function_ex(&(VALKYRIE_G(zce)->function_table), NULL, zvalidate_func, &zretval, 1, argv, 0, NULL TSRMLS_CC); + if (result == FAILURE) { + php_error(E_ERROR, "Could not call function Valkyrie_Validate::%s\n", validate_func); + return 0; + } + + if (Z_TYPE_P(zretval) != IS_BOOL) { + php_error(E_ERROR, "Invalid return type for Valkyrie_Validate::%s", validate_func); + return 0; + } + if (Z_BVAL_P(zretval) == 0) { + php_error(E_ERROR, "Invalid type for parameter %s", param_name); + return 0; + } + + efree(file); + efree(validate_func); + efree(argv); } - /* TODO: search on _COOKIES and finally on _REQUEST too ? */ - /* param was not received */ - if (param_received == NULL) { - php_error(E_ERROR, "Param %s not received\n", param_name); - return 0; - } - - validate_func = (char *)emalloc(8 + strlen(param_type) + 1); - strcpy(validate_func, "validate"); - strcat(validate_func, param_type); - /*printf("validate_func = %s\n", validate_func);*/ - - /* call validation function */ - ZVAL_STRING(zvalidate_func, validate_func, 1); - ZVAL_FALSE(retval); - argv = emalloc(sizeof(zval **)); - argv[0] = &param_received[0]; - result = call_user_function_ex(&(VALKYRIE_G(zce)->function_table), NULL, zvalidate_func, &retval, 1, argv, 0, NULL TSRMLS_CC); - if (result == FAILURE) { - php_error(E_ERROR, "Could not call function Valkyrie_Validate::%s\n", validate_func); - return 0; - } + } - if (Z_TYPE_P(retval) != IS_BOOL) { - php_error(E_ERROR, "Invalid return type for Valkyrie_Validate::%s", validate_func); - return 0; - } - if (Z_BVAL_P(retval) == 0) { - php_error(E_ERROR, "Invalid type for parameter %s", param_name); - return 0; + /* check all received params were declared if "strict" is set */ + if (VALKYRIE_G(strict) == 1) { + while (post_received != NULL) { + if (post_received->received == 0) { + php_error(E_ERROR, "Received undeclared POST param %s\n", post_received->name); + return 0; + } + aux_ptr = post_received; + post_received = post_received->next; + efree(aux_ptr); + } + while (get_received != NULL) { + if (get_received->received == 0) { + php_error(E_ERROR, "Received undeclared GET param %s\n", get_received->name); + return 0; + } + aux_ptr = get_received; + get_received = get_received->next; + efree(aux_ptr); } - - efree(file); - efree(validate_func); - efree(argv); } + zval_dtor(zvalidate_func); - zval_dtor(retval); + FREE_ZVAL(zvalidate_func); + zval_dtor(zretval); + FREE_ZVAL(zretval); return 1; }
« previous php.pecl.cvs (#515) next »