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.

515 lines
13 KiB

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