Commit adcd9b03893 for php

commit adcd9b03893f1cae2acbe87e01e8361fc7f7ec1c
Author: Gina Peter Banyard <girgias@php.net>
Date:   Sun Nov 22 17:46:03 2026 +0000

    streams: move some stream related globals from std to main (#25344)

    This is to try and fix out dependency mess so that main does not depend on ext/standard

diff --git a/UPGRADING.INTERNALS b/UPGRADING.INTERNALS
index 7349f29a6a7..3e2a66d85e7 100644
--- a/UPGRADING.INTERNALS
+++ b/UPGRADING.INTERNALS
@@ -36,6 +36,17 @@ PHP 8.7 INTERNALS UPGRADE NOTES
     zend_constant.attributes can therefore be shared.
     Use zend_constant_attributes_offset() to get the offset of a constant's own attributes.
     Added zend_get_attribute_str_ex() to look up an attribute at a raw offset.
+  . The following globals have been moved from ext/standard/file.h to
+    php_globals.h, meaning the access macro is now PG() and no longer FG():
+    * auto_detect_line_endings
+    * default_socket_timeout
+    * user_stream_current_filename
+    * stream_wrappers
+    * stream_filters
+    * wrapper_logged_errors
+    * stream_error_state
+    The motivation is to remove cyclic dependencies from PHP, as such it may be
+    possible to omit including ext/standard/file.h to access those globals.

 - Removed:
   . The ZEND_LOG_XOR() macro was removed.
diff --git a/ext/openssl/xp_ssl.c b/ext/openssl/xp_ssl.c
index 91521c72930..eeb26791940 100644
--- a/ext/openssl/xp_ssl.c
+++ b/ext/openssl/xp_ssl.c
@@ -20,7 +20,6 @@
 #endif

 #include "php.h"
-#include "ext/standard/file.h"
 #include "ext/uri/php_uri.h"
 #include "streams/php_streams_int.h"
 #include "zend_smart_str.h"
@@ -3726,9 +3725,9 @@ static int php_openssl_sockop_set_option(php_stream *stream, int option, int val
 				if (value == -1) {
 					if (sslsock->s.timeout.tv_sec == -1) {
 #ifdef _WIN32
-						tv.tv_sec = (long)FG(default_socket_timeout);
+						tv.tv_sec = (long)PG(default_socket_timeout);
 #else
-						tv.tv_sec = (time_t)FG(default_socket_timeout);
+						tv.tv_sec = (time_t)PG(default_socket_timeout);
 #endif
 						tv.tv_usec = 0;
 					} else {
@@ -4099,9 +4098,9 @@ php_stream *php_openssl_ssl_socket_factory(const char *proto, size_t protolen,
 	sslsock->s.is_blocked = true;
 	/* this timeout is used by standard stream funcs, therefore it should use the default value */
 #ifdef _WIN32
-	sslsock->s.timeout.tv_sec = (long)FG(default_socket_timeout);
+	sslsock->s.timeout.tv_sec = (long)PG(default_socket_timeout);
 #else
-	sslsock->s.timeout.tv_sec = (time_t)FG(default_socket_timeout);
+	sslsock->s.timeout.tv_sec = (time_t)PG(default_socket_timeout);
 #endif
 	sslsock->s.timeout.tv_usec = 0;

diff --git a/ext/sockets/sockets.c b/ext/sockets/sockets.c
index 30621c063e2..53b7e62a6e8 100644
--- a/ext/sockets/sockets.c
+++ b/ext/sockets/sockets.c
@@ -24,9 +24,7 @@
 #include "php.h"

 #include "php_network.h"
-#include "ext/standard/file.h"
 #include "ext/standard/info.h"
-#include "php_ini.h"
 #ifdef PHP_WIN32
 # include "windows_common.h"
 # include <windows.h>
@@ -2791,7 +2789,7 @@ PHP_FUNCTION(socket_export_stream)
 	stream_data = (php_netstream_data_t *) stream->abstract;
 	stream_data->socket = socket->bsd_socket;
 	stream_data->is_blocked = socket->blocking;
-	stream_data->timeout.tv_sec = FG(default_socket_timeout);
+	stream_data->timeout.tv_sec = PG(default_socket_timeout);
 	stream_data->timeout.tv_usec = 0;

 	php_stream_to_zval(stream, &socket->zstream);
diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c
index 8443908a6a8..b8fd17fdba2 100644
--- a/ext/standard/basic_functions.c
+++ b/ext/standard/basic_functions.c
@@ -420,13 +420,6 @@ PHP_RINIT_FUNCTION(basic) /* {{{ */

 	/* Setup default context */
 	FG(default_context) = NULL;
-
-	/* Default to global wrappers only */
-	FG(stream_wrappers) = NULL;
-
-	/* Default to global filters only */
-	FG(stream_filters) = NULL;
-
 	return SUCCESS;
 }
 /* }}} */
@@ -459,9 +452,6 @@ PHP_RSHUTDOWN_FUNCTION(basic) /* {{{ */
 		}
 	}

-	/* FG(stream_wrappers) and FG(stream_filters) are destroyed
-	 * during php_request_shutdown() */
-
 	PHP_RSHUTDOWN(filestat)(SHUTDOWN_FUNC_ARGS_PASSTHRU);
 #ifdef HAVE_SYSLOG_H
 	BASIC_RSHUTDOWN_SUBMODULE(syslog);
diff --git a/ext/standard/file.c b/ext/standard/file.c
index e76310dd133..ee6adbe27aa 100644
--- a/ext/standard/file.c
+++ b/ext/standard/file.c
@@ -136,19 +136,9 @@ static void file_globals_dtor(php_file_globals *file_globals_p)
 #endif
 }

-static PHP_INI_MH(OnUpdateAutoDetectLineEndings)
-{
-	if (zend_ini_parse_bool(new_value)) {
-		zend_error(E_DEPRECATED, "auto_detect_line_endings is deprecated");
-	}
-	return OnUpdateBool(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
-}
-
 PHP_INI_BEGIN()
 	STD_PHP_INI_ENTRY("user_agent", NULL, PHP_INI_ALL, OnUpdateStr, user_agent, php_file_globals, file_globals)
 	STD_PHP_INI_ENTRY("from", NULL, PHP_INI_ALL, OnUpdateStr, from_address, php_file_globals, file_globals)
-	STD_PHP_INI_ENTRY("default_socket_timeout", "60", PHP_INI_ALL, OnUpdateLong, default_socket_timeout, php_file_globals, file_globals)
-	STD_PHP_INI_BOOLEAN("auto_detect_line_endings", "0", PHP_INI_ALL, OnUpdateAutoDetectLineEndings, auto_detect_line_endings, php_file_globals, file_globals)
 PHP_INI_END()

 PHP_MINIT_FUNCTION(file)
diff --git a/ext/standard/file.h b/ext/standard/file.h
index 9ba5f5b8b93..08dede7ef9e 100644
--- a/ext/standard/file.h
+++ b/ext/standard/file.h
@@ -91,16 +91,9 @@ php_meta_tags_token php_next_meta_token(php_meta_tags_data *);
 typedef struct {
 	int pclose_ret;
 	size_t def_chunk_size;
-	bool auto_detect_line_endings;
-	zend_long default_socket_timeout;
 	zend_string *user_agent; /* for the http wrapper */
 	zend_string *from_address; /* for the ftp and http wrappers */
-	const char *user_stream_current_filename; /* for simple recursion protection */
 	php_stream_context *default_context;
-	HashTable *stream_wrappers;			/* per-request copy of url_stream_wrappers_hash */
-	HashTable *stream_filters;			/* per-request copy of stream_filters_hash */
-	HashTable *wrapper_logged_errors;	/* key: wrapper address; value: linked list of error entries */
-	php_stream_error_state stream_error_state;
 	int pclose_wait;
 #ifdef HAVE_GETHOSTBYNAME_R
 	struct hostent tmp_host_info;
diff --git a/ext/standard/fsock.c b/ext/standard/fsock.c
index 61e6ccf979f..1b8cbbe1f34 100644
--- a/ext/standard/fsock.c
+++ b/ext/standard/fsock.c
@@ -72,7 +72,7 @@ static void php_fsockopen_stream(INTERNAL_FUNCTION_PARAMETERS, int persistent)
 	RETVAL_FALSE;

 	if (timeout_is_null) {
-		timeout = (double)FG(default_socket_timeout);
+		timeout = (double)PG(default_socket_timeout);
 	}

 	if (persistent) {
diff --git a/ext/standard/http_fopen_wrapper.c b/ext/standard/http_fopen_wrapper.c
index cdfb971f81f..f229c32e0e6 100644
--- a/ext/standard/http_fopen_wrapper.c
+++ b/ext/standard/http_fopen_wrapper.c
@@ -554,9 +554,9 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
 #endif
 	} else {
 #ifndef PHP_WIN32
-		timeout.tv_sec = FG(default_socket_timeout);
+		timeout.tv_sec = PG(default_socket_timeout);
 #else
-		timeout.tv_sec = (long)FG(default_socket_timeout);
+		timeout.tv_sec = (long)PG(default_socket_timeout);
 #endif
 		timeout.tv_usec = 0;
 	}
diff --git a/ext/standard/streamsfuncs.c b/ext/standard/streamsfuncs.c
index 22739539efc..a8362ea4f87 100644
--- a/ext/standard/streamsfuncs.c
+++ b/ext/standard/streamsfuncs.c
@@ -130,7 +130,7 @@ PHP_FUNCTION(stream_socket_client)
 	RETVAL_FALSE;

 	if (timeout_is_null) {
-		timeout = (double)FG(default_socket_timeout);
+		timeout = (double)PG(default_socket_timeout);
 	} else if (!zend_finite(timeout)) {
 		zend_argument_value_error(4, "must be a finite value");
 		RETURN_THROWS();
@@ -290,7 +290,7 @@ PHP_FUNCTION(stream_socket_accept)
 	ZEND_PARSE_PARAMETERS_END();

 	if (timeout_is_null) {
-		timeout = (double)FG(default_socket_timeout);
+		timeout = (double)PG(default_socket_timeout);
 	} else if (!zend_finite(timeout)) {
 		zend_argument_value_error(2, "must be a finite value");
 		RETURN_THROWS();
diff --git a/main/main.c b/main/main.c
index ed78b96315b..aa616313207 100644
--- a/main/main.c
+++ b/main/main.c
@@ -799,6 +799,15 @@ static PHP_INI_MH(OnChangeMailForceExtra)
 }
 /* }}} */

+static PHP_INI_MH(OnUpdateAutoDetectLineEndings)
+{
+	if (zend_ini_parse_bool(new_value)) {
+		zend_error(E_DEPRECATED, "auto_detect_line_endings is deprecated");
+	}
+	return OnUpdateBool(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
+}
+
+
 /* defined in browscap.c */
 PHP_INI_MH(OnChangeBrowscap);

@@ -921,6 +930,8 @@ PHP_INI_BEGIN()
 	STD_PHP_INI_ENTRY("syslog.facility",		"LOG_USER",		PHP_INI_SYSTEM,		OnSetFacility,		syslog_facility,	php_core_globals,		core_globals)
 	STD_PHP_INI_ENTRY("syslog.ident",		"php",			PHP_INI_SYSTEM,		OnUpdateString,		syslog_ident,		php_core_globals,		core_globals)
 	STD_PHP_INI_ENTRY("syslog.filter",		"no-ctrl",		PHP_INI_ALL,		OnSetLogFilter,		syslog_filter,		php_core_globals, 		core_globals)
+	STD_PHP_INI_ENTRY("default_socket_timeout", "60", PHP_INI_ALL, OnUpdateLong, default_socket_timeout, php_core_globals, core_globals)
+	STD_PHP_INI_BOOLEAN("auto_detect_line_endings", "0", PHP_INI_ALL, OnUpdateAutoDetectLineEndings, auto_detect_line_endings, php_core_globals, core_globals)
 PHP_INI_END()
 /* }}} */

@@ -1891,6 +1902,12 @@ zend_result php_request_startup(void)
 	signal(SIGCHLD, sigchld_handler);
 #endif

+	/* Default to global wrappers only */
+	PG(stream_wrappers) = NULL;
+
+	/* Default to global filters only */
+	PG(stream_filters) = NULL;
+
 	zend_try {
 		PG(in_error_log) = 0;
 		PG(during_request_startup) = 1;
diff --git a/main/network.c b/main/network.c
index cdca7efdac7..89c151b4555 100644
--- a/main/network.c
+++ b/main/network.c
@@ -1292,7 +1292,7 @@ PHPAPI php_stream *_php_stream_sock_open_from_socket(php_socket_t socket, const
 	memset(sock, 0, sizeof(php_netstream_data_t));

 	sock->is_blocked = true;
-	sock->timeout.tv_sec = FG(default_socket_timeout);
+	sock->timeout.tv_sec = PG(default_socket_timeout);
 	sock->timeout.tv_usec = 0;
 	sock->socket = socket;

diff --git a/main/php_globals.h b/main/php_globals.h
index 6bfdef2b9d1..876c57df906 100644
--- a/main/php_globals.h
+++ b/main/php_globals.h
@@ -138,6 +138,7 @@ struct _php_core_globals {
 	bool allow_url_fopen;
 	bool enable_post_data_reading;
 	bool report_zend_debug;
+	bool auto_detect_line_endings;

 	int last_error_type;
 	uint32_t last_error_lineno;
@@ -174,6 +175,12 @@ struct _php_core_globals {
 	char *syslog_ident;
 	zend_long syslog_filter;
 	zend_long error_log_mode;
+	zend_long default_socket_timeout;
+	const char *user_stream_current_filename; /* for simple recursion protection */
+	HashTable *stream_wrappers;			/* per-request copy of url_stream_wrappers_hash */
+	HashTable *stream_filters;			/* per-request copy of stream_filters_hash */
+	HashTable *wrapper_logged_errors;	/* key: wrapper address; value: linked list of error entries */
+	php_stream_error_state stream_error_state;
 };


diff --git a/main/streams/filter.c b/main/streams/filter.c
index df50ef017cd..b59f5052281 100644
--- a/main/streams/filter.c
+++ b/main/streams/filter.c
@@ -14,13 +14,12 @@

 #include "php.h"
 #include "php_network.h"
-#include "ext/standard/file.h"  /* For FG(stream_filters) */
 #include <stddef.h>
 #include <fcntl.h>

 #include "php_streams_int.h"

-/* Global filter hash, copied to FG(stream_filters) on registration of volatile filter */
+/* Global filter hash, copied to PG(stream_filters) on registration of volatile filter */
 static HashTable stream_filters_hash;

 /* Should only be used during core initialization */
@@ -32,7 +31,7 @@ PHPAPI HashTable *php_get_stream_filters_hash_global(void)
 /* Normal hash selection/retrieval call */
 PHPAPI HashTable *php_get_stream_filters_hash(void)
 {
-	return (FG(stream_filters) ? FG(stream_filters) : &stream_filters_hash);
+	return (PG(stream_filters) ? PG(stream_filters) : &stream_filters_hash);
 }

 /* API for registering GLOBAL filters */
@@ -53,13 +52,13 @@ PHPAPI zend_result php_stream_filter_unregister_factory(const char *filterpatter
 /* API for registering VOLATILE wrappers */
 PHPAPI zend_result php_stream_filter_register_factory_volatile(zend_string *filterpattern, const php_stream_filter_factory *factory)
 {
-	if (!FG(stream_filters)) {
-		ALLOC_HASHTABLE(FG(stream_filters));
-		zend_hash_init(FG(stream_filters), zend_hash_num_elements(&stream_filters_hash) + 1, NULL, NULL, 0);
-		zend_hash_copy(FG(stream_filters), &stream_filters_hash, NULL);
+	if (!PG(stream_filters)) {
+		ALLOC_HASHTABLE(PG(stream_filters));
+		zend_hash_init(PG(stream_filters), zend_hash_num_elements(&stream_filters_hash) + 1, NULL, NULL, 0);
+		zend_hash_copy(PG(stream_filters), &stream_filters_hash, NULL);
 	}

-	return zend_hash_add_ptr(FG(stream_filters), filterpattern, (void*)factory) ? SUCCESS : FAILURE;
+	return zend_hash_add_ptr(PG(stream_filters), filterpattern, (void*)factory) ? SUCCESS : FAILURE;
 }

 /* Buckets */
@@ -216,7 +215,7 @@ PHPAPI void php_stream_bucket_unlink(php_stream_bucket *bucket)
  * charsets (for example) but still be able to provide them all as filters */
 PHPAPI php_stream_filter *php_stream_filter_create(const char *filtername, zval *filterparams, bool persistent)
 {
-	HashTable *filter_hash = (FG(stream_filters) ? FG(stream_filters) : &stream_filters_hash);
+	HashTable *filter_hash = (PG(stream_filters) ? PG(stream_filters) : &stream_filters_hash);
 	const php_stream_filter_factory *factory = NULL;
 	php_stream_filter *filter = NULL;
 	size_t n;
diff --git a/main/streams/stream_errors.c b/main/streams/stream_errors.c
index 211cf4df025..bcdc163ee1e 100644
--- a/main/streams/stream_errors.c
+++ b/main/streams/stream_errors.c
@@ -191,7 +191,7 @@ static bool php_stream_has_terminating_error(const php_stream_error_entry *entry

 static inline php_stream_error_operation *php_stream_get_operation_at_depth(uint32_t depth)
 {
-	php_stream_error_state *state = &FG(stream_error_state);
+	php_stream_error_state *state = &PG(stream_error_state);

 	if (depth < PHP_STREAM_ERROR_OPERATION_POOL_SIZE) {
 		return &state->operation_pool[depth];
@@ -204,7 +204,7 @@ static inline php_stream_error_operation *php_stream_get_operation_at_depth(uint

 static inline php_stream_error_operation *php_stream_get_parent_operation(void)
 {
-	const php_stream_error_state *state = &FG(stream_error_state);
+	const php_stream_error_state *state = &PG(stream_error_state);

 	if (state->operation_depth <= state->operation_floor) {
 		return NULL;
@@ -229,7 +229,7 @@ static void php_stream_error_entry_free(php_stream_error_entry *entry)

 PHPAPI void php_stream_error_state_cleanup(void)
 {
-	php_stream_error_state *state = &FG(stream_error_state);
+	php_stream_error_state *state = &PG(stream_error_state);

 	state->operation_floor = 0;
 	state->refused_operations = 0;
@@ -266,7 +266,7 @@ PHPAPI void php_stream_error_state_cleanup(void)

 PHPAPI void php_stream_error_get_last(zval *return_value)
 {
-	const php_stream_error_state *state = &FG(stream_error_state);
+	const php_stream_error_state *state = &PG(stream_error_state);

 	if (!state->stored_errors) {
 		ZVAL_EMPTY_ARRAY(return_value);
@@ -278,7 +278,7 @@ PHPAPI void php_stream_error_get_last(zval *return_value)

 PHPAPI void php_stream_error_clear_stored(void)
 {
-	php_stream_error_state *state = &FG(stream_error_state);
+	php_stream_error_state *state = &PG(stream_error_state);

 	php_stream_stored_error *stored = state->stored_errors;
 	while (stored) {
@@ -296,7 +296,7 @@ PHPAPI void php_stream_error_clear_stored(void)

 PHPAPI php_stream_error_operation *php_stream_error_operation_begin(void)
 {
-	php_stream_error_state *state = &FG(stream_error_state);
+	php_stream_error_state *state = &PG(stream_error_state);

 	if (state->operation_depth >= PHP_STREAM_ERROR_MAX_DEPTH) {
 		php_error_docref(NULL, E_WARNING,
@@ -338,7 +338,7 @@ PHPAPI php_stream_error_operation *php_stream_error_operation_begin(void)
 static void php_stream_error_add(zend_enum_StreamErrorCode code, const char *wrapper_name,
 		zend_string *message, const char *docref, int severity, bool terminating)
 {
-	php_stream_error_operation *op = FG(stream_error_state).current_operation;
+	php_stream_error_operation *op = PG(stream_error_state).current_operation;
 	if (!op) {
 		zend_string_release(message);
 		return;
@@ -448,7 +448,7 @@ static void php_stream_report_errors(const php_stream_context *context, const ph

 PHPAPI void php_stream_error_operation_end(const php_stream_context *context)
 {
-	php_stream_error_state *state = &FG(stream_error_state);
+	php_stream_error_state *state = &PG(stream_error_state);
 	php_stream_error_operation *op = state->current_operation;

 	if (state->refused_operations > 0) {
@@ -559,7 +559,7 @@ PHPAPI void php_stream_error_operation_end(const php_stream_context *context)

 PHPAPI void php_stream_error_operation_end_for_stream(const php_stream *stream)
 {
-	php_stream_error_state *state = &FG(stream_error_state);
+	php_stream_error_state *state = &PG(stream_error_state);
 	php_stream_error_operation *op = state->current_operation;

 	if (state->refused_operations > 0) {
@@ -586,7 +586,7 @@ PHPAPI void php_stream_error_operation_end_for_stream(const php_stream *stream)

 PHPAPI void php_stream_error_operation_abort(void)
 {
-	php_stream_error_state *state = &FG(stream_error_state);
+	php_stream_error_state *state = &PG(stream_error_state);
 	php_stream_error_operation *op = state->current_operation;

 	if (state->refused_operations > 0) {
@@ -613,7 +613,7 @@ static void php_stream_wrapper_error_internal(const char *wrapper_name, const ph
 		const char *docref, int severity, bool terminating,
 		zend_enum_StreamErrorCode code, zend_string *message)
 {
-	bool implicit_operation = (FG(stream_error_state).current_operation == NULL);
+	bool implicit_operation = (PG(stream_error_state).current_operation == NULL);
 	if (implicit_operation) {
 		php_stream_error_operation_begin();
 	}
@@ -710,19 +710,19 @@ static void php_stream_wrapper_log_store_error(zend_string *message, zend_enum_S
 	entry->severity = severity;
 	entry->terminating = terminating;

-	if (!FG(wrapper_logged_errors)) {
-		ALLOC_HASHTABLE(FG(wrapper_logged_errors));
-		zend_hash_init(FG(wrapper_logged_errors), 8, NULL, php_stream_error_list_dtor, 0);
+	if (!PG(wrapper_logged_errors)) {
+		ALLOC_HASHTABLE(PG(wrapper_logged_errors));
+		zend_hash_init(PG(wrapper_logged_errors), 8, NULL, php_stream_error_list_dtor, 0);
 	}

 	zend_llist *list
-			= zend_hash_str_find_ptr(FG(wrapper_logged_errors), wrapper_name, strlen(wrapper_name));
+			= zend_hash_str_find_ptr(PG(wrapper_logged_errors), wrapper_name, strlen(wrapper_name));

 	if (!list) {
 		zend_llist new_list;
 		zend_llist_init(
 				&new_list, sizeof(php_stream_error_entry *), php_stream_error_entry_dtor_legacy, 0);
-		list = zend_hash_str_update_mem(FG(wrapper_logged_errors), wrapper_name,
+		list = zend_hash_str_update_mem(PG(wrapper_logged_errors), wrapper_name,
 				strlen(wrapper_name), &new_list, sizeof(new_list));
 	}

@@ -750,11 +750,11 @@ PHPAPI void php_stream_wrapper_log_error(const php_stream_wrapper *wrapper,

 static zend_llist *php_stream_get_wrapper_errors_list(const char *wrapper_name)
 {
-	if (!FG(wrapper_logged_errors)) {
+	if (!PG(wrapper_logged_errors)) {
 		return NULL;
 	}
 	return (zend_llist *) zend_hash_str_find_ptr(
-			FG(wrapper_logged_errors), wrapper_name, strlen(wrapper_name));
+			PG(wrapper_logged_errors), wrapper_name, strlen(wrapper_name));
 }

 PHPAPI void php_stream_display_wrapper_name_errors(const char *wrapper_name,
@@ -839,8 +839,8 @@ PHPAPI void php_stream_display_wrapper_errors(const php_stream_wrapper *wrapper,

 PHPAPI void php_stream_tidy_wrapper_name_error_log(const char *wrapper_name)
 {
-	if (FG(wrapper_logged_errors)) {
-		zend_hash_str_del(FG(wrapper_logged_errors), wrapper_name, strlen(wrapper_name));
+	if (PG(wrapper_logged_errors)) {
+		zend_hash_str_del(PG(wrapper_logged_errors), wrapper_name, strlen(wrapper_name));
 	}
 }

diff --git a/main/streams/streams.c b/main/streams/streams.c
index 579e336c0f3..8175c666dab 100644
--- a/main/streams/streams.c
+++ b/main/streams/streams.c
@@ -34,7 +34,7 @@
 #include "php_streams_int.h"

 /* {{{ resource and registration code */
-/* Global wrapper hash, copied to FG(stream_wrappers) on registration of volatile wrapper */
+/* Global wrapper hash, copied to PG(stream_wrappers) on registration of volatile wrapper */
 static HashTable url_stream_wrappers_hash;
 static int le_stream = -1; /* true global */
 static int le_pstream = -1; /* true global */
@@ -57,7 +57,7 @@ PHPAPI int php_file_le_stream_filter(void)

 PHPAPI HashTable *php_stream_get_url_stream_wrappers_hash(void)
 {
-	return (FG(stream_wrappers) ? FG(stream_wrappers) : &url_stream_wrappers_hash);
+	return (PG(stream_wrappers) ? PG(stream_wrappers) : &url_stream_wrappers_hash);
 }

 PHPAPI HashTable *php_stream_get_url_stream_wrappers_hash_global(void)
@@ -184,7 +184,7 @@ fprintf(stderr, "stream_alloc: %s:%p persistent=%s\n", ops->label, ret, persiste
 	ret->open_lineno = __zend_orig_lineno ? __zend_orig_lineno : __zend_lineno;
 #endif

-	if (FG(auto_detect_line_endings)) {
+	if (PG(auto_detect_line_endings)) {
 		ret->flags |= PHP_STREAM_FLAG_DETECT_EOL;
 	}

@@ -1804,23 +1804,23 @@ static void stream_resource_persistent_dtor(zend_resource *rsrc)

 void php_shutdown_stream_hashes(void)
 {
-	FG(user_stream_current_filename) = NULL;
-	if (FG(stream_wrappers)) {
-		zend_hash_destroy(FG(stream_wrappers));
-		efree(FG(stream_wrappers));
-		FG(stream_wrappers) = NULL;
+	PG(user_stream_current_filename) = NULL;
+	if (PG(stream_wrappers)) {
+		zend_hash_destroy(PG(stream_wrappers));
+		efree(PG(stream_wrappers));
+		PG(stream_wrappers) = NULL;
 	}

-	if (FG(stream_filters)) {
-		zend_hash_destroy(FG(stream_filters));
-		efree(FG(stream_filters));
-		FG(stream_filters) = NULL;
+	if (PG(stream_filters)) {
+		zend_hash_destroy(PG(stream_filters));
+		efree(PG(stream_filters));
+		PG(stream_filters) = NULL;
 	}

-	if (FG(wrapper_logged_errors)) {
-		zend_hash_destroy(FG(wrapper_logged_errors));
-		efree(FG(wrapper_logged_errors));
-		FG(wrapper_logged_errors) = NULL;
+	if (PG(wrapper_logged_errors)) {
+		zend_hash_destroy(PG(wrapper_logged_errors));
+		efree(PG(wrapper_logged_errors));
+		PG(wrapper_logged_errors) = NULL;
 	}

 	php_stream_error_state_cleanup();
@@ -1894,9 +1894,9 @@ PHPAPI zend_result php_unregister_url_stream_wrapper(const char *protocol)

 static void clone_wrapper_hash(void)
 {
-	ALLOC_HASHTABLE(FG(stream_wrappers));
-	zend_hash_init(FG(stream_wrappers), zend_hash_num_elements(&url_stream_wrappers_hash), NULL, NULL, 0);
-	zend_hash_copy(FG(stream_wrappers), &url_stream_wrappers_hash, NULL);
+	ALLOC_HASHTABLE(PG(stream_wrappers));
+	zend_hash_init(PG(stream_wrappers), zend_hash_num_elements(&url_stream_wrappers_hash), NULL, NULL, 0);
+	zend_hash_copy(PG(stream_wrappers), &url_stream_wrappers_hash, NULL);
 }

 /* API for registering VOLATILE wrappers */
@@ -1906,27 +1906,27 @@ PHPAPI zend_result php_register_url_stream_wrapper_volatile(zend_string *protoco
 		return FAILURE;
 	}

-	if (!FG(stream_wrappers)) {
+	if (!PG(stream_wrappers)) {
 		clone_wrapper_hash();
 	}

-	return zend_hash_add_ptr(FG(stream_wrappers), protocol, wrapper) ? SUCCESS : FAILURE;
+	return zend_hash_add_ptr(PG(stream_wrappers), protocol, wrapper) ? SUCCESS : FAILURE;
 }

 PHPAPI zend_result php_unregister_url_stream_wrapper_volatile(zend_string *protocol)
 {
-	if (!FG(stream_wrappers)) {
+	if (!PG(stream_wrappers)) {
 		clone_wrapper_hash();
 	}

-	return zend_hash_del(FG(stream_wrappers), protocol);
+	return zend_hash_del(PG(stream_wrappers), protocol);
 }
 /* }}} */

 /* {{{ php_stream_locate_url_wrapper */
 PHPAPI php_stream_wrapper *php_stream_locate_url_wrapper(const char *path, const char **path_for_open, int options)
 {
-	const HashTable *wrapper_hash = (FG(stream_wrappers) ? FG(stream_wrappers) : &url_stream_wrappers_hash);
+	const HashTable *wrapper_hash = (PG(stream_wrappers) ? PG(stream_wrappers) : &url_stream_wrappers_hash);
 	php_stream_wrapper *wrapper = NULL;
 	const char *p, *protocol = NULL;
 	size_t n = 0;
@@ -2012,7 +2012,7 @@ PHPAPI php_stream_wrapper *php_stream_locate_url_wrapper(const char *path, const
 			return NULL;
 		}

-		if (FG(stream_wrappers)) {
+		if (PG(stream_wrappers)) {
 		/* The file:// wrapper may have been disabled/overridden */

 			if (wrapper) {
diff --git a/main/streams/transports.c b/main/streams/transports.c
index ca0ab7a3d31..a1410aa4a0e 100644
--- a/main/streams/transports.c
+++ b/main/streams/transports.c
@@ -14,7 +14,6 @@

 #include "php.h"
 #include "php_streams_int.h"
-#include "ext/standard/file.h" /* For FG(default_socket_timeout) */

 static HashTable xport_hash;

@@ -63,7 +62,7 @@ PHPAPI php_stream *_php_stream_xport_create(const char *name, size_t namelen, in
 	zend_string *error_text = NULL;
 	struct timeval default_timeout = { 0, 0 };

-	default_timeout.tv_sec = FG(default_socket_timeout);
+	default_timeout.tv_sec = PG(default_socket_timeout);

 	if (timeout == NULL) {
 		timeout = &default_timeout;
diff --git a/main/streams/userspace.c b/main/streams/userspace.c
index 088eb21bfda..aeb49e15ee1 100644
--- a/main/streams/userspace.c
+++ b/main/streams/userspace.c
@@ -15,7 +15,6 @@

 #include "php.h"
 #include "php_globals.h"
-#include "ext/standard/file.h"
 #include "ext/standard/flock_compat.h"
 #ifdef HAVE_SYS_FILE_H
 #include <sys/file.h>
@@ -288,12 +287,12 @@ static php_stream *user_wrapper_opener(php_stream_wrapper *wrapper, const char *
 	bool old_in_user_include;

 	/* Try to catch bad usage without preventing flexibility */
-	if (FG(user_stream_current_filename) != NULL && strcmp(filename, FG(user_stream_current_filename)) == 0) {
+	if (PG(user_stream_current_filename) != NULL && strcmp(filename, PG(user_stream_current_filename)) == 0) {
 		php_stream_wrapper_log_warn(wrapper, context, options,
 				RecursionDetected, "infinite recursion prevented");
 		return NULL;
 	}
-	FG(user_stream_current_filename) = filename;
+	PG(user_stream_current_filename) = filename;

 	/* if the user stream was registered as local and we are in include context,
 		we add allow_url_include restrictions to allow_url_fopen ones */
@@ -363,7 +362,7 @@ static php_stream *user_wrapper_opener(php_stream_wrapper *wrapper, const char *
 	zval_ptr_dtor(&args[3]);

 end:
-	FG(user_stream_current_filename) = NULL;
+	PG(user_stream_current_filename) = NULL;
 	PG(in_user_include) = old_in_user_include;
 	if (stream == NULL) {
 		if (us->object) {
@@ -393,12 +392,12 @@ static php_stream *user_wrapper_opendir(php_stream_wrapper *wrapper, const char
 	php_stream *stream = NULL;

 	/* Try to catch bad usage without preventing flexibility */
-	if (FG(user_stream_current_filename) != NULL && strcmp(filename, FG(user_stream_current_filename)) == 0) {
+	if (PG(user_stream_current_filename) != NULL && strcmp(filename, PG(user_stream_current_filename)) == 0) {
 		php_stream_wrapper_log_warn(wrapper, context, options,
 				RecursionDetected, "infinite recursion prevented");
 		return NULL;
 	}
-	FG(user_stream_current_filename) = filename;
+	PG(user_stream_current_filename) = filename;

 	us = emalloc(sizeof(*us));
 	us->wrapper = uwrap;
@@ -444,7 +443,7 @@ static php_stream *user_wrapper_opendir(php_stream_wrapper *wrapper, const char
 	zval_ptr_dtor(&zretval);

 end:
-	FG(user_stream_current_filename) = NULL;
+	PG(user_stream_current_filename) = NULL;
 	if (stream == NULL) {
 		if (us->object) {
 			OBJ_RELEASE(us->object);
diff --git a/main/streams/xp_socket.c b/main/streams/xp_socket.c
index a363f807053..94b3f1cdb86 100644
--- a/main/streams/xp_socket.c
+++ b/main/streams/xp_socket.c
@@ -12,8 +12,7 @@
   +----------------------------------------------------------------------+
 */

-#include "php.h"
-#include "ext/standard/file.h"
+#include "php.h" /* Needed for PHPAPI */
 #include "php_streams.h"
 #include "php_io.h"

@@ -354,7 +353,7 @@ static int php_sockop_set_option(php_stream *stream, int option, int value, void

 				if (value == -1) {
 					if (sock->timeout.tv_sec == -1) {
-						tv.tv_sec = FG(default_socket_timeout);
+						tv.tv_sec = PG(default_socket_timeout);
 						tv.tv_usec = 0;
 					} else {
 						tv = sock->timeout;
@@ -1166,7 +1165,7 @@ PHPAPI php_stream *php_stream_generic_socket_factory(const char *proto, size_t p
 	memset(sock, 0, sizeof(php_netstream_data_t));

 	sock->is_blocked = true;
-	sock->timeout.tv_sec = FG(default_socket_timeout);
+	sock->timeout.tv_sec = PG(default_socket_timeout);
 	sock->timeout.tv_usec = 0;

 	/* we don't know the socket until we have determined if we are binding or
diff --git a/sapi/cli/php_cli.c b/sapi/cli/php_cli.c
index fc62c0dd4b4..80c498354f6 100644
--- a/sapi/cli/php_cli.c
+++ b/sapi/cli/php_cli.c
@@ -214,7 +214,7 @@ static inline bool sapi_cli_select(php_socket_t fd)
 {
 	struct timeval tv;

-	tv.tv_sec = (long)FG(default_socket_timeout);
+	tv.tv_sec = (long)PG(default_socket_timeout);
 	tv.tv_usec = 0;

 	return php_pollfd_for(fd, POLLOUT, &tv) != -1;