From 44408b65144231507ec4d70bae9ec8901e23474b Mon Sep 17 00:00:00 2001 From: DubbleClick Date: Sun, 3 Aug 2025 17:29:47 +0700 Subject: [PATCH 1/5] 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 4e76ff14..a975981d 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 d2437dbf..c07daaa0 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 24241e05..9d40153d 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 c03c836c..40c25346 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 2/5] 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 00000000..9a4a3379 --- /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 00000000..7850eed2 --- /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 77a4000d..0545211b 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 3/5] 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 9a4a3379..007dfb29 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 7850eed2..8a3015ff 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 4/5] 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 007dfb29..cb19e5f4 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 0545211b..f8d00853 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 5/5] fix 80500 --- frankenphp.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/frankenphp.c b/frankenphp.c index f8d00853..ad0283d7 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);