]> granicus.if.org Git - php/commitdiff
@ - Added output_add_rewrite_var() and output_remove_rewrite_var() to inject
authorThies C. Arntzen <thies@php.net>
Fri, 3 May 2002 08:00:41 +0000 (08:00 +0000)
committerThies C. Arntzen <thies@php.net>
Fri, 3 May 2002 08:00:41 +0000 (08:00 +0000)
@   and remove variables from the URL-Rewriter. (thies)
i have also modified the session module to use this - so it doesn't
need to fiddle with the output-system any more

ext/session/php_session.h
ext/session/session.c
ext/standard/basic_functions.c
ext/standard/url_scanner_ex.h
ext/standard/url_scanner_ex.re
main/output.c

index ed54dd4da5996558846e4444ed59cab83b905bd8..8bd90773b5c2b4d38a0a7befd2db402d4a4aec52 100644 (file)
@@ -115,7 +115,6 @@ typedef struct _php_ps_globals {
        zend_bool use_cookies;
        zend_bool use_trans_sid;        /* contains the INI value of whether to use trans-sid */
        zend_bool apply_trans_sid;      /* whether or not to enable trans-sid for the current request */
-       zend_bool output_handler_registered;
 } php_ps_globals;
 
 typedef php_ps_globals zend_ps_globals;
