Skip to content

Commit 7d14dad

Browse files
committed
MFH: Fixed mess with CGI/CLI -d command line option (now it works with cgi; constants are working exactly like in php.ini; with FastCGI -d affects all requests).
1 parent 2332e4f commit 7d14dad

File tree

10 files changed

+110
-61
lines changed

10 files changed

+110
-61
lines changed

NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? Sep 2006, PHP 5.2.0
4+
- Fixed mess with CGI/CLI -d option (now it works with cgi; constants are
5+
working exactly like in php.ini; with FastCGI -d affects all requests).
6+
(Dmitry)
47
- Fixed bug #38844 (curl_easy_strerror() is defined only since cURL 7.12.0).
58
(Tony)
69
- Fixed bug #38574 (missing curl constants and improper constant detection).

Zend/zend_ini.h

+1
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ END_EXTERN_C()
193193
typedef void (*zend_ini_parser_cb_t)(zval *arg1, zval *arg2, int callback_type, void *arg);
194194
BEGIN_EXTERN_C()
195195
ZEND_API int zend_parse_ini_file(zend_file_handle *fh, zend_bool unbuffered_errors, zend_ini_parser_cb_t ini_parser_cb, void *arg);
196+
ZEND_API int zend_parse_ini_string(char *str, zend_bool unbuffered_errors, zend_ini_parser_cb_t ini_parser_cb, void *arg);
196197
END_EXTERN_C()
197198

198199
#define ZEND_INI_PARSER_ENTRY 1

Zend/zend_ini_parser.y

+30-3
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,14 @@ static void ini_error(char *str)
157157
TSRMLS_FETCH();
158158

159159
currently_parsed_filename = zend_ini_scanner_get_filename(TSRMLS_C);
160-
error_buf_len = 128+strlen(currently_parsed_filename); /* should be more than enough */
161-
error_buf = (char *) emalloc(error_buf_len);
160+
if (currently_parsed_filename) {
161+
error_buf_len = 128+strlen(currently_parsed_filename); /* should be more than enough */
162+
error_buf = (char *) emalloc(error_buf_len);
162163

163-
sprintf(error_buf, "Error parsing %s on line %d\n", currently_parsed_filename, zend_ini_scanner_get_lineno(TSRMLS_C));
164+
sprintf(error_buf, "Error parsing %s on line %d\n", currently_parsed_filename, zend_ini_scanner_get_lineno(TSRMLS_C));
165+
} else {
166+
error_buf = estrdup("Invalid configuration directive\n");
167+
}
164168

165169
if (CG(ini_parser_unbuffered_errors)) {
166170
#ifdef PHP_WIN32
@@ -202,6 +206,29 @@ ZEND_API int zend_parse_ini_file(zend_file_handle *fh, zend_bool unbuffered_erro
202206
}
203207

204208

209+
ZEND_API int zend_parse_ini_string(char *str, zend_bool unbuffered_errors, zend_ini_parser_cb_t ini_parser_cb, void *arg)
210+
{
211+
zend_ini_parser_param ini_parser_param;
212+
TSRMLS_FETCH();
213+
214+
ini_parser_param.ini_parser_cb = ini_parser_cb;
215+
ini_parser_param.arg = arg;
216+
217+
CG(ini_parser_param) = &ini_parser_param;
218+
if (zend_ini_prepare_string_for_scanning(str TSRMLS_CC)==FAILURE) {
219+
return FAILURE;
220+
}
221+
222+
CG(ini_parser_unbuffered_errors) = unbuffered_errors;
223+
224+
if (ini_parse(TSRMLS_C)) {
225+
return SUCCESS;
226+
} else {
227+
return FAILURE;
228+
}
229+
}
230+
231+
205232
%}
206233

207234
%pure_parser

