From 5c3350a40c85865b5366f9f39a290d39b315206f Mon Sep 17 00:00:00 2001 From: Calvin Buckley Date: Mon, 24 Jun 2024 16:47:23 -0300 Subject: [PATCH 1/8] Show build provider information in "php -v" Vendors such as distributions can set the `PHP_BUILD_PROVIDER` variable, that gets printed in phpinfo. However, I find that users check `php -v` more often than phpinfo to see what PHP they're running. The problem with this is that it does not show that build provider information. This change makes the build provider information printed on an additional line of the version information. --- sapi/cli/php_cli.c | 8 +++++++- sapi/cli/tests/001.phpt | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/sapi/cli/php_cli.c b/sapi/cli/php_cli.c index 9997fc4919024..bd27900db4751 100644 --- a/sapi/cli/php_cli.c +++ b/sapi/cli/php_cli.c @@ -625,7 +625,7 @@ static int do_cli(int argc, char **argv) /* {{{ */ goto out; case 'v': /* show php version & quit */ - php_printf("PHP %s (%s) (built: %s %s) (%s)\nCopyright (c) The PHP Group\n%s", + php_printf("PHP %s (%s) (built: %s %s) (%s)\nCopyright (c) The PHP Group\n%s%s", PHP_VERSION, cli_sapi_module.name, __DATE__, __TIME__, #ifdef ZTS "ZTS" @@ -643,6 +643,12 @@ static int do_cli(int argc, char **argv) /* {{{ */ #endif #ifdef HAVE_GCOV " GCOV" +#endif + , +#ifdef PHP_BUILD_PROVIDER + "This build by " PHP_BUILD_PROVIDER "\n" +#else + "" #endif , get_zend_version() diff --git a/sapi/cli/tests/001.phpt b/sapi/cli/tests/001.phpt index aced33623eff4..101030be77b77 100644 --- a/sapi/cli/tests/001.phpt +++ b/sapi/cli/tests/001.phpt @@ -14,6 +14,7 @@ echo "Done\n"; --EXPECTF-- string(%d) "PHP %s (cli) (built: %s)%s Copyright (c) The PHP Group +%A Zend Engine v%s, Copyright (c) Zend Technologies " Done From 1ff39e0d2581ab4012b8b2853401213e56aad7f0 Mon Sep 17 00:00:00 2001 From: Calvin Buckley Date: Mon, 24 Jun 2024 19:04:58 -0300 Subject: [PATCH 2/8] Put on same line so it works with or without env var Unbreaks build without PHP_BUILD_PROVIDER set. --- sapi/cli/tests/001.phpt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sapi/cli/tests/001.phpt b/sapi/cli/tests/001.phpt index 101030be77b77..e13ab8def300f 100644 --- a/sapi/cli/tests/001.phpt +++ b/sapi/cli/tests/001.phpt @@ -14,7 +14,6 @@ echo "Done\n"; --EXPECTF-- string(%d) "PHP %s (cli) (built: %s)%s Copyright (c) The PHP Group -%A -Zend Engine v%s, Copyright (c) Zend Technologies +%AZend Engine v%s, Copyright (c) Zend Technologies " Done From 3ac991c09f1a6e5e5fad3902a3f70118fc4d54f2 Mon Sep 17 00:00:00 2001 From: Calvin Buckley Date: Tue, 25 Jun 2024 13:13:10 -0300 Subject: [PATCH 3/8] change wording in provider version text better grammatically; many different possibilities here though --- sapi/cli/php_cli.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sapi/cli/php_cli.c b/sapi/cli/php_cli.c index bd27900db4751..c22ad47b70417 100644 --- a/sapi/cli/php_cli.c +++ b/sapi/cli/php_cli.c @@ -646,7 +646,7 @@ static int do_cli(int argc, char **argv) /* {{{ */ #endif , #ifdef PHP_BUILD_PROVIDER - "This build by " PHP_BUILD_PROVIDER "\n" + "Built by " PHP_BUILD_PROVIDER "\n" #else "" #endif From f1e4adbe184757194c999cb9fc0440fb8d627fbf Mon Sep 17 00:00:00 2001 From: Calvin Buckley Date: Tue, 25 Jun 2024 15:47:03 -0300 Subject: [PATCH 4/8] Unify SAPI version printing This makes it so that all of the SAPIs share the same code for printing version information. This is useful in case of any future changes to the version information, such as i.e. adding build provider to the output. --- main/main.c | 32 ++++++++++++++++++++++++++++++++ main/php_main.h | 5 +++++ sapi/cgi/cgi_main.c | 6 +----- sapi/cgi/tests/001.phpt | 2 +- sapi/cli/php_cli.c | 29 +---------------------------- sapi/fpm/fpm/fpm_main.c | 6 +----- sapi/litespeed/lsapi_main.c | 6 +----- sapi/phpdbg/phpdbg.c | 9 +-------- 8 files changed, 43 insertions(+), 52 deletions(-) diff --git a/main/main.c b/main/main.c index aa80837f65745..dd85c752f7ae2 100644 --- a/main/main.c +++ b/main/main.c @@ -108,6 +108,38 @@ PHPAPI unsigned int php_version_id(void) return PHP_VERSION_ID; } +PHPAPI void php_print_version(sapi_module_struct *sapi_module) +{ + php_printf("PHP %s (%s) (built: %s %s) (%s)\nCopyright (c) The PHP Group\n%s%s", + PHP_VERSION, sapi_module->name, __DATE__, __TIME__, +#ifdef ZTS + "ZTS" +#else + "NTS" +#endif +#ifdef PHP_BUILD_COMPILER + " " PHP_BUILD_COMPILER +#endif +#ifdef PHP_BUILD_ARCH + " " PHP_BUILD_ARCH +#endif +#if ZEND_DEBUG + " DEBUG" +#endif +#ifdef HAVE_GCOV + " GCOV" +#endif + , +#ifdef PHP_BUILD_PROVIDER + "Built by " PHP_BUILD_PROVIDER "\n" +#else + "" +#endif + , + get_zend_version() + ); +} + /* {{{ PHP_INI_MH */ static PHP_INI_MH(OnSetFacility) { diff --git a/main/php_main.h b/main/php_main.h index 1fb460f07f267..e958521a48efa 100644 --- a/main/php_main.h +++ b/main/php_main.h @@ -36,6 +36,11 @@ PHPAPI const char *php_version(void); */ PHPAPI unsigned int php_version_id(void); +/* Prints the PHP version string for the -v option. It's in main/ so that + * it can be shared between SAPIs. + */ +PHPAPI void php_print_version(sapi_module_struct *sapi_module); + PHPAPI zend_result php_request_startup(void); PHPAPI void php_request_shutdown(void *dummy); PHPAPI zend_result php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_module); diff --git a/sapi/cgi/cgi_main.c b/sapi/cgi/cgi_main.c index 8901b16d05a8d..bb6a99d8302bb 100644 --- a/sapi/cgi/cgi_main.c +++ b/sapi/cgi/cgi_main.c @@ -2377,11 +2377,7 @@ consult the installation file that came with this distribution, or visit \n\ } SG(headers_sent) = 1; SG(request_info).no_headers = 1; -#if ZEND_DEBUG - php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version()); -#else - php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version()); -#endif + php_print_version(&cgi_sapi_module); php_request_shutdown((void *) 0); fcgi_shutdown(); exit_status = 0; diff --git a/sapi/cgi/tests/001.phpt b/sapi/cgi/tests/001.phpt index 948f4ce30477c..c4b539aa83bc6 100644 --- a/sapi/cgi/tests/001.phpt +++ b/sapi/cgi/tests/001.phpt @@ -17,6 +17,6 @@ echo "Done\n"; --EXPECTF-- string(%d) "PHP %s (cgi%s (built: %s Copyright (c) The PHP Group -Zend Engine v%s, Copyright (c) Zend Technologies +%AZend Engine v%s, Copyright (c) Zend Technologies " Done diff --git a/sapi/cli/php_cli.c b/sapi/cli/php_cli.c index c22ad47b70417..5d4f20ec96f2a 100644 --- a/sapi/cli/php_cli.c +++ b/sapi/cli/php_cli.c @@ -625,34 +625,7 @@ static int do_cli(int argc, char **argv) /* {{{ */ goto out; case 'v': /* show php version & quit */ - php_printf("PHP %s (%s) (built: %s %s) (%s)\nCopyright (c) The PHP Group\n%s%s", - PHP_VERSION, cli_sapi_module.name, __DATE__, __TIME__, -#ifdef ZTS - "ZTS" -#else - "NTS" -#endif -#ifdef PHP_BUILD_COMPILER - " " PHP_BUILD_COMPILER -#endif -#ifdef PHP_BUILD_ARCH - " " PHP_BUILD_ARCH -#endif -#if ZEND_DEBUG - " DEBUG" -#endif -#ifdef HAVE_GCOV - " GCOV" -#endif - , -#ifdef PHP_BUILD_PROVIDER - "Built by " PHP_BUILD_PROVIDER "\n" -#else - "" -#endif - , - get_zend_version() - ); + php_print_version(&cli_sapi_module); sapi_deactivate(); goto out; diff --git a/sapi/fpm/fpm/fpm_main.c b/sapi/fpm/fpm/fpm_main.c index ae38b213e9887..d138738b27981 100644 --- a/sapi/fpm/fpm/fpm_main.c +++ b/sapi/fpm/fpm/fpm_main.c @@ -1705,11 +1705,7 @@ int main(int argc, char *argv[]) SG(headers_sent) = 1; SG(request_info).no_headers = 1; -#if ZEND_DEBUG - php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version()); -#else - php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version()); -#endif + php_print_version(&sapi_module); php_request_shutdown((void *) 0); fcgi_shutdown(); exit_status = FPM_EXIT_OK; diff --git a/sapi/litespeed/lsapi_main.c b/sapi/litespeed/lsapi_main.c index d0a81c7cb6c77..259d4a985cc66 100644 --- a/sapi/litespeed/lsapi_main.c +++ b/sapi/litespeed/lsapi_main.c @@ -1272,11 +1272,7 @@ static int cli_main( int argc, char * argv[] ) break; case 'v': if (php_request_startup() != FAILURE) { -#if ZEND_DEBUG - php_printf("PHP %s (%s) (built: %s %s) (DEBUG)\nCopyright (c) The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version()); -#else - php_printf("PHP %s (%s) (built: %s %s)\nCopyright (c) The PHP Group\n%s", PHP_VERSION, sapi_module.name, __DATE__, __TIME__, get_zend_version()); -#endif + php_print_version(&sapi_module); #ifdef PHP_OUTPUT_NEWAPI php_output_end_all(); #else diff --git a/sapi/phpdbg/phpdbg.c b/sapi/phpdbg/phpdbg.c index ce64c9b88deee..250c9a066038e 100644 --- a/sapi/phpdbg/phpdbg.c +++ b/sapi/phpdbg/phpdbg.c @@ -1369,14 +1369,7 @@ int main(int argc, char **argv) /* {{{ */ if (show_help) { phpdbg_do_help_cmd(exec); } else if (show_version) { - phpdbg_out( - "phpdbg %s (built: %s %s)\nPHP %s, Copyright (c) The PHP Group\n%s", - PHPDBG_VERSION, - __DATE__, - __TIME__, - PHP_VERSION, - get_zend_version() - ); + php_print_version(&phpdbg_sapi_module); } PHPDBG_G(flags) |= PHPDBG_IS_QUITTING; php_module_shutdown(); From c8e9fea98b1310592ec061c230f685c385671b31 Mon Sep 17 00:00:00 2001 From: Calvin Buckley Date: Thu, 27 Jun 2024 15:02:33 -0300 Subject: [PATCH 5/8] Make include for php_print_version explicit --- sapi/phpdbg/phpdbg.c | 1 + 1 file changed, 1 insertion(+) diff --git a/sapi/phpdbg/phpdbg.c b/sapi/phpdbg/phpdbg.c index 250c9a066038e..52e2fa745d146 100644 --- a/sapi/phpdbg/phpdbg.c +++ b/sapi/phpdbg/phpdbg.c @@ -30,6 +30,7 @@ #include "phpdbg_arginfo.h" #include "zend_vm.h" #include "php_ini_builder.h" +#include "php_main.h" #include "ext/standard/basic_functions.h" From cf362d3d3f67a792e610c01b06fdbfa4d024cfb6 Mon Sep 17 00:00:00 2001 From: Calvin Buckley Date: Mon, 15 Jul 2024 11:46:49 -0300 Subject: [PATCH 6/8] Preserve phpdbg version and output channel php_printf doesn't have same semantics, as phpdbg_out could be on a different output than stdout/err. Also add the phpdbg version (in case it differs from PHP's, to keep similar output before this PR) --- main/main.c | 13 +++++++++++-- main/php_main.h | 1 + sapi/phpdbg/phpdbg.c | 9 ++++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/main/main.c b/main/main.c index dd85c752f7ae2..3fb06967176c1 100644 --- a/main/main.c +++ b/main/main.c @@ -108,9 +108,10 @@ PHPAPI unsigned int php_version_id(void) return PHP_VERSION_ID; } -PHPAPI void php_print_version(sapi_module_struct *sapi_module) +PHPAPI char *php_get_version(sapi_module_struct *sapi_module) { - php_printf("PHP %s (%s) (built: %s %s) (%s)\nCopyright (c) The PHP Group\n%s%s", + char *version_info; + int size = spprintf(&version_info, 0, "PHP %s (%s) (built: %s %s) (%s)\nCopyright (c) The PHP Group\n%s%s", PHP_VERSION, sapi_module->name, __DATE__, __TIME__, #ifdef ZTS "ZTS" @@ -138,6 +139,14 @@ PHPAPI void php_print_version(sapi_module_struct *sapi_module) , get_zend_version() ); + return version_info; +} + +PHPAPI void php_print_version(sapi_module_struct *sapi_module) +{ + char *version_info = php_get_version(sapi_module); + php_printf("%s", version_info); + efree(version_info); } /* {{{ PHP_INI_MH */ diff --git a/main/php_main.h b/main/php_main.h index e958521a48efa..889fcf48501f0 100644 --- a/main/php_main.h +++ b/main/php_main.h @@ -39,6 +39,7 @@ PHPAPI unsigned int php_version_id(void); /* Prints the PHP version string for the -v option. It's in main/ so that * it can be shared between SAPIs. */ +PHPAPI char *php_get_version(sapi_module_struct *sapi_module); PHPAPI void php_print_version(sapi_module_struct *sapi_module); PHPAPI zend_result php_request_startup(void); diff --git a/sapi/phpdbg/phpdbg.c b/sapi/phpdbg/phpdbg.c index 52e2fa745d146..ad77309f5058f 100644 --- a/sapi/phpdbg/phpdbg.c +++ b/sapi/phpdbg/phpdbg.c @@ -1370,7 +1370,14 @@ int main(int argc, char **argv) /* {{{ */ if (show_help) { phpdbg_do_help_cmd(exec); } else if (show_version) { - php_print_version(&phpdbg_sapi_module); + char *version_info = php_get_version(&phpdbg_sapi_module); + /* we also want to include phpdbg version */ + char *prepended_version_info; + int pvi_size = spprintf(&prepended_version_info, 0, + "phpdbg %s, %s", PHPDBG_VERSION, version_info); + phpdbg_out(prepended_version_info); + efree(prepended_version_info); + efree(version_info); } PHPDBG_G(flags) |= PHPDBG_IS_QUITTING; php_module_shutdown(); From 2ac7cdd4ed7268df1a169d0e685963bf7a420693 Mon Sep 17 00:00:00 2001 From: Calvin Buckley Date: Mon, 15 Jul 2024 15:28:29 -0300 Subject: [PATCH 7/8] remove size variables we don't use them and CI doesn't like unused variables --- main/main.c | 2 +- sapi/phpdbg/phpdbg.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/main/main.c b/main/main.c index 3fb06967176c1..b627dc23ece46 100644 --- a/main/main.c +++ b/main/main.c @@ -111,7 +111,7 @@ PHPAPI unsigned int php_version_id(void) PHPAPI char *php_get_version(sapi_module_struct *sapi_module) { char *version_info; - int size = spprintf(&version_info, 0, "PHP %s (%s) (built: %s %s) (%s)\nCopyright (c) The PHP Group\n%s%s", + spprintf(&version_info, 0, "PHP %s (%s) (built: %s %s) (%s)\nCopyright (c) The PHP Group\n%s%s", PHP_VERSION, sapi_module->name, __DATE__, __TIME__, #ifdef ZTS "ZTS" diff --git a/sapi/phpdbg/phpdbg.c b/sapi/phpdbg/phpdbg.c index ad77309f5058f..2258f402e86e0 100644 --- a/sapi/phpdbg/phpdbg.c +++ b/sapi/phpdbg/phpdbg.c @@ -1373,7 +1373,7 @@ int main(int argc, char **argv) /* {{{ */ char *version_info = php_get_version(&phpdbg_sapi_module); /* we also want to include phpdbg version */ char *prepended_version_info; - int pvi_size = spprintf(&prepended_version_info, 0, + spprintf(&prepended_version_info, 0, "phpdbg %s, %s", PHPDBG_VERSION, version_info); phpdbg_out(prepended_version_info); efree(prepended_version_info); From 29b81cfc0537afaab182f1b69893ba71679c9ecd Mon Sep 17 00:00:00 2001 From: Calvin Buckley Date: Mon, 5 Aug 2024 20:15:45 -0300 Subject: [PATCH 8/8] Fix format string insecurity --- sapi/phpdbg/phpdbg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sapi/phpdbg/phpdbg.c b/sapi/phpdbg/phpdbg.c index 2258f402e86e0..f26e523c9ac0e 100644 --- a/sapi/phpdbg/phpdbg.c +++ b/sapi/phpdbg/phpdbg.c @@ -1375,7 +1375,7 @@ int main(int argc, char **argv) /* {{{ */ char *prepended_version_info; spprintf(&prepended_version_info, 0, "phpdbg %s, %s", PHPDBG_VERSION, version_info); - phpdbg_out(prepended_version_info); + phpdbg_out("%s", prepended_version_info); efree(prepended_version_info); efree(version_info); }