@@ -172,8 +171,6 @@ typedef struct ps_serializer_struct {
 #define PS_SERIALIZER_ENTRY(x) \
        { #x, PS_SERIALIZER_ENCODE_NAME(x), PS_SERIALIZER_DECODE_NAME(x) }
 
-PHPAPI void session_adapt_url(const char *, size_t, char **, size_t * TSRMLS_DC);
-
 void php_set_session_var(char *name, size_t namelen, zval *state_val, php_unserialize_data_t *var_hash TSRMLS_DC);
 int php_get_session_var(char *name, size_t namelen, zval ***state_var TSRMLS_DC);
 
index 6588d0992f45067c04bfc636b26e6f1a96ce05ff..fe151e076f709e987f6c02b1b43e639c705a3ce6 100644 (file)
@@ -79,34 +79,6 @@ ZEND_DECLARE_MODULE_GLOBALS(ps);
 
 static ps_module *_php_find_ps_module(char *name TSRMLS_DC);
 static const ps_serializer *_php_find_ps_serializer(char *name TSRMLS_DC);
-static void php_session_end_output_handler(TSRMLS_D);
-
-static void php_session_output_handler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
-{
-       if ((PS(session_status) == php_session_active)) {
-               *handled_output = url_adapt_ext_ex(output, output_len, PS(session_name), PS(id), handled_output_len, (zend_bool) (mode&PHP_OUTPUT_HANDLER_END ? 1 : 0) TSRMLS_CC);
-       } else {
-               *handled_output = NULL;
-       }
-}
-
-
-static void php_session_start_output_handler(uint chunk_size TSRMLS_DC)
-{
-       php_url_scanner_activate(TSRMLS_C);
-       php_url_scanner_ex_activate(TSRMLS_C);
-       php_start_ob_buffer(NULL, chunk_size, 1 TSRMLS_CC);
-       php_ob_set_internal_handler(php_session_output_handler, chunk_size, "trans sid session", 1 TSRMLS_CC);
-       PS(output_handler_registered) = 1;
-}
-
-
-static void php_session_end_output_handler(TSRMLS_D)
-{
-       php_url_scanner_ex_deactivate(TSRMLS_C);
-       php_url_scanner_deactivate(TSRMLS_C);
-}
-
 
 static PHP_INI_MH(OnUpdateSaveHandler)
 {
@@ -956,7 +928,7 @@ PHPAPI void php_session_start(TSRMLS_D)
 
        PS(session_status) = php_session_active;
        if (PS(apply_trans_sid)) {
-               php_session_start_output_handler(4096 TSRMLS_CC);
+               php_url_scanner_add_var(PS(session_name), strlen(PS(session_name)), PS(id), strlen(PS(id)), 0);
        }
 
        php_session_cache_limiter(TSRMLS_C);
@@ -1404,21 +1376,13 @@ PHP_FUNCTION(session_unset)
 /* }}} */
 
 
-PHPAPI void session_adapt_url(const char *url, size_t urllen, char **new, size_t *newlen TSRMLS_DC)
-{
-       if (PS(apply_trans_sid) && (PS(session_status) == php_session_active)) {
-               *new = url_adapt_single_url(url, urllen, PS(session_name), PS(id), newlen TSRMLS_CC);
-       }
-}
-
-
 static void php_rinit_session_globals(TSRMLS_D)
 {              
        zend_hash_init(&PS(vars), 0, NULL, NULL, 0);
+       php_url_scanner_remove_var(PS(session_name), strlen(PS(session_name))); /* save even if we haven't registered */
        PS(id) = NULL;
        PS(session_status) = php_session_none;
        PS(mod_data) = NULL;
-       PS(output_handler_registered) = 0;
        PS(http_session_vars) = NULL;
 }
 
@@ -1477,9 +1441,6 @@ PHP_FUNCTION(session_write_close)
 
 PHP_RSHUTDOWN_FUNCTION(session)
 {
-       if (PS(output_handler_registered)) {
-               php_session_end_output_handler(TSRMLS_C);
-       }
        php_session_flush(TSRMLS_C);
        php_rshutdown_session_globals(TSRMLS_C);
        return SUCCESS;
index 286ba2845a89aa4d1049932958193158fcae9a4e..56b9d27c9034c0bea89f98aa39dbd62bbfcac464 100644 (file)
@@ -809,6 +809,10 @@ function_entry basic_functions[] = {
 #endif
        PHP_FE(deaggregate,                                             first_arg_force_ref)
        PHP_FE(aggregation_info,                                first_arg_force_ref)
+
+       PHP_FE(output_add_rewrite_var,                                                                                  NULL)
+       PHP_FE(output_remove_rewrite_var,                                                                               NULL)
+
        {NULL, NULL, NULL}
 };
 
@@ -1062,6 +1066,7 @@ PHP_RINIT_FUNCTION(basic)
        PHP_RINIT(filestat) (INIT_FUNC_ARGS_PASSTHRU);
        PHP_RINIT(syslog) (INIT_FUNC_ARGS_PASSTHRU);
        PHP_RINIT(dir) (INIT_FUNC_ARGS_PASSTHRU);
+       PHP_RINIT(url_scanner_ex) (INIT_FUNC_ARGS_PASSTHRU);
 
        return SUCCESS;
 }
@@ -1090,6 +1095,7 @@ PHP_RSHUTDOWN_FUNCTION(basic)
        PHP_RSHUTDOWN(filestat) (SHUTDOWN_FUNC_ARGS_PASSTHRU);
        PHP_RSHUTDOWN(syslog) (SHUTDOWN_FUNC_ARGS_PASSTHRU);
        PHP_RSHUTDOWN(assert) (SHUTDOWN_FUNC_ARGS_PASSTHRU);
+       PHP_RSHUTDOWN(url_scanner_ex) (SHUTDOWN_FUNC_ARGS_PASSTHRU);
 
        if (BG(user_tick_functions)) {
                zend_llist_destroy(BG(user_tick_functions));
index a9b105d687a5fd1970482a8b2d3856365656fa27..3cc16c85311df9799e3e44a6c7bfedf4d9099e55 100644 (file)
 
 PHP_MINIT_FUNCTION(url_scanner_ex);
 PHP_MSHUTDOWN_FUNCTION(url_scanner_ex);
-int php_url_scanner_ex_activate(TSRMLS_D);
-int php_url_scanner_ex_deactivate(TSRMLS_D);
 
-char *url_adapt_ext_ex(const char *src, size_t srclen, const char *name, const char *value, size_t *newlen, zend_bool do_flush TSRMLS_DC);
+PHP_RINIT_FUNCTION(url_scanner_ex);
+PHP_RSHUTDOWN_FUNCTION(url_scanner_ex);
 
-char *url_adapt_single_url(const char *url, size_t urllen, const char *name, const char *value, size_t *newlen TSRMLS_DC);
+PHP_FUNCTION(output_add_rewrite_var);
+PHP_FUNCTION(output_remove_rewrite_var);
 
-char *url_adapt_flush(size_t * TSRMLS_DC);
+int php_url_scanner_add_var(char *name, int name_len, char *value, int value_len, int urlencode TSRMLS_DC);
+int php_url_scanner_remove_var(char *name, int name_len TSRMLS_DC);
+
+int php_url_scanner_ex_activate(TSRMLS_D);
+int php_url_scanner_ex_deactivate(TSRMLS_D);
 
 #include "php_smart_str_public.h"
 
@@ -43,8 +47,9 @@ typedef struct {
        smart_str result;
 
        /* The data which is appended to each relative URL */
-       smart_str q_name;
-       smart_str q_value;
+       HashTable *rewrite_vars;
+
+       int active;
 
        char *lookup_data;
        int state;
@@ -53,4 +58,9 @@ typedef struct {
        HashTable *tags;
 } url_adapt_state_ex_t;
 
+typedef struct {
+       smart_str var;
+       smart_str val;
+} url_adapt_var_t;
+
 #endif
index 4d7c507894085eab2dd4a61e089f7a7774c4ff71..ffbca6f261e90ba8aa992529e51246d5aceb9de6 100644 (file)
@@ -32,7 +32,6 @@
 #include "basic_functions.h"
 #undef STATE_TAG
 
-#define url_adapt_ext url_adapt_ext_ex
 #define url_scanner url_scanner_ex
 
 #include "php_smart_str.h"
@@ -96,11 +95,15 @@ alpha = [a-zA-Z];
 #define YYLIMIT q
 #define YYMARKER r
        
-static inline void append_modified_url(smart_str *url, smart_str *dest, smart_str *name, smart_str *val, const char *separator)
+static inline void append_modified_url(smart_str *url, smart_str *dest, HashTable *rewrite_vars, const char *separator)
 {
        register const char *p, *q;
        const char *bash = NULL;
        const char *sep = "?";
+       char *string_key;
+       url_adapt_var_t *value;
+       int num_key;
+
        
        q = (p = url->c) + url->len;
 
@@ -124,10 +127,16 @@ done:
        else
                smart_str_append(dest, url);
 
-       smart_str_appends(dest, sep);
-       smart_str_append(dest, name);
-       smart_str_appendc(dest, '=');
-       smart_str_append(dest, val);
+       zend_hash_internal_pointer_reset(rewrite_vars);
+       while (zend_hash_get_current_key(rewrite_vars, &string_key, &num_key, 0) != HASH_KEY_NON_EXISTANT) {
+               zend_hash_get_current_data(rewrite_vars, &value);
+               smart_str_appends(dest, sep);
+               smart_str_append(dest, &(value->var));
+               smart_str_appendc(dest, '=');
+               smart_str_append(dest, &(value->val));
+               zend_hash_move_forward(rewrite_vars);
+               sep = separator;  /* switch from ? to now! */
+       }
 
        if (bash)
                smart_str_appendl(dest, bash, q - bash);
@@ -150,7 +159,7 @@ static inline void tag_arg(url_adapt_state_ex_t *ctx, char quotes, char type TSR
        if (quotes)
                smart_str_appendc(&ctx->result, type);
        if (f) {
-               append_modified_url(&ctx->val, &ctx->result, &ctx->q_name, &ctx->q_value, PG(arg_separator).output);
+               append_modified_url(&ctx->val, &ctx->result, ctx->rewrite_vars, PG(arg_separator).output);
        } else {
                smart_str_append(&ctx->result, &ctx->val);
        }
@@ -185,11 +194,20 @@ static inline void passthru(STD_PARA)
 static inline void handle_form(STD_PARA) 
 {
        if (ctx->tag.len == 4 && strncasecmp(ctx->tag.c, "form", 4) == 0) {
-               smart_str_appends(&ctx->result, "<input type=\"hidden\" name=\""); 
-               smart_str_append(&ctx->result, &ctx->q_name);
-               smart_str_appends(&ctx->result, "\" value=\"");
-               smart_str_append(&ctx->result, &ctx->q_value);
-               smart_str_appends(&ctx->result, "\" />");
+               char *string_key;
+               url_adapt_var_t *value;
+               int num_key;
+
+               zend_hash_internal_pointer_reset(ctx->rewrite_vars);
+               while (zend_hash_get_current_key(ctx->rewrite_vars, &string_key, &num_key, 0) != HASH_KEY_NON_EXISTANT) {
+                       zend_hash_get_current_data(ctx->rewrite_vars, &value);
+                       smart_str_appends(&ctx->result, "<input type=\"hidden\" name=\""); 
+                       smart_str_append(&ctx->result, &(value->var));
+                       smart_str_appends(&ctx->result, "\" value=\"");
+                       smart_str_append(&ctx->result, &(value->val));
+                       smart_str_appends(&ctx->result, "\" />");
+                       zend_hash_move_forward(ctx->rewrite_vars);
+               }
        }
 }
 
@@ -318,34 +336,13 @@ stop:
 }
 
 
-char *url_adapt_single_url(const char *url, size_t urllen, const char *name, const char *value, size_t *newlen TSRMLS_DC)
-{
-       smart_str surl = {0};
-       smart_str buf = {0};
-       smart_str sname = {0};
-       smart_str sval = {0};
-
-       smart_str_setl(&surl, url, urllen);
-       smart_str_sets(&sname, name);
-       smart_str_sets(&sval, value);
-
-       append_modified_url(&surl, &buf, &sname, &sval, PG(arg_separator).output);
-
-       smart_str_0(&buf);
-       if (newlen) *newlen = buf.len;
-       
-       return buf.c;
-}
-
-char *url_adapt_ext(const char *src, size_t srclen, const char *name, const char *value, size_t *newlen, zend_bool do_flush TSRMLS_DC)
+static char *url_adapt_ext(const char *src, size_t srclen, size_t *newlen, zend_bool do_flush TSRMLS_DC)
 {
        url_adapt_state_ex_t *ctx;
        char *retval;
 
        ctx = &BG(url_adapt_state_ex);
 
-       smart_str_sets(&ctx->q_name, name);
-       smart_str_sets(&ctx->q_value, value);
        xx_mainloop(ctx, src, srclen TSRMLS_CC);
 
        *newlen = ctx->result.len;
@@ -389,14 +386,66 @@ int php_url_scanner_ex_deactivate(TSRMLS_D)
        return SUCCESS;
 }
 
+static void php_url_scanner_output_handler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
+{
+    if (BG(url_adapt_state_ex).rewrite_vars && zend_hash_num_elements(BG(url_adapt_state_ex).rewrite_vars)) {
+        *handled_output = url_adapt_ext(output, output_len, handled_output_len, (zend_bool) (mode&PHP_OUTPUT_HANDLER_END ? 1 : 0) TSRMLS_CC);
+    } else {
+        *handled_output = NULL;
+    }
+}
+
+static void php_url_scanner_var_dtor(url_adapt_var_t *var)
+{
+       smart_str_free(&(var->var));
+       smart_str_free(&(var->val));
+}
+
+int php_url_scanner_add_var(char *name, int name_len, char *value, int value_len, int urlencode TSRMLS_DC)
+{
+       url_adapt_var_t var;
+       char *encoded;
+       int encoded_len;
+
+       if (! BG(url_adapt_state_ex).active) {
+               int chunk_size = 4096; /* XXX where should we get chunk_size from? */
+
+               php_url_scanner_ex_activate(TSRMLS_C);
+               php_start_ob_buffer(NULL, chunk_size, 1 TSRMLS_CC);
+               php_ob_set_internal_handler(php_url_scanner_output_handler, chunk_size, "URL-Rewriter", 1 TSRMLS_CC);
+               BG(url_adapt_state_ex).active = 1;
+       }
+
+       if (! BG(url_adapt_state_ex).rewrite_vars) {
+               BG(url_adapt_state_ex).rewrite_vars = emalloc(sizeof(HashTable));
+               zend_hash_init(BG(url_adapt_state_ex).rewrite_vars, 0, NULL, (void (*)(void *)) php_url_scanner_var_dtor, 0);
+       }
+
+       smart_str_setl(&(var.var), estrndup(name, name_len), name_len);
+
+       if (urlencode) {
+               encoded = php_url_encode(value, value_len, &encoded_len);
+               smart_str_setl(&(var.val), encoded, encoded_len);
+       } else {
+               smart_str_setl(&(var.val), estrndup(value, value_len), value_len);
+       }
+
+       return zend_hash_add(BG(url_adapt_state_ex).rewrite_vars, name, name_len, &var, sizeof(url_adapt_var_t), NULL);
+}
+
+int php_url_scanner_remove_var(char *name, int name_len TSRMLS_DC)
+{
+       if (BG(url_adapt_state_ex).rewrite_vars) {
+               return zend_hash_del(BG(url_adapt_state_ex).rewrite_vars, name, name_len);
+       }
+
+       return FAILURE;
+}
+
 PHP_MINIT_FUNCTION(url_scanner)
 {
-       url_adapt_state_ex_t *ctx;
-       
-       ctx = &BG(url_adapt_state_ex);
+       BG(url_adapt_state_ex).tags = NULL;
 
-       ctx->tags = NULL;
-       
        REGISTER_INI_ENTRIES();
        return SUCCESS;
 }
@@ -406,5 +455,30 @@ PHP_MSHUTDOWN_FUNCTION(url_scanner)
        UNREGISTER_INI_ENTRIES();
        zend_hash_destroy(BG(url_adapt_state_ex).tags);
        free(BG(url_adapt_state_ex).tags);
+
+       return SUCCESS;
+}
+
+PHP_RINIT_FUNCTION(url_scanner)
+{
+       BG(url_adapt_state_ex).active = 0;
+       BG(url_adapt_state_ex).rewrite_vars = NULL;
+
+       return SUCCESS;
+}
+
+PHP_RSHUTDOWN_FUNCTION(url_scanner)
+{
+       if (BG(url_adapt_state_ex).active) {
+               php_url_scanner_ex_deactivate(TSRMLS_C);
+               BG(url_adapt_state_ex).active = 0;
+       }
+
+       if (BG(url_adapt_state_ex).rewrite_vars) {
+               zend_hash_destroy(BG(url_adapt_state_ex).rewrite_vars);
+               efree(BG(url_adapt_state_ex).rewrite_vars);
+               BG(url_adapt_state_ex).rewrite_vars = NULL;
+       }
+
        return SUCCESS;
 }
index 27c275c5b6a631d840a7ef32c1791a53bd9f2cd1..1c9299825fc790b4003eee2f55387ee2a6f4eb41 100644 (file)
@@ -22,6 +22,7 @@
 #include "php.h"
 #include "ext/standard/head.h"
 #include "ext/standard/basic_functions.h"
+#include "ext/standard/url_scanner_ex.h"
 #include "SAPI.h"
 
 /* output functions */
@@ -790,6 +791,38 @@ PHPAPI int php_get_output_start_lineno(TSRMLS_D)
        return OG(output_start_lineno);
 }
 
+PHP_FUNCTION(output_remove_rewrite_var)
+{
+       char *name;
+       int name_len;
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) {
+               RETURN_FALSE;
+       }
+
+       if (php_url_scanner_remove_var(name, name_len TSRMLS_CC) == SUCCESS) {
+               RETURN_TRUE;
+       } else {
+               RETURN_FALSE;
+       }
+}
+
+PHP_FUNCTION(output_add_rewrite_var)
+{
+       char *name, *value;
+       int name_len, value_len;
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &name, &name_len, &value, &value_len) == FAILURE) {
+               RETURN_FALSE;
+       }
+
+       if (php_url_scanner_add_var(name, name_len, value, value_len, 1 TSRMLS_CC) == SUCCESS) {
+               RETURN_TRUE;
+       } else {
+               RETURN_FALSE;
+       }
+}
+
 /*
  * Local variables:
  * tab-width: 4