|
| 1 | +#include <SAPI.h> |
| 2 | +#include <Zend/zend_alloc.h> |
| 3 | +#include <Zend/zend_exceptions.h> |
| 4 | +#include <Zend/zend_interfaces.h> |
| 5 | +#include <Zend/zend_types.h> |
| 6 | +#include <errno.h> |
| 7 | +#include <ext/spl/spl_exceptions.h> |
| 8 | +#include <ext/standard/head.h> |
| 9 | +#include <inttypes.h> |
| 10 | +#include <php.h> |
| 11 | +#include <php_config.h> |
| 12 | +#include <php_ini.h> |
| 13 | +#include <php_main.h> |
| 14 | +#include <php_output.h> |
| 15 | +#include <php_variables.h> |
| 16 | +#include <php_version.h> |
| 17 | +#include <pthread.h> |
| 18 | +#include <sapi/embed/php_embed.h> |
| 19 | +#include <signal.h> |
| 20 | +#include <stdint.h> |
| 21 | +#include <stdio.h> |
| 22 | +#include <stdlib.h> |
| 23 | +#include <unistd.h> |
| 24 | +#if defined(__linux__) |
| 25 | +#include <sys/prctl.h> |
| 26 | +#elif defined(__FreeBSD__) || defined(__OpenBSD__) |
| 27 | +#include <pthread_np.h> |
| 28 | +#endif |
| 29 | + |
| 30 | +typedef struct { |
| 31 | + char *script; |
| 32 | + int argc; |
| 33 | + char **argv; |
| 34 | + bool eval; |
| 35 | +} cli_exec_args_t; |
| 36 | +cli_exec_args_t *cli_args; |
| 37 | + |
| 38 | +/* Function declaration to avoid implicit declaration error */ |
| 39 | +void register_server_variable_filtered(const char *key, char **val, size_t *val_len, zval *track_vars_array); |
| 40 | + |
| 41 | +/* |
| 42 | + * CLI code is adapted from |
| 43 | + * https://github.com/php/php-src/blob/master/sapi/cli/php_cli.c Copyright (c) |
| 44 | + * The PHP Group Licensed under The PHP License Original uthors: Edin Kadribasic |
| 45 | + * <edink@php.net>, Marcus Boerger <helly@php.net> and Johannes Schlueter |
| 46 | + * <johannes@php.net> Parts based on CGI SAPI Module by Rasmus Lerdorf, Stig |
| 47 | + * Bakken and Zeev Suraski |
| 48 | + */ |
| 49 | +static void cli_register_file_handles(bool no_close) /* {{{ */ |
| 50 | +{ |
| 51 | + php_stream *s_in, *s_out, *s_err; |
| 52 | + php_stream_context *sc_in = NULL, *sc_out = NULL, *sc_err = NULL; |
| 53 | + zend_constant ic, oc, ec; |
| 54 | + |
| 55 | + s_in = php_stream_open_wrapper_ex("php://stdin", "rb", 0, NULL, sc_in); |
| 56 | + s_out = php_stream_open_wrapper_ex("php://stdout", "wb", 0, NULL, sc_out); |
| 57 | + s_err = php_stream_open_wrapper_ex("php://stderr", "wb", 0, NULL, sc_err); |
| 58 | + |
| 59 | + if (s_in == NULL || s_out == NULL || s_err == NULL) { |
| 60 | + if (s_in) |
| 61 | + php_stream_close(s_in); |
| 62 | + if (s_out) |
| 63 | + php_stream_close(s_out); |
| 64 | + if (s_err) |
| 65 | + php_stream_close(s_err); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + if (no_close) { |
| 70 | + s_in->flags |= PHP_STREAM_FLAG_NO_CLOSE; |
| 71 | + s_out->flags |= PHP_STREAM_FLAG_NO_CLOSE; |
| 72 | + s_err->flags |= PHP_STREAM_FLAG_NO_CLOSE; |
| 73 | + } |
| 74 | + |
| 75 | + /*s_in_process = s_in;*/ |
| 76 | + |
| 77 | + php_stream_to_zval(s_in, &ic.value); |
| 78 | + php_stream_to_zval(s_out, &oc.value); |
| 79 | + php_stream_to_zval(s_err, &ec.value); |
| 80 | + |
| 81 | + ZEND_CONSTANT_SET_FLAGS(&ic, CONST_CS, 0); |
| 82 | + ic.name = zend_string_init_interned("STDIN", sizeof("STDIN") - 1, 0); |
| 83 | + zend_register_constant(&ic); |
| 84 | + |
| 85 | + ZEND_CONSTANT_SET_FLAGS(&oc, CONST_CS, 0); |
| 86 | + oc.name = zend_string_init_interned("STDOUT", sizeof("STDOUT") - 1, 0); |
| 87 | + zend_register_constant(&oc); |
| 88 | + |
| 89 | + ZEND_CONSTANT_SET_FLAGS(&ec, CONST_CS, 0); |
| 90 | + ec.name = zend_string_init_interned("STDERR", sizeof("STDERR") - 1, 0); |
| 91 | + zend_register_constant(&ec); |
| 92 | +} |
| 93 | +/* }}} */ |
| 94 | + |
| 95 | +static void sapi_cli_register_variables(zval *track_vars_array) /* {{{ */ |
| 96 | +{ |
| 97 | + size_t len = strlen(cli_args->script); |
| 98 | + char *docroot = ""; |
| 99 | + |
| 100 | + /* |
| 101 | + * In CGI mode, we consider the environment to be a part of the server |
| 102 | + * variables |
| 103 | + */ |
| 104 | + php_import_environment_variables(track_vars_array); |
| 105 | + |
| 106 | + /* Build the special-case PHP_SELF variable for the CLI version */ |
| 107 | + register_server_variable_filtered("PHP_SELF", &cli_args->script, &len, |
| 108 | + track_vars_array); |
| 109 | + register_server_variable_filtered("SCRIPT_NAME", &cli_args->script, &len, |
| 110 | + track_vars_array); |
| 111 | + |
| 112 | + /* filenames are empty for stdin */ |
| 113 | + register_server_variable_filtered("SCRIPT_FILENAME", &cli_args->script, &len, |
| 114 | + track_vars_array); |
| 115 | + register_server_variable_filtered("PATH_TRANSLATED", &cli_args->script, &len, |
| 116 | + track_vars_array); |
| 117 | + |
| 118 | + /* just make it available */ |
| 119 | + len = 0U; |
| 120 | + register_server_variable_filtered("DOCUMENT_ROOT", &docroot, &len, |
| 121 | + track_vars_array); |
| 122 | +} |
| 123 | +/* }}} */ |
| 124 | + |
| 125 | +void *emulate_script_cli(void *arg) { |
| 126 | + void *exit_status; |
| 127 | + cli_exec_args_t* args = arg; |
| 128 | + cli_args = args; |
| 129 | + bool eval = args->eval; |
| 130 | + |
| 131 | + /* |
| 132 | + * The SAPI name "cli" is hardcoded into too many programs... let's usurp it. |
| 133 | + */ |
| 134 | + php_embed_module.name = "cli"; |
| 135 | + php_embed_module.pretty_name = "PHP CLI embedded in FrankenPHP"; |
| 136 | + php_embed_module.register_server_variables = sapi_cli_register_variables; |
| 137 | + |
| 138 | + php_embed_init(cli_args->argc, cli_args->argv); |
| 139 | + |
| 140 | + cli_register_file_handles(false); |
| 141 | + zend_first_try { |
| 142 | + if (eval) { |
| 143 | + /* evaluate the cli_args->script as literal PHP code (php-cli -r "...") */ |
| 144 | + zend_eval_string_ex(cli_args->script, NULL, "Command line code", 1); |
| 145 | + } else { |
| 146 | + zend_file_handle file_handle; |
| 147 | + zend_stream_init_filename(&file_handle, cli_args->script); |
| 148 | + |
| 149 | + CG(skip_shebang) = 1; |
| 150 | + php_execute_script(&file_handle); |
| 151 | + } |
| 152 | + } |
| 153 | + zend_end_try(); |
| 154 | + |
| 155 | + exit_status = (void *)(intptr_t)EG(exit_status); |
| 156 | + |
| 157 | + php_embed_shutdown(); |
| 158 | + |
| 159 | + return exit_status; |
| 160 | +} |
0 commit comments