php_functions.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 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 "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) && !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 TSRMLS_DC)
  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. int filename_len;
  65. request_rec *rr;
  66. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p", &filename, &filename_len) == FAILURE) {
  67. return;
  68. }
  69. if (!(rr = php_apache_lookup_uri(filename TSRMLS_CC))) {
  70. php_error_docref(NULL TSRMLS_CC, 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 TSRMLS_CC, 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(TSRMLS_C);
  80. php_header(TSRMLS_C);
  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 TSRMLS_CC, 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, 1)
  99. PHP_FUNCTION(apache_lookup_uri)
  100. {
  101. request_rec *rr;
  102. char *filename;
  103. int filename_len;
  104. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p", &filename, &filename_len) == FAILURE) {
  105. return;
  106. }
  107. if (!(rr = php_apache_lookup_uri(filename TSRMLS_CC))) {
  108. php_error_docref(NULL TSRMLS_CC, 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 TSRMLS_CC, 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, 1);
  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, 1);
  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. int note_name_len, note_val_len;
  190. char *old_note_val=NULL;
  191. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "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, 1);
  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. int 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 TSRMLS_CC, "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=NULL;
  242. int 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 TSRMLS_CC, "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, 1);
  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, 1);
  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), 1);
  297. } else {
  298. add_next_index_string(return_value, s, 1);
  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.len - 1) >= 0) {
  328. tmp1.c[tmp1.len - 1] = '\0';
  329. }
  330. php_info_print_table_start();
  331. if (apv && *apv) {
  332. php_info_print_table_row(2, "Apache Version", apv);
  333. }
  334. snprintf(tmp, sizeof(tmp), "%d", MODULE_MAGIC_NUMBER);
  335. php_info_print_table_row(2, "Apache API Version", tmp);
  336. if (serv->server_admin && *(serv->server_admin)) {
  337. php_info_print_table_row(2, "Server Administrator", serv->server_admin);
  338. }
  339. snprintf(tmp, sizeof(tmp), "%s:%u", serv->server_hostname, serv->port);
  340. php_info_print_table_row(2, "Hostname:Port", tmp);
  341. #if !defined(WIN32) && !defined(WINNT) && !defined(NETWARE)
  342. #if MODULE_MAGIC_NUMBER_MAJOR >= 20081201
  343. snprintf(tmp, sizeof(tmp), "%s(%d)/%d", ap_unixd_config.user_name, ap_unixd_config.user_id, ap_unixd_config.group_id);
  344. #else
  345. snprintf(tmp, sizeof(tmp), "%s(%d)/%d", unixd_config.user_name, unixd_config.user_id, unixd_config.group_id);
  346. #endif
  347. php_info_print_table_row(2, "User/Group", tmp);
  348. #endif
  349. ap_mpm_query(AP_MPMQ_MAX_REQUESTS_DAEMON, &max_requests);
  350. 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);
  351. php_info_print_table_row(2, "Max Requests", tmp);
  352. apr_snprintf(tmp, sizeof tmp,
  353. "Connection: %" APR_TIME_T_FMT " - Keep-Alive: %" APR_TIME_T_FMT,
  354. apr_time_sec(serv->timeout), apr_time_sec(serv->keep_alive_timeout));
  355. php_info_print_table_row(2, "Timeouts", tmp);
  356. php_info_print_table_row(2, "Virtual Server", (serv->is_virtual ? "Yes" : "No"));
  357. php_info_print_table_row(2, "Server Root", ap_server_root);
  358. php_info_print_table_row(2, "Loaded Modules", tmp1.c);
  359. smart_str_free(&tmp1);
  360. php_info_print_table_end();
  361. DISPLAY_INI_ENTRIES();
  362. {
  363. const apr_array_header_t *arr = apr_table_elts(((php_struct *) SG(server_context))->r->subprocess_env);
  364. char *key, *val;
  365. SECTION("Apache Environment");
  366. php_info_print_table_start();
  367. php_info_print_table_header(2, "Variable", "Value");
  368. APR_ARRAY_FOREACH_OPEN(arr, key, val)
  369. if (!val) {
  370. val = "";
  371. }
  372. php_info_print_table_row(2, key, val);
  373. APR_ARRAY_FOREACH_CLOSE()
  374. php_info_print_table_end();
  375. SECTION("HTTP Headers Information");
  376. php_info_print_table_start();
  377. php_info_print_table_colspan_header(2, "HTTP Request Headers");
  378. php_info_print_table_row(2, "HTTP Request", ((php_struct *) SG(server_context))->r->the_request);
  379. arr = apr_table_elts(((php_struct *) SG(server_context))->r->headers_in);
  380. APR_ARRAY_FOREACH_OPEN(arr, key, val)
  381. if (!val) {
  382. val = "";
  383. }
  384. php_info_print_table_row(2, key, val);
  385. APR_ARRAY_FOREACH_CLOSE()
  386. php_info_print_table_colspan_header(2, "HTTP Response Headers");
  387. arr = apr_table_elts(((php_struct *) SG(server_context))->r->headers_out);
  388. APR_ARRAY_FOREACH_OPEN(arr, key, val)
  389. if (!val) {
  390. val = "";
  391. }
  392. php_info_print_table_row(2, key, val);
  393. APR_ARRAY_FOREACH_CLOSE()
  394. php_info_print_table_end();
  395. }
  396. }
  397. /* {{{ arginfo */
  398. ZEND_BEGIN_ARG_INFO_EX(arginfo_apache2handler_lookup_uri, 0, 0, 1)
  399. ZEND_ARG_INFO(0, filename)
  400. ZEND_END_ARG_INFO()
  401. ZEND_BEGIN_ARG_INFO_EX(arginfo_apache2handler_virtual, 0, 0, 1)
  402. ZEND_ARG_INFO(0, uri)
  403. ZEND_END_ARG_INFO()
  404. ZEND_BEGIN_ARG_INFO(arginfo_apache2handler_response_headers, 0)
  405. ZEND_END_ARG_INFO()
  406. ZEND_BEGIN_ARG_INFO(arginfo_apache2handler_getallheaders, 0)
  407. ZEND_END_ARG_INFO()
  408. ZEND_BEGIN_ARG_INFO_EX(arginfo_apache2handler_note, 0, 0, 1)
  409. ZEND_ARG_INFO(0, note_name)
  410. ZEND_ARG_INFO(0, note_value)
  411. ZEND_END_ARG_INFO()
  412. ZEND_BEGIN_ARG_INFO_EX(arginfo_apache2handler_setenv, 0, 0, 2)
  413. ZEND_ARG_INFO(0, variable)
  414. ZEND_ARG_INFO(0, value)
  415. ZEND_ARG_INFO(0, walk_to_top)
  416. ZEND_END_ARG_INFO()
  417. ZEND_BEGIN_ARG_INFO_EX(arginfo_apache2handler_getenv, 0, 0, 1)
  418. ZEND_ARG_INFO(0, variable)
  419. ZEND_ARG_INFO(0, walk_to_top)
  420. ZEND_END_ARG_INFO()
  421. ZEND_BEGIN_ARG_INFO(arginfo_apache2handler_get_version, 0)
  422. ZEND_END_ARG_INFO()
  423. ZEND_BEGIN_ARG_INFO(arginfo_apache2handler_get_modules, 0)
  424. ZEND_END_ARG_INFO()
  425. /* }}} */
  426. static const zend_function_entry apache_functions[] = {
  427. PHP_FE(apache_lookup_uri, arginfo_apache2handler_lookup_uri)
  428. PHP_FE(virtual, arginfo_apache2handler_virtual)
  429. PHP_FE(apache_request_headers, arginfo_apache2handler_getallheaders)
  430. PHP_FE(apache_response_headers, arginfo_apache2handler_response_headers)
  431. PHP_FE(apache_setenv, arginfo_apache2handler_setenv)
  432. PHP_FE(apache_getenv, arginfo_apache2handler_getenv)
  433. PHP_FE(apache_note, arginfo_apache2handler_note)
  434. PHP_FE(apache_get_version, arginfo_apache2handler_get_version)
  435. PHP_FE(apache_get_modules, arginfo_apache2handler_get_modules)
  436. PHP_FALIAS(getallheaders, apache_request_headers, arginfo_apache2handler_getallheaders)
  437. {NULL, NULL, NULL}
  438. };
  439. PHP_INI_BEGIN()
  440. STD_PHP_INI_ENTRY("xbithack", "0", PHP_INI_ALL, OnUpdateLong, xbithack, php_apache2_info_struct, php_apache2_info)
  441. STD_PHP_INI_ENTRY("engine", "1", PHP_INI_ALL, OnUpdateLong, engine, php_apache2_info_struct, php_apache2_info)
  442. STD_PHP_INI_ENTRY("last_modified", "0", PHP_INI_ALL, OnUpdateLong, last_modified, php_apache2_info_struct, php_apache2_info)
  443. PHP_INI_END()
  444. static PHP_MINIT_FUNCTION(apache)
  445. {
  446. #ifdef ZTS
  447. ts_allocate_id(&php_apache2_info_id, sizeof(php_apache2_info_struct), (ts_allocate_ctor) NULL, NULL);
  448. #endif
  449. REGISTER_INI_ENTRIES();
  450. return SUCCESS;
  451. }
  452. static PHP_MSHUTDOWN_FUNCTION(apache)
  453. {
  454. UNREGISTER_INI_ENTRIES();
  455. return SUCCESS;
  456. }
  457. zend_module_entry php_apache_module = {
  458. STANDARD_MODULE_HEADER,
  459. "apache2handler",
  460. apache_functions,
  461. PHP_MINIT(apache),
  462. PHP_MSHUTDOWN(apache),
  463. NULL,
  464. NULL,
  465. PHP_MINFO(apache),
  466. NULL,
  467. STANDARD_MODULE_PROPERTIES
  468. };
  469. /*
  470. * Local variables:
  471. * tab-width: 4
  472. * c-basic-offset: 4
  473. * End:
  474. * vim600: sw=4 ts=4 fdm=marker
  475. * vim<600: sw=4 ts=4
  476. */