Zend/zend_ini_scanner.h

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ BEGIN_EXTERN_C()
2626
int zend_ini_scanner_get_lineno(TSRMLS_D);
2727
char *zend_ini_scanner_get_filename(TSRMLS_D);
2828
int zend_ini_open_file_for_scanning(zend_file_handle *fh TSRMLS_DC);
29+
int zend_ini_prepare_string_for_scanning(char *str TSRMLS_DC);
2930
void zend_ini_close_file(zend_file_handle *fh TSRMLS_DC);
3031
int ini_lex(zval *ini_lval TSRMLS_DC);
3132
END_EXTERN_C()

Zend/zend_ini_scanner.l

+11
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ int zend_ini_open_file_for_scanning(zend_file_handle *fh TSRMLS_DC)
8383
}
8484

8585

86+
int zend_ini_prepare_string_for_scanning(char *str TSRMLS_DC)
87+
{
88+
int len = strlen(str);
89+
90+
yyin = NULL;
91+
yy_scan_buffer(str, len + 2 TSRMLS_CC);
92+
ini_filename = NULL;
93+
return SUCCESS;
94+
}
95+
96+
8697
void zend_ini_close_file(zend_file_handle *fh TSRMLS_DC)
8798
{
8899
zend_stream_close(fh);

main/SAPI.c

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ SAPI_API sapi_module_struct sapi_module;
7676

7777
SAPI_API void sapi_startup(sapi_module_struct *sf)
7878
{
79+
sf->ini_entries = NULL;
7980
sapi_module = *sf;
8081

8182
#ifdef ZTS

main/SAPI.h

+2
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ struct _sapi_module_struct {
257257

258258
void (*ini_defaults)(HashTable *configuration_hash);
259259
int phpinfo_as_text;
260+
261+
char *ini_entries;
260262
};
261263

262264

main/php_ini.c

+5
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,11 @@ int php_init_config(TSRMLS_D)
529529
zend_llist_destroy(&scanned_ini_list);
530530
}
531531
}
532+
533+
if (sapi_module.ini_entries) {
534+
zend_parse_ini_string(sapi_module.ini_entries, 1, php_config_ini_parser_cb, &extension_lists);
535+
}
536+
532537
return SUCCESS;
533538
}
534539
/* }}} */

sapi/cgi/cgi_main.c

+21-18
Original file line numberDiff line numberDiff line change
@@ -919,21 +919,6 @@ static void init_request_info(TSRMLS_D)
919919
}
920920
/* }}} */
921921

922-
static void define_command_line_ini_entry(char *arg)
923-
{
924-
char *name, *value;
925-
926-
name = arg;
927-
value = strchr(arg, '=');
928-
if (value) {
929-
*value = 0;
930-
value++;
931-
} else {
932-
value = "1";
933-
}
934-
zend_alter_ini_entry(name, strlen(name) + 1, value, strlen(value), PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
935-
}
936-
937922
#if PHP_FASTCGI
938923
/**
939924
* Clean up child processes upon exit
@@ -987,6 +972,7 @@ int main(int argc, char *argv[])
987972
int orig_optind = php_optind;
988973
char *orig_optarg = php_optarg;
989974
char *script_file = NULL;
975+
int ini_entries_len = 0;
990976
#if FORCE_CGI_REDIRECT
991977
long force_redirect = 1;
992978
char *redirect_status_env = NULL;
@@ -1076,6 +1062,23 @@ int main(int argc, char *argv[])
10761062
case 'n':
10771063
cgi_sapi_module.php_ini_ignore = 1;
10781064
break;
1065+
case 'd': {
1066+
/* define ini entries on command line */
1067+
int len = strlen(php_optarg);
1068+
1069+
if (strchr(php_optarg, '=')) {
1070+
cgi_sapi_module.ini_entries = realloc(cgi_sapi_module.ini_entries, ini_entries_len + len + sizeof("\n\0"));
1071+
memcpy(cgi_sapi_module.ini_entries + ini_entries_len, php_optarg, len);
1072+
memcpy(cgi_sapi_module.ini_entries + ini_entries_len + len, "\n\0", sizeof("\n\0"));
1073+
ini_entries_len += len + sizeof("\n\0") - 2;
1074+
} else {
1075+
cgi_sapi_module.ini_entries = realloc(cgi_sapi_module.ini_entries, ini_entries_len + len + sizeof("=1\n\0"));
1076+
memcpy(cgi_sapi_module.ini_entries + ini_entries_len, php_optarg, len);
1077+
memcpy(cgi_sapi_module.ini_entries + ini_entries_len + len, "=1\n\0", sizeof("=1\n\0"));
1078+
ini_entries_len += len + sizeof("=1\n\0") - 2;
1079+
}
1080+
break;
1081+
}
10791082
#if PHP_FASTCGI
10801083
#ifndef PHP_WIN32
10811084
/* if we're started on command line, check to see if
@@ -1378,9 +1381,6 @@ consult the installation file that came with this distribution, or visit \n\
13781381
case 'C': /* don't chdir to the script directory */
13791382
SG(options) |= SAPI_OPTION_NO_CHDIR;
13801383
break;
1381-
case 'd': /* define ini entries on command line */
1382-
define_command_line_ini_entry(php_optarg);
1383-
break;
13841384

