You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

564 lines
15 KiB

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