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.

520 lines
13 KiB

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