13851385
case 'e': /* enable extended info output */
13861386
CG(extended_info) = 1;
@@ -1702,6 +1702,9 @@ consult the installation file that came with this distribution, or visit \n\
17021702
if (cgi_sapi_module.php_ini_path_override) {
17031703
free(cgi_sapi_module.php_ini_path_override);
17041704
}
1705+
if (cgi_sapi_module.ini_entries) {
1706+
free(cgi_sapi_module.ini_entries);
1707+
}
17051708
} zend_catch {
17061709
exit_status = 255;
17071710
} zend_end_try();

sapi/cli/php_cli.c

+35-40
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@
106106
#define PHP_MODE_REFLECTION_CLASS 9
107107
#define PHP_MODE_REFLECTION_EXTENSION 10
108108

109+
#define HARDCODED_INI \
110+
"html_errors=0\n" \
111+
"register_argc_argv=1\n" \
112+
"implicit_flush=1\n" \
113+
"output_buffering=0\n" \
114+
"max_execution_time=0\n" \
115+
"max_input_time=-1\n"
116+
117+
109118
static char *php_optarg = NULL;
110119
static int php_optind = 1;
111120
#if (HAVE_LIBREADLINE || HAVE_LIBEDIT) && !defined(COMPILE_DL_READLINE)
@@ -342,10 +351,6 @@ static int php_cli_startup(sapi_module_struct *sapi_module)
342351
zend_hash_update(configuration_hash, name, sizeof(name), tmp, sizeof(zval), (void**)&entry);\
343352
Z_STRVAL_P(entry) = zend_strndup(Z_STRVAL_P(entry), Z_STRLEN_P(entry))
344353

