1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | http://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Author: Sascha Schumann <sascha@schumann.cx> |
14 +----------------------------------------------------------------------+
15 */
16
17 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
18
19 #include "php.h"
20 #ifdef strcasecmp
21 # undef strcasecmp
22 #endif
23 #ifdef strncasecmp
24 # undef strncasecmp
25 #endif
26 #include "zend_smart_str.h"
27 #include "ext/standard/info.h"
28 #include "ext/standard/head.h"
29 #include "php_ini.h"
30 #include "SAPI.h"
31
32 #define CORE_PRIVATE
33 #include "apr_strings.h"
34 #include "apr_time.h"
35 #include "ap_config.h"
36 #include "util_filter.h"
37 #include "httpd.h"
38 #include "http_config.h"
39 #include "http_request.h"
40 #include "http_core.h"
41 #include "http_protocol.h"
42 #include "http_log.h"
43 #include "http_main.h"
44 #include "util_script.h"
45 #include "http_core.h"
46 #include "ap_mpm.h"
47 #ifndef PHP_WIN32
48 #include "unixd.h"
49 #endif
50
51 #include "php_apache.h"
52 #include "php_functions_arginfo.h"
53
54 #ifdef ZTS
55 int php_apache2_info_id;
56 #else
57 php_apache2_info_struct php_apache2_info;
58 #endif
59
60 #define SECTION(name) PUTS("<h2>" name "</h2>\n")
61
php_apache_lookup_uri(char * filename)62 static request_rec *php_apache_lookup_uri(char *filename)
63 {
64 php_struct *ctx = SG(server_context);
65
66 if (!filename || !ctx || !ctx->r) {
67 return NULL;
68 }
69
70 return ap_sub_req_lookup_uri(filename, ctx->r, ctx->r->output_filters);
71 }
72
73 /* {{{ Perform an apache sub-request */
PHP_FUNCTION(virtual)74 PHP_FUNCTION(virtual)
75 {
76 char *filename;
77 size_t filename_len;
78 request_rec *rr;
79
80 if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &filename, &filename_len) == FAILURE) {
81 RETURN_THROWS();
82 }
83
84 if (!(rr = php_apache_lookup_uri(filename))) {
85 php_error_docref(NULL, E_WARNING, "Unable to include '%s' - URI lookup failed", filename);
86 RETURN_FALSE;
87 }
88
89 if (rr->status != HTTP_OK) {
90 php_error_docref(NULL, E_WARNING, "Unable to include '%s' - error finding URI", filename);
91 ap_destroy_sub_req(rr);
92 RETURN_FALSE;
93 }
94
95 /* Flush everything. */
96 php_output_end_all();
97 php_header();
98
99 /* Ensure that the ap_r* layer for the main request is flushed, to
100 * work around http://issues.apache.org/bugzilla/show_bug.cgi?id=17629 */
101 ap_rflush(rr->main);
102
103 if (ap_run_sub_req(rr)) {
104 php_error_docref(NULL, E_WARNING, "Unable to include '%s' - request execution failed", filename);
105 ap_destroy_sub_req(rr);
106 RETURN_FALSE;
107 }
108 ap_destroy_sub_req(rr);
109 RETURN_TRUE;
110 }
111 /* }}} */
112
113 #define ADD_LONG(name) \
114 add_property_long(return_value, #name, rr->name)
115 #define ADD_TIME(name) \
116 add_property_long(return_value, #name, apr_time_sec(rr->name));
117 #define ADD_STRING(name) \
118 if (rr->name) add_property_string(return_value, #name, (char *) rr->name)
119
PHP_FUNCTION(apache_lookup_uri)120 PHP_FUNCTION(apache_lookup_uri)
121 {
122 request_rec *rr;
123 char *filename;
124 size_t filename_len;
125
126 if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &filename, &filename_len) == FAILURE) {
127 RETURN_THROWS();
128 }
129
130 if (!(rr = php_apache_lookup_uri(filename))) {
131 php_error_docref(NULL, E_WARNING, "Unable to include '%s' - URI lookup failed", filename);
132 RETURN_FALSE;
133 }
134
135 if (rr->status == HTTP_OK) {
136 object_init(return_value);
137
138 ADD_LONG(status);
139 ADD_STRING(the_request);
140 ADD_STRING(status_line);
141 ADD_STRING(method);
142 ADD_TIME(mtime);
143 ADD_LONG(clength);
144 #if MODULE_MAGIC_NUMBER < 20020506
145 ADD_STRING(boundary);
146 #endif
147 ADD_STRING(range);
148 ADD_LONG(chunked);
149 ADD_STRING(content_type);
150 ADD_STRING(handler);
151 ADD_LONG(no_cache);
152 ADD_LONG(no_local_copy);
153 ADD_STRING(unparsed_uri);
154 ADD_STRING(uri);
155 ADD_STRING(filename);
156 ADD_STRING(path_info);
157 ADD_STRING(args);
158 ADD_LONG(allowed);
159 ADD_LONG(sent_bodyct);
160 ADD_LONG(bytes_sent);
161 ADD_LONG(mtime);
162 ADD_TIME(request_time);
163
164 ap_destroy_sub_req(rr);
165 return;
166 }
167
168 php_error_docref(NULL, E_WARNING, "Unable to include '%s' - error finding URI", filename);
169 ap_destroy_sub_req(rr);
170 RETURN_FALSE;
171 }
172
173 /* {{{ Fetch all HTTP request headers */
PHP_FUNCTION(apache_request_headers)174 PHP_FUNCTION(apache_request_headers)
175 {
176 php_struct *ctx;
177 const apr_array_header_t *arr;
178 char *key, *val;
179
180 if (zend_parse_parameters_none() == FAILURE) {
181 RETURN_THROWS();
182 }
183
184 array_init(return_value);
185
186 ctx = SG(server_context);
187 arr = apr_table_elts(ctx->r->headers_in);
188
189 APR_ARRAY_FOREACH_OPEN(arr, key, val)
190 if (!val) val = "";
191 add_assoc_string(return_value, key, val);
192 APR_ARRAY_FOREACH_CLOSE()
193 }
194 /* }}} */
195
196 /* {{{ Fetch all HTTP response headers */
PHP_FUNCTION(apache_response_headers)197 PHP_FUNCTION(apache_response_headers)
198 {
199 php_struct *ctx;
200 const apr_array_header_t *arr;
201 char *key, *val;
202
203 if (zend_parse_parameters_none() == FAILURE) {
204 RETURN_THROWS();
205 }
206
207 array_init(return_value);
208
209 ctx = SG(server_context);
210 arr = apr_table_elts(ctx->r->headers_out);
211
212 APR_ARRAY_FOREACH_OPEN(arr, key, val)
213 if (!val) val = "";
214 add_assoc_string(return_value, key, val);
215 APR_ARRAY_FOREACH_CLOSE()
216 }
217 /* }}} */
218
219 /* {{{ Get and set Apache request notes */
PHP_FUNCTION(apache_note)220 PHP_FUNCTION(apache_note)
221 {
222 php_struct *ctx;
223 char *note_name, *note_val = NULL;
224 size_t note_name_len, note_val_len;
225 char *old_note_val=NULL;
226
227 if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|s!", ¬e_name, ¬e_name_len, ¬e_val, ¬e_val_len) == FAILURE) {
228 RETURN_THROWS();
229 }
230
231 ctx = SG(server_context);
232
233 old_note_val = (char *) apr_table_get(ctx->r->notes, note_name);
234
235 if (note_val) {
236 apr_table_set(ctx->r->notes, note_name, note_val);
237 }
238
239 if (old_note_val) {
240 RETURN_STRING(old_note_val);
241 }
242
243 RETURN_FALSE;
244 }
245 /* }}} */
246
247
248 /* {{{ Set an Apache subprocess_env variable */
249 /*
250 * XXX this doesn't look right. shouldn't it be the parent ?*/
PHP_FUNCTION(apache_setenv)251 PHP_FUNCTION(apache_setenv)
252 {
253 php_struct *ctx;
254 char *variable=NULL, *string_val=NULL;
255 size_t variable_len, string_val_len;
256 zend_bool walk_to_top = 0;
257 int arg_count = ZEND_NUM_ARGS();
258 request_rec *r;
259
260 if (zend_parse_parameters(arg_count, "ss|b", &variable, &variable_len, &string_val, &string_val_len, &walk_to_top) == FAILURE) {
261 RETURN_THROWS();
262 }
263
264 ctx = SG(server_context);
265
266 r = ctx->r;
267 if (arg_count == 3) {
268 if (walk_to_top) {
269 while(r->prev) {
270 r = r->prev;
271 }
272 }
273 }
274
275 apr_table_set(r->subprocess_env, variable, string_val);
276
277 RETURN_TRUE;
278 }
279 /* }}} */
280
281 /* {{{ Get an Apache subprocess_env variable */
282 /*
283 * XXX: shouldn't this be the parent not the 'prev'
284 */
PHP_FUNCTION(apache_getenv)285 PHP_FUNCTION(apache_getenv)
286 {
287 php_struct *ctx;
288 char *variable;
289 size_t variable_len;
290 zend_bool walk_to_top = 0;
291 int arg_count = ZEND_NUM_ARGS();
292 char *env_val=NULL;
293 request_rec *r;
294
295 if (zend_parse_parameters(arg_count, "s|b", &variable, &variable_len, &walk_to_top) == FAILURE) {
296 RETURN_THROWS();
297 }
298
299 ctx = SG(server_context);
300
301 r = ctx->r;
302 if (arg_count == 2) {
303 if (walk_to_top) {
304 while(r->prev) {
305 r = r->prev;
306 }
307 }
308 }
309
310 env_val = (char*) apr_table_get(r->subprocess_env, variable);
311
312 if (env_val != NULL) {
313 RETURN_STRING(env_val);
314 }
315
316 RETURN_FALSE;
317 }
318 /* }}} */
319
php_apache_get_version()320 static char *php_apache_get_version()
321 {
322 #if MODULE_MAGIC_NUMBER_MAJOR >= 20060905
323 return (char *) ap_get_server_banner();
324 #else
325 return (char *) ap_get_server_version();
326 #endif
327 }
328
329 /* {{{ Fetch Apache version */
PHP_FUNCTION(apache_get_version)330 PHP_FUNCTION(apache_get_version)
331 {
332 char *apv = php_apache_get_version();
333
334 if (apv && *apv) {
335 RETURN_STRING(apv);
336 } else {
337 RETURN_FALSE;
338 }
339 }
340 /* }}} */
341
342 /* {{{ Get a list of loaded Apache modules */
PHP_FUNCTION(apache_get_modules)343 PHP_FUNCTION(apache_get_modules)
344 {
345 int n;
346 char *p;
347
348 array_init(return_value);
349
350 for (n = 0; ap_loaded_modules[n]; ++n) {
351 char *s = (char *) ap_loaded_modules[n]->name;
352 if ((p = strchr(s, '.'))) {
353 add_next_index_stringl(return_value, s, (p - s));
354 } else {
355 add_next_index_string(return_value, s);
356 }
357 }
358 }
359 /* }}} */
360
PHP_MINFO_FUNCTION(apache)361 PHP_MINFO_FUNCTION(apache)
362 {
363 char *apv = php_apache_get_version();
364 smart_str tmp1 = {0};
365 char tmp[1024];
366 int n, max_requests;
367 char *p;
368 server_rec *serv = ((php_struct *) SG(server_context))->r->server;
369 #ifndef PHP_WIN32
370 # if MODULE_MAGIC_NUMBER_MAJOR >= 20081201
371 AP_DECLARE_DATA extern unixd_config_rec ap_unixd_config;
372 # else
373 AP_DECLARE_DATA extern unixd_config_rec unixd_config;
374 # endif
375 #endif
376
377 for (n = 0; ap_loaded_modules[n]; ++n) {
378 char *s = (char *) ap_loaded_modules[n]->name;
379 if ((p = strchr(s, '.'))) {
380 smart_str_appendl(&tmp1, s, (p - s));
381 } else {
382 smart_str_appends(&tmp1, s);
383 }
384 smart_str_appendc(&tmp1, ' ');
385 }
386 if (tmp1.s) {
387 if (tmp1.s->len > 0) {
388 tmp1.s->val[tmp1.s->len - 1] = '\0';
389 } else {
390 tmp1.s->val[0] = '\0';
391 }
392 }
393
394 php_info_print_table_start();
395 if (apv && *apv) {
396 php_info_print_table_row(2, "Apache Version", apv);
397 }
398 snprintf(tmp, sizeof(tmp), "%d", MODULE_MAGIC_NUMBER);
399 php_info_print_table_row(2, "Apache API Version", tmp);
400
401 if (serv->server_admin && *(serv->server_admin)) {
402 php_info_print_table_row(2, "Server Administrator", serv->server_admin);
403 }
404
405 snprintf(tmp, sizeof(tmp), "%s:%u", serv->server_hostname, serv->port);
406 php_info_print_table_row(2, "Hostname:Port", tmp);
407
408 #ifndef PHP_WIN32
409 #if MODULE_MAGIC_NUMBER_MAJOR >= 20081201
410 snprintf(tmp, sizeof(tmp), "%s(%d)/%d", ap_unixd_config.user_name, ap_unixd_config.user_id, ap_unixd_config.group_id);
411 #else
412 snprintf(tmp, sizeof(tmp), "%s(%d)/%d", unixd_config.user_name, unixd_config.user_id, unixd_config.group_id);
413 #endif
414 php_info_print_table_row(2, "User/Group", tmp);
415 #endif
416
417 ap_mpm_query(AP_MPMQ_MAX_REQUESTS_DAEMON, &max_requests);
418 snprintf(tmp, sizeof(tmp), "Per Child: %d - Keep Alive: %s - Max Per Connection: %d", max_requests, (serv->keep_alive ? "on":"off"), serv->keep_alive_max);
419 php_info_print_table_row(2, "Max Requests", tmp);
420
421 apr_snprintf(tmp, sizeof tmp,
422 "Connection: %" APR_TIME_T_FMT " - Keep-Alive: %" APR_TIME_T_FMT,
423 apr_time_sec(serv->timeout), apr_time_sec(serv->keep_alive_timeout));
424 php_info_print_table_row(2, "Timeouts", tmp);
425
426 php_info_print_table_row(2, "Virtual Server", (serv->is_virtual ? "Yes" : "No"));
427 php_info_print_table_row(2, "Server Root", ap_server_root);
428 php_info_print_table_row(2, "Loaded Modules", tmp1.s->val);
429
430 smart_str_free(&tmp1);
431 php_info_print_table_end();
432
433 DISPLAY_INI_ENTRIES();
434
435 {
436 const apr_array_header_t *arr = apr_table_elts(((php_struct *) SG(server_context))->r->subprocess_env);
437 char *key, *val;
438
439 SECTION("Apache Environment");
440 php_info_print_table_start();
441 php_info_print_table_header(2, "Variable", "Value");
442 APR_ARRAY_FOREACH_OPEN(arr, key, val)
443 if (!val) {
444 val = "";
445 }
446 php_info_print_table_row(2, key, val);
447 APR_ARRAY_FOREACH_CLOSE()
448
449 php_info_print_table_end();
450
451 SECTION("HTTP Headers Information");
452 php_info_print_table_start();
453 php_info_print_table_colspan_header(2, "HTTP Request Headers");
454 php_info_print_table_row(2, "HTTP Request", ((php_struct *) SG(server_context))->r->the_request);
455
456 arr = apr_table_elts(((php_struct *) SG(server_context))->r->headers_in);
457 APR_ARRAY_FOREACH_OPEN(arr, key, val)
458 if (!val) {
459 val = "";
460 }
461 php_info_print_table_row(2, key, val);
462 APR_ARRAY_FOREACH_CLOSE()
463
464 php_info_print_table_colspan_header(2, "HTTP Response Headers");
465 arr = apr_table_elts(((php_struct *) SG(server_context))->r->headers_out);
466 APR_ARRAY_FOREACH_OPEN(arr, key, val)
467 if (!val) {
468 val = "";
469 }
470 php_info_print_table_row(2, key, val);
471 APR_ARRAY_FOREACH_CLOSE()
472
473 php_info_print_table_end();
474 }
475 }
476
477 PHP_INI_BEGIN()
478 STD_PHP_INI_BOOLEAN("xbithack", "0", PHP_INI_ALL, OnUpdateBool, xbithack, php_apache2_info_struct, php_apache2_info)
479 STD_PHP_INI_BOOLEAN("engine", "1", PHP_INI_ALL, OnUpdateBool, engine, php_apache2_info_struct, php_apache2_info)
480 STD_PHP_INI_BOOLEAN("last_modified", "0", PHP_INI_ALL, OnUpdateBool, last_modified, php_apache2_info_struct, php_apache2_info)
PHP_INI_END()481 PHP_INI_END()
482
483 static PHP_MINIT_FUNCTION(apache)
484 {
485 #ifdef ZTS
486 ts_allocate_id(&php_apache2_info_id, sizeof(php_apache2_info_struct), (ts_allocate_ctor) NULL, NULL);
487 #endif
488 REGISTER_INI_ENTRIES();
489 return SUCCESS;
490 }
491
PHP_MSHUTDOWN_FUNCTION(apache)492 static PHP_MSHUTDOWN_FUNCTION(apache)
493 {
494 UNREGISTER_INI_ENTRIES();
495 return SUCCESS;
496 }
497
498 zend_module_entry php_apache_module = {
499 STANDARD_MODULE_HEADER,
500 "apache2handler",
501 ext_functions,
502 PHP_MINIT(apache),
503 PHP_MSHUTDOWN(apache),
504 NULL,
505 NULL,
506 PHP_MINFO(apache),
507 PHP_VERSION,
508 STANDARD_MODULE_PROPERTIES
509 };
510