]> granicus.if.org Git - php/commitdiff
Add stream_wrapper_unregister()
authorSara Golemon <pollita@php.net>
Fri, 10 Sep 2004 20:45:35 +0000 (20:45 +0000)
committerSara Golemon <pollita@php.net>
Fri, 10 Sep 2004 20:45:35 +0000 (20:45 +0000)
Disables a wrapper (user-defined or built-in) for the life of the request.

Add stream_wrapper_restore()
Restores the wrapper originally defined at the time the request started
to the protocol name mentioned.

NEWS
ext/standard/basic_functions.c
ext/standard/streamsfuncs.h
main/php_streams.h
main/streams/streams.c
main/streams/userspace.c

diff --git a/NEWS b/NEWS
index c189512900743979d06bbfc5f8a2185febb32edf..eaa7887c1a5616632e94c9b29aac02fbedb8337f 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -14,6 +14,8 @@ PHP                                                                        NEWS
   . array_intersect_ukey() (Christiano Duarte)
   . stream_context_get_default() (Wez)
   . stream_socket_enable_crypto() (Wez)
+  . stream_wrapper_unregister() (Sara)
+  . stream_wrapper_restore() (Sara)
   . DomDocumentFragment->appendXML() (Christian)
   . SimpleXMLElement->registerXPathNamespace() (Christian)
   . mysqli->client_info property (Georg)
