From 7d0f35459c9759a8a49df7e114a58210d0a2bf14 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Tue, 2 Oct 2018 01:18:55 +0200 Subject: [PATCH 1/2] Add Ini() method to change ini values on an engine context --- context.c | 4 ++++ context.go | 12 ++++++++++++ context_test.go | 18 ++++++++++++++++++ include/context.h | 5 +++-- include/php5/_context.h | 1 + include/php7/_context.h | 1 + src/php5/_context.c | 6 ++++++ src/php7/_context.c | 11 +++++++++++ 8 files changed, 56 insertions(+), 2 deletions(-) diff --git a/context.c b/context.c index bc8ebc4..3cdcd8c 100644 --- a/context.c +++ b/context.c @@ -99,6 +99,10 @@ void context_bind(engine_context *context, char *name, void *value) { _context_bind(name, v->internal); } +void context_ini(engine_context *context, char *name, char *value) { + _context_ini(name, value); +} + void context_destroy(engine_context *context) { php_request_shutdown(NULL); diff --git a/context.go b/context.go index ca51427..25b4977 100644 --- a/context.go +++ b/context.go @@ -53,6 +53,18 @@ func (c *Context) Bind(name string, val interface{}) error { return nil } +// Ini allows setting ini file values into the current execution context. +func (c *Context) Ini(name, value string) error { + n := C.CString(name) + defer C.free(unsafe.Pointer(n)) + v := C.CString(value) + defer C.free(unsafe.Pointer(v)) + + _, err := C.context_ini(c.context, n, v) + + return err +} + // Exec executes a PHP script pointed to by filename in the current execution // context, and returns an error, if any. Output produced by the script is // written to the context's pre-defined io.Writer instance. diff --git a/context_test.go b/context_test.go index a2948da..31a8f5f 100644 --- a/context_test.go +++ b/context_test.go @@ -283,6 +283,24 @@ func TestContextBind(t *testing.T) { c.Destroy() } +func TestContextIni(t *testing.T) { + c, _ := e.NewContext() + defer c.Destroy() + + path := "/path/set/by/go" + c.Ini("include_path", path) + + val, err := c.Eval("return ini_get('include_path');") + if err != nil { + t.Errorf("Unexpected error while running script: %v", err) + return + } + + if val.String() != path { + t.Errorf("Expected %s, have %s", path, val.String()) + } +} + func TestContextDestroy(t *testing.T) { c, _ := e.NewContext() c.Destroy() diff --git a/include/context.h b/include/context.h index 07bb676..d44d81a 100644 --- a/include/context.h +++ b/include/context.h @@ -9,9 +9,10 @@ typedef struct _engine_context { } engine_context; engine_context *context_new(); -void context_exec(engine_context *context, char *filename); -void *context_eval(engine_context *context, char *script); +void context_exec(engine_context *context, char *filename, int *exit); +void *context_eval(engine_context *context, char *script, int *exit); void context_bind(engine_context *context, char *name, void *value); +void context_ini(engine_context *context, char *name, char *value); void context_destroy(engine_context *context); #include "_context.h" diff --git a/include/php5/_context.h b/include/php5/_context.h index f629328..c853625 100644 --- a/include/php5/_context.h +++ b/include/php5/_context.h @@ -6,6 +6,7 @@ #define ___CONTEXT_H___ static void _context_bind(char *name, zval *value); +static void _context_ini(char *name, char *value); static void _context_eval(zend_op_array *op, zval *ret); #endif diff --git a/include/php7/_context.h b/include/php7/_context.h index f629328..391ba69 100644 --- a/include/php7/_context.h +++ b/include/php7/_context.h @@ -7,5 +7,6 @@ static void _context_bind(char *name, zval *value); static void _context_eval(zend_op_array *op, zval *ret); +static void _context_ini(char *name, char *value); #endif diff --git a/src/php5/_context.c b/src/php5/_context.c index f1d0849..e8333cc 100644 --- a/src/php5/_context.c +++ b/src/php5/_context.c @@ -6,6 +6,12 @@ static void _context_bind(char *name, zval *value) { ZEND_SET_SYMBOL(EG(active_symbol_table), name, value); } +static void _context_ini(char *name, char *value) { + if (zend_alter_ini_entry_ex(name, strlen(name) + 1, value, strlen(value) + 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0) == FAILURE) { + errno = 1; + } +} + static void _context_eval(zend_op_array *op, zval *ret) { zend_op_array *oparr = EG(active_op_array); zval *retval = NULL; diff --git a/src/php7/_context.c b/src/php7/_context.c index dc75275..5df39a7 100644 --- a/src/php7/_context.c +++ b/src/php7/_context.c @@ -6,6 +6,17 @@ static void _context_bind(char *name, zval *value) { zend_hash_str_update(&EG(symbol_table), name, strlen(name), value); } +static void _context_ini(char *name, char *value) { + zend_string *n, *v; + + // Use "permanent" strings + n = zend_string_init(name, strlen(name), 1); + v = zend_string_init(value, strlen(value), 1); + if (zend_alter_ini_entry_ex(n, v, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0) == FAILURE) { + errno = 1; + } +} + static void _context_eval(zend_op_array *op, zval *ret) { EG(no_extensions) = 1; From e65b619ad63c50c154b65ecdf2ae52c890ec1a3f Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Tue, 2 Oct 2018 01:27:09 +0200 Subject: [PATCH 2/2] Fix leftover from branch messup --- include/context.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/context.h b/include/context.h index d44d81a..e614b7a 100644 --- a/include/context.h +++ b/include/context.h @@ -9,8 +9,8 @@ typedef struct _engine_context { } engine_context; engine_context *context_new(); -void context_exec(engine_context *context, char *filename, int *exit); -void *context_eval(engine_context *context, char *script, int *exit); +void context_exec(engine_context *context, char *filename); +void *context_eval(engine_context *context, char *script); void context_bind(engine_context *context, char *name, void *value); void context_ini(engine_context *context, char *name, char *value); void context_destroy(engine_context *context);