Back to index

php5  5.3.10
apache_config.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Author: Sascha Schumann <sascha@schumann.cx>                         |
00016    +----------------------------------------------------------------------+
00017  */
00018 
00019 /* $Id: apache_config.c 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
00022 
00023 #include "php.h"
00024 #include "php_ini.h"
00025 #include "php_apache.h"
00026 
00027 #include "apr_strings.h"
00028 #include "ap_config.h"
00029 #include "util_filter.h"
00030 #include "httpd.h"
00031 #include "http_config.h"
00032 #include "http_request.h"
00033 #include "http_core.h"
00034 #include "http_protocol.h"
00035 #include "http_log.h"
00036 #include "http_main.h"
00037 #include "util_script.h"
00038 #include "http_core.h"
00039 
00040 #ifdef PHP_AP_DEBUG
00041 #define phpapdebug(a) fprintf a
00042 #else
00043 #define phpapdebug(a)
00044 #endif
00045 
00046 typedef struct {
00047        HashTable config;
00048 } php_conf_rec;
00049 
00050 typedef struct {
00051        char *value;
00052        size_t value_len;
00053        char status;
00054        char htaccess;
00055 } php_dir_entry;
00056 
00057 static const char *real_value_hnd(cmd_parms *cmd, void *dummy, const char *name, const char *value, int status)
00058 {
00059        php_conf_rec *d = dummy;
00060        php_dir_entry e;
00061 
00062        phpapdebug((stderr, "Getting %s=%s for %p (%d)\n", name, value, dummy, zend_hash_num_elements(&d->config)));
00063        
00064        if (!strncasecmp(value, "none", sizeof("none"))) {
00065               value = "";
00066        }
00067        
00068        e.value = apr_pstrdup(cmd->pool, value);
00069        e.value_len = strlen(value);
00070        e.status = status;
00071        e.htaccess = ((cmd->override & (RSRC_CONF|ACCESS_CONF)) == 0);
00072 
00073        zend_hash_update(&d->config, (char *) name, strlen(name) + 1, &e, sizeof(e), NULL);
00074        return NULL;
00075 }
00076 
00077 static const char *php_apache_value_handler(cmd_parms *cmd, void *dummy, const char *name, const char *value)
00078 {
00079        return real_value_hnd(cmd, dummy, name, value, PHP_INI_PERDIR);
00080 }
00081 
00082 static const char *php_apache_admin_value_handler(cmd_parms *cmd, void *dummy, const char *name, const char *value)
00083 {
00084        return real_value_hnd(cmd, dummy, name, value, PHP_INI_SYSTEM);
00085 }
00086 
00087 static const char *real_flag_hnd(cmd_parms *cmd, void *dummy, const char *arg1, const char *arg2, int status)
00088 {
00089        char bool_val[2];
00090 
00091        if (!strcasecmp(arg2, "On") || (arg2[0] == '1' && arg2[1] == '\0')) {
00092               bool_val[0] = '1';
00093        } else {
00094               bool_val[0] = '0';
00095        }
00096        bool_val[1] = 0;
00097 
00098        return real_value_hnd(cmd, dummy, arg1, bool_val, status);
00099 }
00100 
00101 static const char *php_apache_flag_handler(cmd_parms *cmd, void *dummy, const char *name, const char *value)
00102 {
00103        return real_flag_hnd(cmd, dummy, name, value, PHP_INI_PERDIR);
00104 }
00105 
00106 static const char *php_apache_admin_flag_handler(cmd_parms *cmd, void *dummy, const char *name, const char *value)
00107 {
00108        return real_flag_hnd(cmd, dummy, name, value, PHP_INI_SYSTEM);
00109 }
00110 
00111 static const char *php_apache_phpini_set(cmd_parms *cmd, void *mconfig, const char *arg)
00112 {
00113        if (apache2_php_ini_path_override) {
00114               return "Only first PHPINIDir directive honored per configuration tree - subsequent ones ignored";
00115        }
00116        apache2_php_ini_path_override = ap_server_root_relative(cmd->pool, arg);
00117        return NULL;
00118 }
00119 
00120 static zend_bool should_overwrite_per_dir_entry(HashTable *target_ht, php_dir_entry *new_per_dir_entry, zend_hash_key *hash_key, void *pData)
00121 {
00122        php_dir_entry *orig_per_dir_entry;
00123 
00124        if (zend_hash_find(target_ht, hash_key->arKey, hash_key->nKeyLength, (void **) &orig_per_dir_entry)==FAILURE) {
00125               return 1; /* does not exist in dest, copy from source */
00126        }
00127 
00128        if (new_per_dir_entry->status >= orig_per_dir_entry->status) {
00129               /* use new entry */
00130               phpapdebug((stderr, "ADDING/OVERWRITING %s (%d vs. %d)\n", hash_key->arKey, new_per_dir_entry->status, orig_per_dir_entry->status));
00131               return 1;
00132        } else {
00133               return 0;
00134        }
00135 }
00136 
00137 
00138 void *merge_php_config(apr_pool_t *p, void *base_conf, void *new_conf)
00139 {
00140        php_conf_rec *d = base_conf, *e = new_conf, *n = NULL;
00141 #if STAS_0
00142        php_dir_entry *pe;
00143        php_dir_entry *data;
00144        char *str;
00145        uint str_len;
00146        ulong num_index;
00147 #endif
00148 
00149        n = create_php_config(p, "merge_php_config");
00150        /* copy old config */
00151        zend_hash_copy(&n->config, &d->config, NULL, NULL, sizeof(php_dir_entry));
00152        /* merge new config */
00153        phpapdebug((stderr, "Merge dir (%p)+(%p)=(%p)\n", base_conf, new_conf, n));
00154        zend_hash_merge_ex(&n->config, &e->config, NULL, sizeof(php_dir_entry), (merge_checker_func_t) should_overwrite_per_dir_entry, NULL);
00155 #if STAS_0
00156        for (zend_hash_internal_pointer_reset(&d->config);
00157                      zend_hash_get_current_key_ex(&d->config, &str, &str_len, 
00158                             &num_index, 0, NULL) == HASH_KEY_IS_STRING;
00159                      zend_hash_move_forward(&d->config)) {
00160               pe = NULL;
00161               zend_hash_get_current_data(&d->config, (void **) &data);
00162               if (zend_hash_find(&n->config, str, str_len, (void **) &pe) == SUCCESS) {
00163                      if (pe->status >= data->status) continue;
00164               }
00165               phpapdebug((stderr, "ADDING/OVERWRITING %s (%d vs. %d)\n", str, data->status, pe?pe->status:-1));
00166               zend_hash_update(&n->config, str, str_len, data, sizeof(*data), NULL);
00167        }
00168 #endif
00169        return n;
00170 }
00171 
00172 char *get_php_config(void *conf, char *name, size_t name_len)
00173 {
00174        php_conf_rec *d = conf;
00175        php_dir_entry *pe;
00176        
00177        if (zend_hash_find(&d->config, name, name_len, (void **) &pe) == SUCCESS) {
00178               return pe->value;
00179        }
00180 
00181        return "";
00182 }
00183 
00184 void apply_config(void *dummy)
00185 {
00186        php_conf_rec *d = dummy;
00187        char *str;
00188        uint str_len;
00189        php_dir_entry *data;
00190        
00191        for (zend_hash_internal_pointer_reset(&d->config);
00192                      zend_hash_get_current_key_ex(&d->config, &str, &str_len, NULL, 0, 
00193                             NULL) == HASH_KEY_IS_STRING;
00194                      zend_hash_move_forward(&d->config)) {
00195               if (zend_hash_get_current_data(&d->config, (void **) &data) == SUCCESS) {
00196                      phpapdebug((stderr, "APPLYING (%s)(%s)\n", str, data->value));
00197                      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) {
00198                             phpapdebug((stderr, "..FAILED\n"));
00199                      }
00200               }
00201        }
00202 }
00203 
00204 const command_rec php_dir_cmds[] =
00205 {
00206        AP_INIT_TAKE2("php_value", php_apache_value_handler, NULL, OR_OPTIONS, "PHP Value Modifier"),
00207        AP_INIT_TAKE2("php_flag", php_apache_flag_handler, NULL, OR_OPTIONS, "PHP Flag Modifier"),
00208        AP_INIT_TAKE2("php_admin_value", php_apache_admin_value_handler, NULL, ACCESS_CONF|RSRC_CONF, "PHP Value Modifier (Admin)"),
00209        AP_INIT_TAKE2("php_admin_flag", php_apache_admin_flag_handler, NULL, ACCESS_CONF|RSRC_CONF, "PHP Flag Modifier (Admin)"),
00210        AP_INIT_TAKE1("PHPINIDir", php_apache_phpini_set, NULL, RSRC_CONF, "Directory containing the php.ini file"),
00211        {NULL}
00212 };
00213 
00214 static apr_status_t destroy_php_config(void *data)
00215 {
00216        php_conf_rec *d = data;
00217 
00218        phpapdebug((stderr, "Destroying config %p\n", data));   
00219        zend_hash_destroy(&d->config);
00220 
00221        return APR_SUCCESS;
00222 }
00223 
00224 void *create_php_config(apr_pool_t *p, char *dummy)
00225 {
00226        php_conf_rec *newx = (php_conf_rec *) apr_pcalloc(p, sizeof(*newx));
00227 
00228        phpapdebug((stderr, "Creating new config (%p) for %s\n", newx, dummy));
00229        zend_hash_init(&newx->config, 0, NULL, NULL, 1);
00230        apr_pool_cleanup_register(p, newx, destroy_php_config, apr_pool_cleanup_null);
00231        return (void *) newx;
00232 }
00233 
00234 /*
00235  * Local variables:
00236  * tab-width: 4
00237  * c-basic-offset: 4
00238  * End:
00239  * vim600: sw=4 ts=4 fdm=marker
00240  * vim<600: sw=4 ts=4
00241  */