php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
php_functions.c
Go to the documentation of this file.
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 | https://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#include "apr_strings.h"
33#include "apr_time.h"
34#include "ap_config.h"
35#include "util_filter.h"
36#include "httpd.h"
37#include "http_config.h"
38#include "http_request.h"
39#include "http_core.h"
40#include "http_protocol.h"
41#include "http_log.h"
42#include "http_main.h"
43#include "util_script.h"
44#include "http_core.h"
45#include "ap_mpm.h"
46#ifndef PHP_WIN32
47#include "unixd.h"
48#endif
49
50#include "php_apache.h"
52
53#ifdef ZTS
54int php_apache2_info_id;
55#else
57#endif
58
59#define SECTION(name) PUTS("<h2>" name "</h2>\n")
60
61static request_rec *php_apache_lookup_uri(char *filename)
62{
63 php_struct *ctx = SG(server_context);
64
65 if (!filename || !ctx || !ctx->r) {
66 return NULL;
67 }
68
69 return ap_sub_req_lookup_uri(filename, ctx->r, ctx->r->output_filters);
70}
71
72/* {{{ Perform an apache sub-request */
74{
75 char *filename;
76 size_t filename_len;
77 request_rec *rr;
78
79 if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &filename, &filename_len) == FAILURE) {
81 }
82
83 if (!(rr = php_apache_lookup_uri(filename))) {
84 php_error_docref(NULL, E_WARNING, "Unable to include '%s' - URI lookup failed", filename);
86 }
87
88 if (rr->status != HTTP_OK) {
89 php_error_docref(NULL, E_WARNING, "Unable to include '%s' - error finding URI", filename);
90 ap_destroy_sub_req(rr);
92 }
93
94 /* Flush everything. */
96 php_header();
97
98 /* Ensure that the ap_r* layer for the main request is flushed, to
99 * work around http://issues.apache.org/bugzilla/show_bug.cgi?id=17629 */
100 ap_rflush(rr->main);
101
102 if (ap_run_sub_req(rr)) {
103 php_error_docref(NULL, E_WARNING, "Unable to include '%s' - request execution failed", filename);
104 ap_destroy_sub_req(rr);
106 }
107 ap_destroy_sub_req(rr);
109}
110/* }}} */
111
112#define ADD_LONG(name) \
113 add_property_long(return_value, #name, rr->name)
114#define ADD_TIME(name) \
115 add_property_long(return_value, #name, apr_time_sec(rr->name));
116#define ADD_STRING(name) \
117 if (rr->name) add_property_string(return_value, #name, (char *) rr->name)
118
120{
121 request_rec *rr;
122 char *filename;
123 size_t filename_len;
124
125 if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &filename, &filename_len) == FAILURE) {
127 }
128
129 if (!(rr = php_apache_lookup_uri(filename))) {
130 php_error_docref(NULL, E_WARNING, "Unable to include '%s' - URI lookup failed", filename);
132 }
133
134 if (rr->status == HTTP_OK) {
136
138 ADD_STRING(the_request);
139 ADD_STRING(status_line);
140 ADD_STRING(method);
141 ADD_TIME(mtime);
142 ADD_LONG(clength);
143 ADD_STRING(range);
144 ADD_LONG(chunked);
145 ADD_STRING(content_type);
147 ADD_LONG(no_cache);
148 ADD_LONG(no_local_copy);
149 ADD_STRING(unparsed_uri);
150 ADD_STRING(uri);
151 ADD_STRING(filename);
152 ADD_STRING(path_info);
154 ADD_LONG(allowed);
155 ADD_LONG(sent_bodyct);
156 ADD_LONG(bytes_sent);
157 ADD_LONG(mtime);
158 ADD_TIME(request_time);
159
160 ap_destroy_sub_req(rr);
161 return;
162 }
163
164 php_error_docref(NULL, E_WARNING, "Unable to include '%s' - error finding URI", filename);
165 ap_destroy_sub_req(rr);
167}
168
169/* {{{ Fetch all HTTP request headers */
171{
172 php_struct *ctx;
173 const apr_array_header_t *arr;
174 char *key, *val;
175
178 }
179
181
182 ctx = SG(server_context);
183 arr = apr_table_elts(ctx->r->headers_in);
184
186 if (!val) val = "";
187 add_assoc_string(return_value, key, val);
189}
190/* }}} */
191
192/* {{{ Fetch all HTTP response headers */
194{
195 php_struct *ctx;
196 const apr_array_header_t *arr;
197 char *key, *val;
198
201 }
202
204
205 ctx = SG(server_context);
206 arr = apr_table_elts(ctx->r->headers_out);
207
209 if (!val) val = "";
210 add_assoc_string(return_value, key, val);
212}
213/* }}} */
214
215/* {{{ Get and set Apache request notes */
217{
218 php_struct *ctx;
219 char *note_name, *note_val = NULL;
220 size_t note_name_len, note_val_len;
221 char *old_note_val=NULL;
222
223 if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|s!", &note_name, &note_name_len, &note_val, &note_val_len) == FAILURE) {
225 }
226
227 ctx = SG(server_context);
228
229 old_note_val = (char *) apr_table_get(ctx->r->notes, note_name);
230
231 if (note_val) {
232 apr_table_set(ctx->r->notes, note_name, note_val);
233 }
234
235 if (old_note_val) {
236 RETURN_STRING(old_note_val);
237 }
238
240}
241/* }}} */
242
243
244/* {{{ Set an Apache subprocess_env variable */
245/*
246 * XXX this doesn't look right. shouldn't it be the parent ?*/
248{
249 php_struct *ctx;
250 char *variable=NULL, *string_val=NULL;
251 size_t variable_len, string_val_len;
252 bool walk_to_top = 0;
253 int arg_count = ZEND_NUM_ARGS();
254 request_rec *r;
255
256 if (zend_parse_parameters(arg_count, "ss|b", &variable, &variable_len, &string_val, &string_val_len, &walk_to_top) == FAILURE) {
258 }
259
260 ctx = SG(server_context);
261
262 r = ctx->r;
263 if (arg_count == 3) {
264 if (walk_to_top) {
265 while(r->prev) {
266 r = r->prev;
267 }
268 }
269 }
270
271 apr_table_set(r->subprocess_env, variable, string_val);
272
274}
275/* }}} */
276
277/* {{{ Get an Apache subprocess_env variable */
278/*
279 * XXX: shouldn't this be the parent not the 'prev'
280 */
282{
283 php_struct *ctx;
284 char *variable;
285 size_t variable_len;
286 bool walk_to_top = 0;
287 int arg_count = ZEND_NUM_ARGS();
288 char *env_val=NULL;
289 request_rec *r;
290
291 if (zend_parse_parameters(arg_count, "s|b", &variable, &variable_len, &walk_to_top) == FAILURE) {
293 }
294
295 ctx = SG(server_context);
296
297 r = ctx->r;
298 if (arg_count == 2) {
299 if (walk_to_top) {
300 while(r->prev) {
301 r = r->prev;
302 }
303 }
304 }
305
306 env_val = (char*) apr_table_get(r->subprocess_env, variable);
307
308 if (env_val != NULL) {
309 RETURN_STRING(env_val);
310 }
311
313}
314/* }}} */
315
316static const char *php_apache_get_version(void)
317{
318 return ap_get_server_banner();
319}
320
321/* {{{ Fetch Apache version */
323{
324 const char *apv = php_apache_get_version();
325
326 if (apv && *apv) {
327 RETURN_STRING(apv);
328 } else {
330 }
331}
332/* }}} */
333
334/* {{{ Get a list of loaded Apache modules */
336{
337 int n;
338 char *p;
339
341
342 for (n = 0; ap_loaded_modules[n]; ++n) {
343 const char *s = ap_loaded_modules[n]->name;
344 if ((p = strchr(s, '.'))) {
346 } else {
348 }
349 }
350}
351/* }}} */
352
354{
355 const char *apv = php_apache_get_version();
356 smart_str tmp1 = {0};
357 char tmp[1024];
358 int n, max_requests;
359 char *p;
360 server_rec *serv = ((php_struct *) SG(server_context))->r->server;
361#ifndef PHP_WIN32
362 AP_DECLARE_DATA extern unixd_config_rec ap_unixd_config;
363#endif
364
365 for (n = 0; ap_loaded_modules[n]; ++n) {
366 const char *s = ap_loaded_modules[n]->name;
367 if (n > 0) {
368 smart_str_appendc(&tmp1, ' ');
369 }
370 if ((p = strchr(s, '.'))) {
371 smart_str_appendl(&tmp1, s, (p - s));
372 } else {
373 smart_str_appends(&tmp1, s);
374 }
375 }
376 if (!tmp1.s) {
377 smart_str_appendc(&tmp1, '/');
378 }
379 smart_str_0(&tmp1);
380
382 if (apv && *apv) {
383 php_info_print_table_row(2, "Apache Version", apv);
384 }
385 snprintf(tmp, sizeof(tmp), "%d", MODULE_MAGIC_NUMBER_MAJOR);
386 php_info_print_table_row(2, "Apache API Version", tmp);
387
388 if (serv->server_admin && *(serv->server_admin)) {
389 php_info_print_table_row(2, "Server Administrator", serv->server_admin);
390 }
391
392 snprintf(tmp, sizeof(tmp), "%s:%u", serv->server_hostname, serv->port);
393 php_info_print_table_row(2, "Hostname:Port", tmp);
394
395#ifndef PHP_WIN32
396 snprintf(tmp, sizeof(tmp), "%s(%d)/%d", ap_unixd_config.user_name, ap_unixd_config.user_id, ap_unixd_config.group_id);
397 php_info_print_table_row(2, "User/Group", tmp);
398#endif
399
400 ap_mpm_query(AP_MPMQ_MAX_REQUESTS_DAEMON, &max_requests);
401 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);
402 php_info_print_table_row(2, "Max Requests", tmp);
403
404 apr_snprintf(tmp, sizeof tmp,
405 "Connection: %" APR_TIME_T_FMT " - Keep-Alive: %" APR_TIME_T_FMT,
406 apr_time_sec(serv->timeout), apr_time_sec(serv->keep_alive_timeout));
407 php_info_print_table_row(2, "Timeouts", tmp);
408
409 php_info_print_table_row(2, "Virtual Server", (serv->is_virtual ? "Yes" : "No"));
410 php_info_print_table_row(2, "Server Root", ap_server_root);
411 php_info_print_table_row(2, "Loaded Modules", ZSTR_VAL(tmp1.s));
412
413 smart_str_free(&tmp1);
415
417
418 {
419 const apr_array_header_t *arr = apr_table_elts(((php_struct *) SG(server_context))->r->subprocess_env);
420 char *key, *val;
421
422 SECTION("Apache Environment");
424 php_info_print_table_header(2, "Variable", "Value");
426 if (!val) {
427 val = "";
428 }
431
433
434 SECTION("HTTP Headers Information");
436 php_info_print_table_colspan_header(2, "HTTP Request Headers");
437 php_info_print_table_row(2, "HTTP Request", ((php_struct *) SG(server_context))->r->the_request);
438
439 arr = apr_table_elts(((php_struct *) SG(server_context))->r->headers_in);
441 if (!val) {
442 val = "";
443 }
446
447 php_info_print_table_colspan_header(2, "HTTP Response Headers");
448 arr = apr_table_elts(((php_struct *) SG(server_context))->r->headers_out);
450 if (!val) {
451 val = "";
452 }
455
457 }
458}
459
461 STD_PHP_INI_BOOLEAN("xbithack", "0", PHP_INI_ALL, OnUpdateBool, xbithack, php_apache2_info_struct, php_apache2_info)
462 STD_PHP_INI_BOOLEAN("engine", "1", PHP_INI_ALL, OnUpdateBool, engine, php_apache2_info_struct, php_apache2_info)
463 STD_PHP_INI_BOOLEAN("last_modified", "0", PHP_INI_ALL, OnUpdateBool, last_modified, php_apache2_info_struct, php_apache2_info)
465
466static PHP_MINIT_FUNCTION(apache)
467{
468#ifdef ZTS
469 ts_allocate_id(&php_apache2_info_id, sizeof(php_apache2_info_struct), (ts_allocate_ctor) NULL, NULL);
470#endif
472 return SUCCESS;
473}
474
475static PHP_MSHUTDOWN_FUNCTION(apache)
476{
478 return SUCCESS;
479}
480
483 "apache2handler",
484 ext_functions,
485 PHP_MINIT(apache),
486 PHP_MSHUTDOWN(apache),
487 NULL,
488 NULL,
489 PHP_MINFO(apache),
492};
#define SG(v)
Definition SAPI.h:160
strchr(string $haystack, string $needle, bool $before_needle=false)
char s[4]
Definition cdf.c:77
DNS_STATUS status
Definition dns_win32.c:49
zend_long n
Definition ffi.c:4979
zval * val
Definition ffi.c:4262
size_t filename_len
#define NULL
Definition gdcache.h:45
#define SUCCESS
Definition hash_sha3.c:261
PHPAPI bool php_header(void)
Definition head.c:70
PHPAPI ZEND_COLD void php_info_print_table_colspan_header(int num_cols, const char *header)
Definition info.c:1119
PHPAPI ZEND_COLD void php_info_print_table_header(int num_cols,...)
Definition info.c:1133
PHPAPI ZEND_COLD void php_error_docref(const char *docref, int type, const char *format,...)
Definition main.c:1173
PHPAPI void php_output_end_all(void)
Definition output.c:322
php_info_print_table_start()
Definition info.c:1064
php_info_print_table_row(2, "PDO Driver for Firebird", "enabled")
php_info_print_table_end()
Definition info.c:1074
#define PHP_FUNCTION
Definition php.h:364
#define PHP_MSHUTDOWN_FUNCTION
Definition php.h:401
#define PHP_MINFO
Definition php.h:396
#define PHP_MINIT_FUNCTION
Definition php.h:400
#define PHP_MSHUTDOWN
Definition php.h:393
#define PHP_MINFO_FUNCTION
Definition php.h:404
#define PHP_MINIT
Definition php.h:392
#define APR_ARRAY_FOREACH_CLOSE()
Definition php_apache.h:66
#define APR_ARRAY_FOREACH_OPEN(arr, key, val)
Definition php_apache.h:57
php_apache2_info_struct php_apache2_info
#define ADD_STRING(name)
zend_module_entry php_apache_module
#define ADD_LONG(name)
#define SECTION(name)
#define ADD_TIME(name)
apache_request_headers()
apache_get_modules()
apache_lookup_uri(string $filename)
apache_response_headers()
apache_getenv(string $variable, bool $walk_to_top=false)
apache_get_version()
apache_note(string $note_name, ?string $note_value=null)
apache_setenv(string $variable, string $value, bool $walk_to_top=false)
#define PHP_INI_ALL
Definition php_ini.h:45
#define PHP_INI_BEGIN
Definition php_ini.h:52
#define STD_PHP_INI_BOOLEAN
Definition php_ini.h:66
#define PHP_INI_END
Definition php_ini.h:53
unsigned char key[REFLECTION_KEY_LEN]
#define PHP_VERSION
Definition php_version.h:7
p
Definition session.c:1105
#define tmp1
request_rec * r
Definition php_apache.h:40
ZEND_API zend_result add_next_index_stringl(zval *arg, const char *str, size_t length)
Definition zend_API.c:2195
ZEND_API zend_result zend_parse_parameters(uint32_t num_args, const char *type_spec,...)
Definition zend_API.c:1300
ZEND_API zend_result add_next_index_string(zval *arg, const char *str)
Definition zend_API.c:2186
ZEND_API void object_init(zval *arg)
Definition zend_API.c:1922
#define ZEND_NUM_ARGS()
Definition zend_API.h:530
#define RETURN_STRING(s)
Definition zend_API.h:1043
#define RETURN_FALSE
Definition zend_API.h:1058
#define zend_parse_parameters_none()
Definition zend_API.h:353
#define RETURN_THROWS()
Definition zend_API.h:1060
#define RETURN_TRUE
Definition zend_API.h:1059
#define array_init(arg)
Definition zend_API.h:537
zval * args
#define snprintf
#define E_WARNING
Definition zend_errors.h:24
#define UNREGISTER_INI_ENTRIES()
Definition zend_ini.h:204
#define REGISTER_INI_ENTRIES()
Definition zend_ini.h:203
#define DISPLAY_INI_ENTRIES()
Definition zend_ini.h:205
#define STANDARD_MODULE_HEADER
struct _zend_module_entry zend_module_entry
#define STANDARD_MODULE_PROPERTIES
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
@ FAILURE
Definition zend_types.h:61
zval * return_value
fbc internal_function handler(call, ret)