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.

516 lines
13 KiB

  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 4 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2003 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.02 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available at through the world-wide-web at |
  10. | http://www.php.net/license/2_02.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. #if !defined(WIN32) && !defined(WINNT)
  41. #include "unixd.h"
  42. #endif
  43. #include "php_apache.h"
  44. #ifdef ZTS
  45. int php_apache2_info_id;
  46. #else
  47. php_apache2_info_struct php_apache2_info;
  48. #endif
  49. #define SECTION(name) PUTS("<h2>" name "</h2>\n")
  50. static request_rec *php_apache_lookup_uri(char *filename TSRMLS_DC)
  51. {
  52. php_struct *ctx;
  53. if (!filename) {
  54. return NULL;
  55. }
  56. ctx = SG(server_context);
  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_end_ob_buffers(1 TSRMLS_CC);
  80. php_header(TSRMLS_C);
  81. if (ap_run_sub_req(rr)) {
  82. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to include '%s' - request execution failed", Z_STRVAL_PP(filename));
  83. ap_destroy_sub_req(rr);
  84. RETURN_FALSE;
  85. }
  86. ap_destroy_sub_req(rr);
  87. RETURN_TRUE;
  88. }
  89. /* }}} */
  90. #define ADD_LONG(name) \
  91. add_property_long(return_value, #name, rr->name)
  92. #define ADD_TIME(name) \
  93. add_property_long(return_value, #name, rr->name / APR_USEC_PER_SEC);
  94. #define ADD_STRING(name) \
  95. if (rr->name) add_property_string(return_value, #name, (char *) rr->name, 1)
  96. PHP_FUNCTION(apache_lookup_uri)
  97. {
  98. request_rec *rr;
  99. zval **filename;
  100. if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &filename) == FAILURE) {
  101. WRONG_PARAM_COUNT;
  102. }
  103. convert_to_string_ex(filename);
  104. if (!(rr = php_apache_lookup_uri(Z_STRVAL_PP(filename) TSRMLS_CC))) {
  105. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to include '%s' - URI lookup failed", Z_STRVAL_PP(filename));
  106. RETURN_FALSE;
  107. }
  108. if (rr->status == HTTP_OK) {
  109. object_init(return_value);
  110. ADD_LONG(status);
  111. ADD_STRING(the_request);
  112. ADD_STRING(status_line);
  113. ADD_STRING(method);
  114. ADD_TIME(mtime);
  115. ADD_LONG(clength);
  116. #if MODULE_MAGIC_NUMBER < 20020506
  117. ADD_STRING(boundary);
  118. #endif
  119. ADD_STRING(range);
  120. ADD_LONG(chunked);
  121. ADD_STRING(content_type);
  122. ADD_STRING(handler);
  123. ADD_LONG(no_cache);
  124. ADD_LONG(no_local_copy);
  125. ADD_STRING(unparsed_uri);
  126. ADD_STRING(uri);
  127. ADD_STRING(filename);
  128. ADD_STRING(path_info);
  129. ADD_STRING(args);
  130. ADD_LONG(allowed);
  131. ADD_LONG(sent_bodyct);
  132. ADD_LONG(bytes_sent);
  133. ADD_LONG(request_time);
  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 = empty_string;
  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 = empty_string;
  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. if (arg_count == 3 && Z_STRVAL_PP(walk_to_top)) {
  217. r = ctx->r;
  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;
  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. extern int ap_max_requests_per_child;
  304. #endif
  305. AP_DECLARE_DATA extern const char *ap_server_root;
  306. for (n = 0; ap_loaded_modules[n]; ++n) {
  307. char *s = (char *) ap_loaded_modules[n]->name;
  308. if ((p = strchr(s, '.'))) {
  309. smart_str_appendl(&tmp1, s, (p - s));
  310. } else {
  311. smart_str_appends(&tmp1, s);
  312. }
  313. smart_str_appendc(&tmp1, ' ');
  314. }
  315. if ((tmp1.len - 1) >= 0) {
  316. tmp1.c[tmp1.len - 1] = '\0';
  317. }
  318. php_info_print_table_start();
  319. if (apv && *apv) {
  320. php_info_print_table_row(2, "Apache Version", apv);
  321. }
  322. sprintf(tmp, "%d", MODULE_MAGIC_NUMBER);
  323. php_info_print_table_row(2, "Apache API Version", tmp);
  324. if (serv->server_admin && *(serv->server_admin)) {
  325. php_info_print_table_row(2, "Servert Administrator", serv->server_admin);
  326. }
  327. sprintf(tmp, "%s:%u", serv->server_hostname, serv->port);
  328. php_info_print_table_row(2, "Hostname:Port", tmp);
  329. #if !defined(WIN32) && !defined(WINNT)
  330. sprintf(tmp, "%s(%d)/%d", unixd_config.user_name, unixd_config.user_id, unixd_config.group_id);
  331. php_info_print_table_row(2, "User/Group", tmp);
  332. sprintf(tmp, "Per Child: %d - Keep Alive: %s - Max Per Connection: %d", ap_max_requests_per_child, (serv->keep_alive ? "on":"off"), serv->keep_alive_max);
  333. #else
  334. sprintf(tmp, "Keep Alive: %s - Max Per Connection: %d", (serv->keep_alive ? "on":"off"), serv->keep_alive_max);
  335. #endif
  336. php_info_print_table_row(2, "Max Requests", tmp);
  337. sprintf(tmp, "Connection: %lld - Keep-Alive: %lld", (serv->timeout / 1000000), (serv->keep_alive_timeout / 1000000));
  338. php_info_print_table_row(2, "Timeouts", tmp);
  339. php_info_print_table_row(2, "Virtual Server", (serv->is_virtual ? "Yes" : "No"));
  340. php_info_print_table_row(2, "Server Root", ap_server_root);
  341. php_info_print_table_row(2, "Loaded Modules", tmp1.c);
  342. smart_str_free(&tmp1);
  343. php_info_print_table_end();
  344. DISPLAY_INI_ENTRIES();
  345. {
  346. const apr_array_header_t *arr = apr_table_elts(((php_struct *) SG(server_context))->r->subprocess_env);
  347. char *key, *val;
  348. SECTION("Apache Environment");
  349. php_info_print_table_start();
  350. php_info_print_table_header(2, "Variable", "Value");
  351. APR_ARRAY_FOREACH_OPEN(arr, key, val)
  352. if (!val) {
  353. val = empty_string;
  354. }
  355. php_info_print_table_row(2, key, val);
  356. APR_ARRAY_FOREACH_CLOSE()
  357. php_info_print_table_end();
  358. SECTION("HTTP Headers Information");
  359. php_info_print_table_start();
  360. php_info_print_table_colspan_header(2, "HTTP Request Headers");
  361. php_info_print_table_row(2, "HTTP Request", ((php_struct *) SG(server_context))->r->the_request);
  362. arr = apr_table_elts(((php_struct *) SG(server_context))->r->headers_in);
  363. APR_ARRAY_FOREACH_OPEN(arr, key, val)
  364. if (!val) {
  365. val = empty_string;
  366. }
  367. php_info_print_table_row(2, key, val);
  368. APR_ARRAY_FOREACH_CLOSE()
  369. php_info_print_table_colspan_header(2, "HTTP Response Headers");
  370. arr = apr_table_elts(((php_struct *) SG(server_context))->r->headers_out);
  371. APR_ARRAY_FOREACH_OPEN(arr, key, val)
  372. if (!val) {
  373. val = empty_string;
  374. }
  375. php_info_print_table_row(2, key, val);
  376. APR_ARRAY_FOREACH_CLOSE()
  377. php_info_print_table_end();
  378. }
  379. }
  380. static function_entry apache_functions[] = {
  381. PHP_FE(apache_lookup_uri, NULL)
  382. PHP_FE(virtual, NULL)
  383. PHP_FE(apache_request_headers, NULL)
  384. PHP_FE(apache_response_headers, NULL)
  385. PHP_FE(apache_setenv, NULL)
  386. PHP_FE(apache_getenv, NULL)
  387. PHP_FE(apache_note, NULL)
  388. PHP_FE(apache_get_version, NULL)
  389. PHP_FE(apache_get_modules, NULL)
  390. PHP_FALIAS(getallheaders, apache_request_headers, NULL)
  391. {NULL, NULL, NULL}
  392. };
  393. PHP_INI_BEGIN()
  394. STD_PHP_INI_ENTRY("xbithack", "0", PHP_INI_ALL, OnUpdateLong, xbithack, php_apache2_info_struct, php_apache2_info)
  395. STD_PHP_INI_ENTRY("engine", "1", PHP_INI_ALL, OnUpdateLong, engine, php_apache2_info_struct, php_apache2_info)
  396. STD_PHP_INI_ENTRY("last_modified", "0", PHP_INI_ALL, OnUpdateLong, last_modified, php_apache2_info_struct, php_apache2_info)
  397. PHP_INI_END()
  398. static PHP_MINIT_FUNCTION(apache)
  399. {
  400. #ifdef ZTS
  401. ts_allocate_id(&php_apache2_info_id, sizeof(php_apache2_info_struct), (ts_allocate_ctor) NULL, NULL);
  402. #endif
  403. REGISTER_INI_ENTRIES();
  404. return SUCCESS;
  405. }
  406. static PHP_MSHUTDOWN_FUNCTION(apache)
  407. {
  408. UNREGISTER_INI_ENTRIES();
  409. return SUCCESS;
  410. }
  411. zend_module_entry php_apache_module = {
  412. STANDARD_MODULE_HEADER,
  413. "apache2handler",
  414. apache_functions,
  415. PHP_MINIT(apache),
  416. PHP_MSHUTDOWN(apache),
  417. NULL,
  418. NULL,
  419. PHP_MINFO(apache),
  420. NULL,
  421. STANDARD_MODULE_PROPERTIES
  422. };
  423. /*
  424. * Local variables:
  425. * tab-width: 4
  426. * c-basic-offset: 4
  427. * End:
  428. * vim600: sw=4 ts=4 fdm=marker
  429. * vim<600: sw=4 ts=4
  430. */