1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2013 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author: Sascha Schumann <sascha@schumann.cx>                         |
16    +----------------------------------------------------------------------+
17  */
18 
19 /* $Id$ */
20 
21 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
22 
23 #include "php.h"
24 #include "php_ini.h"
25 #include "php_apache.h"
26 
27 #include "apr_strings.h"
28 #include "ap_config.h"
29 #include "util_filter.h"
30 #include "httpd.h"
31 #include "http_config.h"
32 #include "http_request.h"
33 #include "http_core.h"
34 #include "http_protocol.h"
35 #include "http_log.h"
36 #include "http_main.h"
37 #include "util_script.h"
38 #include "http_core.h"
39 
40 #ifdef PHP_AP_DEBUG
41 #define phpapdebug(a) fprintf a
42 #else
43 #define phpapdebug(a)
44 #endif
45 
46 typedef struct {
47 	HashTable config;
48 } php_conf_rec;
49 
50 typedef struct {
51 	char *value;
52 	size_t value_len;
53 	char status;
54 	char htaccess;
55 } php_dir_entry;
56 
real_value_hnd(cmd_parms * cmd,void * dummy,const char * name,const char * value,int status)57 static const char *real_value_hnd(cmd_parms *cmd, void *dummy, const char *name, const char *value, int status)
58 {
59 	php_conf_rec *d = dummy;
60 	php_dir_entry e;
61 
62 	phpapdebug((stderr, "Getting %s=%s for %p (%d)\n", name, value, dummy, zend_hash_num_elements(&d->config)));
63 
64 	if (!strncasecmp(value, "none", sizeof("none"))) {
65 		value = "";
66 	}
67 
68 	e.value = apr_pstrdup(cmd->pool, value);
69 	e.value_len = strlen(value);
70 	e.status = status;
71 	e.htaccess = ((cmd->override & (RSRC_CONF|ACCESS_CONF)) == 0);
72 
73 	zend_hash_update(&d->config, (char *) name, strlen(name) + 1, &e, sizeof(e), NULL);
74 	return NULL;
75 }
76 
php_apache_value_handler(cmd_parms * cmd,void * dummy,const char * name,const char * value)77 static const char *php_apache_value_handler(cmd_parms *cmd, void *dummy, const char *name, const char *value)
78 {
79 	return real_value_hnd(cmd, dummy, name, value, PHP_INI_PERDIR);
80 }
81 
php_apache_admin_value_handler(cmd_parms * cmd,void * dummy,const char * name,const char * value)82 static const char *php_apache_admin_value_handler(cmd_parms *cmd, void *dummy, const char *name, const char *value)
83 {
84 	return real_value_hnd(cmd, dummy, name, value, PHP_INI_SYSTEM);
85 }
86 
real_flag_hnd(cmd_parms * cmd,void * dummy,const char * arg1,const char * arg2,int status)87 static const char *real_flag_hnd(cmd_parms *cmd, void *dummy, const char *arg1, const char *arg2, int status)
88 {
89 	char bool_val[2];
90 
91 	if (!strcasecmp(arg2, "On") || (arg2[0] == '1' && arg2[1] == '\0')) {
92 		bool_val[0] = '1';
93 	} else {
94 		bool_val[0] = '0';
95 	}
96 	bool_val[1] = 0;
97 
98 	return real_value_hnd(cmd, dummy, arg1, bool_val, status);
99 }
100 
php_apache_flag_handler(cmd_parms * cmd,void * dummy,const char * name,const char * value)101 static const char *php_apache_flag_handler(cmd_parms *cmd, void *dummy, const char *name, const char *value)
102 {
103 	return real_flag_hnd(cmd, dummy, name, value, PHP_INI_PERDIR);
104 }
105 
php_apache_admin_flag_handler(cmd_parms * cmd,void * dummy,const char * name,const char * value)106 static const char *php_apache_admin_flag_handler(cmd_parms *cmd, void *dummy, const char *name, const char *value)
107 {
108 	return real_flag_hnd(cmd, dummy, name, value, PHP_INI_SYSTEM);
109 }
110 
php_apache_phpini_set(cmd_parms * cmd,void * mconfig,const char * arg)111 static const char *php_apache_phpini_set(cmd_parms *cmd, void *mconfig, const char *arg)
112 {
113 	if (apache2_php_ini_path_override) {
114 		return "Only first PHPINIDir directive honored per configuration tree - subsequent ones ignored";
115 	}
116 	apache2_php_ini_path_override = ap_server_root_relative(cmd->pool, arg);
117 	return NULL;
118 }
119 
120 
merge_php_config(apr_pool_t * p,void * base_conf,void * new_conf)121 void *merge_php_config(apr_pool_t *p, void *base_conf, void *new_conf)
122 {
123 	php_conf_rec *d = base_conf, *e = new_conf, *n = NULL;
124 	php_dir_entry *pe;
125 	php_dir_entry *data;
126 	char *str;
127 	uint str_len;
128 	ulong num_index;
129 
130 	n = create_php_config(p, "merge_php_config");
131 	zend_hash_copy(&n->config, &e->config, NULL, NULL, sizeof(php_dir_entry));
132 
133 	phpapdebug((stderr, "Merge dir (%p)+(%p)=(%p)\n", base_conf, new_conf, n));
134 	for (zend_hash_internal_pointer_reset(&d->config);
135 			zend_hash_get_current_key_ex(&d->config, &str, &str_len,
136 				&num_index, 0, NULL) == HASH_KEY_IS_STRING;
137 			zend_hash_move_forward(&d->config)) {
138 		pe = NULL;
139 		zend_hash_get_current_data(&d->config, (void **) &data);
140 		if (zend_hash_find(&n->config, str, str_len, (void **) &pe) == SUCCESS) {
141 			if (pe->status >= data->status) continue;
142 		}
143 		zend_hash_update(&n->config, str, str_len, data, sizeof(*data), NULL);
144 		phpapdebug((stderr, "ADDING/OVERWRITING %s (%d vs. %d)\n", str, data->status, pe?pe->status:-1));
145 	}
146 
147 	return n;
148 }
149 
get_php_config(void * conf,char * name,size_t name_len)150 char *get_php_config(void *conf, char *name, size_t name_len)
151 {
152 	php_conf_rec *d = conf;
153 	php_dir_entry *pe;
154 
155 	if (zend_hash_find(&d->config, name, name_len, (void **) &pe) == SUCCESS) {
156 		return pe->value;
157 	}
158 
159 	return "";
160 }
161 
apply_config(void * dummy)162 void apply_config(void *dummy)
163 {
164 	php_conf_rec *d = dummy;
165 	char *str;
166 	uint str_len;
167 	php_dir_entry *data;
168 
169 	for (zend_hash_internal_pointer_reset(&d->config);
170 			zend_hash_get_current_key_ex(&d->config, &str, &str_len, NULL, 0,
171 				NULL) == HASH_KEY_IS_STRING;
172 			zend_hash_move_forward(&d->config)) {
173 		zend_hash_get_current_data(&d->config, (void **) &data);
174 		phpapdebug((stderr, "APPLYING (%s)(%s)\n", str, data->value));
175 		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) {
176 			phpapdebug((stderr, "..FAILED\n"));
177 		}
178 	}
179 }
180 
181 const command_rec php_dir_cmds[] =
182 {
183 	AP_INIT_TAKE2("php_value", php_apache_value_handler, NULL, OR_OPTIONS, "PHP Value Modifier"),
184 	AP_INIT_TAKE2("php_flag", php_apache_flag_handler, NULL, OR_OPTIONS, "PHP Flag Modifier"),
185 	AP_INIT_TAKE2("php_admin_value", php_apache_admin_value_handler, NULL, ACCESS_CONF|RSRC_CONF, "PHP Value Modifier (Admin)"),
186 	AP_INIT_TAKE2("php_admin_flag", php_apache_admin_flag_handler, NULL, ACCESS_CONF|RSRC_CONF, "PHP Flag Modifier (Admin)"),
187 	AP_INIT_TAKE1("PHPINIDir", php_apache_phpini_set, NULL, RSRC_CONF, "Directory containing the php.ini file"),
188 	{NULL}
189 };
190 
destroy_php_config(void * data)191 static apr_status_t destroy_php_config(void *data)
192 {
193 	php_conf_rec *d = data;
194 
195 	phpapdebug((stderr, "Destroying config %p\n", data));
196 	zend_hash_destroy(&d->config);
197 
198 	return APR_SUCCESS;
199 }
200 
create_php_config(apr_pool_t * p,char * dummy)201 void *create_php_config(apr_pool_t *p, char *dummy)
202 {
203 	php_conf_rec *newx = (php_conf_rec *) apr_pcalloc(p, sizeof(*newx));
204 
205 	phpapdebug((stderr, "Creating new config (%p) for %s\n", newx, dummy));
206 	zend_hash_init(&newx->config, 0, NULL, NULL, 1);
207 	apr_pool_cleanup_register(p, newx, destroy_php_config, apr_pool_cleanup_null);
208 	return (void *) newx;
209 }
210 
211 /*
212  * Local variables:
213  * tab-width: 4
214  * c-basic-offset: 4
215  * End:
216  * vim600: sw=4 ts=4 fdm=marker
217  * vim<600: sw=4 ts=4
218  */
219