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.

536 lines
14 KiB

19 years ago
19 years ago
23 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2007 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. zval **filename;
  64. request_rec *rr;
  65. if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &filename) == FAILURE) {
  66. WRONG_PARAM_COUNT;
  67. }
  68. convert_to_string_ex(filename);
  69. if (!(rr = php_apache_lookup_uri(Z_STRVAL_PP(filename) TSRMLS_CC))) {
  70. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to include '%s' - URI lookup failed", Z_STRVAL_PP(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", Z_STRVAL_PP(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", Z_STRVAL_PP(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. zval **filename;
  103. if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &filename) == FAILURE) {
  104. WRONG_PARAM_COUNT;
  105. }
  106. convert_to_string_ex(filename);
  107. if (!(rr = php_apache_lookup_uri(Z_STRVAL_PP(filename) TSRMLS_CC))) {
  108. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to include '%s' - URI lookup failed", Z_STRVAL_PP(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", Z_STRVAL_PP(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_NUM_ARGS()) {
  153. WRONG_PARAM_COUNT;
  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_NUM_ARGS()) {
  172. WRONG_PARAM_COUNT;
  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. zval **note_name, **note_val;
  189. char *old_note_val=NULL;
  190. int arg_count = ZEND_NUM_ARGS();
  191. if (arg_count<1 || arg_count>2 ||
  192. zend_get_parameters_ex(arg_count, &note_name, &note_val) == FAILURE) {
  193. WRONG_PARAM_COUNT;
  194. }
  195. ctx = SG(server_context);
  196. convert_to_string_ex(note_name);
  197. old_note_val = (char *) apr_table_get(ctx->r->notes, Z_STRVAL_PP(note_name));
  198. if (arg_count == 2) {
  199. convert_to_string_ex(note_val);
  200. apr_table_set(ctx->r->notes, Z_STRVAL_PP(note_name), Z_STRVAL_PP(note_val));
  201. }
  202. if (old_note_val) {
  203. RETURN_STRING(old_note_val, 1);
  204. } else {
  205. RETURN_FALSE;
  206. }
  207. }
  208. /* }}} */
  209. /* {{{ proto bool apache_setenv(string variable, string value [, bool walk_to_top])
  210. Set an Apache subprocess_env variable */
  211. /*
  212. * XXX this doesn't look right. shouldn't it be the parent ?*/
  213. PHP_FUNCTION(apache_setenv)
  214. {
  215. php_struct *ctx;
  216. zval **variable=NULL, **string_val=NULL, **walk_to_top=NULL;
  217. int arg_count = ZEND_NUM_ARGS();
  218. request_rec *r;
  219. if (arg_count < 2 || arg_count > 3 ||
  220. zend_get_parameters_ex(arg_count, &variable, &string_val, &walk_to_top) == FAILURE) {
  221. WRONG_PARAM_COUNT;
  222. }
  223. ctx = SG(server_context);
  224. r = ctx->r;
  225. if (arg_count == 3) {
  226. convert_to_boolean_ex(walk_to_top);
  227. if (Z_LVAL_PP(walk_to_top)) {
  228. while(r->prev) {
  229. r = r->prev;
  230. }
  231. }
  232. }
  233. convert_to_string_ex(variable);
  234. convert_to_string_ex(string_val);
  235. apr_table_set(r->subprocess_env, Z_STRVAL_PP(variable), Z_STRVAL_PP(string_val));
  236. RETURN_TRUE;
  237. }
  238. /* }}} */
  239. /* {{{ proto bool apache_getenv(string variable [, bool walk_to_top])
  240. Get an Apache subprocess_env variable */
  241. /*
  242. * XXX: shouldn't this be the parent not the 'prev'
  243. */
  244. PHP_FUNCTION(apache_getenv)
  245. {
  246. php_struct *ctx;
  247. zval **variable=NULL, **walk_to_top=NULL;
  248. int arg_count = ZEND_NUM_ARGS();
  249. char *env_val=NULL;
  250. request_rec *r;
  251. if (arg_count<1 || arg_count>2 ||
  252. zend_get_parameters_ex(arg_count, &variable, &walk_to_top) == FAILURE) {
  253. WRONG_PARAM_COUNT;
  254. }
  255. ctx = SG(server_context);
  256. r = ctx->r;
  257. if (arg_count == 2) {
  258. convert_to_boolean_ex(walk_to_top);
  259. if (Z_LVAL_PP(walk_to_top)) {
  260. while(r->prev) {
  261. r = r->prev;
  262. }
  263. }
  264. }
  265. convert_to_string_ex(variable);
  266. env_val = (char*) apr_table_get(r->subprocess_env, Z_STRVAL_PP(variable));
  267. if (env_val != NULL) {
  268. RETURN_STRING(env_val, 1);
  269. } else {
  270. RETURN_FALSE;
  271. }
  272. }
  273. /* }}} */
  274. static char *php_apache_get_version()
  275. {
  276. #if MODULE_MAGIC_NUMBER_MAJOR >= 20060905
  277. return (char *) ap_get_server_banner();
  278. #else
  279. return (char *) ap_get_server_version();
  280. #endif
  281. }
  282. /* {{{ proto string apache_get_version(void)
  283. Fetch Apache version */
  284. PHP_FUNCTION(apache_get_version)
  285. {
  286. char *apv = php_apache_get_version();
  287. if (apv && *apv) {
  288. RETURN_STRING(apv, 1);
  289. } else {
  290. RETURN_FALSE;
  291. }
  292. }
  293. /* }}} */
  294. /* {{{ proto array apache_get_modules(void)
  295. Get a list of loaded Apache modules */
  296. PHP_FUNCTION(apache_get_modules)
  297. {
  298. int n;
  299. char *p;
  300. array_init(return_value);
  301. for (n = 0; ap_loaded_modules[n]; ++n) {
  302. char *s = (char *) ap_loaded_modules[n]->name;
  303. if ((p = strchr(s, '.'))) {
  304. add_next_index_stringl(return_value, s, (p - s), 1);
  305. } else {
  306. add_next_index_string(return_value, s, 1);
  307. }
  308. }
  309. }
  310. /* }}} */
  311. PHP_MINFO_FUNCTION(apache)
  312. {
  313. char *apv = php_apache_get_version();
  314. smart_str tmp1 = {0};
  315. char tmp[1024];
  316. int n, max_requests;
  317. char *p;
  318. server_rec *serv = ((php_struct *) SG(server_context))->r->server;
  319. #if !defined(WIN32) && !defined(WINNT)
  320. AP_DECLARE_DATA extern unixd_config_rec unixd_config;
  321. #endif
  322. for (n = 0; ap_loaded_modules[n]; ++n) {
  323. char *s = (char *) ap_loaded_modules[n]->name;
  324. if ((p = strchr(s, '.'))) {
  325. smart_str_appendl(&tmp1, s, (p - s));
  326. } else {
  327. smart_str_appends(&tmp1, s);
  328. }
  329. smart_str_appendc(&tmp1, ' ');
  330. }
  331. if ((tmp1.len - 1) >= 0) {
  332. tmp1.c[tmp1.len - 1] = '\0';
  333. }
  334. php_info_print_table_start();
  335. if (apv && *apv) {
  336. php_info_print_table_row(2, "Apache Version", apv);
  337. }
  338. sprintf(tmp, "%d", MODULE_MAGIC_NUMBER);
  339. php_info_print_table_row(2, "Apache API Version", tmp);
  340. if (serv->server_admin && *(serv->server_admin)) {
  341. php_info_print_table_row(2, "Server Administrator", serv->server_admin);
  342. }
  343. sprintf(tmp, "%s:%u", serv->server_hostname, serv->port);
  344. php_info_print_table_row(2, "Hostname:Port", tmp);
  345. #if !defined(WIN32) && !defined(WINNT)
  346. sprintf(tmp, "%s(%d)/%d", unixd_config.user_name, unixd_config.user_id, unixd_config.group_id);
  347. php_info_print_table_row(2, "User/Group", tmp);
  348. #endif
  349. ap_mpm_query(AP_MPMQ_MAX_REQUESTS_DAEMON, &max_requests);
  350. sprintf(tmp, "Per Child: %d - Keep Alive: %s - Max Per Connection: %d", max_requests, (serv->keep_alive ? "on":"off"), serv->keep_alive_max);
  351. php_info_print_table_row(2, "Max Requests", tmp);
  352. apr_snprintf(tmp, sizeof tmp,
  353. "Connection: %" APR_TIME_T_FMT " - Keep-Alive: %" APR_TIME_T_FMT,
  354. apr_time_sec(serv->timeout), apr_time_sec(serv->keep_alive_timeout));
  355. php_info_print_table_row(2, "Timeouts", tmp);
  356. php_info_print_table_row(2, "Virtual Server", (serv->is_virtual ? "Yes" : "No"));
  357. php_info_print_table_row(2, "Server Root", ap_server_root);
  358. php_info_print_table_row(2, "Loaded Modules", tmp1.c);
  359. smart_str_free(&tmp1);
  360. php_info_print_table_end();
  361. DISPLAY_INI_ENTRIES();
  362. {
  363. const apr_array_header_t *arr = apr_table_elts(((php_struct *) SG(server_context))->r->subprocess_env);
  364. char *key, *val;
  365. SECTION("Apache Environment");
  366. php_info_print_table_start();
  367. php_info_print_table_header(2, "Variable", "Value");
  368. APR_ARRAY_FOREACH_OPEN(arr, key, val)
  369. if (!val) {
  370. val = "";
  371. }
  372. php_info_print_table_row(2, key, val);
  373. APR_ARRAY_FOREACH_CLOSE()
  374. php_info_print_table_end();
  375. SECTION("HTTP Headers Information");
  376. php_info_print_table_start();
  377. php_info_print_table_colspan_header(2, "HTTP Request Headers");
  378. php_info_print_table_row(2, "HTTP Request", ((php_struct *) SG(server_context))->r->the_request);
  379. arr = apr_table_elts(((php_struct *) SG(server_context))->r->headers_in);
  380. APR_ARRAY_FOREACH_OPEN(arr, key, val)
  381. if (!val) {
  382. val = "";
  383. }
  384. php_info_print_table_row(2, key, val);
  385. APR_ARRAY_FOREACH_CLOSE()
  386. php_info_print_table_colspan_header(2, "HTTP Response Headers");
  387. arr = apr_table_elts(((php_struct *) SG(server_context))->r->headers_out);
  388. APR_ARRAY_FOREACH_OPEN(arr, key, val)
  389. if (!val) {
  390. val = "";
  391. }
  392. php_info_print_table_row(2, key, val);
  393. APR_ARRAY_FOREACH_CLOSE()
  394. php_info_print_table_end();
  395. }
  396. }
  397. static zend_function_entry apache_functions[] = {
  398. PHP_FE(apache_lookup_uri, NULL)
  399. PHP_FE(virtual, NULL)
  400. PHP_FE(apache_request_headers, NULL)
  401. PHP_FE(apache_response_headers, NULL)
  402. PHP_FE(apache_setenv, NULL)
  403. PHP_FE(apache_getenv, NULL)
  404. PHP_FE(apache_note, NULL)
  405. PHP_FE(apache_get_version, NULL)
  406. PHP_FE(apache_get_modules, NULL)
  407. PHP_FALIAS(getallheaders, apache_request_headers, NULL)
  408. {NULL, NULL, NULL}
  409. };
  410. PHP_INI_BEGIN()
  411. STD_PHP_INI_ENTRY("xbithack", "0", PHP_INI_ALL, OnUpdateLong, xbithack, php_apache2_info_struct, php_apache2_info)
  412. STD_PHP_INI_ENTRY("engine", "1", PHP_INI_ALL, OnUpdateLong, engine, php_apache2_info_struct, php_apache2_info)
  413. STD_PHP_INI_ENTRY("last_modified", "0", PHP_INI_ALL, OnUpdateLong, last_modified, php_apache2_info_struct, php_apache2_info)
  414. PHP_INI_END()
  415. static PHP_MINIT_FUNCTION(apache)
  416. {
  417. #ifdef ZTS
  418. ts_allocate_id(&php_apache2_info_id, sizeof(php_apache2_info_struct), (ts_allocate_ctor) NULL, NULL);
  419. #endif
  420. REGISTER_INI_ENTRIES();
  421. return SUCCESS;
  422. }
  423. static PHP_MSHUTDOWN_FUNCTION(apache)
  424. {
  425. UNREGISTER_INI_ENTRIES();
  426. return SUCCESS;
  427. }
  428. zend_module_entry php_apache_module = {
  429. STANDARD_MODULE_HEADER,
  430. "apache2handler",
  431. apache_functions,
  432. PHP_MINIT(apache),
  433. PHP_MSHUTDOWN(apache),
  434. NULL,
  435. NULL,
  436. PHP_MINFO(apache),
  437. NULL,
  438. STANDARD_MODULE_PROPERTIES
  439. };
  440. /*
  441. * Local variables:
  442. * tab-width: 4
  443. * c-basic-offset: 4
  444. * End:
  445. * vim600: sw=4 ts=4 fdm=marker
  446. * vim<600: sw=4 ts=4
  447. */