345-
/* hard coded ini settings must be set in main() */
346-
#define INI_HARDCODED(name,value)\
347-
zend_alter_ini_entry(name, sizeof(name), value, strlen(value), PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
348-
349354
static void sapi_cli_ini_defaults(HashTable *configuration_hash)
350355
{
351356
zval *tmp, *entry;
@@ -451,29 +456,6 @@ static void php_cli_usage(char *argv0)
451456
}
452457
/* }}} */
453458

454-
static void define_command_line_ini_entry(char *arg TSRMLS_DC)
455-
{
456-
char *name, *value;
457-
458-
name = arg;
459-
value = strchr(arg, '=');
460-
if (value) {
461-
*value = 0;
462-
value++;
463-
} else {
464-
value = "1";
465-
}
466-
467-
if (!strcasecmp(name, "extension")) { /* load function module */
468-
zval extension, zval;
469-
ZVAL_STRING(&extension, value, 0);
470-
php_dl(&extension, MODULE_PERSISTENT, &zval, 1 TSRMLS_CC);
471-
} else {
472-
zend_alter_ini_entry(name, strlen(name)+1, value, strlen(value), PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
473-
}
474-
}
475-
476-
477459
static php_stream *s_in_process = NULL;
478460

479461
static void cli_register_file_handles(TSRMLS_D)
@@ -602,6 +584,7 @@ int main(int argc, char *argv[])
602584
int argc = __argc;
603585
char **argv = __argv;
604586
#endif
587+
int ini_entries_len = 0;
605588

606589
#if defined(PHP_WIN32) && defined(_DEBUG) && defined(PHP_WIN32_DEBUG_HEAP)
607590
{
@@ -646,6 +629,10 @@ int main(int argc, char *argv[])
646629
setmode(_fileno(stderr), O_BINARY); /* make the stdio mode be binary */
647630
#endif
648631

632+
ini_entries_len = strlen(HARDCODED_INI);
633+
cli_sapi_module.ini_entries = malloc(ini_entries_len+2);
634+
memcpy(cli_sapi_module.ini_entries, HARDCODED_INI, ini_entries_len+1);
635+
cli_sapi_module.ini_entries[ini_entries_len+1] = 0;
649636

650637
while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0))!=-1) {
651638
switch (c) {
@@ -655,6 +642,23 @@ int main(int argc, char *argv[])
655642
case 'n':
656643
cli_sapi_module.php_ini_ignore = 1;
657644
break;
645+
case 'd': {
646+
/* define ini entries on command line */
647+
int len = strlen(php_optarg);
648+
649+
if (strchr(php_optarg, '=')) {
650+
cli_sapi_module.ini_entries = realloc(cli_sapi_module.ini_entries, ini_entries_len + len + sizeof("\n\0"));
651+
memcpy(cli_sapi_module.ini_entries + ini_entries_len, php_optarg, len);
652+
memcpy(cli_sapi_module.ini_entries + ini_entries_len + len, "\n\0", sizeof("\n\0"));
653+
ini_entries_len += len + sizeof("\n\0") - 2;
654+
} else {
655+
cli_sapi_module.ini_entries = realloc(cli_sapi_module.ini_entries, ini_entries_len + len + sizeof("=1\n\0"));
656+
memcpy(cli_sapi_module.ini_entries + ini_entries_len, php_optarg, len);
657+
memcpy(cli_sapi_module.ini_entries + ini_entries_len + len, "=1\n\0", sizeof("=1\n\0"));
658+
ini_entries_len += len + sizeof("=1\n\0") - 2;
659+
}
660+
break;
661+
}
658662
}
659663
}
660664
php_optind = orig_optind;
@@ -683,7 +687,6 @@ int main(int argc, char *argv[])
683687
module_started = 1;
684688

685689
zend_first_try {
686-
zend_uv.html_errors = 0; /* tell the engine we're in non-html mode */
687690
CG(in_compilation) = 0; /* not initialized but needed for several options */
688691
EG(uninitialized_zval_ptr) = NULL;
689692

@@ -693,21 +696,9 @@ int main(int argc, char *argv[])
693696
goto out_err;
694697
}
695698

696-
/* here is the place for hard coded defaults which cannot be overwritten in the ini file */
697-
INI_HARDCODED("register_argc_argv", "1");
698-
INI_HARDCODED("html_errors", "0");
699-
INI_HARDCODED("implicit_flush", "1");
700-
INI_HARDCODED("output_buffering", "0");
701-
INI_HARDCODED("max_execution_time", "0");
702-
INI_HARDCODED("max_input_time", "-1");
703-
704699
while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0)) != -1) {
705700
switch (c) {
706701

707-
case 'd': /* define ini entries on command line */
708-
define_command_line_ini_entry(php_optarg TSRMLS_CC);
709-
break;
710-
711702
case 'h': /* help & quit */
712703
case '?':
713704
if (php_request_startup(TSRMLS_C)==FAILURE) {
@@ -1260,6 +1251,10 @@ int main(int argc, char *argv[])
12601251
if (cli_sapi_module.php_ini_path_override) {
12611252
free(cli_sapi_module.php_ini_path_override);
12621253
}
1254+
if (cli_sapi_module.ini_entries) {
1255+
free(cli_sapi_module.ini_entries);
1256+
}
1257+
12631258
if (module_started) {
12641259
php_module_shutdown(TSRMLS_C);
12651260
}

0 commit comments

Comments
 (0)