1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 5 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2014 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 | Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
16 | Stig S�ther Bakken <ssb@php.net> |
17 | David Sklar <sklar@student.net> |
18 +----------------------------------------------------------------------+
19 */
20 /* $Id$ */
21
22 #include "php_apache_http.h"
23
24 #if defined(PHP_WIN32) || defined(NETWARE)
25 #include "zend.h"
26 #include "ap_compat.h"
27 #endif
28
29 #ifdef ZTS
30 int php_apache_info_id;
31 #else
32 php_apache_info_struct php_apache_info;
33 #endif
34
35 #define SECTION(name) PUTS("<h2>" name "</h2>\n")
36
37 #ifndef PHP_WIN32
38 extern module *top_module;
39 extern module **ap_loaded_modules;
40 #else
41 extern __declspec(dllimport) module *top_module;
42 extern __declspec(dllimport) module **ap_loaded_modules;
43 #endif
44
45 PHP_FUNCTION(virtual);
46 PHP_FUNCTION(apache_request_headers);
47 PHP_FUNCTION(apache_response_headers);
48 PHP_FUNCTION(apachelog);
49 PHP_FUNCTION(apache_note);
50 PHP_FUNCTION(apache_lookup_uri);
51 PHP_FUNCTION(apache_child_terminate);
52 PHP_FUNCTION(apache_setenv);
53 PHP_FUNCTION(apache_get_version);
54 PHP_FUNCTION(apache_get_modules);
55 PHP_FUNCTION(apache_reset_timeout);
56
57 PHP_MINFO_FUNCTION(apache);
58
59 ZEND_BEGIN_ARG_INFO(arginfo_apache_child_terminate, 0)
60 ZEND_END_ARG_INFO()
61
62 ZEND_BEGIN_ARG_INFO_EX(arginfo_apache_note, 0, 0, 1)
63 ZEND_ARG_INFO(0, note_name)
64 ZEND_ARG_INFO(0, note_value)
65 ZEND_END_ARG_INFO()
66
67 ZEND_BEGIN_ARG_INFO_EX(arginfo_apache_virtual, 0, 0, 1)
68 ZEND_ARG_INFO(0, filename)
69 ZEND_END_ARG_INFO()
70
71 ZEND_BEGIN_ARG_INFO(arginfo_apache_request_headers, 0)
72 ZEND_END_ARG_INFO()
73
74 ZEND_BEGIN_ARG_INFO(arginfo_apache_response_headers, 0)
75 ZEND_END_ARG_INFO()
76
77 ZEND_BEGIN_ARG_INFO_EX(arginfo_apache_setenv, 0, 0, 2)
78 ZEND_ARG_INFO(0, variable)
79 ZEND_ARG_INFO(0, value)
80 ZEND_ARG_INFO(0, walk_to_top)
81 ZEND_END_ARG_INFO()
82
83 ZEND_BEGIN_ARG_INFO_EX(arginfo_apache_lookup_uri, 0, 0, 1)
84 ZEND_ARG_INFO(0, uri)
85 ZEND_END_ARG_INFO()
86
87 ZEND_BEGIN_ARG_INFO(arginfo_apache_get_version, 0)
88 ZEND_END_ARG_INFO()
89
90 ZEND_BEGIN_ARG_INFO(arginfo_apache_get_modules, 0)
91 ZEND_END_ARG_INFO()
92
93 ZEND_BEGIN_ARG_INFO(arginfo_apache_reset_timeout, 0)
94 ZEND_END_ARG_INFO()
95
96
97
98 const zend_function_entry apache_functions[] = {
99 PHP_FE(virtual, arginfo_apache_virtual)
100 PHP_FE(apache_request_headers, arginfo_apache_request_headers)
101 PHP_FE(apache_note, arginfo_apache_note)
102 PHP_FE(apache_lookup_uri, arginfo_apache_lookup_uri)
103 PHP_FE(apache_child_terminate, arginfo_apache_child_terminate)
104 PHP_FE(apache_setenv, arginfo_apache_setenv)
105 PHP_FE(apache_response_headers, arginfo_apache_response_headers)
106 PHP_FE(apache_get_version, arginfo_apache_get_version)
107 PHP_FE(apache_get_modules, arginfo_apache_get_modules)
108 PHP_FE(apache_reset_timeout, arginfo_apache_reset_timeout)
109 PHP_FALIAS(getallheaders, apache_request_headers, arginfo_apache_request_headers)
110 {NULL, NULL, NULL}
111 };
112
113
114 PHP_INI_BEGIN()
115 STD_PHP_INI_ENTRY("xbithack", "0", PHP_INI_ALL, OnUpdateLong, xbithack, php_apache_info_struct, php_apache_info)
116 STD_PHP_INI_ENTRY("engine", "1", PHP_INI_ALL, OnUpdateLong, engine, php_apache_info_struct, php_apache_info)
117 STD_PHP_INI_ENTRY("last_modified", "0", PHP_INI_ALL, OnUpdateLong, last_modified, php_apache_info_struct, php_apache_info)
118 STD_PHP_INI_ENTRY("child_terminate", "0", PHP_INI_ALL, OnUpdateLong, terminate_child, php_apache_info_struct, php_apache_info)
PHP_INI_END()119 PHP_INI_END()
120
121
122
123 static void php_apache_globals_ctor(php_apache_info_struct *apache_globals TSRMLS_DC)
124 {
125 apache_globals->in_request = 0;
126 }
127
128
PHP_MINIT_FUNCTION(apache)129 static PHP_MINIT_FUNCTION(apache)
130 {
131 #ifdef ZTS
132 ts_allocate_id(&php_apache_info_id, sizeof(php_apache_info_struct), (ts_allocate_ctor) php_apache_globals_ctor, NULL);
133 #else
134 php_apache_globals_ctor(&php_apache_info TSRMLS_CC);
135 #endif
136 REGISTER_INI_ENTRIES();
137 return SUCCESS;
138 }
139
140
PHP_MSHUTDOWN_FUNCTION(apache)141 static PHP_MSHUTDOWN_FUNCTION(apache)
142 {
143 UNREGISTER_INI_ENTRIES();
144 return SUCCESS;
145 }
146
147 zend_module_entry apache_module_entry = {
148 STANDARD_MODULE_HEADER,
149 "apache",
150 apache_functions,
151 PHP_MINIT(apache),
152 PHP_MSHUTDOWN(apache),
153 NULL,
154 NULL,
155 PHP_MINFO(apache),
156 NO_VERSION_YET,
157 STANDARD_MODULE_PROPERTIES
158 };
159
160 /* {{{ PHP_MINFO_FUNCTION
161 */
PHP_MINFO_FUNCTION(apache)162 PHP_MINFO_FUNCTION(apache)
163 {
164 char *apv = (char *) ap_get_server_version();
165 module *modp = NULL;
166 char output_buf[128];
167 #if !defined(WIN32) && !defined(WINNT)
168 char name[64];
169 char modulenames[1024];
170 char *p;
171 #endif
172 server_rec *serv;
173 extern char server_root[MAX_STRING_LEN];
174 extern uid_t user_id;
175 extern char *user_name;
176 extern gid_t group_id;
177 extern int max_requests_per_child;
178
179 serv = ((request_rec *) SG(server_context))->server;
180
181
182 php_info_print_table_start();
183
184 #ifdef PHP_WIN32
185 php_info_print_table_row(1, "Apache for Windows 95/NT");
186 php_info_print_table_end();
187 php_info_print_table_start();
188 #elif defined(NETWARE)
189 php_info_print_table_row(1, "Apache for NetWare");
190 php_info_print_table_end();
191 php_info_print_table_start();
192 #else
193 php_info_print_table_row(2, "APACHE_INCLUDE", PHP_APACHE_INCLUDE);
194 php_info_print_table_row(2, "APACHE_TARGET", PHP_APACHE_TARGET);
195 #endif
196
197 if (apv && *apv) {
198 php_info_print_table_row(2, "Apache Version", apv);
199 }
200
201 #ifdef APACHE_RELEASE
202 snprintf(output_buf, sizeof(output_buf), "%d", APACHE_RELEASE);
203 php_info_print_table_row(2, "Apache Release", output_buf);
204 #endif
205 snprintf(output_buf, sizeof(output_buf), "%d", MODULE_MAGIC_NUMBER);
206 php_info_print_table_row(2, "Apache API Version", output_buf);
207 snprintf(output_buf, sizeof(output_buf), "%s:%u", serv->server_hostname, serv->port);
208 php_info_print_table_row(2, "Hostname:Port", output_buf);
209 #if !defined(WIN32) && !defined(WINNT)
210 snprintf(output_buf, sizeof(output_buf), "%s(%d)/%d", user_name, (int)user_id, (int)group_id);
211 php_info_print_table_row(2, "User/Group", output_buf);
212 snprintf(output_buf, sizeof(output_buf), "Per Child: %d - Keep Alive: %s - Max Per Connection: %d", max_requests_per_child, serv->keep_alive ? "on":"off", serv->keep_alive_max);
213 php_info_print_table_row(2, "Max Requests", output_buf);
214 #endif
215 snprintf(output_buf, sizeof(output_buf), "Connection: %d - Keep-Alive: %d", serv->timeout, serv->keep_alive_timeout);
216 php_info_print_table_row(2, "Timeouts", output_buf);
217 #if !defined(WIN32) && !defined(WINNT)
218 /*
219 This block seems to be working on NetWare; But it seems to be showing
220 all modules instead of just the loaded ones
221 */
222 php_info_print_table_row(2, "Server Root", server_root);
223
224 strcpy(modulenames, "");
225 for(modp = top_module; modp; modp = modp->next) {
226 strlcpy(name, modp->name, sizeof(name));
227 if ((p = strrchr(name, '.'))) {
228 *p='\0'; /* Cut off ugly .c extensions on module names */
229 }
230 strlcat(modulenames, name, sizeof(modulenames));
231 if (modp->next) {
232 strlcat(modulenames, ", ", sizeof(modulenames));
233 }
234 }
235 php_info_print_table_row(2, "Loaded Modules", modulenames);
236 #endif
237
238 php_info_print_table_end();
239
240 DISPLAY_INI_ENTRIES();
241
242 {
243 register int i;
244 array_header *arr;
245 table_entry *elts;
246 request_rec *r;
247
248 r = ((request_rec *) SG(server_context));
249 arr = table_elts(r->subprocess_env);
250 elts = (table_entry *)arr->elts;
251
252 SECTION("Apache Environment");
253 php_info_print_table_start();
254 php_info_print_table_header(2, "Variable", "Value");
255 for (i=0; i < arr->nelts; i++) {
256 php_info_print_table_row(2, elts[i].key, elts[i].val);
257 }
258 php_info_print_table_end();
259 }
260
261 {
262 array_header *env_arr;
263 table_entry *env;
264 int i;
265 request_rec *r;
266
267 r = ((request_rec *) SG(server_context));
268 SECTION("HTTP Headers Information");
269 php_info_print_table_start();
270 php_info_print_table_colspan_header(2, "HTTP Request Headers");
271 php_info_print_table_row(2, "HTTP Request", r->the_request);
272 env_arr = table_elts(r->headers_in);
273 env = (table_entry *)env_arr->elts;
274 for (i = 0; i < env_arr->nelts; ++i) {
275 if (env[i].key) {
276 php_info_print_table_row(2, env[i].key, env[i].val);
277 }
278 }
279 php_info_print_table_colspan_header(2, "HTTP Response Headers");
280 env_arr = table_elts(r->headers_out);
281 env = (table_entry *)env_arr->elts;
282 for(i = 0; i < env_arr->nelts; ++i) {
283 if (env[i].key) {
284 php_info_print_table_row(2, env[i].key, env[i].val);
285 }
286 }
287 php_info_print_table_end();
288 }
289 }
290 /* }}} */
291
292 /* {{{ proto bool apache_child_terminate(void)
293 Terminate apache process after this request */
PHP_FUNCTION(apache_child_terminate)294 PHP_FUNCTION(apache_child_terminate)
295 {
296 #ifndef MULTITHREAD
297 if (AP(terminate_child)) {
298 ap_child_terminate( ((request_rec *)SG(server_context)) );
299 RETURN_TRUE;
300 } else { /* tell them to get lost! */
301 php_error_docref(NULL TSRMLS_CC, E_WARNING, "This function is disabled");
302 RETURN_FALSE;
303 }
304 #else
305 php_error_docref(NULL TSRMLS_CC, E_WARNING, "This function is not supported in this build");
306 RETURN_FALSE;
307 #endif
308 }
309 /* }}} */
310
311 /* {{{ proto string apache_note(string note_name [, string note_value])
312 Get and set Apache request notes */
PHP_FUNCTION(apache_note)313 PHP_FUNCTION(apache_note)
314 {
315 char *note_name, *note_val = NULL;
316 int note_name_len, note_val_len;
317 char *old_val;
318
319 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", ¬e_name, ¬e_name_len, ¬e_val, ¬e_val_len) == FAILURE) {
320 return;
321 }
322
323 old_val = (char *) table_get(((request_rec *)SG(server_context))->notes, note_name);
324
325 if (note_val) {
326 table_set(((request_rec *)SG(server_context))->notes, note_name, note_val);
327 }
328
329 if (old_val) {
330 RETURN_STRING(old_val, 1);
331 }
332
333 RETURN_FALSE;
334 }
335 /* }}} */
336
337 /* {{{ proto bool virtual(string filename)
338 Perform an Apache sub-request */
339 /* This function is equivalent to <!--#include virtual...-->
340 * in mod_include. It does an Apache sub-request. It is useful
341 * for including CGI scripts or .shtml files, or anything else
342 * that you'd parse through Apache (for .phtml files, you'd probably
343 * want to use <?Include>. This only works when PHP is compiled
344 * as an Apache module, since it uses the Apache API for doing
345 * sub requests.
346 */
PHP_FUNCTION(virtual)347 PHP_FUNCTION(virtual)
348 {
349 char *filename;
350 int filename_len;
351 request_rec *rr = NULL;
352
353 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p", &filename, &filename_len) == FAILURE) {
354 return;
355 }
356
357 if (!(rr = sub_req_lookup_uri (filename, ((request_rec *) SG(server_context))))) {
358 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to include '%s' - URI lookup failed", filename);
359 if (rr)
360 destroy_sub_req (rr);
361 RETURN_FALSE;
362 }
363
364 if (rr->status != 200) {
365 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to include '%s' - error finding URI", filename);
366 if (rr)
367 destroy_sub_req (rr);
368 RETURN_FALSE;
369 }
370
371 php_output_end_all(TSRMLS_C);
372 php_header(TSRMLS_C);
373
374 if (run_sub_req(rr)) {
375 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to include '%s' - request execution failed", filename);
376 if (rr)
377 destroy_sub_req (rr);
378 RETURN_FALSE;
379 }
380
381 if (rr)
382 destroy_sub_req (rr);
383
384 RETURN_TRUE;
385 }
386 /* }}} */
387
388 /* {{{ proto array getallheaders(void)
389 Alias for apache_request_headers() */
390 /* }}} */
391
392 /* {{{ proto array apache_request_headers(void)
393 Fetch all HTTP request headers */
PHP_FUNCTION(apache_request_headers)394 PHP_FUNCTION(apache_request_headers)
395 {
396 array_header *env_arr;
397 table_entry *tenv;
398 int i;
399
400 array_init(return_value);
401 env_arr = table_elts(((request_rec *) SG(server_context))->headers_in);
402 tenv = (table_entry *)env_arr->elts;
403 for (i = 0; i < env_arr->nelts; ++i) {
404 if (!tenv[i].key) {
405 continue;
406 }
407 if (add_assoc_string(return_value, tenv[i].key, (tenv[i].val==NULL) ? "" : tenv[i].val, 1)==FAILURE) {
408 RETURN_FALSE;
409 }
410 }
411 }
412 /* }}} */
413
414 /* {{{ proto array apache_response_headers(void)
415 Fetch all HTTP response headers */
PHP_FUNCTION(apache_response_headers)416 PHP_FUNCTION(apache_response_headers)
417 {
418 array_header *env_arr;
419 table_entry *tenv;
420 int i;
421
422 array_init(return_value);
423 env_arr = table_elts(((request_rec *) SG(server_context))->headers_out);
424 tenv = (table_entry *)env_arr->elts;
425 for (i = 0; i < env_arr->nelts; ++i) {
426 if (!tenv[i].key) continue;
427 if (add_assoc_string(return_value, tenv[i].key, (tenv[i].val==NULL) ? "" : tenv[i].val, 1)==FAILURE) {
428 RETURN_FALSE;
429 }
430 }
431 }
432 /* }}} */
433
434 /* {{{ proto bool apache_setenv(string variable, string value [, bool walk_to_top])
435 Set an Apache subprocess_env variable */
PHP_FUNCTION(apache_setenv)436 PHP_FUNCTION(apache_setenv)
437 {
438 int var_len, val_len;
439 zend_bool top=0;
440 char *var = NULL, *val = NULL;
441 request_rec *r = (request_rec *) SG(server_context);
442
443 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|b", &var, &var_len, &val, &val_len, &top) == FAILURE) {
444 return;
445 }
446
447 while(top) {
448 if(r->prev) r = r->prev;
449 else break;
450 }
451
452 ap_table_setn(r->subprocess_env, ap_pstrndup(r->pool, var, var_len), ap_pstrndup(r->pool, val, val_len));
453 RETURN_TRUE;
454 }
455 /* }}} */
456
457 /* {{{ proto object apache_lookup_uri(string URI)
458 Perform a partial request of the given URI to obtain information about it */
PHP_FUNCTION(apache_lookup_uri)459 PHP_FUNCTION(apache_lookup_uri)
460 {
461 char *filename;
462 int filename_len;
463 request_rec *rr=NULL;
464
465 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE) {
466 return;
467 }
468
469 if (!(rr = sub_req_lookup_uri(filename, ((request_rec *) SG(server_context))))) {
470 php_error_docref(NULL TSRMLS_CC, E_WARNING, "URI lookup failed '%s'", filename);
471 RETURN_FALSE;
472 }
473
474 object_init(return_value);
475 add_property_long(return_value,"status", rr->status);
476
477 if (rr->the_request) {
478 add_property_string(return_value,"the_request", rr->the_request, 1);
479 }
480 if (rr->status_line) {
481 add_property_string(return_value,"status_line", (char *)rr->status_line, 1);
482 }
483 if (rr->method) {
484 add_property_string(return_value,"method", (char *)rr->method, 1);
485 }
486 if (rr->content_type) {
487 add_property_string(return_value,"content_type", (char *)rr->content_type, 1);
488 }
489 if (rr->handler) {
490 add_property_string(return_value,"handler", (char *)rr->handler, 1);
491 }
492 if (rr->uri) {
493 add_property_string(return_value,"uri", rr->uri, 1);
494 }
495 if (rr->filename) {
496 add_property_string(return_value,"filename", rr->filename, 1);
497 }
498 if (rr->path_info) {
499 add_property_string(return_value,"path_info", rr->path_info, 1);
500 }
501 if (rr->args) {
502 add_property_string(return_value,"args", rr->args, 1);
503 }
504 if (rr->boundary) {
505 add_property_string(return_value,"boundary", rr->boundary, 1);
506 }
507
508 add_property_long(return_value,"no_cache", rr->no_cache);
509 add_property_long(return_value,"no_local_copy", rr->no_local_copy);
510 add_property_long(return_value,"allowed", rr->allowed);
511 add_property_long(return_value,"sent_bodyct", rr->sent_bodyct);
512 add_property_long(return_value,"bytes_sent", rr->bytes_sent);
513 add_property_long(return_value,"byterange", rr->byterange);
514 add_property_long(return_value,"clength", rr->clength);
515
516 #if MODULE_MAGIC_NUMBER >= 19980324
517 if (rr->unparsed_uri) {
518 add_property_string(return_value,"unparsed_uri", rr->unparsed_uri, 1);
519 }
520 if(rr->mtime) {
521 add_property_long(return_value,"mtime", rr->mtime);
522 }
523 #endif
524 if(rr->request_time) {
525 add_property_long(return_value,"request_time", rr->request_time);
526 }
527
528 destroy_sub_req(rr);
529 }
530 /* }}} */
531
532
533 #if 0
534 /*
535 This function is most likely a bad idea. Just playing with it for now.
536 */
537 PHP_FUNCTION(apache_exec_uri)
538 {
539 char *filename;
540 int filename_len;
541 request_rec *rr=NULL;
542
543 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE) {
544 return;
545 }
546
547 if(!(rr = ap_sub_req_lookup_uri(filename, ((request_rec *) SG(server_context))))) {
548 php_error_docref(NULL TSRMLS_CC, E_WARNING, "URI lookup failed", filename);
549 RETURN_FALSE;
550 }
551
552 RETVAL_LONG(ap_run_sub_req(rr));
553 ap_destroy_sub_req(rr);
554 }
555 #endif
556
557 /* {{{ proto string apache_get_version(void)
558 Fetch Apache version */
PHP_FUNCTION(apache_get_version)559 PHP_FUNCTION(apache_get_version)
560 {
561 char *apv = (char *) ap_get_server_version();
562
563 if (apv && *apv) {
564 RETURN_STRING(apv, 1);
565 }
566
567 RETURN_FALSE;
568 }
569 /* }}} */
570
571 /* {{{ proto array apache_get_modules(void)
572 Get a list of loaded Apache modules */
PHP_FUNCTION(apache_get_modules)573 PHP_FUNCTION(apache_get_modules)
574 {
575 int n;
576 char *p;
577
578 array_init(return_value);
579
580 for (n = 0; ap_loaded_modules[n]; ++n) {
581 char *s = (char *) ap_loaded_modules[n]->name;
582 if ((p = strchr(s, '.'))) {
583 add_next_index_stringl(return_value, s, (p - s), 1);
584 } else {
585 add_next_index_string(return_value, s, 1);
586 }
587 }
588 }
589 /* }}} */
590
591 /* {{{ proto bool apache_reset_timeout(void)
592 Reset the Apache write timer */
PHP_FUNCTION(apache_reset_timeout)593 PHP_FUNCTION(apache_reset_timeout)
594 {
595 ap_reset_timeout((request_rec *)SG(server_context));
596 RETURN_TRUE;
597 }
598 /* }}} */
599
600 /*
601 * Local variables:
602 * tab-width: 4
603 * c-basic-offset: 4
604 * End:
605 * vim600: sw=4 ts=4 fdm=marker
606 * vim<600: sw=4 ts=4
607 */
608