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.

575 lines
15 KiB

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