From 804501165786b18f96b9254a3993e51766baf68b Mon Sep 17 00:00:00 2001 From: Robert Landers Date: Sat, 19 Jul 2025 16:14:56 +0200 Subject: [PATCH 01/26] simply use the built-in php-cli Signed-off-by: Robert Landers --- frankenphp.c | 118 +-------------------------------------------------- 1 file changed, 2 insertions(+), 116 deletions(-) diff --git a/frankenphp.c b/frankenphp.c index 9f1a085648..77a4000dde 100644 --- a/frankenphp.c +++ b/frankenphp.c @@ -25,6 +25,7 @@ #elif defined(__FreeBSD__) || defined(__OpenBSD__) #include #endif +#include #include "_cgo_export.h" #include "frankenphp_arginfo.h" @@ -1020,123 +1021,8 @@ static char *cli_script; static int cli_argc; static char **cli_argv; -/* - * CLI code is adapted from - * https://github.com/php/php-src/blob/master/sapi/cli/php_cli.c Copyright (c) - * The PHP Group Licensed under The PHP License Original uthors: Edin Kadribasic - * , Marcus Boerger and Johannes Schlueter - * Parts based on CGI SAPI Module by Rasmus Lerdorf, Stig - * Bakken and Zeev Suraski - */ -static void cli_register_file_handles(bool no_close) /* {{{ */ -{ - php_stream *s_in, *s_out, *s_err; - php_stream_context *sc_in = NULL, *sc_out = NULL, *sc_err = NULL; - zend_constant ic, oc, ec; - - s_in = php_stream_open_wrapper_ex("php://stdin", "rb", 0, NULL, sc_in); - s_out = php_stream_open_wrapper_ex("php://stdout", "wb", 0, NULL, sc_out); - s_err = php_stream_open_wrapper_ex("php://stderr", "wb", 0, NULL, sc_err); - - if (s_in == NULL || s_out == NULL || s_err == NULL) { - if (s_in) - php_stream_close(s_in); - if (s_out) - php_stream_close(s_out); - if (s_err) - php_stream_close(s_err); - return; - } - - if (no_close) { - s_in->flags |= PHP_STREAM_FLAG_NO_CLOSE; - s_out->flags |= PHP_STREAM_FLAG_NO_CLOSE; - s_err->flags |= PHP_STREAM_FLAG_NO_CLOSE; - } - - /*s_in_process = s_in;*/ - - php_stream_to_zval(s_in, &ic.value); - php_stream_to_zval(s_out, &oc.value); - php_stream_to_zval(s_err, &ec.value); - - ZEND_CONSTANT_SET_FLAGS(&ic, CONST_CS, 0); - ic.name = zend_string_init_interned("STDIN", sizeof("STDIN") - 1, 0); - zend_register_constant(&ic); - - ZEND_CONSTANT_SET_FLAGS(&oc, CONST_CS, 0); - oc.name = zend_string_init_interned("STDOUT", sizeof("STDOUT") - 1, 0); - zend_register_constant(&oc); - - ZEND_CONSTANT_SET_FLAGS(&ec, CONST_CS, 0); - ec.name = zend_string_init_interned("STDERR", sizeof("STDERR") - 1, 0); - zend_register_constant(&ec); -} -/* }}} */ - -static void sapi_cli_register_variables(zval *track_vars_array) /* {{{ */ -{ - size_t len = strlen(cli_script); - char *docroot = ""; - - /* - * In CGI mode, we consider the environment to be a part of the server - * variables - */ - php_import_environment_variables(track_vars_array); - - /* Build the special-case PHP_SELF variable for the CLI version */ - register_server_variable_filtered("PHP_SELF", &cli_script, &len, - track_vars_array); - register_server_variable_filtered("SCRIPT_NAME", &cli_script, &len, - track_vars_array); - - /* filenames are empty for stdin */ - register_server_variable_filtered("SCRIPT_FILENAME", &cli_script, &len, - track_vars_array); - register_server_variable_filtered("PATH_TRANSLATED", &cli_script, &len, - track_vars_array); - - /* just make it available */ - len = 0U; - register_server_variable_filtered("DOCUMENT_ROOT", &docroot, &len, - track_vars_array); -} -/* }}} */ - static void *execute_script_cli(void *arg) { - void *exit_status; - bool eval = (bool)arg; - - /* - * The SAPI name "cli" is hardcoded into too many programs... let's usurp it. - */ - php_embed_module.name = "cli"; - php_embed_module.pretty_name = "PHP CLI embedded in FrankenPHP"; - php_embed_module.register_server_variables = sapi_cli_register_variables; - - php_embed_init(cli_argc, cli_argv); - - cli_register_file_handles(false); - zend_first_try { - if (eval) { - /* evaluate the cli_script as literal PHP code (php-cli -r "...") */ - zend_eval_string_ex(cli_script, NULL, "Command line code", 1); - } else { - zend_file_handle file_handle; - zend_stream_init_filename(&file_handle, cli_script); - - CG(skip_shebang) = 1; - php_execute_script(&file_handle); - } - } - zend_end_try(); - - exit_status = (void *)(intptr_t)EG(exit_status); - - php_embed_shutdown(); - - return exit_status; + return (void *)(intptr_t)do_php_cli(cli_argc, cli_argv); } int frankenphp_execute_script_cli(char *script, int argc, char **argv, From 44408b65144231507ec4d70bae9ec8901e23474b Mon Sep 17 00:00:00 2001 From: DubbleClick Date: Sun, 3 Aug 2025 17:29:47 +0700 Subject: [PATCH 02/26] refactor code around calling do_php_cli --- caddy/php-cli.go | 21 +++++++-------------- frankenphp.go | 7 ++----- frankenphp_test.go | 2 +- internal/testcli/main.go | 6 +----- 4 files changed, 11 insertions(+), 25 deletions(-) diff --git a/caddy/php-cli.go b/caddy/php-cli.go index 4e76ff147a..a975981d22 100644 --- a/caddy/php-cli.go +++ b/caddy/php-cli.go @@ -1,9 +1,9 @@ package caddy import ( - "errors" "os" "path/filepath" + "strings" caddycmd "github.com/caddyserver/caddy/v2/cmd" "github.com/dunglas/frankenphp" @@ -26,23 +26,16 @@ Executes a PHP script similarly to the CLI SAPI.`, } func cmdPHPCLI(fs caddycmd.Flags) (int, error) { - args := os.Args[2:] - if len(args) < 1 { - return 1, errors.New("the path to the PHP script is required") - } + // php's cli sapi expects the 0th arg to be the program itself, only filter out 'php-cli' arg + args := append([]string{os.Args[0]}, os.Args[2:]...) - if frankenphp.EmbeddedAppPath != "" { - if _, err := os.Stat(args[0]); err != nil { - args[0] = filepath.Join(frankenphp.EmbeddedAppPath, args[0]) + if frankenphp.EmbeddedAppPath != "" && len(args) > 1 && !strings.HasPrefix(args[1], "-") && strings.HasSuffix(args[1], ".php") { + if _, err := os.Stat(args[1]); err != nil { + args[1] = filepath.Join(frankenphp.EmbeddedAppPath, args[1]) } } - var status int - if len(args) >= 2 && args[0] == "-r" { - status = frankenphp.ExecutePHPCode(args[1]) - } else { - status = frankenphp.ExecuteScriptCLI(args[0], args) - } + status := frankenphp.ExecuteScriptCLI(args) os.Exit(status) diff --git a/frankenphp.go b/frankenphp.go index d2437dbf06..c07daaa063 100644 --- a/frankenphp.go +++ b/frankenphp.go @@ -634,14 +634,11 @@ func go_is_context_done(threadIndex C.uintptr_t) C.bool { // ExecuteScriptCLI executes the PHP script passed as parameter. // It returns the exit status code of the script. -func ExecuteScriptCLI(script string, args []string) int { - cScript := C.CString(script) - defer C.free(unsafe.Pointer(cScript)) - +func ExecuteScriptCLI(args []string) int { argc, argv := convertArgs(args) defer freeArgs(argv) - return int(C.frankenphp_execute_script_cli(cScript, argc, (**C.char)(unsafe.Pointer(&argv[0])), false)) + return int(C.frankenphp_execute_script_cli(nil, argc, (**C.char)(unsafe.Pointer(&argv[0])), false)) } func ExecutePHPCode(phpCode string) int { diff --git a/frankenphp_test.go b/frankenphp_test.go index 24241e058b..9d40153d79 100644 --- a/frankenphp_test.go +++ b/frankenphp_test.go @@ -834,7 +834,7 @@ func ExampleExecuteScriptCLI() { os.Exit(1) } - os.Exit(frankenphp.ExecuteScriptCLI(os.Args[1], os.Args)) + os.Exit(frankenphp.ExecuteScriptCLI(os.Args)) } func BenchmarkHelloWorld(b *testing.B) { diff --git a/internal/testcli/main.go b/internal/testcli/main.go index c03c836c4d..40c2534695 100644 --- a/internal/testcli/main.go +++ b/internal/testcli/main.go @@ -13,9 +13,5 @@ func main() { os.Exit(1) } - if len(os.Args) == 3 && os.Args[1] == "-r" { - os.Exit(frankenphp.ExecutePHPCode(os.Args[2])) - } - - os.Exit(frankenphp.ExecuteScriptCLI(os.Args[1], os.Args)) + os.Exit(frankenphp.ExecuteScriptCLI(os.Args)) } From 2b75b0056bd9b3fc2591013f84efb87277b71644 Mon Sep 17 00:00:00 2001 From: DubbleClick Date: Sun, 3 Aug 2025 17:40:52 +0700 Subject: [PATCH 03/26] move old cli sapi emulation out to emulate_php_cli.c --- emulate_php_cli.c | 135 ++++++++++++++++++++++++++++++++++++++++++++++ emulate_php_cli.h | 1 + frankenphp.c | 10 ++++ 3 files changed, 146 insertions(+) create mode 100644 emulate_php_cli.c create mode 100644 emulate_php_cli.h diff --git a/emulate_php_cli.c b/emulate_php_cli.c new file mode 100644 index 0000000000..9a4a33792a --- /dev/null +++ b/emulate_php_cli.c @@ -0,0 +1,135 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * CLI code is adapted from + * https://github.com/php/php-src/blob/master/sapi/cli/php_cli.c Copyright (c) + * The PHP Group Licensed under The PHP License Original uthors: Edin Kadribasic + * , Marcus Boerger and Johannes Schlueter + * Parts based on CGI SAPI Module by Rasmus Lerdorf, Stig + * Bakken and Zeev Suraski + */ +static void cli_register_file_handles(bool no_close) /* {{{ */ +{ + php_stream *s_in, *s_out, *s_err; + php_stream_context *sc_in = NULL, *sc_out = NULL, *sc_err = NULL; + zend_constant ic, oc, ec; + + s_in = php_stream_open_wrapper_ex("php://stdin", "rb", 0, NULL, sc_in); + s_out = php_stream_open_wrapper_ex("php://stdout", "wb", 0, NULL, sc_out); + s_err = php_stream_open_wrapper_ex("php://stderr", "wb", 0, NULL, sc_err); + + if (s_in == NULL || s_out == NULL || s_err == NULL) { + if (s_in) + php_stream_close(s_in); + if (s_out) + php_stream_close(s_out); + if (s_err) + php_stream_close(s_err); + return; + } + + if (no_close) { + s_in->flags |= PHP_STREAM_FLAG_NO_CLOSE; + s_out->flags |= PHP_STREAM_FLAG_NO_CLOSE; + s_err->flags |= PHP_STREAM_FLAG_NO_CLOSE; + } + + /*s_in_process = s_in;*/ + + php_stream_to_zval(s_in, &ic.value); + php_stream_to_zval(s_out, &oc.value); + php_stream_to_zval(s_err, &ec.value); + + ZEND_CONSTANT_SET_FLAGS(&ic, CONST_CS, 0); + ic.name = zend_string_init_interned("STDIN", sizeof("STDIN") - 1, 0); + zend_register_constant(&ic); + + ZEND_CONSTANT_SET_FLAGS(&oc, CONST_CS, 0); + oc.name = zend_string_init_interned("STDOUT", sizeof("STDOUT") - 1, 0); + zend_register_constant(&oc); + + ZEND_CONSTANT_SET_FLAGS(&ec, CONST_CS, 0); + ec.name = zend_string_init_interned("STDERR", sizeof("STDERR") - 1, 0); + zend_register_constant(&ec); +} +/* }}} */ + +static void sapi_cli_register_variables(zval *track_vars_array) /* {{{ */ +{ + size_t len = strlen(cli_script); + char *docroot = ""; + + /* + * In CGI mode, we consider the environment to be a part of the server + * variables + */ + php_import_environment_variables(track_vars_array); + + /* Build the special-case PHP_SELF variable for the CLI version */ + register_server_variable_filtered("PHP_SELF", &cli_script, &len, + track_vars_array); + register_server_variable_filtered("SCRIPT_NAME", &cli_script, &len, + track_vars_array); + + /* filenames are empty for stdin */ + register_server_variable_filtered("SCRIPT_FILENAME", &cli_script, &len, + track_vars_array); + register_server_variable_filtered("PATH_TRANSLATED", &cli_script, &len, + track_vars_array); + + /* just make it available */ + len = 0U; + register_server_variable_filtered("DOCUMENT_ROOT", &docroot, &len, + track_vars_array); +} +/* }}} */ + +void *emulate_script_cli(void *arg) { + void *exit_status; + bool eval = (bool)arg; + + /* + * The SAPI name "cli" is hardcoded into too many programs... let's usurp it. + */ + php_embed_module.name = "cli"; + php_embed_module.pretty_name = "PHP CLI embedded in FrankenPHP"; + php_embed_module.register_server_variables = sapi_cli_register_variables; + + php_embed_init(cli_argc, cli_argv); + + cli_register_file_handles(false); + zend_first_try { + if (eval) { + /* evaluate the cli_script as literal PHP code (php-cli -r "...") */ + zend_eval_string_ex(cli_script, NULL, "Command line code", 1); + } else { + zend_file_handle file_handle; + zend_stream_init_filename(&file_handle, cli_script); + + CG(skip_shebang) = 1; + php_execute_script(&file_handle); + } + } + zend_end_try(); + + exit_status = (void *)(intptr_t)EG(exit_status); + + php_embed_shutdown(); + + return exit_status; +} \ No newline at end of file diff --git a/emulate_php_cli.h b/emulate_php_cli.h new file mode 100644 index 0000000000..7850eed277 --- /dev/null +++ b/emulate_php_cli.h @@ -0,0 +1 @@ +void *emulate_script_cli(void *arg); \ No newline at end of file diff --git a/frankenphp.c b/frankenphp.c index 77a4000dde..0545211b82 100644 --- a/frankenphp.c +++ b/frankenphp.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -25,7 +26,12 @@ #elif defined(__FreeBSD__) || defined(__OpenBSD__) #include #endif + +#if PHP_VERSION_ID >= 85000 #include +#else +#include "emulate_php_cli.h" +#endif #include "_cgo_export.h" #include "frankenphp_arginfo.h" @@ -1022,7 +1028,11 @@ static int cli_argc; static char **cli_argv; static void *execute_script_cli(void *arg) { +#if PHP_VERSION_ID >= 85000 return (void *)(intptr_t)do_php_cli(cli_argc, cli_argv); +#else + return emulate_php_cli(arg); +#endif } int frankenphp_execute_script_cli(char *script, int argc, char **argv, From a8762a06121b3094a995707b559d05a69c39ac4d Mon Sep 17 00:00:00 2001 From: DubbleClick Date: Sun, 3 Aug 2025 17:46:16 +0700 Subject: [PATCH 04/26] EOF newlines --- emulate_php_cli.c | 2 +- emulate_php_cli.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/emulate_php_cli.c b/emulate_php_cli.c index 9a4a33792a..007dfb296e 100644 --- a/emulate_php_cli.c +++ b/emulate_php_cli.c @@ -132,4 +132,4 @@ void *emulate_script_cli(void *arg) { php_embed_shutdown(); return exit_status; -} \ No newline at end of file +} diff --git a/emulate_php_cli.h b/emulate_php_cli.h index 7850eed277..8a3015ff3d 100644 --- a/emulate_php_cli.h +++ b/emulate_php_cli.h @@ -1 +1 @@ -void *emulate_script_cli(void *arg); \ No newline at end of file +void *emulate_script_cli(void *arg); From dce590c713e8edbfbe9bc7bc7164fde5c8b23716 Mon Sep 17 00:00:00 2001 From: DubbleClick Date: Sun, 3 Aug 2025 21:30:50 +0700 Subject: [PATCH 05/26] shuffle things to work --- emulate_php_cli.c | 45 +++++++++++++++++++++++++++++++++++---------- frankenphp.c | 26 +++++++++++++++----------- 2 files changed, 50 insertions(+), 21 deletions(-) diff --git a/emulate_php_cli.c b/emulate_php_cli.c index 007dfb296e..cb19e5f40a 100644 --- a/emulate_php_cli.c +++ b/emulate_php_cli.c @@ -14,6 +14,29 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include +#if defined(__linux__) +#include +#elif defined(__FreeBSD__) || defined(__OpenBSD__) +#include +#endif + +typedef struct { + char *script; + int argc; + char **argv; + bool eval; +} cli_exec_args_t; +cli_exec_args_t *cli_args; + +/* Function declaration to avoid implicit declaration error */ +void register_server_variable_filtered(const char *key, char **val, size_t *val_len, zval *track_vars_array); /* * CLI code is adapted from @@ -71,7 +94,7 @@ static void cli_register_file_handles(bool no_close) /* {{{ */ static void sapi_cli_register_variables(zval *track_vars_array) /* {{{ */ { - size_t len = strlen(cli_script); + size_t len = strlen(cli_args->script); char *docroot = ""; /* @@ -81,15 +104,15 @@ static void sapi_cli_register_variables(zval *track_vars_array) /* {{{ */ php_import_environment_variables(track_vars_array); /* Build the special-case PHP_SELF variable for the CLI version */ - register_server_variable_filtered("PHP_SELF", &cli_script, &len, + register_server_variable_filtered("PHP_SELF", &cli_args->script, &len, track_vars_array); - register_server_variable_filtered("SCRIPT_NAME", &cli_script, &len, + register_server_variable_filtered("SCRIPT_NAME", &cli_args->script, &len, track_vars_array); /* filenames are empty for stdin */ - register_server_variable_filtered("SCRIPT_FILENAME", &cli_script, &len, + register_server_variable_filtered("SCRIPT_FILENAME", &cli_args->script, &len, track_vars_array); - register_server_variable_filtered("PATH_TRANSLATED", &cli_script, &len, + register_server_variable_filtered("PATH_TRANSLATED", &cli_args->script, &len, track_vars_array); /* just make it available */ @@ -101,7 +124,9 @@ static void sapi_cli_register_variables(zval *track_vars_array) /* {{{ */ void *emulate_script_cli(void *arg) { void *exit_status; - bool eval = (bool)arg; + cli_exec_args_t* args = arg; + cli_args = args; + bool eval = args->eval; /* * The SAPI name "cli" is hardcoded into too many programs... let's usurp it. @@ -110,16 +135,16 @@ void *emulate_script_cli(void *arg) { php_embed_module.pretty_name = "PHP CLI embedded in FrankenPHP"; php_embed_module.register_server_variables = sapi_cli_register_variables; - php_embed_init(cli_argc, cli_argv); + php_embed_init(cli_args->argc, cli_args->argv); cli_register_file_handles(false); zend_first_try { if (eval) { - /* evaluate the cli_script as literal PHP code (php-cli -r "...") */ - zend_eval_string_ex(cli_script, NULL, "Command line code", 1); + /* evaluate the cli_args->script as literal PHP code (php-cli -r "...") */ + zend_eval_string_ex(cli_args->script, NULL, "Command line code", 1); } else { zend_file_handle file_handle; - zend_stream_init_filename(&file_handle, cli_script); + zend_stream_init_filename(&file_handle, cli_args->script); CG(skip_shebang) = 1; php_execute_script(&file_handle); diff --git a/frankenphp.c b/frankenphp.c index 0545211b82..f8d008531f 100644 --- a/frankenphp.c +++ b/frankenphp.c @@ -755,7 +755,7 @@ void frankenphp_register_variable_safe(char *key, char *val, size_t val_len, } } -static inline void register_server_variable_filtered(const char *key, +void register_server_variable_filtered(const char *key, char **val, size_t *val_len, zval *track_vars_array) { @@ -1022,16 +1022,20 @@ int frankenphp_execute_script(char *file_name) { return status; } -/* Use global variables to store CLI arguments to prevent useless allocations */ -static char *cli_script; -static int cli_argc; -static char **cli_argv; +typedef struct { + char *script; + int argc; + char **argv; + bool eval; +} cli_exec_args_t; static void *execute_script_cli(void *arg) { + cli_exec_args_t *args = (cli_exec_args_t *)arg; + #if PHP_VERSION_ID >= 85000 - return (void *)(intptr_t)do_php_cli(cli_argc, cli_argv); + return (void *)(intptr_t)do_php_cli(args->argc, args->argv); #else - return emulate_php_cli(arg); + return (void *)(intptr_t)emulate_script_cli(args); #endif } @@ -1041,15 +1045,15 @@ int frankenphp_execute_script_cli(char *script, int argc, char **argv, int err; void *exit_status; - cli_script = script; - cli_argc = argc; - cli_argv = argv; + cli_exec_args_t args = { + .script = script, .argc = argc, .argv = argv, .eval = eval + }; /* * Start the script in a dedicated thread to prevent conflicts between Go and * PHP signal handlers */ - err = pthread_create(&thread, NULL, execute_script_cli, (void *)eval); + err = pthread_create(&thread, NULL, execute_script_cli, &args); if (err != 0) { return err; } From e335244596820b341f19d1aa6b0099a3a46efff0 Mon Sep 17 00:00:00 2001 From: DubbleClick Date: Sun, 3 Aug 2025 22:24:03 +0700 Subject: [PATCH 06/26] fix 80500 --- frankenphp.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/frankenphp.c b/frankenphp.c index f8d008531f..ad0283d76e 100644 --- a/frankenphp.c +++ b/frankenphp.c @@ -27,7 +27,7 @@ #include #endif -#if PHP_VERSION_ID >= 85000 +#if PHP_VERSION_ID >= 80500 #include #else #include "emulate_php_cli.h" @@ -1031,8 +1031,10 @@ typedef struct { static void *execute_script_cli(void *arg) { cli_exec_args_t *args = (cli_exec_args_t *)arg; + volatile int v = PHP_VERSION_ID; + (void)v; -#if PHP_VERSION_ID >= 85000 +#if PHP_VERSION_ID >= 80500 return (void *)(intptr_t)do_php_cli(args->argc, args->argv); #else return (void *)(intptr_t)emulate_script_cli(args); From 4726593abcc110c5a8fce40a5bf707cf5ddcdd6e Mon Sep 17 00:00:00 2001 From: henderkes Date: Fri, 6 Mar 2026 15:19:14 +0700 Subject: [PATCH 07/26] clang format --- emulate_php_cli.c | 5 +++-- frankenphp.c | 10 ++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/emulate_php_cli.c b/emulate_php_cli.c index cb19e5f40a..faef1ed142 100644 --- a/emulate_php_cli.c +++ b/emulate_php_cli.c @@ -36,7 +36,8 @@ typedef struct { cli_exec_args_t *cli_args; /* Function declaration to avoid implicit declaration error */ -void register_server_variable_filtered(const char *key, char **val, size_t *val_len, zval *track_vars_array); +void register_server_variable_filtered(const char *key, char **val, + size_t *val_len, zval *track_vars_array); /* * CLI code is adapted from @@ -124,7 +125,7 @@ static void sapi_cli_register_variables(zval *track_vars_array) /* {{{ */ void *emulate_script_cli(void *arg) { void *exit_status; - cli_exec_args_t* args = arg; + cli_exec_args_t *args = arg; cli_args = args; bool eval = args->eval; diff --git a/frankenphp.c b/frankenphp.c index 09a1f262af..d798160d0c 100644 --- a/frankenphp.c +++ b/frankenphp.c @@ -949,10 +949,9 @@ void frankenphp_register_variable_safe(char *key, char *val, size_t val_len, } } -void register_server_variable_filtered(const char *key, - char **val, - size_t *val_len, - zval *track_vars_array) { +void register_server_variable_filtered(const char *key, char **val, + size_t *val_len, + zval *track_vars_array) { if (sapi_module.input_filter(PARSE_SERVER, key, val, *val_len, val_len)) { php_register_variable_safe(key, *val, *val_len, track_vars_array); } @@ -1271,8 +1270,7 @@ int frankenphp_execute_script_cli(char *script, int argc, char **argv, void *exit_status; cli_exec_args_t args = { - .script = script, .argc = argc, .argv = argv, .eval = eval - }; + .script = script, .argc = argc, .argv = argv, .eval = eval}; /* * Start the script in a dedicated thread to prevent conflicts between Go and From 9ca03588d42c51ffb2829fefe8b12f5903c40f5c Mon Sep 17 00:00:00 2001 From: henderkes Date: Fri, 6 Mar 2026 16:38:31 +0700 Subject: [PATCH 08/26] update for new compilation in php-src --- caddy/php-cli.go | 2 +- frankenphp.c | 6 ++---- frankenphp.go | 9 --------- 3 files changed, 3 insertions(+), 14 deletions(-) diff --git a/caddy/php-cli.go b/caddy/php-cli.go index a975981d22..53b754a59f 100644 --- a/caddy/php-cli.go +++ b/caddy/php-cli.go @@ -35,7 +35,7 @@ func cmdPHPCLI(fs caddycmd.Flags) (int, error) { } } - status := frankenphp.ExecuteScriptCLI(args) + status := frankenphp.ExecuteScriptCLI(args[0], args) os.Exit(status) diff --git a/frankenphp.c b/frankenphp.c index d798160d0c..0504a23ec5 100644 --- a/frankenphp.c +++ b/frankenphp.c @@ -36,9 +36,7 @@ #include #endif -#if PHP_VERSION_ID >= 80500 -#include -#else +#ifndef HAVE_EMBED_CLI #include "emulate_php_cli.h" #endif @@ -1256,7 +1254,7 @@ static void *execute_script_cli(void *arg) { volatile int v = PHP_VERSION_ID; (void)v; -#if PHP_VERSION_ID >= 80500 +#if HAVE_EMBED_CLI return (void *)(intptr_t)do_php_cli(args->argc, args->argv); #else return (void *)(intptr_t)emulate_script_cli(args); diff --git a/frankenphp.go b/frankenphp.go index 99fd34ec0a..168fb1af98 100644 --- a/frankenphp.go +++ b/frankenphp.go @@ -750,15 +750,6 @@ func go_is_context_done(threadIndex C.uintptr_t) C.bool { return C.bool(phpThreads[threadIndex].frankenPHPContext().isDone) } -func ExecuteScriptCLI(args []string) int { - // Ensure extensions are registered before CLI execution - registerExtensions() - argc, argv := convertArgs(args) - defer freeArgs(argv) - - return int(C.frankenphp_execute_script_cli(nil, argc, (**C.char)(unsafe.Pointer(&argv[0])), false)) -} - func convertArgs(args []string) (C.int, []*C.char) { argc := C.int(len(args)) argv := make([]*C.char, argc) From 0f28cc044b36944095d9c8fa545f940668492160 Mon Sep 17 00:00:00 2001 From: henderkes Date: Fri, 6 Mar 2026 16:45:06 +0700 Subject: [PATCH 09/26] fix cli test --- internal/testcli/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/testcli/main.go b/internal/testcli/main.go index 40c2534695..5f8dc97cff 100644 --- a/internal/testcli/main.go +++ b/internal/testcli/main.go @@ -13,5 +13,5 @@ func main() { os.Exit(1) } - os.Exit(frankenphp.ExecuteScriptCLI(os.Args)) + os.Exit(frankenphp.ExecuteScriptCLI(os.Args[0], os.Args[1:])) } From 30ff79665698f3b099131435a29e152da24a0f04 Mon Sep 17 00:00:00 2001 From: henderkes Date: Fri, 6 Mar 2026 17:00:03 +0700 Subject: [PATCH 10/26] remove duplicate test --- frankenphp_test.go | 9 --------- 1 file changed, 9 deletions(-) diff --git a/frankenphp_test.go b/frankenphp_test.go index 08714c9232..0301b20276 100644 --- a/frankenphp_test.go +++ b/frankenphp_test.go @@ -809,15 +809,6 @@ func ExampleServeHTTP() { log.Fatal(http.ListenAndServe(":8080", nil)) } -func ExampleExecuteScriptCLI() { - if len(os.Args) <= 1 { - log.Println("Usage: my-program script.php") - os.Exit(1) - } - - os.Exit(frankenphp.ExecuteScriptCLI(os.Args)) -} - func BenchmarkHelloWorld(b *testing.B) { require.NoError(b, frankenphp.Init()) b.Cleanup(frankenphp.Shutdown) From c40a2572be77511d1d65b9179b9c84c72b54deb3 Mon Sep 17 00:00:00 2001 From: henderkes Date: Fri, 6 Mar 2026 17:07:16 +0700 Subject: [PATCH 11/26] fix expected args for php-cli, though this won't help the strangely failing tests --- cli_test.go | 2 +- internal/testcli/main.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cli_test.go b/cli_test.go index f9ee03fea2..7a06acadab 100644 --- a/cli_test.go +++ b/cli_test.go @@ -51,5 +51,5 @@ func ExampleExecuteScriptCLI() { os.Exit(1) } - os.Exit(frankenphp.ExecuteScriptCLI(os.Args[1], os.Args)) + os.Exit(frankenphp.ExecuteScriptCLI(os.Args[0], os.Args)) } diff --git a/internal/testcli/main.go b/internal/testcli/main.go index 5f8dc97cff..8086f549e8 100644 --- a/internal/testcli/main.go +++ b/internal/testcli/main.go @@ -13,5 +13,5 @@ func main() { os.Exit(1) } - os.Exit(frankenphp.ExecuteScriptCLI(os.Args[0], os.Args[1:])) + os.Exit(frankenphp.ExecuteScriptCLI(os.Args[0], os.Args)) } From 82a62b0db93ad6dbb28685038c564fe212bd6e3a Mon Sep 17 00:00:00 2001 From: henderkes Date: Fri, 6 Mar 2026 17:39:03 +0700 Subject: [PATCH 12/26] executephpcode --- internal/testcli/main.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internal/testcli/main.go b/internal/testcli/main.go index 8086f549e8..490b0763f6 100644 --- a/internal/testcli/main.go +++ b/internal/testcli/main.go @@ -13,5 +13,9 @@ func main() { os.Exit(1) } + if len(os.Args) == 3 && os.Args[1] == "-r" { + os.Exit(frankenphp.ExecutePHPCode(os.Args[2])) + } + os.Exit(frankenphp.ExecuteScriptCLI(os.Args[0], os.Args)) } From 1cbc658911bab7e3506a7beaf56887169bb3da68 Mon Sep 17 00:00:00 2001 From: henderkes Date: Fri, 6 Mar 2026 18:08:34 +0700 Subject: [PATCH 13/26] move script detection to C side, not Go side (because we only want to detect anything when HAVE_EMBED_CLI is false) --- cli.go | 9 --------- emulate_php_cli.c | 29 +++++++++++++++++++++++++---- internal/testcli/main.go | 4 ---- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/cli.go b/cli.go index 96821a2392..a96153a14a 100644 --- a/cli.go +++ b/cli.go @@ -18,12 +18,3 @@ func ExecuteScriptCLI(script string, args []string) int { return int(C.frankenphp_execute_script_cli(cScript, argc, (**C.char)(unsafe.Pointer(&argv[0])), false)) } - -func ExecutePHPCode(phpCode string) int { - // Ensure extensions are registered before CLI execution - registerExtensions() - - cCode := C.CString(phpCode) - defer C.free(unsafe.Pointer(cCode)) - return int(C.frankenphp_execute_script_cli(cCode, 0, nil, true)) -} diff --git a/emulate_php_cli.c b/emulate_php_cli.c index faef1ed142..ed440d019f 100644 --- a/emulate_php_cli.c +++ b/emulate_php_cli.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #if defined(__linux__) #include @@ -127,7 +128,27 @@ void *emulate_script_cli(void *arg) { void *exit_status; cli_exec_args_t *args = arg; cli_args = args; - bool eval = args->eval; + + /* Parse argv to detect -r (eval mode) and find the script path */ + bool eval = false; + char *script = NULL; + for (int i = 1; i < args->argc; i++) { + if (strcmp(args->argv[i], "-r") == 0 && i + 1 < args->argc) { + eval = true; + script = args->argv[i + 1]; + break; + } else if (args->argv[i][0] != '-') { + script = args->argv[i]; + break; + } + } + + if (script == NULL) { + return (void *)(intptr_t)1; + } + + /* Update cli_args->script so sapi_cli_register_variables uses the right path */ + cli_args->script = script; /* * The SAPI name "cli" is hardcoded into too many programs... let's usurp it. @@ -141,11 +162,11 @@ void *emulate_script_cli(void *arg) { cli_register_file_handles(false); zend_first_try { if (eval) { - /* evaluate the cli_args->script as literal PHP code (php-cli -r "...") */ - zend_eval_string_ex(cli_args->script, NULL, "Command line code", 1); + /* evaluate script as literal PHP code (php-cli -r "...") */ + zend_eval_string_ex(script, NULL, "Command line code", 1); } else { zend_file_handle file_handle; - zend_stream_init_filename(&file_handle, cli_args->script); + zend_stream_init_filename(&file_handle, script); CG(skip_shebang) = 1; php_execute_script(&file_handle); diff --git a/internal/testcli/main.go b/internal/testcli/main.go index 490b0763f6..8086f549e8 100644 --- a/internal/testcli/main.go +++ b/internal/testcli/main.go @@ -13,9 +13,5 @@ func main() { os.Exit(1) } - if len(os.Args) == 3 && os.Args[1] == "-r" { - os.Exit(frankenphp.ExecutePHPCode(os.Args[2])) - } - os.Exit(frankenphp.ExecuteScriptCLI(os.Args[0], os.Args)) } From e2b611379cd22be63af9998f723217066dc1f8c8 Mon Sep 17 00:00:00 2001 From: henderkes Date: Fri, 6 Mar 2026 18:23:52 +0700 Subject: [PATCH 14/26] fix on windows? --- emulate_php_cli.c | 16 ++++++++++++++-- emulate_php_cli.h | 4 +++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/emulate_php_cli.c b/emulate_php_cli.c index ed440d019f..92d58212ba 100644 --- a/emulate_php_cli.c +++ b/emulate_php_cli.c @@ -1,4 +1,7 @@ -#include +#ifndef HAVE_EMBED_CLI + +#include "frankenphp.h" +#include #include #include #include @@ -8,7 +11,11 @@ #include #include #include +#ifdef PHP_WIN32 +#include +#else #include +#endif #include #include #include @@ -21,7 +28,9 @@ #include #include #include +#ifndef ZEND_WIN32 #include +#endif #if defined(__linux__) #include #elif defined(__FreeBSD__) || defined(__OpenBSD__) @@ -147,7 +156,8 @@ void *emulate_script_cli(void *arg) { return (void *)(intptr_t)1; } - /* Update cli_args->script so sapi_cli_register_variables uses the right path */ + /* Update cli_args->script so sapi_cli_register_variables uses the right path + */ cli_args->script = script; /* @@ -180,3 +190,5 @@ void *emulate_script_cli(void *arg) { return exit_status; } + +#endif /* !HAVE_EMBED_CLI */ diff --git a/emulate_php_cli.h b/emulate_php_cli.h index 8a3015ff3d..61680bb8ff 100644 --- a/emulate_php_cli.h +++ b/emulate_php_cli.h @@ -1 +1,3 @@ -void *emulate_script_cli(void *arg); +#ifndef HAVE_EMBED_CLI +void *emulate_script_cli(void *arg); +#endif From 72633eff83e703c3431b7070dc2a5e4a5da952bf Mon Sep 17 00:00:00 2001 From: henderkes Date: Sun, 8 Mar 2026 00:57:32 +0700 Subject: [PATCH 15/26] php 8.6 --- emulate_php_cli.c | 9 ++++----- emulate_php_cli.h | 2 +- frankenphp.c | 6 ++++-- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/emulate_php_cli.c b/emulate_php_cli.c index 92d58212ba..8cea05d881 100644 --- a/emulate_php_cli.c +++ b/emulate_php_cli.c @@ -1,7 +1,4 @@ -#ifndef HAVE_EMBED_CLI - -#include "frankenphp.h" -#include +#include #include #include #include @@ -37,6 +34,8 @@ #include #endif +#if PHP_VERSION_ID >= 80600 + typedef struct { char *script; int argc; @@ -191,4 +190,4 @@ void *emulate_script_cli(void *arg) { return exit_status; } -#endif /* !HAVE_EMBED_CLI */ +#endif /* PHP_VERSION_ID >= 80600 */ diff --git a/emulate_php_cli.h b/emulate_php_cli.h index 61680bb8ff..c86b11f0ae 100644 --- a/emulate_php_cli.h +++ b/emulate_php_cli.h @@ -1,3 +1,3 @@ -#ifndef HAVE_EMBED_CLI +#if PHP_VERSION_ID < 80600 void *emulate_script_cli(void *arg); #endif diff --git a/frankenphp.c b/frankenphp.c index 0504a23ec5..f5e90bf99b 100644 --- a/frankenphp.c +++ b/frankenphp.c @@ -36,7 +36,9 @@ #include #endif -#ifndef HAVE_EMBED_CLI +#if PHP_VERSION_ID >= 80600 +#include +#else #include "emulate_php_cli.h" #endif @@ -1254,7 +1256,7 @@ static void *execute_script_cli(void *arg) { volatile int v = PHP_VERSION_ID; (void)v; -#if HAVE_EMBED_CLI +#if PHP_VERSION_ID >= 80600 return (void *)(intptr_t)do_php_cli(args->argc, args->argv); #else return (void *)(intptr_t)emulate_script_cli(args); From dc9dd5f5131e2940af2397789a8fd43c60040162 Mon Sep 17 00:00:00 2001 From: henderkes Date: Sun, 8 Mar 2026 01:01:43 +0700 Subject: [PATCH 16/26] undefined references --- emulate_php_cli.c | 4 ---- emulate_php_cli.h | 4 +--- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/emulate_php_cli.c b/emulate_php_cli.c index 8cea05d881..9ddf80a24f 100644 --- a/emulate_php_cli.c +++ b/emulate_php_cli.c @@ -34,8 +34,6 @@ #include #endif -#if PHP_VERSION_ID >= 80600 - typedef struct { char *script; int argc; @@ -189,5 +187,3 @@ void *emulate_script_cli(void *arg) { return exit_status; } - -#endif /* PHP_VERSION_ID >= 80600 */ diff --git a/emulate_php_cli.h b/emulate_php_cli.h index c86b11f0ae..8a3015ff3d 100644 --- a/emulate_php_cli.h +++ b/emulate_php_cli.h @@ -1,3 +1 @@ -#if PHP_VERSION_ID < 80600 -void *emulate_script_cli(void *arg); -#endif +void *emulate_script_cli(void *arg); From 5bab8f50bb2885623288d23b99b4e5ab39e15600 Mon Sep 17 00:00:00 2001 From: henderkes Date: Sun, 8 Mar 2026 01:11:28 +0700 Subject: [PATCH 17/26] fix windows again --- emulate_php_cli.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/emulate_php_cli.c b/emulate_php_cli.c index 9ddf80a24f..50a46ddb7d 100644 --- a/emulate_php_cli.c +++ b/emulate_php_cli.c @@ -1,4 +1,5 @@ -#include +#include "frankenphp.h" +#include #include #include #include From 89628f0edac4d8aa5d81828a7bec1186b0d50a06 Mon Sep 17 00:00:00 2001 From: henderkes Date: Tue, 28 Jul 2026 19:26:13 +0700 Subject: [PATCH 18/26] fix borked merge --- emulate_php_cli.c | 9 ++--- emulate_php_cli.h | 9 ++++- frankenphp.c | 95 +---------------------------------------------- 3 files changed, 12 insertions(+), 101 deletions(-) diff --git a/emulate_php_cli.c b/emulate_php_cli.c index 50a46ddb7d..59c901bdd7 100644 --- a/emulate_php_cli.c +++ b/emulate_php_cli.c @@ -1,4 +1,5 @@ #include "frankenphp.h" +#include "emulate_php_cli.h" #include #include #include @@ -35,12 +36,7 @@ #include #endif -typedef struct { - char *script; - int argc; - char **argv; - bool eval; -} cli_exec_args_t; + cli_exec_args_t *cli_args; /* Function declaration to avoid implicit declaration error */ @@ -151,6 +147,7 @@ void *emulate_script_cli(void *arg) { } if (script == NULL) { + fprintf(stderr, "this functionality is not available in frankenphp php-cli with a php version lower than 8.6\n"); return (void *)(intptr_t)1; } diff --git a/emulate_php_cli.h b/emulate_php_cli.h index 8a3015ff3d..a0703c4b19 100644 --- a/emulate_php_cli.h +++ b/emulate_php_cli.h @@ -1 +1,8 @@ -void *emulate_script_cli(void *arg); +typedef struct { + char *script; + int argc; + char **argv; + bool eval; +} cli_exec_args_t; +extern cli_exec_args_t *cli_args; +void *emulate_script_cli(void *arg); diff --git a/frankenphp.c b/frankenphp.c index 22b7b9ac58..19b2c8d5d5 100644 --- a/frankenphp.c +++ b/frankenphp.c @@ -47,9 +47,8 @@ #if PHP_VERSION_ID >= 80600 #include -#else -#include "emulate_php_cli.h" #endif +#include "emulate_php_cli.h" #include "_cgo_export.h" #include "frankenphp_arginfo.h" @@ -1755,102 +1754,10 @@ bool frankenphp_new_php_thread(uintptr_t thread_index) { return true; } -/* Use global variables to store CLI arguments to prevent useless allocations */ -static char *cli_script; -static int cli_argc; -static char **cli_argv; - -/* - * CLI code is adapted from - * https://github.com/php/php-src/blob/master/sapi/cli/php_cli.c Copyright (c) - * The PHP Group Licensed under The PHP License Original uthors: Edin Kadribasic - * , Marcus Boerger and Johannes Schlueter - * Parts based on CGI SAPI Module by Rasmus Lerdorf, Stig - * Bakken and Zeev Suraski - */ -static void cli_register_file_handles(void) { - php_stream *s_in, *s_out, *s_err; - php_stream_context *sc_in = NULL, *sc_out = NULL, *sc_err = NULL; - zend_constant ic, oc, ec; - - s_in = php_stream_open_wrapper_ex("php://stdin", "rb", 0, NULL, sc_in); - s_out = php_stream_open_wrapper_ex("php://stdout", "wb", 0, NULL, sc_out); - s_err = php_stream_open_wrapper_ex("php://stderr", "wb", 0, NULL, sc_err); - - /* Release stream resources, but don't free the underlying handles. Othewrise, - * extensions which write to stderr or company during mshutdown/gshutdown - * won't have the expected functionality. - */ - if (s_in) - s_in->flags |= PHP_STREAM_FLAG_NO_RSCR_DTOR_CLOSE; - if (s_out) - s_out->flags |= PHP_STREAM_FLAG_NO_RSCR_DTOR_CLOSE; - if (s_err) - s_err->flags |= PHP_STREAM_FLAG_NO_RSCR_DTOR_CLOSE; - - if (s_in == NULL || s_out == NULL || s_err == NULL) { - if (s_in) - php_stream_close(s_in); - if (s_out) - php_stream_close(s_out); - if (s_err) - php_stream_close(s_err); - return; - } - - /*s_in_process = s_in;*/ - - php_stream_to_zval(s_in, &ic.value); - php_stream_to_zval(s_out, &oc.value); - php_stream_to_zval(s_err, &ec.value); - - ZEND_CONSTANT_SET_FLAGS(&ic, CONST_CS, 0); - ic.name = zend_string_init_interned("STDIN", sizeof("STDIN") - 1, 0); - zend_register_constant(&ic); - - ZEND_CONSTANT_SET_FLAGS(&oc, CONST_CS, 0); - oc.name = zend_string_init_interned("STDOUT", sizeof("STDOUT") - 1, 0); - zend_register_constant(&oc); - - ZEND_CONSTANT_SET_FLAGS(&ec, CONST_CS, 0); - ec.name = zend_string_init_interned("STDERR", sizeof("STDERR") - 1, 0); - zend_register_constant(&ec); -} - -static void sapi_cli_register_variables(zval *track_vars_array) /* {{{ */ -{ - size_t len = strlen(cli_script); - char *docroot = ""; - - /* - * In CGI mode, we consider the environment to be a part of the server - * variables - */ - php_import_environment_variables(track_vars_array); - - /* Build the special-case PHP_SELF variable for the CLI version */ - register_server_variable_filtered("PHP_SELF", &cli_script, &len, - track_vars_array); - register_server_variable_filtered("SCRIPT_NAME", &cli_script, &len, - track_vars_array); - - /* filenames are empty for stdin */ - register_server_variable_filtered("SCRIPT_FILENAME", &cli_script, &len, - track_vars_array); - register_server_variable_filtered("PATH_TRANSLATED", &cli_script, &len, - track_vars_array); - - /* just make it available */ - len = 0U; - register_server_variable_filtered("DOCUMENT_ROOT", &docroot, &len, - track_vars_array); -} /* }}} */ static void *execute_script_cli(void *arg) { cli_exec_args_t *args = (cli_exec_args_t *)arg; - volatile int v = PHP_VERSION_ID; - (void)v; #ifndef PHP_WIN32 sigset_t cli_signals; From 67c97ebf7c38c8ead7f95af1e750d0cde9d4ac95 Mon Sep 17 00:00:00 2001 From: Marc Date: Tue, 28 Jul 2026 19:32:32 +0700 Subject: [PATCH 19/26] Update emulate_php_cli.c Signed-off-by: Marc --- emulate_php_cli.c | 1 - 1 file changed, 1 deletion(-) diff --git a/emulate_php_cli.c b/emulate_php_cli.c index 59c901bdd7..c695b3aaf7 100644 --- a/emulate_php_cli.c +++ b/emulate_php_cli.c @@ -36,7 +36,6 @@ #include #endif - cli_exec_args_t *cli_args; /* Function declaration to avoid implicit declaration error */ From cd7a90895e8041c26ac10364d84824a3ff3a47fc Mon Sep 17 00:00:00 2001 From: henderkes Date: Tue, 28 Jul 2026 19:34:54 +0700 Subject: [PATCH 20/26] move var --- emulate_php_cli.c | 8 ++++++-- frankenphp.c | 8 -------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/emulate_php_cli.c b/emulate_php_cli.c index c695b3aaf7..73cf7f57a0 100644 --- a/emulate_php_cli.c +++ b/emulate_php_cli.c @@ -38,9 +38,13 @@ cli_exec_args_t *cli_args; -/* Function declaration to avoid implicit declaration error */ void register_server_variable_filtered(const char *key, char **val, - size_t *val_len, zval *track_vars_array); + size_t *val_len, + zval *track_vars_array) { + if (sapi_module.input_filter(PARSE_SERVER, key, val, *val_len, val_len)) { + php_register_variable_safe(key, *val, *val_len, track_vars_array); + } +} /* * CLI code is adapted from diff --git a/frankenphp.c b/frankenphp.c index 19b2c8d5d5..91911df0e2 100644 --- a/frankenphp.c +++ b/frankenphp.c @@ -1365,14 +1365,6 @@ void frankenphp_register_variable_safe(char *key, char *val, size_t val_len, } } -void register_server_variable_filtered(const char *key, char **val, - size_t *val_len, - zval *track_vars_array) { - if (sapi_module.input_filter(PARSE_SERVER, key, val, *val_len, val_len)) { - php_register_variable_safe(key, *val, *val_len, track_vars_array); - } -} - static void frankenphp_register_variables(zval *track_vars_array) { /* https://www.php.net/manual/en/reserved.variables.server.php */ From c4ddc34da46fca459af99f3368922fa83bfea00e Mon Sep 17 00:00:00 2001 From: henderkes Date: Tue, 28 Jul 2026 19:38:09 +0700 Subject: [PATCH 21/26] clang format --- emulate_php_cli.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/emulate_php_cli.c b/emulate_php_cli.c index 73cf7f57a0..f33f360359 100644 --- a/emulate_php_cli.c +++ b/emulate_php_cli.c @@ -1,5 +1,5 @@ -#include "frankenphp.h" -#include "emulate_php_cli.h" +#include "emulate_php_cli.h" +#include "frankenphp.h" #include #include #include @@ -38,9 +38,9 @@ cli_exec_args_t *cli_args; -void register_server_variable_filtered(const char *key, char **val, - size_t *val_len, - zval *track_vars_array) { +static void register_server_variable_filtered(const char *key, char **val, + size_t *val_len, + zval *track_vars_array) { if (sapi_module.input_filter(PARSE_SERVER, key, val, *val_len, val_len)) { php_register_variable_safe(key, *val, *val_len, track_vars_array); } @@ -150,7 +150,8 @@ void *emulate_script_cli(void *arg) { } if (script == NULL) { - fprintf(stderr, "this functionality is not available in frankenphp php-cli with a php version lower than 8.6\n"); + fprintf(stderr, "this functionality is not available in frankenphp php-cli " + "with a php version lower than 8.6\n"); return (void *)(intptr_t)1; } From a137c20bb91544db1937728181180cd031599d5c Mon Sep 17 00:00:00 2001 From: henderkes Date: Tue, 28 Jul 2026 19:45:54 +0700 Subject: [PATCH 22/26] bool undefined --- emulate_php_cli.c | 6 +++--- emulate_php_cli.h | 7 ++++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/emulate_php_cli.c b/emulate_php_cli.c index f33f360359..e190daae97 100644 --- a/emulate_php_cli.c +++ b/emulate_php_cli.c @@ -1,6 +1,4 @@ -#include "emulate_php_cli.h" -#include "frankenphp.h" -#include +#include #include #include #include @@ -35,6 +33,8 @@ #elif defined(__FreeBSD__) || defined(__OpenBSD__) #include #endif +#include "emulate_php_cli.h" +#include "frankenphp.h" cli_exec_args_t *cli_args; diff --git a/emulate_php_cli.h b/emulate_php_cli.h index a0703c4b19..2f4a29080c 100644 --- a/emulate_php_cli.h +++ b/emulate_php_cli.h @@ -1,4 +1,7 @@ -typedef struct { +#ifndef _EMULATE_PHP_CLI_H +#define _EMULATE_PHP_CLI_H + +typedef struct { char *script; int argc; char **argv; @@ -6,3 +9,5 @@ } cli_exec_args_t; extern cli_exec_args_t *cli_args; void *emulate_script_cli(void *arg); + +#endif From 6ac40fd9f22fb007945fd20d352bae7aaa1471bf Mon Sep 17 00:00:00 2001 From: henderkes Date: Tue, 28 Jul 2026 20:10:49 +0700 Subject: [PATCH 23/26] fix windows --- emulate_php_cli.c | 1 - emulate_php_cli.h | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/emulate_php_cli.c b/emulate_php_cli.c index e190daae97..b6be2a7da6 100644 --- a/emulate_php_cli.c +++ b/emulate_php_cli.c @@ -34,7 +34,6 @@ #include #endif #include "emulate_php_cli.h" -#include "frankenphp.h" cli_exec_args_t *cli_args; diff --git a/emulate_php_cli.h b/emulate_php_cli.h index 2f4a29080c..c12b2409c8 100644 --- a/emulate_php_cli.h +++ b/emulate_php_cli.h @@ -1,6 +1,8 @@ #ifndef _EMULATE_PHP_CLI_H #define _EMULATE_PHP_CLI_H +#include "frankenphp.h" + typedef struct { char *script; int argc; From 93e774a8d5b5e5da8f72da06c1b691e577726a02 Mon Sep 17 00:00:00 2001 From: henderkes Date: Tue, 28 Jul 2026 20:20:57 +0700 Subject: [PATCH 24/26] just use intsafe.h --- cgo.go | 2 +- emulate_php_cli.c | 5 +++-- emulate_php_cli.h | 2 -- frankenphp.h | 29 +---------------------------- internal/testext/exttest.go | 2 +- 5 files changed, 6 insertions(+), 34 deletions(-) diff --git a/cgo.go b/cgo.go index 8dc2609007..653329c956 100644 --- a/cgo.go +++ b/cgo.go @@ -6,6 +6,6 @@ package frankenphp // #cgo unix LDFLAGS: -lphp -lm -lutil // #cgo linux LDFLAGS: -ldl -lresolv // #cgo darwin LDFLAGS: -Wl,-rpath,/usr/local/lib -liconv -ldl -// #cgo windows CFLAGS: -D_WINDOWS -DWINDOWS=1 -DZEND_WIN32=1 -DPHP_WIN32=1 -DWIN32 -D_MBCS -D_USE_MATH_DEFINES -DNDebug -DNDEBUG -DZEND_DEBUG=0 -DZTS=1 -DFD_SETSIZE=256 +// #cgo windows CFLAGS: -D_WINDOWS -DWINDOWS=1 -DZEND_WIN32=1 -DPHP_WIN32=1 -DWIN32 -D_MBCS -D_USE_MATH_DEFINES -DNDebug -DNDEBUG -DZEND_DEBUG=0 -DZTS=1 -DFD_SETSIZE=256 -DENABLE_INTSAFE_SIGNED_FUNCTIONS // #cgo windows LDFLAGS: -lpthreadVC3 import "C" diff --git a/emulate_php_cli.c b/emulate_php_cli.c index b6be2a7da6..f33f360359 100644 --- a/emulate_php_cli.c +++ b/emulate_php_cli.c @@ -1,4 +1,6 @@ -#include +#include "emulate_php_cli.h" +#include "frankenphp.h" +#include #include #include #include @@ -33,7 +35,6 @@ #elif defined(__FreeBSD__) || defined(__OpenBSD__) #include #endif -#include "emulate_php_cli.h" cli_exec_args_t *cli_args; diff --git a/emulate_php_cli.h b/emulate_php_cli.h index c12b2409c8..2f4a29080c 100644 --- a/emulate_php_cli.h +++ b/emulate_php_cli.h @@ -1,8 +1,6 @@ #ifndef _EMULATE_PHP_CLI_H #define _EMULATE_PHP_CLI_H -#include "frankenphp.h" - typedef struct { char *script; int argc; diff --git a/frankenphp.h b/frankenphp.h index db32a82fe0..99ac0ab7ec 100644 --- a/frankenphp.h +++ b/frankenphp.h @@ -7,38 +7,11 @@ #define WIN32_LEAN_AND_MEAN #endif -// Explicitly include Winsock2 BEFORE windows.h +#include #include #include #include #include - -// Fix for missing IntSafe functions (LongLongAdd) when building with Clang -#ifdef __clang__ -#ifndef INTSAFE_E_ARITHMETIC_OVERFLOW -#define INTSAFE_E_ARITHMETIC_OVERFLOW ((HRESULT)0x80070216L) -#endif - -#ifndef LongLongAdd -static inline HRESULT LongLongAdd(LONGLONG llAugend, LONGLONG llAddend, - LONGLONG *pllResult) { - if (__builtin_add_overflow(llAugend, llAddend, pllResult)) { - return INTSAFE_E_ARITHMETIC_OVERFLOW; - } - return S_OK; -} -#endif - -#ifndef LongLongSub -static inline HRESULT LongLongSub(LONGLONG llMinuend, LONGLONG llSubtrahend, - LONGLONG *pllResult) { - if (__builtin_sub_overflow(llMinuend, llSubtrahend, pllResult)) { - return INTSAFE_E_ARITHMETIC_OVERFLOW; - } - return S_OK; -} -#endif -#endif #endif #include diff --git a/internal/testext/exttest.go b/internal/testext/exttest.go index bbbe9bbd22..8e9f74cac8 100644 --- a/internal/testext/exttest.go +++ b/internal/testext/exttest.go @@ -8,7 +8,7 @@ package testext // #cgo unix LDFLAGS: -L/usr/local/lib -L/usr/lib -lphp -lm -lutil // #cgo linux LDFLAGS: -ldl -lresolv // #cgo darwin LDFLAGS: -Wl,-rpath,/usr/local/lib -L/opt/homebrew/lib -L/opt/homebrew/opt/libiconv/lib -liconv -ldl -// #cgo windows CFLAGS: -D_WINDOWS -DWINDOWS=1 -DZEND_WIN32=1 -DPHP_WIN32=1 -DWIN32 -D_MBCS -D_USE_MATH_DEFINES -DNDebug -DNDEBUG -DZEND_DEBUG=0 -DZTS=1 -DFD_SETSIZE=256 +// #cgo windows CFLAGS: -D_WINDOWS -DWINDOWS=1 -DZEND_WIN32=1 -DPHP_WIN32=1 -DWIN32 -D_MBCS -D_USE_MATH_DEFINES -DNDebug -DNDEBUG -DZEND_DEBUG=0 -DZTS=1 -DFD_SETSIZE=256 -DENABLE_INTSAFE_SIGNED_FUNCTIONS // #include "extension.h" import "C" import ( From 3a6c5e67c993feb3af26b89423897de7f8402ffe Mon Sep 17 00:00:00 2001 From: henderkes Date: Tue, 28 Jul 2026 20:26:54 +0700 Subject: [PATCH 25/26] include bool --- emulate_php_cli.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/emulate_php_cli.h b/emulate_php_cli.h index 2f4a29080c..9179ceb7e7 100644 --- a/emulate_php_cli.h +++ b/emulate_php_cli.h @@ -1,6 +1,8 @@ #ifndef _EMULATE_PHP_CLI_H #define _EMULATE_PHP_CLI_H +#include + typedef struct { char *script; int argc; From eaeba146e3305a9fde34571908add9b374aadab6 Mon Sep 17 00:00:00 2001 From: henderkes Date: Tue, 28 Jul 2026 20:46:14 +0700 Subject: [PATCH 26/26] add test for internal cli emulation not trying to run -i/-v/etc. in php <= 8.6 --- cli_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/cli_test.go b/cli_test.go index b0fdedc026..5a07dd8d2c 100644 --- a/cli_test.go +++ b/cli_test.go @@ -46,6 +46,34 @@ func TestExecuteCLICode(t *testing.T) { assert.Equal(t, stdoutStderrStr, `Hello World`) } +// `-i` (and any other invocation without a script) is only supported since PHP +// 8.6, where the real CLI SAPI is reused. older versions must fail cleanly. +func TestExecuteCLIPHPInfo(t *testing.T) { + if _, err := os.Stat("internal/testcli/testcli"); err != nil { + t.Skip("internal/testcli/testcli has not been compiled, run `cd internal/testcli/ && go build`") + } + + cmd := exec.Command("internal/testcli/testcli", "-i") + stdoutStderr, err := cmd.CombinedOutput() + stdoutStderrStr := string(stdoutStderr) + + if frankenphp.Version().VersionID < 80600 { + assert.Error(t, err) + + var exitError *exec.ExitError + if errors.As(err, &exitError) { + assert.Equal(t, 1, exitError.ExitCode()) + } + + assert.Contains(t, stdoutStderrStr, "this functionality is not available in frankenphp php-cli") + + return + } + + assert.NoError(t, err, "output: %s", stdoutStderrStr) + assert.Contains(t, stdoutStderrStr, "PHP Version => "+frankenphp.Version().Version) +} + // Regression test for https://github.com/php/frankenphp/issues/1902. A // long-running CLI script that installs pcntl_signal handlers must // receive its own signals reliably