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.

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