Skip to content

Commit 020e42e

Browse files
committed
Merge pull request #1 from krakjoe/undef
integrated new function appserver_redefine
2 parents f84f6d0 + 46cb06f commit 020e42e

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

src/appserver.c

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const zend_function_entry appserver_functions[] = {
5353
PHP_FE(appserver_get_headers, NULL)
5454
PHP_FE(appserver_register_file_upload, NULL)
5555
PHP_FE(appserver_set_headers_sent, NULL)
56+
PHP_FE(appserver_redefine, NULL)
5657
PHP_FE_END
5758
};
5859

@@ -167,6 +168,13 @@ static void php_appserver_init_globals(zend_appserver_globals *appserver_globals
167168
appserver_globals->headers = NULL;
168169
}
169170

171+
static inline void php_appserver_free_redefined(zval **pzval)
172+
{
173+
TSRMLS_FETCH();
174+
zval_dtor(*pzval);
175+
efree(*pzval);
176+
}
177+
170178
PHP_MSHUTDOWN_FUNCTION(appserver)
171179
{
172180
/* uncomment this line if you have INI entries
@@ -195,12 +203,15 @@ PHP_MINIT_FUNCTION(appserver)
195203
PHP_RINIT_FUNCTION(appserver)
196204
{
197205
APPSERVER_GLOBALS(headers) = appserver_llist_allocate(appserver_llist_string_destor);
198-
206+
zend_hash_init(
207+
&APPSERVER_GLOBALS(redefined), 16, NULL, (dtor_func_t) php_appserver_free_redefined, 0);
199208
return SUCCESS;
200209
}
201210

202211
PHP_RSHUTDOWN_FUNCTION(appserver)
203212
{
213+
zend_hash_destroy(
214+
&APPSERVER_GLOBALS(redefined));
204215
return SUCCESS;
205216
}
206217

@@ -215,6 +226,65 @@ PHP_MINFO_FUNCTION(appserver)
215226
*/
216227
}
217228

229+
/* {{{ proto boolean appserver_redefine(string $constant [, mixed $value])
230+
redefine/undefine constant at runtime ... /* }}} */
231+
PHP_FUNCTION(appserver_redefine)
232+
{
233+
char *name;
234+
zend_uint name_len;
235+
zval *pzval = NULL;
236+
237+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z", &name, &name_len, &pzval) == FAILURE) {
238+
return;
239+
}
240+
241+
{
242+
zend_constant *defined = NULL;
243+
244+
if (zend_hash_find(EG(zend_constants), name, name_len+1, (void**)&defined) == FAILURE) {
245+
char *lname = zend_str_tolower_dup(name, name_len);
246+
247+
if (zend_hash_find(
248+
EG(zend_constants), lname, name_len+1, (void**)&defined) == SUCCESS) {
249+
if (defined->flags & CONST_CS)
250+
defined = NULL;
251+
}
252+
253+
efree(lname);
254+
}
255+
256+
if (defined != NULL) {
257+
/* change user constant */
258+
if ((defined->module_number == PHP_USER_CONSTANT)) {
259+
zend_constant container = *defined;
260+
261+
if (pzval)
262+
container.name = zend_strndup(container.name, container.name_len);
263+
264+
if (zend_hash_del(EG(zend_constants), name, name_len+1)==SUCCESS) {
265+
if (pzval) {
266+
SEPARATE_ZVAL(&pzval);
267+
container.value = *pzval;
268+
zend_hash_next_index_insert(
269+
&APPSERVER_GLOBALS(redefined), &pzval, sizeof(zval**), NULL);
270+
Z_ADDREF_P(pzval);
271+
zend_register_constant(&container TSRMLS_CC);
272+
}
273+
}
274+
} else {
275+
/* change internal constant */
276+
SEPARATE_ZVAL(&pzval);
277+
defined->value = *pzval;
278+
zend_hash_next_index_insert(
279+
&APPSERVER_GLOBALS(redefined), &pzval, sizeof(zval**), NULL);
280+
Z_ADDREF_P(pzval);
281+
}
282+
}
283+
284+
RETURN_FALSE;
285+
}
286+
}
287+
218288
PHP_FUNCTION(appserver_register_file_upload)
219289
{
220290
char *path;

src/php_appserver.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ extern zend_module_entry appserver_module_entry;
3535
# define PHP_APPSERVER_API
3636
#endif
3737

38+
3839
#if PHP_VERSION_ID >= 50300
3940
# define AS_SAPI_HEADER_OP_DC , sapi_header_op_enum op
4041
# define AS_SAPI_HEADER_OP_CC , op
@@ -66,10 +67,12 @@ PHP_MINFO_FUNCTION(appserver);
6667
PHP_FUNCTION(appserver_get_headers);
6768
PHP_FUNCTION(appserver_register_file_upload);
6869
PHP_FUNCTION(appserver_set_headers_sent);
70+
PHP_FUNCTION(appserver_redefine);
6971

7072
ZEND_BEGIN_MODULE_GLOBALS(appserver)
7173
appserver_llist *headers;
7274
HashTable *uploaded_files;
75+
HashTable redefined;
7376
long pproftrace;
7477
ZEND_END_MODULE_GLOBALS(appserver)
7578

0 commit comments

Comments
 (0)