diff options
Diffstat (limited to 'sapi/apache2filter')
-rw-r--r-- | sapi/apache2filter/CREDITS | 2 | ||||
-rw-r--r-- | sapi/apache2filter/EXPERIMENTAL | 5 | ||||
-rw-r--r-- | sapi/apache2filter/README | 71 | ||||
-rw-r--r-- | sapi/apache2filter/apache_config.c | 218 | ||||
-rw-r--r-- | sapi/apache2filter/config.m4 | 139 | ||||
-rwxr-xr-x | sapi/apache2filter/config.w32 | 39 | ||||
-rw-r--r-- | sapi/apache2filter/php.sym | 1 | ||||
-rw-r--r-- | sapi/apache2filter/php_apache.h | 81 | ||||
-rw-r--r-- | sapi/apache2filter/php_functions.c | 425 | ||||
-rw-r--r-- | sapi/apache2filter/sapi_apache2.c | 764 |
10 files changed, 1745 insertions, 0 deletions
diff --git a/sapi/apache2filter/CREDITS b/sapi/apache2filter/CREDITS new file mode 100644 index 0000000..c298a9b --- /dev/null +++ b/sapi/apache2filter/CREDITS @@ -0,0 +1,2 @@ +Apache 2.0 Filter +Sascha Schumann, Aaron Bannert diff --git a/sapi/apache2filter/EXPERIMENTAL b/sapi/apache2filter/EXPERIMENTAL new file mode 100644 index 0000000..293159a --- /dev/null +++ b/sapi/apache2filter/EXPERIMENTAL @@ -0,0 +1,5 @@ +this module is experimental, +its functions may change their names +or move to extension all together +so do not rely to much on them +you have been warned! diff --git a/sapi/apache2filter/README b/sapi/apache2filter/README new file mode 100644 index 0000000..3054e20 --- /dev/null +++ b/sapi/apache2filter/README @@ -0,0 +1,71 @@ +WHAT IS THIS? + + This module exploits the layered I/O support in Apache 2.0. + +HOW DOES IT WORK? + + In Apache 2.0, you have handlers which generate content (like + reading a script from disk). The content goes then through + a chain of filters. PHP can be such a filter, so that it processes + your script and hands the output to the next filter (which will + usually cause a write to the network). + +DOES IT WORK? + + It is experimental as interfaces in Apache 2.0 might change in the + future. + +HOW TO INSTALL + + This SAPI module is known to work with Apache 2.0.40. + + $ cd apache-2.x + $ cd src + $ ./configure --enable-so + $ make install + + For testing purposes, you might want to use --with-mpm=prefork. + (Albeit PHP also works with threaded MPMs.) + + Configure PHP 4: + + $ cd php-4.x + $ ./configure --with-apxs2=/path/to/apache-2.0/bin/apxs + $ make install + + At the end of conf/httpd.conf, add: + + AddType application/x-httpd-php .php + + If you would like to enable source code highlighting functionality add: + + AddType application/x-httpd-php-source .phps + + That's it. Now start bin/httpd. + +HOW TO CONFIGURE + + The Apache 2.0 PHP module supports a new configuration directive that + allows an admin to override the php.ini search path. For example, + place your php.ini file in Apache's ServerRoot/conf directory and + add this to your httpd.conf file: + + PHPINIDir "conf" + +DEBUGGING APACHE AND PHP + + To debug Apache, we recommened: + + 1. Use the Prefork MPM (Apache 1.3-like process model) by + configuring Apache with '--with-mpm=prefork'. + 2. Start httpd using -DONE_PROCESS (e.g. (gdb) r -DONE_PROCESS). + + If you want to debug a part of the PHP startup procedure, set a + breakpoint on 'load_module'. Step through it until apr_dso_load() is + done. Then you can set a breakpoint on any PHP-related symbol. + +TODO + + PHP functions like apache_sub_req (see php_functions.c) + Protocol handlers + Passing script data to engine without temporary file diff --git a/sapi/apache2filter/apache_config.c b/sapi/apache2filter/apache_config.c new file mode 100644 index 0000000..333c7b8 --- /dev/null +++ b/sapi/apache2filter/apache_config.c @@ -0,0 +1,218 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2013 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Sascha Schumann <sascha@schumann.cx> | + +----------------------------------------------------------------------+ + */ + +/* $Id$ */ + +#define ZEND_INCLUDE_FULL_WINDOWS_HEADERS + +#include "php.h" +#include "php_ini.h" +#include "php_apache.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" + +#ifdef PHP_AP_DEBUG +#define phpapdebug(a) fprintf a +#else +#define phpapdebug(a) +#endif + +typedef struct { + HashTable config; +} php_conf_rec; + +typedef struct { + char *value; + size_t value_len; + char status; + char htaccess; +} 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; + + phpapdebug((stderr, "Getting %s=%s for %p (%d)\n", name, value, dummy, zend_hash_num_elements(&d->config))); + + if (!strncasecmp(value, "none", sizeof("none"))) { + value = ""; + } + + e.value = apr_pstrdup(cmd->pool, value); + e.value_len = strlen(value); + e.status = status; + e.htaccess = ((cmd->override & (RSRC_CONF|ACCESS_CONF)) == 0); + + zend_hash_update(&d->config, (char *) 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_PERDIR); +} + +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); +} + +static const char *real_flag_hnd(cmd_parms *cmd, void *dummy, const char *arg1, const char *arg2, int status) +{ + char bool_val[2]; + + if (!strcasecmp(arg2, "On") || (arg2[0] == '1' && arg2[1] == '\0')) { + bool_val[0] = '1'; + } else { + bool_val[0] = '0'; + } + bool_val[1] = 0; + + return real_value_hnd(cmd, dummy, arg1, bool_val, status); +} + +static const char *php_apache_flag_handler(cmd_parms *cmd, void *dummy, const char *name, const char *value) +{ + return real_flag_hnd(cmd, dummy, name, value, PHP_INI_PERDIR); +} + +static const char *php_apache_admin_flag_handler(cmd_parms *cmd, void *dummy, const char *name, const char *value) +{ + return real_flag_hnd(cmd, dummy, name, value, PHP_INI_SYSTEM); +} + +static const char *php_apache_phpini_set(cmd_parms *cmd, void *mconfig, const char *arg) +{ + if (apache2_php_ini_path_override) { + return "Only first PHPINIDir directive honored per configuration tree - subsequent ones ignored"; + } + apache2_php_ini_path_override = ap_server_root_relative(cmd->pool, arg); + return NULL; +} + + +void *merge_php_config(apr_pool_t *p, void *base_conf, void *new_conf) +{ + php_conf_rec *d = base_conf, *e = new_conf, *n = NULL; + php_dir_entry *pe; + php_dir_entry *data; + char *str; + uint str_len; + ulong num_index; + + n = create_php_config(p, "merge_php_config"); + zend_hash_copy(&n->config, &e->config, NULL, NULL, sizeof(php_dir_entry)); + + phpapdebug((stderr, "Merge dir (%p)+(%p)=(%p)\n", base_conf, new_conf, n)); + for (zend_hash_internal_pointer_reset(&d->config); + zend_hash_get_current_key_ex(&d->config, &str, &str_len, + &num_index, 0, NULL) == HASH_KEY_IS_STRING; + zend_hash_move_forward(&d->config)) { + pe = NULL; + zend_hash_get_current_data(&d->config, (void **) &data); + if (zend_hash_find(&n->config, str, str_len, (void **) &pe) == SUCCESS) { + if (pe->status >= data->status) continue; + } + zend_hash_update(&n->config, str, str_len, data, sizeof(*data), NULL); + phpapdebug((stderr, "ADDING/OVERWRITING %s (%d vs. %d)\n", str, data->status, pe?pe->status:-1)); + } + + return n; +} + +char *get_php_config(void *conf, char *name, size_t name_len) +{ + php_conf_rec *d = conf; + php_dir_entry *pe; + + if (zend_hash_find(&d->config, name, name_len, (void **) &pe) == SUCCESS) { + return pe->value; + } + + return ""; +} + +void apply_config(void *dummy) +{ + php_conf_rec *d = dummy; + char *str; + uint 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, 0, + NULL) == HASH_KEY_IS_STRING; + zend_hash_move_forward(&d->config)) { + zend_hash_get_current_data(&d->config, (void **) &data); + phpapdebug((stderr, "APPLYING (%s)(%s)\n", str, data->value)); + if (zend_alter_ini_entry(str, str_len, data->value, data->value_len, data->status, data->htaccess?PHP_INI_STAGE_HTACCESS:PHP_INI_STAGE_ACTIVATE) == FAILURE) { + phpapdebug((stderr, "..FAILED\n")); + } + } +} + +const command_rec php_dir_cmds[] = +{ + AP_INIT_TAKE2("php_value", php_apache_value_handler, NULL, OR_OPTIONS, "PHP Value Modifier"), + AP_INIT_TAKE2("php_flag", php_apache_flag_handler, NULL, OR_OPTIONS, "PHP Flag Modifier"), + AP_INIT_TAKE2("php_admin_value", php_apache_admin_value_handler, NULL, ACCESS_CONF|RSRC_CONF, "PHP Value Modifier (Admin)"), + AP_INIT_TAKE2("php_admin_flag", php_apache_admin_flag_handler, NULL, ACCESS_CONF|RSRC_CONF, "PHP Flag Modifier (Admin)"), + AP_INIT_TAKE1("PHPINIDir", php_apache_phpini_set, NULL, RSRC_CONF, "Directory containing the php.ini file"), + {NULL} +}; + +static apr_status_t destroy_php_config(void *data) +{ + php_conf_rec *d = data; + + phpapdebug((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)); + + phpapdebug((stderr, "Creating new config (%p) for %s\n", newx, dummy)); + zend_hash_init(&newx->config, 0, NULL, NULL, 1); + apr_pool_cleanup_register(p, newx, destroy_php_config, apr_pool_cleanup_null); + return (void *) newx; +} + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ diff --git a/sapi/apache2filter/config.m4 b/sapi/apache2filter/config.m4 new file mode 100644 index 0000000..c49488d --- /dev/null +++ b/sapi/apache2filter/config.m4 @@ -0,0 +1,139 @@ +dnl +dnl $Id$ +dnl + +PHP_ARG_WITH(apxs2filter,, +[ --with-apxs2filter[=FILE] + EXPERIMENTAL: Build shared Apache 2.0 Filter module. FILE is the optional + pathname to the Apache apxs tool [apxs]], no, no) + +AC_MSG_CHECKING([for Apache 2.0 filter-module support via DSO through APXS]) + +if test "$PHP_APXS2FILTER" != "no"; then + if test "$PHP_APXS2FILTER" = "yes"; then + APXS=apxs + $APXS -q CFLAGS >/dev/null 2>&1 + if test "$?" != "0" && test -x /usr/sbin/apxs; then + APXS=/usr/sbin/apxs + fi + else + PHP_EXPAND_PATH($PHP_APXS2FILTER, APXS) + fi + + $APXS -q CFLAGS >/dev/null 2>&1 + if test "$?" != "0"; then + AC_MSG_RESULT() + AC_MSG_RESULT() + AC_MSG_RESULT([Sorry, I cannot run apxs. Possible reasons follow:]) + AC_MSG_RESULT() + AC_MSG_RESULT([1. Perl is not installed]) + AC_MSG_RESULT([2. apxs was not found. Try to pass the path using --with-apxs2filter=/path/to/apxs]) + AC_MSG_RESULT([3. Apache was not built using --enable-so (the apxs usage page is displayed)]) + AC_MSG_RESULT() + AC_MSG_RESULT([The output of $APXS follows:]) + $APXS -q CFLAGS + AC_MSG_ERROR([Aborting]) + fi + + APXS_INCLUDEDIR=`$APXS -q INCLUDEDIR` + APXS_BINDIR=`$APXS -q BINDIR` + APXS_HTTPD=`$APXS -q SBINDIR`/`$APXS -q TARGET` + APXS_CFLAGS=`$APXS -q CFLAGS` + APU_BINDIR=`$APXS -q APU_BINDIR` + APR_BINDIR=`$APXS -q APR_BINDIR` + + # Pick up ap[ru]-N-config if using httpd >=2.1 + APR_CONFIG=`$APXS -q APR_CONFIG 2>/dev/null || + echo $APR_BINDIR/apr-config` + APU_CONFIG=`$APXS -q APU_CONFIG 2>/dev/null || + echo $APU_BINDIR/apu-config` + + APR_CFLAGS="`$APR_CONFIG --cppflags --includes`" + APU_CFLAGS="`$APU_CONFIG --includes`" + + for flag in $APXS_CFLAGS; do + case $flag in + -D*) APACHE_CPPFLAGS="$APACHE_CPPFLAGS $flag";; + esac + done + + APACHE_CFLAGS="$APACHE_CPPFLAGS -I$APXS_INCLUDEDIR $APR_CFLAGS $APU_CFLAGS" + + # Test that we're trying to configure with apache 2.x + PHP_AP_EXTRACT_VERSION($APXS_HTTPD) + if test "$APACHE_VERSION" -le 2000000; then + AC_MSG_ERROR([You have enabled Apache 2 support while your server is Apache 1.3. Please use the appropiate switch --with-apxs (without the 2)]) + elif test "$APACHE_VERSION" -lt 2000040; then + AC_MSG_ERROR([Please note that Apache version >= 2.0.40 is required]) + fi + + APXS_LIBEXECDIR='$(INSTALL_ROOT)'`$APXS -q LIBEXECDIR` + if test -z `$APXS -q SYSCONFDIR`; then + INSTALL_IT="\$(mkinstalldirs) '$APXS_LIBEXECDIR' && \ + $APXS -S LIBEXECDIR='$APXS_LIBEXECDIR' \ + -i -n php5" + else + APXS_SYSCONFDIR='$(INSTALL_ROOT)'`$APXS -q SYSCONFDIR` + INSTALL_IT="\$(mkinstalldirs) '$APXS_LIBEXECDIR' && \ + \$(mkinstalldirs) '$APXS_SYSCONFDIR' && \ + $APXS -S LIBEXECDIR='$APXS_LIBEXECDIR' \ + -S SYSCONFDIR='$APXS_SYSCONFDIR' \ + -i -a -n php5" + fi + + case $host_alias in + *aix*) + EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,-brtl -Wl,-bI:$APXS_LIBEXECDIR/httpd.exp" + PHP_SELECT_SAPI(apache2filter, shared, sapi_apache2.c apache_config.c php_functions.c, $APACHE_CFLAGS) + INSTALL_IT="$INSTALL_IT $SAPI_LIBTOOL" + ;; + *darwin*) + dnl When using bundles on Darwin, we must resolve all symbols. However, + dnl the linker does not recursively look at the bundle loader and + dnl pull in its dependencies. Therefore, we must pull in the APR + dnl and APR-util libraries. + if test -x "$APR_CONFIG"; then + MH_BUNDLE_FLAGS="`$APR_CONFIG --ldflags --link-ld --libs`" + fi + if test -x "$APU_CONFIG"; then + MH_BUNDLE_FLAGS="`$APU_CONFIG --ldflags --link-ld --libs` $MH_BUNDLE_FLAGS" + fi + MH_BUNDLE_FLAGS="-bundle -bundle_loader $APXS_HTTPD $MH_BUNDLE_FLAGS" + PHP_SUBST(MH_BUNDLE_FLAGS) + PHP_SELECT_SAPI(apache2filter, bundle, sapi_apache2.c apache_config.c php_functions.c, $APACHE_CFLAGS) + SAPI_SHARED=libs/libphp5.so + INSTALL_IT="$INSTALL_IT $SAPI_SHARED" + ;; + *beos*) + if test -f _APP_; then `rm _APP_`; fi + `ln -s $APXS_BINDIR/httpd _APP_` + EXTRA_LIBS="$EXTRA_LIBS _APP_" + PHP_SELECT_SAPI(apache2filter, shared, sapi_apache2.c apache_config.c php_functions.c, $APACHE_CFLAGS) + INSTALL_IT="$INSTALL_IT $SAPI_LIBTOOL" + ;; + *) + PHP_SELECT_SAPI(apache2filter, shared, sapi_apache2.c apache_config.c php_functions.c, $APACHE_CFLAGS) + INSTALL_IT="$INSTALL_IT $SAPI_LIBTOOL" + ;; + esac + + if test "$APACHE_VERSION" -lt 2004001; then + APXS_MPM=`$APXS -q MPM_NAME` + if test "$APXS_MPM" != "prefork" && test "$APXS_MPM" != "peruser" && test "$APXS_MPM" != "itk"; then + PHP_BUILD_THREAD_SAFE + fi + else + APACHE_THREADED_MPM=`$APXS_HTTPD -V | grep 'threaded:.*yes'` + if test -n "$APACHE_THREADED_MPM"; then + PHP_BUILD_THREAD_SAFE + fi + fi + AC_MSG_RESULT(yes) + PHP_SUBST(APXS) +else + AC_MSG_RESULT(no) +fi + +dnl ## Local Variables: +dnl ## tab-width: 4 +dnl ## End: diff --git a/sapi/apache2filter/config.w32 b/sapi/apache2filter/config.w32 new file mode 100755 index 0000000..361d031 --- /dev/null +++ b/sapi/apache2filter/config.w32 @@ -0,0 +1,39 @@ +// vim:ft=javascript +// $Id$ + +ARG_ENABLE('apache2filter', 'Build Apache 2.x filter', 'no'); + +if (PHP_APACHE2FILTER != "no") { + if (PHP_ZTS == "no") { + WARNING("Apache2 module requires an --enable-zts build of PHP on windows"); + } else if (CHECK_HEADER_ADD_INCLUDE("httpd.h", "CFLAGS_APACHE2FILTER", PHP_PHP_BUILD + "\\include\\apache2") && + CHECK_LIB("libhttpd.lib", "apache2filter", PHP_PHP_BUILD + "\\lib\\apache2") && + CHECK_LIB("libapr.lib", "apache2filter", PHP_PHP_BUILD + "\\lib\\apache2") && + CHECK_LIB("libaprutil.lib", "apache2filter", PHP_PHP_BUILD + "\\lib\\apache2") + ) { + SAPI('apache2filter', 'sapi_apache2.c apache_config.c php_functions.c', + 'php' + PHP_VERSION + 'apache2_filter.dll', + '/D PHP_APACHE2_EXPORTS /I win32'); + } else { + WARNING("Could not find apache2 filter libraries/headers"); + } +} + +ARG_ENABLE('apache2-2filter', 'Build Apache 2.2.x filter', 'no'); + +if (PHP_APACHE2_2FILTER != "no") { + if (PHP_ZTS == "no") { + WARNING("Apache2 module requires an --enable-zts build of PHP on windows"); + } else if (CHECK_HEADER_ADD_INCLUDE("httpd.h", "CFLAGS_APACHE2_2FILTER", PHP_PHP_BUILD + "\\include\\apache2_2") && + CHECK_LIB("libhttpd.lib", "apache2_2filter", PHP_PHP_BUILD + "\\lib\\apache2_2") && + CHECK_LIB("libapr-1.lib", "apache2_2filter", PHP_PHP_BUILD + "\\lib\\apache2_2") && + CHECK_LIB("libaprutil-1.lib", "apache2_2filter", PHP_PHP_BUILD + "\\lib\\apache2_2") + ) { + SAPI('apache2_2filter', 'sapi_apache2.c apache_config.c php_functions.c', + 'php' + PHP_VERSION + 'apache2_2_filter.dll', + '/D PHP_APACHE2_EXPORTS /I win32', + 'sapi\\apache2_2filter'); + } else { + WARNING("Could not find apache2.2 filter libraries/headers"); + } +} diff --git a/sapi/apache2filter/php.sym b/sapi/apache2filter/php.sym new file mode 100644 index 0000000..9ad0f0a --- /dev/null +++ b/sapi/apache2filter/php.sym @@ -0,0 +1 @@ +php5_module diff --git a/sapi/apache2filter/php_apache.h b/sapi/apache2filter/php_apache.h new file mode 100644 index 0000000..4ee3c0a --- /dev/null +++ b/sapi/apache2filter/php_apache.h @@ -0,0 +1,81 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2013 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Sascha Schumann <sascha@schumann.cx> | + +----------------------------------------------------------------------+ + */ + +/* $Id$ */ + +#ifndef PHP_APACHE_H +#define PHP_APACHE_H + +#include "httpd.h" +#include "http_config.h" +#include "http_core.h" + +/* Declare this so we can get to it from outside the sapi_apache2.c file */ +extern module AP_MODULE_DECLARE_DATA php5_module; + +/* A way to specify the location of the php.ini dir in an apache directive */ +extern char *apache2_php_ini_path_override; + +/* The server_context used by PHP */ +typedef struct php_struct { + int state; + request_rec *r; + ap_filter_t *f; /* downstream output filters after the PHP filter. */ + /* Length of post_data buffer */ + int post_len; + /* Index for reading from buffer */ + int post_idx; + /* stat structure of the current file */ + struct stat finfo; + /* Buffer for request body filter */ + char *post_data; + /* Whether or not we've processed PHP in the output filters yet. */ + int request_processed; +} php_struct; + +typedef struct _php_apr_bucket_brigade { + apr_bucket_brigade *bb; +} php_apr_bucket_brigade; + +void *merge_php_config(apr_pool_t *p, void *base_conf, void *new_conf); +void *create_php_config(apr_pool_t *p, char *dummy); +char *get_php_config(void *conf, char *name, size_t name_len); +void apply_config(void *); +extern const command_rec php_dir_cmds[]; + +static size_t php_apache_read_stream(void *, char *, size_t TSRMLS_DC); +static size_t php_apache_fsizer_stream(void * TSRMLS_DC); + +#define APR_ARRAY_FOREACH_OPEN(arr, key, val) \ +{ \ + apr_table_entry_t *elts; \ + int i; \ + elts = (apr_table_entry_t *) arr->elts; \ + for (i = 0; i < arr->nelts; i++) { \ + key = elts[i].key; \ + val = elts[i].val; + +#define APR_ARRAY_FOREACH_CLOSE() }} + +/* fix for gcc4 visibility patch */ +#ifndef PHP_WIN32 +# undef AP_MODULE_DECLARE_DATA +# define AP_MODULE_DECLARE_DATA PHPAPI +#endif + +#endif /* PHP_APACHE_H */ diff --git a/sapi/apache2filter/php_functions.c b/sapi/apache2filter/php_functions.c new file mode 100644 index 0000000..e96ceda --- /dev/null +++ b/sapi/apache2filter/php_functions.c @@ -0,0 +1,425 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2013 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Sascha Schumann <sascha@schumann.cx> | + +----------------------------------------------------------------------+ + */ + +/* $Id$ */ + + +#define ZEND_INCLUDE_FULL_WINDOWS_HEADERS + +#include "php.h" +#include "ext/standard/php_smart_str.h" +#include "ext/standard/info.h" +#include "SAPI.h" + +#define CORE_PRIVATE +#include "apr_strings.h" +#include "apr_time.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 request_rec *php_apache_lookup_uri(char *filename TSRMLS_DC) +{ + php_struct *ctx; + + if (!filename) { + return NULL; + } + + ctx = SG(server_context); + return ap_sub_req_lookup_uri(filename, ctx->f->r, ctx->f->next); +} + +/* {{{ proto bool virtual(string uri) + Perform an apache sub-request */ +PHP_FUNCTION(virtual) +{ + char *filename; + int filename_len; + request_rec *rr; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p", &filename, &filename_len) == FAILURE) { + return; + } + + if (!(rr = php_apache_lookup_uri(filename TSRMLS_CC))) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to include '%s' - URI lookup failed", filename); + RETURN_FALSE; + } + + if (rr->status == HTTP_OK) { + if (ap_run_sub_req(rr)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to include '%s' - request execution failed", filename); + ap_destroy_sub_req(rr); + RETURN_FALSE; + } + ap_destroy_sub_req(rr); + RETURN_TRUE; + } + + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to include '%s' - error finding URI", filename); + ap_destroy_sub_req(rr); + RETURN_FALSE; +} +/* }}} */ + +#define ADD_LONG(name) \ + add_property_long(return_value, #name, rr->name) +#define ADD_TIME(name) \ + add_property_long(return_value, #name, apr_time_sec(rr->name)); +#define ADD_STRING(name) \ + if (rr->name) add_property_string(return_value, #name, (char *) rr->name, 1) + +PHP_FUNCTION(apache_lookup_uri) +{ + request_rec *rr; + char *filename; + int filename_len; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p", &filename, &filename_len) == FAILURE) { + return; + } + + if (!(rr = php_apache_lookup_uri(filename TSRMLS_CC))) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to include '%s' - URI lookup failed", filename); + RETURN_FALSE; + } + + if (rr->status == HTTP_OK) { + object_init(return_value); + + ADD_LONG(status); + ADD_STRING(the_request); + ADD_STRING(status_line); + ADD_STRING(method); + ADD_TIME(mtime); + ADD_LONG(clength); +#if MODULE_MAGIC_NUMBER < 20020506 + ADD_STRING(boundary); +#endif + ADD_STRING(range); + ADD_LONG(chunked); + ADD_STRING(content_type); + ADD_STRING(handler); + ADD_LONG(no_cache); + ADD_LONG(no_local_copy); + ADD_STRING(unparsed_uri); + ADD_STRING(uri); + ADD_STRING(filename); + ADD_STRING(path_info); + ADD_STRING(args); + ADD_LONG(allowed); + ADD_LONG(sent_bodyct); + ADD_LONG(bytes_sent); + ADD_LONG(mtime); + ADD_TIME(request_time); + + ap_destroy_sub_req(rr); + return; + } + + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to include '%s' - error finding URI", filename); + ap_destroy_sub_req(rr); + RETURN_FALSE; +} + +/* {{{ proto array getallheaders(void) + Fetch all HTTP request headers */ +PHP_FUNCTION(apache_request_headers) +{ + php_struct *ctx; + const apr_array_header_t *arr; + char *key, *val; + + array_init(return_value); + + ctx = SG(server_context); + arr = apr_table_elts(ctx->f->r->headers_in); + + APR_ARRAY_FOREACH_OPEN(arr, key, val) + if (!val) val = ""; + add_assoc_string(return_value, key, val, 1); + APR_ARRAY_FOREACH_CLOSE() +} +/* }}} */ + +/* {{{ proto array apache_response_headers(void) + Fetch all HTTP response headers */ +PHP_FUNCTION(apache_response_headers) +{ + php_struct *ctx; + const apr_array_header_t *arr; + char *key, *val; + + array_init(return_value); + + ctx = SG(server_context); + arr = apr_table_elts(ctx->f->r->headers_out); + + APR_ARRAY_FOREACH_OPEN(arr, key, val) + if (!val) val = ""; + add_assoc_string(return_value, key, val, 1); + APR_ARRAY_FOREACH_CLOSE() +} +/* }}} */ + +/* {{{ proto string apache_note(string note_name [, string note_value]) + Get and set Apache request notes */ +PHP_FUNCTION(apache_note) +{ + php_struct *ctx; + char *note_name, *note_val = NULL; + int note_name_len, note_val_len; + char *old_note_val=NULL; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", ¬e_name, ¬e_name_len, ¬e_val, ¬e_val_len) == FAILURE) { + return; + } + + ctx = SG(server_context); + + old_note_val = (char *) apr_table_get(ctx->r->notes, note_name); + + if (note_val) { + apr_table_set(ctx->r->notes, note_name, note_val); + } + + if (old_note_val) { + RETURN_STRING(old_note_val, 1); + } + + RETURN_FALSE; +} +/* }}} */ + + +/* {{{ proto bool apache_setenv(string variable, string value [, bool walk_to_top]) + Set an Apache subprocess_env variable */ +PHP_FUNCTION(apache_setenv) +{ + php_struct *ctx; + char *variable=NULL, *string_val=NULL; + int variable_len, string_val_len; + zend_bool walk_to_top = 0; + int arg_count = ZEND_NUM_ARGS(); + + if (zend_parse_parameters(arg_count TSRMLS_CC, "ss|b", &variable, &variable_len, &string_val, &string_val_len, &walk_to_top) == FAILURE) { + return; + } + + ctx = SG(server_context); + + if (arg_count == 3 && walk_to_top) { + while(ctx->f->r->prev) { + ctx->f->r = ctx->f->r->prev; + } + } + + apr_table_set(ctx->r->subprocess_env, variable, string_val); + + RETURN_TRUE; +} +/* }}} */ + +/* {{{ proto bool apache_getenv(string variable [, bool walk_to_top]) + Get an Apache subprocess_env variable */ +PHP_FUNCTION(apache_getenv) +{ + php_struct *ctx; + char *variable=NULL; + int variable_len; + zend_bool walk_to_top = 0; + int arg_count = ZEND_NUM_ARGS(); + char *env_val=NULL; + + if (zend_parse_parameters(arg_count TSRMLS_CC, "s|b", &variable, &variable_len, &walk_to_top) == FAILURE) { + return; + } + + ctx = SG(server_context); + + if (arg_count == 2 && walk_to_top) { + while(ctx->f->r->prev) { + ctx->f->r = ctx->f->r->prev; + } + } + + env_val = (char*) apr_table_get(ctx->r->subprocess_env, variable); + if (env_val != NULL) { + RETURN_STRING(env_val, 1); + } + + RETURN_FALSE; +} +/* }}} */ + +static char *php_apache_get_version() +{ +#if MODULE_MAGIC_NUMBER_MAJOR >= 20060905 + return (char *) ap_get_server_banner(); +#else + return (char *) ap_get_server_version(); +#endif +} + +/* {{{ proto string apache_get_version(void) + Fetch Apache version */ +PHP_FUNCTION(apache_get_version) +{ + char *apv = php_apache_get_version(); + + if (apv && *apv) { + RETURN_STRING(apv, 1); + } else { + RETURN_FALSE; + } +} +/* }}} */ + +/* {{{ proto array apache_get_modules(void) + Get a list of loaded Apache modules */ +PHP_FUNCTION(apache_get_modules) +{ + int n; + char *p; + + array_init(return_value); + + for (n = 0; ap_loaded_modules[n]; ++n) { + char *s = (char *) ap_loaded_modules[n]->name; + if ((p = strchr(s, '.'))) { + add_next_index_stringl(return_value, s, (p - s), 1); + } else { + add_next_index_string(return_value, s, 1); + } + } +} +/* }}} */ + +PHP_MINFO_FUNCTION(apache) +{ + char *apv = php_apache_get_version(); + smart_str tmp1 = {0}; + int n; + char *p; + + for (n = 0; ap_loaded_modules[n]; ++n) { + char *s = (char *) ap_loaded_modules[n]->name; + if ((p = strchr(s, '.'))) { + smart_str_appendl(&tmp1, s, (p - s)); + } else { + smart_str_appends(&tmp1, s); + } + smart_str_appendc(&tmp1, ' '); + } + if ((tmp1.len - 1) >= 0) { + tmp1.c[tmp1.len - 1] = '\0'; + } + + php_info_print_table_start(); + if (apv && *apv) { + php_info_print_table_row(2, "Apache Version", apv); + } + php_info_print_table_row(2, "Loaded Modules", tmp1.c); + smart_str_free(&tmp1); + php_info_print_table_end(); +} + +/* {{{ arginfo */ +ZEND_BEGIN_ARG_INFO_EX(arginfo_apache2filter_lookup_uri, 0, 0, 1) + ZEND_ARG_INFO(0, filename) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_apache2filter_virtual, 0, 0, 1) + ZEND_ARG_INFO(0, uri) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO(arginfo_apache2filter_getallheaders, 0) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO(arginfo_apache2filter_response_headers, 0) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_apache2filter_note, 0, 0, 1) + ZEND_ARG_INFO(0, note_name) + ZEND_ARG_INFO(0, note_value) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_apache2filter_setenv, 0, 0, 2) + ZEND_ARG_INFO(0, variable) + ZEND_ARG_INFO(0, value) + ZEND_ARG_INFO(0, walk_to_top) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_apache2filter_getenv, 0, 0, 1) + ZEND_ARG_INFO(0, variable) + ZEND_ARG_INFO(0, walk_to_top) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO(arginfo_apache2filter_get_version, 0) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO(arginfo_apache2filter_get_modules, 0) +ZEND_END_ARG_INFO() +/* }}} */ + +static const zend_function_entry apache_functions[] = { + PHP_FE(apache_lookup_uri, arginfo_apache2filter_lookup_uri) + PHP_FE(virtual, arginfo_apache2filter_virtual) + PHP_FE(apache_request_headers, arginfo_apache2filter_getallheaders) + PHP_FE(apache_response_headers, arginfo_apache2filter_response_headers) + PHP_FE(apache_setenv, arginfo_apache2filter_setenv) + PHP_FE(apache_getenv, arginfo_apache2filter_getenv) + PHP_FE(apache_note, arginfo_apache2filter_note) + PHP_FE(apache_get_version, arginfo_apache2filter_get_version) + PHP_FE(apache_get_modules, arginfo_apache2filter_get_modules) + PHP_FALIAS(getallheaders, apache_request_headers, arginfo_apache2filter_getallheaders) + {NULL, NULL, NULL} +}; + +zend_module_entry php_apache_module = { + STANDARD_MODULE_HEADER, + "apache2filter", + apache_functions, + NULL, + NULL, + NULL, + NULL, + PHP_MINFO(apache), + NULL, + STANDARD_MODULE_PROPERTIES +}; + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ diff --git a/sapi/apache2filter/sapi_apache2.c b/sapi/apache2filter/sapi_apache2.c new file mode 100644 index 0000000..8ce490e --- /dev/null +++ b/sapi/apache2filter/sapi_apache2.c @@ -0,0 +1,764 @@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2013 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Authors: Sascha Schumann <sascha@schumann.cx> | + | Parts based on Apache 1.3 SAPI module by | + | Rasmus Lerdorf and Zeev Suraski | + +----------------------------------------------------------------------+ + */ + +/* $Id$ */ + +#include <fcntl.h> + +#define ZEND_INCLUDE_FULL_WINDOWS_HEADERS + +#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" +#ifndef NETWARE +#include "ext/standard/php_standard.h" +#else +#include "ext/standard/basic_functions.h" +#endif + +#include "apr_strings.h" +#include "ap_config.h" +#include "apr_buckets.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 "ap_mpm.h" + +#include "php_apache.h" + +/* UnixWare and Netware define shutdown to _shutdown, which causes problems later + * on when using a structure member named shutdown. Since this source + * file does not use the system call shutdown, it is safe to #undef it. + */ +#undef shutdown + +/* A way to specify the location of the php.ini dir in an apache directive */ +char *apache2_php_ini_path_override = NULL; + +static int +php_apache_sapi_ub_write(const char *str, uint str_length TSRMLS_DC) +{ + apr_bucket *b; + apr_bucket_brigade *bb; + apr_bucket_alloc_t *ba; + ap_filter_t *f; /* remaining output filters */ + php_struct *ctx; + + ctx = SG(server_context); + f = ctx->f; + + if (str_length == 0) return 0; + + ba = f->c->bucket_alloc; + bb = apr_brigade_create(ctx->r->pool, ba); + + b = apr_bucket_transient_create(str, str_length, ba); + APR_BRIGADE_INSERT_TAIL(bb, b); + + if (ap_pass_brigade(f->next, bb) != APR_SUCCESS || ctx->r->connection->aborted) { + php_handle_aborted_connection(); + } + + return str_length; /* we always consume all the data passed to us. */ +} + +static int +php_apache_sapi_header_handler(sapi_header_struct *sapi_header, sapi_header_op_enum op, sapi_headers_struct *sapi_headers TSRMLS_DC) +{ + php_struct *ctx; + ap_filter_t *f; + char *val, *ptr; + + ctx = SG(server_context); + f = ctx->r->output_filters; + + switch(op) { + case SAPI_HEADER_DELETE: + apr_table_unset(ctx->r->headers_out, sapi_header->header); + return 0; + + case SAPI_HEADER_DELETE_ALL: + apr_table_clear(ctx->r->headers_out); + return 0; + + case SAPI_HEADER_ADD: + case SAPI_HEADER_REPLACE: + val = strchr(sapi_header->header, ':'); + + if (!val) { + sapi_free_header(sapi_header); + return 0; + } + ptr = val; + + *val = '\0'; + + do { + val++; + } while (*val == ' '); + + if (!strcasecmp(sapi_header->header, "content-type")) + ctx->r->content_type = apr_pstrdup(ctx->r->pool, val); + else if (!strcasecmp(sapi_header->header, "content-length")) + ap_set_content_length(ctx->r, strtol(val, (char **)NULL, 10)); + else if (op == SAPI_HEADER_REPLACE) + apr_table_set(ctx->r->headers_out, sapi_header->header, val); + else + apr_table_add(ctx->r->headers_out, sapi_header->header, val); + + *ptr = ':'; + return SAPI_HEADER_ADD; + + default: + return 0; + } +} + +static int +php_apache_sapi_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC) +{ + php_struct *ctx = SG(server_context); + + ctx->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 TSRMLS_DC) +{ + int n; + int to_read; + php_struct *ctx = SG(server_context); + + to_read = ctx->post_len - ctx->post_idx; + n = MIN(to_read, count_bytes); + + if (n > 0) { + memcpy(buf, ctx->post_data + ctx->post_idx, n); + ctx->post_idx += n; + } else { + if (ctx->post_data) free(ctx->post_data); + ctx->post_data = NULL; + } + + return n; +} + +static struct stat* +php_apache_sapi_get_stat(TSRMLS_D) +{ + php_struct *ctx = SG(server_context); + + ctx->finfo.st_uid = ctx->r->finfo.user; + ctx->finfo.st_gid = ctx->r->finfo.group; + ctx->finfo.st_dev = ctx->r->finfo.device; + ctx->finfo.st_ino = ctx->r->finfo.inode; +#ifdef NETWARE + ctx->finfo.st_atime.tv_sec = apr_time_sec(ctx->r->finfo.atime); + ctx->finfo.st_mtime.tv_sec = apr_time_sec(ctx->r->finfo.mtime); + ctx->finfo.st_ctime.tv_sec = apr_time_sec(ctx->r->finfo.ctime); +#else + ctx->finfo.st_atime = apr_time_sec(ctx->r->finfo.atime); + ctx->finfo.st_mtime = apr_time_sec(ctx->r->finfo.mtime); + ctx->finfo.st_ctime = apr_time_sec(ctx->r->finfo.ctime); +#endif + + ctx->finfo.st_size = ctx->r->finfo.size; + ctx->finfo.st_nlink = ctx->r->finfo.nlink; + + return &ctx->finfo; +} + +static char * +php_apache_sapi_read_cookies(TSRMLS_D) +{ + php_struct *ctx = SG(server_context); + const char *http_cookie; + + http_cookie = apr_table_get(ctx->r->headers_in, "cookie"); + + /* The SAPI interface should use 'const char *' */ + return (char *) http_cookie; +} + +static char * +php_apache_sapi_getenv(char *name, size_t name_len TSRMLS_DC) +{ + php_struct *ctx = SG(server_context); + const char *env_var; + + env_var = apr_table_get(ctx->r->subprocess_env, name); + + return (char *) env_var; +} + +static void +php_apache_sapi_register_variables(zval *track_vars_array TSRMLS_DC) +{ + php_struct *ctx = SG(server_context); + const apr_array_header_t *arr = apr_table_elts(ctx->r->subprocess_env); + char *key, *val; + unsigned int new_val_len; + + APR_ARRAY_FOREACH_OPEN(arr, key, val) + if (!val) { + val = ""; + } + if (sapi_module.input_filter(PARSE_SERVER, key, &val, strlen(val), &new_val_len TSRMLS_CC)) { + php_register_variable_safe(key, val, new_val_len, track_vars_array TSRMLS_CC); + } + APR_ARRAY_FOREACH_CLOSE() + + php_register_variable("PHP_SELF", ctx->r->uri, track_vars_array TSRMLS_CC); + if (sapi_module.input_filter(PARSE_SERVER, "PHP_SELF", &ctx->r->uri, strlen(ctx->r->uri), &new_val_len TSRMLS_CC)) { + php_register_variable_safe("PHP_SELF", ctx->r->uri, new_val_len, track_vars_array TSRMLS_CC); + } +} + +static void +php_apache_sapi_flush(void *server_context) +{ + php_struct *ctx; + apr_bucket_brigade *bb; + apr_bucket_alloc_t *ba; + apr_bucket *b; + ap_filter_t *f; /* output filters */ + TSRMLS_FETCH(); + + ctx = server_context; + + /* If we haven't registered a server_context yet, + * then don't bother flushing. */ + if (!server_context) + return; + + sapi_send_headers(TSRMLS_C); + + ctx->r->status = SG(sapi_headers).http_response_code; + SG(headers_sent) = 1; + + f = ctx->f; + + /* Send a flush bucket down the filter chain. The current default + * handler seems to act on the first flush bucket, but ignores + * all further flush buckets. + */ + + ba = ctx->r->connection->bucket_alloc; + bb = apr_brigade_create(ctx->r->pool, ba); + b = apr_bucket_flush_create(ba); + APR_BRIGADE_INSERT_TAIL(bb, b); + if (ap_pass_brigade(f->next, bb) != APR_SUCCESS || ctx->r->connection->aborted) { + php_handle_aborted_connection(); + } +} + +static void php_apache_sapi_log_message(char *msg TSRMLS_DC) +{ + php_struct *ctx; + + ctx = SG(server_context); + + if (ctx == NULL) { /* we haven't initialized our ctx yet, oh well */ + ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, 0, NULL, "%s", msg); + } + else { + ap_log_error(APLOG_MARK, APLOG_ERR, 0, ctx->r->server, "%s", msg); + } +} + +static int +php_apache_disable_caching(ap_filter_t *f) +{ + /* Identify PHP scripts as non-cacheable, thus preventing + * Apache from sending a 304 status when the browser sends + * If-Modified-Since header. + */ + f->r->no_local_copy = 1; + + return OK; +} + +static double php_apache_sapi_get_request_time(TSRMLS_D) +{ + php_struct *ctx = SG(server_context); + return ((double) apr_time_as_msec(ctx->r->request_time)) / 1000.0; +} + +extern zend_module_entry php_apache_module; + +static int php_apache2_startup(sapi_module_struct *sapi_module) +{ + if (php_module_startup(sapi_module, &php_apache_module, 1)==FAILURE) { + return FAILURE; + } + return SUCCESS; +} + +static sapi_module_struct apache2_sapi_module = { + "apache2filter", + "Apache 2.0 Filter", + + php_apache2_startup, /* startup */ + php_module_shutdown_wrapper, /* shutdown */ + + NULL, /* activate */ + NULL, /* deactivate */ + + php_apache_sapi_ub_write, /* unbuffered write */ + php_apache_sapi_flush, /* flush */ + php_apache_sapi_get_stat, /* get uid */ + php_apache_sapi_getenv, /* 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, + php_apache_sapi_log_message, /* Log message */ + php_apache_sapi_get_request_time, /* Get Request Time */ + NULL, /* Child terminate */ + + STANDARD_SAPI_MODULE_PROPERTIES +}; + +static int php_input_filter(ap_filter_t *f, apr_bucket_brigade *bb, + ap_input_mode_t mode, apr_read_type_e block, apr_off_t readbytes) +{ + php_struct *ctx; + long old_index; + apr_bucket *b; + const char *str; + apr_size_t n; + apr_status_t rv; + TSRMLS_FETCH(); + + if (f->r->proxyreq) { + return ap_get_brigade(f->next, bb, mode, block, readbytes); + } + + ctx = SG(server_context); + if (ctx == NULL) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, f->r, + "php failed to get server context"); + return HTTP_INTERNAL_SERVER_ERROR; + } + + if ((rv = ap_get_brigade(f->next, bb, mode, block, readbytes)) != APR_SUCCESS) { + return rv; + } + + for (b = APR_BRIGADE_FIRST(bb); b != APR_BRIGADE_SENTINEL(bb); b = APR_BUCKET_NEXT(b)) { + apr_bucket_read(b, &str, &n, APR_NONBLOCK_READ); + if (n > 0) { + old_index = ctx->post_len; + ctx->post_len += n; + ctx->post_data = realloc(ctx->post_data, ctx->post_len + 1); + memcpy(ctx->post_data + old_index, str, n); + } + } + return APR_SUCCESS; +} + +static void php_apache_request_ctor(ap_filter_t *f, php_struct *ctx TSRMLS_DC) +{ + char *content_type; + char *content_length; + const char *auth; + + PG(during_request_startup) = 0; + SG(sapi_headers).http_response_code = !f->r->status ? HTTP_OK : f->r->status; + SG(request_info).content_type = apr_table_get(f->r->headers_in, "Content-Type"); +#undef safe_strdup +#define safe_strdup(x) ((x)?strdup((x)):NULL) + SG(request_info).query_string = safe_strdup(f->r->args); + SG(request_info).request_method = f->r->method; + SG(request_info).proto_num = f->r->proto_num; + SG(request_info).request_uri = safe_strdup(f->r->uri); + SG(request_info).path_translated = safe_strdup(f->r->filename); + f->r->no_local_copy = 1; + content_type = sapi_get_default_content_type(TSRMLS_C); + f->r->content_type = apr_pstrdup(f->r->pool, content_type); + SG(request_info).post_data = ctx->post_data; + SG(request_info).post_data_length = ctx->post_len; + + efree(content_type); + + content_length = (char *) apr_table_get(f->r->headers_in, "Content-Length"); + SG(request_info).content_length = (content_length ? atol(content_length) : 0); + + apr_table_unset(f->r->headers_out, "Content-Length"); + apr_table_unset(f->r->headers_out, "Last-Modified"); + apr_table_unset(f->r->headers_out, "Expires"); + apr_table_unset(f->r->headers_out, "ETag"); + + auth = apr_table_get(f->r->headers_in, "Authorization"); + php_handle_auth_data(auth TSRMLS_CC); + + if (SG(request_info).auth_user == NULL && f->r->user) { + SG(request_info).auth_user = estrdup(f->r->user); + } + + ctx->r->user = apr_pstrdup(ctx->r->pool, SG(request_info).auth_user); + + php_request_startup(TSRMLS_C); +} + +static void php_apache_request_dtor(ap_filter_t *f TSRMLS_DC) +{ + php_apr_bucket_brigade *pbb = (php_apr_bucket_brigade *)f->ctx; + + php_request_shutdown(NULL); + + if (SG(request_info).query_string) { + free(SG(request_info).query_string); + } + if (SG(request_info).request_uri) { + free(SG(request_info).request_uri); + } + if (SG(request_info).path_translated) { + free(SG(request_info).path_translated); + } + + apr_brigade_destroy(pbb->bb); +} + +static int php_output_filter(ap_filter_t *f, apr_bucket_brigade *bb) +{ + php_struct *ctx; + void *conf = ap_get_module_config(f->r->per_dir_config, &php5_module); + char *p = get_php_config(conf, "engine", sizeof("engine")); + zend_file_handle zfd; + php_apr_bucket_brigade *pbb; + apr_bucket *b; + TSRMLS_FETCH(); + + if (f->r->proxyreq) { + zend_try { + zend_ini_deactivate(TSRMLS_C); + } zend_end_try(); + return ap_pass_brigade(f->next, bb); + } + + /* handle situations where user turns the engine off */ + if (*p == '0') { + zend_try { + zend_ini_deactivate(TSRMLS_C); + } zend_end_try(); + return ap_pass_brigade(f->next, bb); + } + + if(f->ctx) { + pbb = (php_apr_bucket_brigade *)f->ctx; + } else { + pbb = f->ctx = apr_palloc(f->r->pool, sizeof(*pbb)); + pbb->bb = apr_brigade_create(f->r->pool, f->c->bucket_alloc); + } + + if(ap_save_brigade(NULL, &pbb->bb, &bb, f->r->pool) != APR_SUCCESS) { + /* Bad */ + } + + apr_brigade_cleanup(bb); + + /* Check to see if the last bucket in this brigade, it not + * we have to wait until then. */ + if(!APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(pbb->bb))) { + return 0; + } + + /* Setup the CGI variables if this is the main request.. */ + if (f->r->main == NULL || + /* .. or if the sub-request envinronment differs from the main-request. */ + f->r->subprocess_env != f->r->main->subprocess_env + ) { + /* setup standard CGI variables */ + ap_add_common_vars(f->r); + ap_add_cgi_vars(f->r); + } + + ctx = SG(server_context); + if (ctx == NULL) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, f->r, + "php failed to get server context"); + zend_try { + zend_ini_deactivate(TSRMLS_C); + } zend_end_try(); + return HTTP_INTERNAL_SERVER_ERROR; + } + + ctx->f = f->next; /* save whatever filters are after us in the chain. */ + + if (ctx->request_processed) { + zend_try { + zend_ini_deactivate(TSRMLS_C); + } zend_end_try(); + return ap_pass_brigade(f->next, bb); + } + + apply_config(conf); + php_apache_request_ctor(f, ctx TSRMLS_CC); + + /* It'd be nice if we could highlight based of a zend_file_handle here.... + * ...but we can't. */ + + zfd.type = ZEND_HANDLE_STREAM; + + zfd.handle.stream.handle = pbb; + zfd.handle.stream.reader = php_apache_read_stream; + zfd.handle.stream.closer = NULL; + zfd.handle.stream.fsizer = php_apache_fsizer_stream; + zfd.handle.stream.isatty = 0; + + zfd.filename = f->r->filename; + zfd.opened_path = NULL; + zfd.free_filename = 0; + + php_execute_script(&zfd TSRMLS_CC); + + apr_table_set(ctx->r->notes, "mod_php_memory_usage", + apr_psprintf(ctx->r->pool, "%u", zend_memory_peak_usage(1 TSRMLS_CC))); + + php_apache_request_dtor(f TSRMLS_CC); + + if (!f->r->main) { + ctx->request_processed = 1; + } + + b = apr_bucket_eos_create(f->c->bucket_alloc); + APR_BRIGADE_INSERT_TAIL(pbb->bb, b); + + /* Pass whatever is left on the brigade. */ + return ap_pass_brigade(f->next, pbb->bb); +} + +static apr_status_t +php_apache_server_shutdown(void *tmp) +{ + apache2_sapi_module.shutdown(&apache2_sapi_module); + sapi_shutdown(); +#ifdef ZTS + tsrm_shutdown(); +#endif + return APR_SUCCESS; +} + +static void php_apache_add_version(apr_pool_t *p) +{ + TSRMLS_FETCH(); + if (PG(expose_php)) { + ap_add_version_component(p, "PHP/" PHP_VERSION); + } +} + +static int php_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp) +{ +#ifndef ZTS + int threaded_mpm; + + ap_mpm_query(AP_MPMQ_IS_THREADED, &threaded_mpm); + if(threaded_mpm) { + ap_log_error(APLOG_MARK, APLOG_CRIT, 0, 0, "Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe. You need to recompile PHP."); + return DONE; + } +#endif + /* When this is NULL, apache won't override the hard-coded default + * php.ini path setting. */ + apache2_php_ini_path_override = NULL; + return OK; +} + +static int +php_apache_server_startup(apr_pool_t *pconf, apr_pool_t *plog, + apr_pool_t *ptemp, server_rec *s) +{ + void *data = NULL; + const char *userdata_key = "apache2filter_post_config"; + + /* Apache will load, unload and then reload a DSO module. This + * prevents us from starting PHP until the second load. */ + apr_pool_userdata_get(&data, userdata_key, s->process->pool); + if (data == NULL) { + /* We must use set() here and *not* setn(), otherwise the + * static string pointed to by userdata_key will be mapped + * to a different location when the DSO is reloaded and the + * pointers won't match, causing get() to return NULL when + * we expected it to return non-NULL. */ + apr_pool_userdata_set((const void *)1, userdata_key, + apr_pool_cleanup_null, s->process->pool); + return OK; + } + + /* Set up our overridden path. */ + if (apache2_php_ini_path_override) { + apache2_sapi_module.php_ini_path_override = apache2_php_ini_path_override; + } +#ifdef ZTS + tsrm_startup(1, 1, 0, NULL); +#endif + sapi_startup(&apache2_sapi_module); + apache2_sapi_module.startup(&apache2_sapi_module); + apr_pool_cleanup_register(pconf, NULL, php_apache_server_shutdown, apr_pool_cleanup_null); + php_apache_add_version(pconf); + + return OK; +} + +static void php_add_filter(request_rec *r, ap_filter_t *f) +{ + int output = (f == r->output_filters); + + /* for those who still have Set*Filter PHP configured */ + while (f) { + if (strcmp(f->frec->name, "PHP") == 0) { + ap_log_error(APLOG_MARK, APLOG_WARNING, + 0, r->server, + "\"Set%sFilter PHP\" already configured for %s", + output ? "Output" : "Input", r->uri); + return; + } + f = f->next; + } + + if (output) { + ap_add_output_filter("PHP", NULL, r, r->connection); + } else { + ap_add_input_filter("PHP", NULL, r, r->connection); + } +} + +static void php_insert_filter(request_rec *r) +{ + int content_type_len = strlen("application/x-httpd-php"); + + if (r->content_type && !strncmp(r->content_type, "application/x-httpd-php", content_type_len-1)) { + if (r->content_type[content_type_len] == '\0' || !strncmp(r->content_type+content_type_len, "-source", sizeof("-source"))) { + php_add_filter(r, r->output_filters); + php_add_filter(r, r->input_filters); + } + } +} + +static apr_status_t php_server_context_cleanup(void *data_) +{ + void **data = data_; + *data = NULL; + return APR_SUCCESS; +} + +static int php_post_read_request(request_rec *r) +{ + php_struct *ctx; + TSRMLS_FETCH(); + + /* Initialize filter context */ + SG(server_context) = ctx = apr_pcalloc(r->pool, sizeof(*ctx)); + + /* register a cleanup so we clear out the SG(server_context) + * after each request. Note: We pass in the pointer to the + * server_context in case this is handled by a different thread. */ + apr_pool_cleanup_register(r->pool, (void *)&SG(server_context), + php_server_context_cleanup, + apr_pool_cleanup_null); + + /* Save the entire request, so we can get the input or output + * filters if we need them. */ + ctx->r = r; + + return OK; +} + +static void php_register_hook(apr_pool_t *p) +{ + ap_hook_pre_config(php_pre_config, NULL, NULL, APR_HOOK_MIDDLE); + ap_hook_post_config(php_apache_server_startup, NULL, NULL, APR_HOOK_MIDDLE); + ap_hook_insert_filter(php_insert_filter, NULL, NULL, APR_HOOK_MIDDLE); + ap_hook_post_read_request(php_post_read_request, NULL, NULL, APR_HOOK_MIDDLE); + ap_register_output_filter("PHP", php_output_filter, php_apache_disable_caching, AP_FTYPE_RESOURCE); + ap_register_input_filter("PHP", php_input_filter, php_apache_disable_caching, AP_FTYPE_RESOURCE); +} + +static size_t php_apache_read_stream(void *handle, char *buf, size_t wantlen TSRMLS_DC) +{ + php_apr_bucket_brigade *pbb = (php_apr_bucket_brigade *)handle; + apr_bucket_brigade *rbb; + apr_size_t readlen; + apr_bucket *b = NULL; + + rbb = pbb->bb; + + if((apr_brigade_partition(pbb->bb, wantlen, &b) == APR_SUCCESS) && b){ + pbb->bb = apr_brigade_split(rbb, b); + } + + readlen = wantlen; + apr_brigade_flatten(rbb, buf, &readlen); + apr_brigade_cleanup(rbb); + + return readlen; +} + +static size_t php_apache_fsizer_stream(void *handle TSRMLS_DC) +{ + php_apr_bucket_brigade *pbb = (php_apr_bucket_brigade *)handle; + apr_off_t actual = 0; + + if (apr_brigade_length(pbb->bb, 1, &actual) == APR_SUCCESS) { + return actual; + } + + return 0; +} + +AP_MODULE_DECLARE_DATA module php5_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 */ + php_dir_cmds, /* command apr_table_t */ + php_register_hook /* register hooks */ +}; + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ |