From 3697d8e3c26e146cc20ef2d7ebe68f3084839fa1 Mon Sep 17 00:00:00 2001 From: krakjoe Date: Fri, 6 Sep 2013 12:49:16 +0100 Subject: [PATCH 1/2] undefine user constants at runtime --- Zend/zend_builtin_functions.c | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/Zend/zend_builtin_functions.c b/Zend/zend_builtin_functions.c index 1ad64e74eaf47..9f6574264c8fe 100644 --- a/Zend/zend_builtin_functions.c +++ b/Zend/zend_builtin_functions.c @@ -42,6 +42,7 @@ static ZEND_FUNCTION(strncasecmp); static ZEND_FUNCTION(each); static ZEND_FUNCTION(error_reporting); static ZEND_FUNCTION(define); +static ZEND_FUNCTION(undefine); static ZEND_FUNCTION(defined); static ZEND_FUNCTION(get_class); static ZEND_FUNCTION(get_called_class); @@ -132,6 +133,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_define, 0, 0, 3) ZEND_ARG_INFO(0, case_insensitive) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO_EX(arginfo_undefine, 0, 0, 1) + ZEND_ARG_INFO(0, constant_name) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(arginfo_defined, 0, 0, 1) ZEND_ARG_INFO(0, constant_name) ZEND_END_ARG_INFO() @@ -253,6 +258,7 @@ static const zend_function_entry builtin_functions[] = { /* {{{ */ ZEND_FE(each, arginfo_each) ZEND_FE(error_reporting, arginfo_error_reporting) ZEND_FE(define, arginfo_define) + ZEND_FE(undefine, arginfo_undefine) ZEND_FE(defined, arginfo_defined) ZEND_FE(get_class, arginfo_get_class) ZEND_FE(get_called_class, arginfo_zend__void) @@ -720,6 +726,38 @@ ZEND_FUNCTION(define) } /* }}} */ +/* {{{ proto bool undefine(string constant_name) + Undefine a constant */ +ZEND_FUNCTION(undefine) +{ + char *name; + zend_uint name_len; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) { + return; + } + + { + zend_constant *defined = NULL; + + if (zend_hash_find(EG(zend_constants), name, name_len+1, (void**)&defined) == FAILURE) { + char *lname = zend_str_tolower_dup(name, name_len); + + zend_hash_find( + EG(zend_constants), lname, name_len+1, (void**)&defined); + + efree(lname); + } + + if (defined != NULL) { + if ((defined->module_number == PHP_USER_CONSTANT)) { + RETURN_BOOL((zend_hash_del(EG(zend_constants), name, name_len+1)==SUCCESS)); + } + } + + RETURN_FALSE; + } +} /* {{{ proto bool defined(string constant_name) Check whether a constant exists */ From 618154efe461b463168b855e3cd182b5f5dafafb Mon Sep 17 00:00:00 2001 From: krakjoe Date: Fri, 6 Sep 2013 13:52:55 +0100 Subject: [PATCH 2/2] optional case sensitivity --- Zend/zend_builtin_functions.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Zend/zend_builtin_functions.c b/Zend/zend_builtin_functions.c index 9f6574264c8fe..af11371e5f92f 100644 --- a/Zend/zend_builtin_functions.c +++ b/Zend/zend_builtin_functions.c @@ -743,9 +743,12 @@ ZEND_FUNCTION(undefine) if (zend_hash_find(EG(zend_constants), name, name_len+1, (void**)&defined) == FAILURE) { char *lname = zend_str_tolower_dup(name, name_len); - zend_hash_find( - EG(zend_constants), lname, name_len+1, (void**)&defined); - + if (zend_hash_find( + EG(zend_constants), lname, name_len+1, (void**)&defined) == SUCCESS) { + if (defined->cs & CONST_CS) + defined = NULL; + } + efree(lname); }