--- /dev/null
+#include "php.h"
+#include "php_ini.h"
+
+#include "apr_strings.h"
+#include "ap_config.h"
+#include "util_filter.h"
+#include "httpd.h"
+#include "http_config.h"
+#include "http_request.h"
+#include "http_core.h"
+#include "http_protocol.h"
+#include "http_log.h"
+#include "http_main.h"
+#include "util_script.h"
+#include "http_core.h"
+
+typedef struct {
+ HashTable config;
+} php_conf_rec;
+
+typedef struct {
+ char *value;
+ size_t value_len;
+ char status;
+} php_dir_entry;
+
+static const char *real_value_hnd(cmd_parms *cmd, void *dummy, const char *name, const char *value, int status)
+{
+ php_conf_rec *d = dummy;
+ php_dir_entry e;
+ php_dir_entry *pe;
+ size_t str_len;
+
+ fprintf(stderr, "Getting %s=%s for %p (%d)\n", name, value, dummy, zend_hash_num_elements(&d->config));
+ e.value = apr_pstrdup(cmd->pool, value);
+ e.value_len = strlen(value);
+ e.status = status;
+
+ str_len = strlen(name);
+
+ if (zend_hash_find(&d->config, name, str_len + 1, &pe) == SUCCESS) {
+ if (pe->status > status)
+ return NULL;
+ }
+
+ zend_hash_update(&d->config, name, strlen(name) + 1, &e, sizeof(e),
+ NULL);
+ return NULL;
+}
+
+static const char *php_apache_value_handler(cmd_parms *cmd, void *dummy, const char *name, const char *value)
+{
+ return real_value_hnd(cmd, dummy, name, value, PHP_INI_USER);
+}
+
+static const char *php_apache_admin_value_handler(cmd_parms *cmd, void *dummy, const char *name, const char *value)
+{
+ return real_value_hnd(cmd, dummy, name, value, PHP_INI_SYSTEM);
+}
+
+void *merge_php_config(apr_pool_t *p, void *base_conf, void *new_conf)
+{
+ php_conf_rec *d = base_conf, *e = new_conf;
+ php_dir_entry *pe;
+ php_dir_entry *data;
+ char *str;
+ ulong str_len;
+ ulong num_index;
+ char buf[256];
+
+ fprintf(stderr, "Merge dir (%p) (%p)\n", base_conf, new_conf);
+ for (zend_hash_internal_pointer_reset(&d->config);
+ zend_hash_get_current_key_ex(&d->config, &str, &str_len, &num_index, NULL) == HASH_KEY_IS_STRING;
+ zend_hash_move_forward(&d->config)) {
+ pe = NULL;
+ zend_hash_get_current_data(&d->config, &data);
+ if (zend_hash_find(&e->config, str, str_len, &pe) == SUCCESS) {
+ if (pe->status >= data->status) continue;
+ }
+ zend_hash_update(&e->config, str, str_len, data, sizeof(*data), NULL);
+ sprintf(buf, "ADDING/OVERWRITING %%%lds (%d vs. %d)\n", str_len, data->status, pe?pe->status:-1);
+ fprintf(stderr, buf, str);
+ }
+ return new_conf;
+}
+
+void apply_config(void *dummy)
+{
+ php_conf_rec *d = dummy;
+ char *str;
+ ulong str_len;
+ php_dir_entry *data;
+
+ for (zend_hash_internal_pointer_reset(&d->config);
+ zend_hash_get_current_key_ex(&d->config, &str, &str_len, NULL, NULL) == HASH_KEY_IS_STRING;
+ zend_hash_move_forward(&d->config)) {
+ zend_hash_get_current_data(&d->config, &data);
+ fprintf(stderr, "APPLYING (%s)(%s)\n", str, data->value);
+ if (php_alter_ini_entry(str, str_len, data->value, data->value_len + 1,
+ data->status, PHP_INI_STAGE_RUNTIME) == FAILURE)
+ fprintf(stderr, "..FAILED\n");
+ }
+}
+
+const command_rec dir_cmds[] =
+{
+ AP_INIT_TAKE2("php_value", php_apache_value_handler, NULL, OR_OPTIONS,
+ "PHP Value Modifier"),
+ AP_INIT_TAKE2("php_admin_value", php_apache_admin_value_handler, NULL, OR_OPTIONS,
+ "PHP Value Modifier"),
+ {NULL}
+};
+
+static apr_status_t destroy_php_config(void *data)
+{
+ php_conf_rec *d = data;
+
+ fprintf(stderr, "Destroying config %p\n", data);
+ zend_hash_destroy(&d->config);
+
+ return APR_SUCCESS;
+}
+
+void *create_php_config(apr_pool_t *p, char *dummy)
+{
+ php_conf_rec *newx =
+ (php_conf_rec *) apr_pcalloc(p, sizeof(*newx));
+
+ fprintf(stderr, "Creating new config (%p) for %s\n", newx, dummy);
+ zend_hash_init(&newx->config, 0, NULL, NULL, 1);
+ apr_register_cleanup(p, newx, destroy_php_config, NULL);
+ return (void *) newx;
+}
+
--- /dev/null
+#include <fcntl.h>
+
+#include "php.h"
+#include "php_main.h"
+#include "php_ini.h"
+#include "php_variables.h"
+#include "SAPI.h"
+
+#include "ext/standard/php_smart_str.h"
+
+#include "apr_strings.h"
+#include "ap_config.h"
+#include "util_filter.h"
+#include "httpd.h"
+#include "http_config.h"
+#include "http_request.h"
+#include "http_core.h"
+#include "http_protocol.h"
+#include "http_log.h"
+#include "http_main.h"
+#include "util_script.h"
+#include "http_core.h"
+
+#include "php_apache.h"
+
+static int
+php_apache_sapi_ub_write(const char *str, uint str_length)
+{
+ ap_bucket *b;
+ ap_bucket_brigade *bb;
+ php_struct *ctx;
+ SLS_FETCH();
+
+ ctx = SG(server_context);
+
+ bb = ap_brigade_create(ctx->f->r->pool);
+ b = ap_bucket_create_transient(str, str_length);
+ AP_BRIGADE_INSERT_TAIL(bb, b);
+ ap_pass_brigade(ctx->f->next, bb);
+
+ return str_length;
+}
+
+static int
+php_apache_sapi_header_handler(sapi_header_struct *sapi_header, sapi_headers_struct *sapi_headers SLS_DC)
+{
+ php_struct *ctx = SG(server_context);
+ char *val;
+
+ val = strchr(sapi_header->header, ':');
+
+ if (!val) return 0;
+
+ *val = '\0';
+
+ do {
+ val++;
+ } while (*val == ' ');
+
+ if (!strcasecmp(sapi_header->header, "content-type"))
+ ctx->f->r->content_type = apr_pstrdup(ctx->f->r->pool, val);
+ else if (sapi_header->replace)
+ apr_table_set(ctx->f->r->headers_out, sapi_header->header, val);
+ else
+ apr_table_add(ctx->f->r->headers_out, sapi_header->header, val);
+
+ sapi_free_header(sapi_header);
+
+ return 0;
+}
+
+static int
+php_apache_sapi_send_headers(sapi_headers_struct *sapi_headers SLS_DC)
+{
+ php_struct *ctx = SG(server_context);
+
+ ctx->f->r->status = SG(sapi_headers).http_response_code;
+
+ return SAPI_HEADER_SENT_SUCCESSFULLY;
+}
+
+static int
+php_apache_sapi_read_post(char *buf, uint count_bytes SLS_DC)
+{
+
+ return 0;
+}
+
+static char *
+php_apache_sapi_read_cookies(SLS_D)
+{
+
+ return 0;
+}
+
+static void
+php_apache_sapi_register_variables(zval *track_vars_array ELS_DC SLS_DC PLS_DC)
+{
+ php_struct *ctx = SG(server_context);
+ apr_array_header_t *arr = apr_table_elts(ctx->f->r->subprocess_env);
+ apr_table_entry_t *elts = (apr_table_entry_t *) arr->elts;
+ int i;
+ char *val;
+
+ for (i = 0; i < arr->nelts; i++) {
+ if (!(val = elts[i].val))
+ val = empty_string;
+
+ php_register_variable(elts[i].key, val, track_vars_array ELS_CC PLS_CC);
+ }
+
+ php_register_variable("PHP_SELF", ctx->f->r->uri, track_vars_array ELS_CC PLS_CC);
+}
+
+static void
+php_apache_sapi_flush(void *server_context)
+{
+ php_struct *ctx = server_context;
+ return;
+
+ /* This does not work yet. Apparently, the default handler
+ interpretes bucket flush as EOS */
+
+ ap_rflush(ctx->f->r);
+}
+
+static sapi_module_struct sapi_module = {
+ "apache2filter",
+ "Apache 2.0 Filter",
+
+ php_module_startup, /* startup */
+ php_module_shutdown_wrapper, /* shutdown */
+
+ NULL, /* activate */
+ NULL, /* deactivate */
+
+ php_apache_sapi_ub_write, /* unbuffered write */
+ php_apache_sapi_flush, /* flush */
+ NULL, /* get uid */
+ NULL, /* getenv */
+
+ php_error, /* error handler */
+
+ php_apache_sapi_header_handler, /* header handler */
+ php_apache_sapi_send_headers, /* send headers handler */
+ NULL, /* send header handler */
+
+ php_apache_sapi_read_post, /* read POST data */
+ php_apache_sapi_read_cookies, /* read Cookies */
+
+ php_apache_sapi_register_variables,
+ NULL, /* Log message */
+
+ NULL, /* Block interruptions */
+ NULL, /* Unblock interruptions */
+
+ STANDARD_SAPI_MODULE_PROPERTIES
+};
+
+
+module MODULE_VAR_EXPORT php4_module;
+
+static int php_filter(ap_filter_t *f, ap_bucket_brigade *bb)
+{
+ php_struct *ctx = f->ctx;
+ ap_bucket *b;
+ apr_status_t rv;
+ const char *str;
+ apr_ssize_t n;
+ void *conf = ap_get_module_config(f->r->per_dir_config,
+ &php4_module);
+ SLS_FETCH();
+
+ if (ctx == NULL) {
+ SG(server_context) = f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
+ ctx->bb = ap_brigade_create(f->c->pool);
+ }
+
+ ctx->f = f;
+
+ if (ctx->state == 0) {
+ char *content_type;
+ CLS_FETCH();
+ ELS_FETCH();
+ PLS_FETCH();
+ SLS_FETCH();
+
+ apply_config(conf);
+ php_request_startup(CLS_C ELS_CC PLS_CC SLS_CC);
+
+ ctx->state++;
+
+ PG(during_request_startup) = 0;
+ SG(sapi_headers).http_response_code = 200;
+ content_type = sapi_get_default_content_type(SLS_C);
+ f->r->content_type = apr_pstrdup(f->r->pool,
+ content_type);
+ efree(content_type);
+ apr_table_set(f->r->headers_in, "Connection", "close");
+ apr_table_unset(f->r->headers_out, "Content-Length");
+ }
+
+ /* moves data from bb to ctx->bb */
+ ap_save_brigade(f, &ctx->bb, &bb);
+
+ if (ctx->state == 1 && AP_BUCKET_IS_EOS(AP_BRIGADE_LAST(ctx->bb))) {
+ int fd;
+ zend_file_handle zfd;
+ smart_str content = {0};
+ ap_bucket *eos;
+ CLS_FETCH();
+ ELS_FETCH();
+ PLS_FETCH();
+
+ ctx->state = 2;
+
+ AP_BRIGADE_FOREACH(b, ctx->bb) {
+ rv = ap_bucket_read(b, &str, &n, 1);
+ if (rv == APR_SUCCESS && n > 0)
+ smart_str_appendl(&content, str, n);
+ }
+ if (!content.c) goto fucked;
+ smart_str_0(&content);
+
+#if 1
+#define FFFF "/tmp/really_silly"
+ fd = open(FFFF, O_WRONLY|O_TRUNC|O_CREAT, 0600);
+
+ write(fd, content.c, content.len);
+
+ close(fd);
+
+ zfd.type = ZEND_HANDLE_FILENAME;
+ zfd.filename = FFFF;
+ zfd.free_filename = 0;
+ zfd.opened_path = NULL;
+
+ php_execute_script(&zfd CLS_CC ELS_CC PLS_CC);
+#else
+ CG(in_compilation) = 1;
+ zend_eval_string(content, NULL, "foo" CLS_CC ELS_CC);
+#endif
+
+ smart_str_free(&content);
+fucked:
+ php_request_shutdown(NULL);
+
+ eos = ap_bucket_create_eos();
+ AP_BRIGADE_INSERT_TAIL(bb, eos);
+ ap_pass_brigade(f->next, bb);
+ }
+
+ return APR_SUCCESS;
+}
+
+static apr_status_t
+php_apache_server_shutdown(void *tmp)
+{
+ sapi_module.shutdown(&sapi_module);
+ sapi_shutdown();
+ tsrm_shutdown();
+ return APR_SUCCESS;
+}
+
+static void
+php_apache_server_startup(apr_pool_t *pchild, server_rec *s)
+{
+ tsrm_startup(1, 1, 0);
+ sapi_startup(&sapi_module);
+ sapi_module.startup(&sapi_module);
+ apr_register_cleanup(pchild, NULL, php_apache_server_shutdown, NULL);
+}
+
+static void php_register_hook(void)
+{
+ ap_hook_child_init(php_apache_server_startup, NULL, NULL, AP_HOOK_MIDDLE);
+ ap_register_output_filter("PHP", php_filter, AP_FTYPE_CONTENT);
+}
+
+module MODULE_VAR_EXPORT php4_module = {
+ STANDARD20_MODULE_STUFF,
+ create_php_config, /* create per-directory config structure */
+ merge_php_config, /* merge per-directory config structures */
+ NULL, /* create per-server config structure */
+ NULL, /* merge per-server config structures */
+ dir_cmds, /* command apr_table_t */
+ NULL, /* handlers */
+ php_register_hook /* register hooks */
+};