index c52434ad358b843ec8575aa4001fcf5a772eaeb0..3aa0eb25e49432f97156f678a671a600d2630195 100644 (file)
@@ -623,6 +623,8 @@ function_entry basic_functions[] = {
        PHP_FE(stream_get_line,                                                                                         NULL)
        PHP_FE(stream_wrapper_register,                                                                                 NULL)
        PHP_FALIAS(stream_register_wrapper, stream_wrapper_register,                    NULL)
+       PHP_FE(stream_wrapper_unregister,                                                                               NULL)
+       PHP_FE(stream_wrapper_restore,                                                                                  NULL)
        PHP_FE(stream_get_wrappers,                                                                                             NULL)
        PHP_FE(stream_get_transports,                                                                                   NULL)
        PHP_FE(get_headers,                                                                                                     NULL)
index 6ec9dba491c4ad628ef0e9347d3a73e05985b4ca..9b391b0c4795e600c7dc949b564457c0d519a2c4 100644 (file)
@@ -43,6 +43,8 @@ PHP_FUNCTION(stream_get_wrappers);
 PHP_FUNCTION(stream_get_line);
 PHP_FUNCTION(stream_get_meta_data);
 PHP_FUNCTION(stream_wrapper_register);
+PHP_FUNCTION(stream_wrapper_unregister);
+PHP_FUNCTION(stream_wrapper_restore);
 PHP_FUNCTION(stream_context_create);
 PHP_FUNCTION(stream_context_set_params);
 PHP_FUNCTION(stream_context_set_option);
index 000b4291c3ecf761d4b0ca7776664b53fd49e1b1..290ec14e32af400cd69460ddab2b7db5a77924e4 100755 (executable)
@@ -510,6 +510,7 @@ BEGIN_EXTERN_C()
 PHPAPI int php_register_url_stream_wrapper(char *protocol, php_stream_wrapper *wrapper TSRMLS_DC);
 PHPAPI int php_unregister_url_stream_wrapper(char *protocol TSRMLS_DC);
 PHPAPI int php_register_url_stream_wrapper_volatile(char *protocol, php_stream_wrapper *wrapper TSRMLS_DC);
+PHPAPI int php_unregister_url_stream_wrapper_volatile(char *protocol TSRMLS_DC);
 PHPAPI php_stream *_php_stream_open_wrapper_ex(char *path, char *mode, int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC);
 PHPAPI php_stream_wrapper *php_stream_locate_url_wrapper(const char *path, char **path_for_open, int options TSRMLS_DC);
 PHPAPI char *php_stream_locate_eol(php_stream *stream, char *buf, size_t buf_len TSRMLS_DC);
@@ -541,6 +542,7 @@ PHPAPI int _php_stream_make_seekable(php_stream *origstream, php_stream **newstr
 /* Give other modules access to the url_stream_wrappers_hash and stream_filters_hash */
 PHPAPI HashTable *_php_stream_get_url_stream_wrappers_hash(TSRMLS_D);
 #define php_stream_get_url_stream_wrappers_hash()      _php_stream_get_url_stream_wrappers_hash(TSRMLS_C)
+PHPAPI HashTable *php_stream_get_url_stream_wrappers_hash_global(void);
 PHPAPI HashTable *_php_get_stream_filters_hash(TSRMLS_D);
 #define php_get_stream_filters_hash()  _php_get_stream_filters_hash(TSRMLS_C)
 PHPAPI HashTable *php_get_stream_filters_hash_global();
index fc5a0b82ee98541879d294436bd0cc8a98593801..0e73f320a694dc133784bdd31250e655d11967ce 100755 (executable)
@@ -54,6 +54,11 @@ PHPAPI HashTable *_php_stream_get_url_stream_wrappers_hash(TSRMLS_D)
        return (FG(stream_wrappers) ? FG(stream_wrappers) : &url_stream_wrappers_hash);
 }
 
+PHPAPI HashTable *php_stream_get_url_stream_wrappers_hash_global(void)
+{
+       return &url_stream_wrappers_hash;
+}
+
 static int _php_stream_release_context(list_entry *le, void *pContext TSRMLS_DC)
 {
        if (le->ptr == pContext) {
@@ -1417,6 +1422,18 @@ PHPAPI int php_register_url_stream_wrapper_volatile(char *protocol, php_stream_w
        return zend_hash_add(FG(stream_wrappers), protocol, strlen(protocol), wrapper, sizeof(*wrapper), NULL);
 }
 
+PHPAPI int php_unregister_url_stream_wrapper_volatile(char *protocol TSRMLS_DC)
+{
+       if (!FG(stream_wrappers)) {
+               php_stream_wrapper tmpwrapper;
+
+               FG(stream_wrappers) = emalloc(sizeof(HashTable));
+               zend_hash_init(FG(stream_wrappers), 0, NULL, NULL, 1);
+               zend_hash_copy(FG(stream_wrappers), &url_stream_wrappers_hash, NULL, &tmpwrapper, sizeof(php_stream_wrapper));
+       }
+
+       return zend_hash_del(FG(stream_wrappers), protocol, strlen(protocol));
+}
 /* }}} */
 
 /* {{{ php_stream_locate_url_wrapper */
index 7bf7801d2f5ab61fad3a99101bc0135749efb479..0b687290923bd4dca67cb98d000acd10a20436c8 100644 (file)
@@ -437,6 +437,62 @@ PHP_FUNCTION(stream_wrapper_register)
 }
 /* }}} */
 
+/* {{{ bool stream_wrapper_unregister(string protocol)
+       Unregister a wrapper for the life of the current request. */
+PHP_FUNCTION(stream_wrapper_unregister)
+{
+       char *protocol;
+       int protocol_len;
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &protocol, &protocol_len) == FAILURE) {
+               RETURN_FALSE;
+       }
+
+       if (php_unregister_url_stream_wrapper_volatile(protocol TSRMLS_CC) == FAILURE) {
+               /* We failed */
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to unregister protocol %s://", protocol);
+               RETURN_FALSE;
+       }
+
+       RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ bool stream_wrapper_restore(string protocol)
+       Restore the original protocol handler, overriding if necessary */
+PHP_FUNCTION(stream_wrapper_restore)
+{
+       char *protocol;
+       int protocol_len;
+       php_stream_wrapper *wrapper = NULL;
+       HashTable *global_wrapper_hash;
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &protocol, &protocol_len) == FAILURE) {
+               RETURN_FALSE;
+       }
+
+       global_wrapper_hash = php_stream_get_url_stream_wrappers_hash_global();
+       if (php_stream_get_url_stream_wrappers_hash() == global_wrapper_hash) {
+               php_error_docref(NULL TSRMLS_CC, E_NOTICE, "%s:// was never changed, nothing to restore", protocol);
+               RETURN_TRUE;
+       }
+
+       if ((zend_hash_find(global_wrapper_hash, protocol, protocol_len, (void**)&wrapper) == FAILURE) || !wrapper) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s:// never existed, nothing to restore", protocol);
+               RETURN_FALSE;
+       }
+
+       /* A failure here could be okay given that the protocol might have been merely unregistered */
+       php_unregister_url_stream_wrapper_volatile(protocol TSRMLS_CC);
+
+       if (php_register_url_stream_wrapper_volatile(protocol, wrapper TSRMLS_CC) == FAILURE) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to restore original %s:// wrapper", protocol);
+               RETURN_FALSE;
+       }       
+
+       RETURN_TRUE;
+}
+/* }}} */
 
 static size_t php_userstreamop_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
 {