php_functions.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 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. #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
  19. #include "php.h"
  20. #include "zend_smart_str.h"
  21. #include "ext/standard/info.h"
  22. #include "ext/standard/head.h"
  23. #include "php_ini.h"
  24. #include "SAPI.h"
  25. #define CORE_PRIVATE
  26. #include "apr_strings.h"
  27. #include "apr_time.h"
  28. #include "ap_config.h"
  29. #include "util_filter.h"
  30. #include "httpd.h"
  31. #include "http_config.h"
  32. #include "http_request.h"
  33. #include "http_core.h"
  34. #include "http_protocol.h"
  35. #include "http_log.h"
  36. #include "http_main.h"
  37. #include "util_script.h"
  38. #include "http_core.h"
  39. #include "ap_mpm.h"
  40. #ifndef PHP_WIN32
  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)
  51. {
  52. php_struct *ctx = SG(server_context);
  53. if (!filename || !ctx || !ctx->r) {
  54. return NULL;
  55. }
  56. return ap_sub_req_lookup_uri(filename, ctx->r, ctx->r->output_filters);
  57. }
  58. /* {{{ proto bool virtual(string uri)
  59. Perform an apache sub-request */
  60. PHP_FUNCTION(virtual)
  61. {
  62. char *filename;
  63. size_t filename_len;
  64. request_rec *rr;
  65. if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &filename, &filename_len) == FAILURE) {
  66. return;
  67. }
  68. if (!(rr = php_apache_lookup_uri(filename))) {
  69. php_error_docref(NULL, E_WARNING, "Unable to include '%s' - URI lookup failed", filename);
  70. RETURN_FALSE;
  71. }
  72. if (rr->status != HTTP_OK) {
  73. php_error_docref(NULL, E_WARNING, "Unable to include '%s' - error finding URI", filename);
  74. ap_destroy_sub_req(rr);
  75. RETURN_FALSE;
  76. }
  77. /* Flush everything. */
  78. php_output_end_all();
  79. php_header();
  80. /* Ensure that the ap_r* layer for the main request is flushed, to
  81. * work around http://issues.apache.org/bugzilla/show_bug.cgi?id=17629 */
  82. ap_rflush(rr->main);
  83. if (ap_run_sub_req(rr)) {
  84. php_error_docref(NULL, E_WARNING, "Unable to include '%s' - request execution failed", filename);
  85. ap_destroy_sub_req(rr);
  86. RETURN_FALSE;
  87. }
  88. ap_destroy_sub_req(rr);
  89. RETURN_TRUE;
  90. }
  91. /* }}} */
  92. #define ADD_LONG(name) \
  93. add_property_long(return_value, #name, rr->name)
  94. #define ADD_TIME(name) \
  95. add_property_long(return_value, #name, apr_time_sec(rr->name));
  96. #define ADD_STRING(name) \
  97. if (rr->name) add_property_string(return_value, #name, (char *) rr->name)
  98. PHP_FUNCTION(apache_lookup_uri)
  99. {
  100. request_rec *rr;
  101. char *filename;
  102. size_t filename_len;
  103. if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &filename, &filename_len) == FAILURE) {
  104. return;
  105. }
  106. if (!(rr = php_apache_lookup_uri(filename))) {
  107. php_error_docref(NULL, E_WARNING, "Unable to include '%s' - URI lookup failed", filename);
  108. RETURN_FALSE;
  109. }
  110. if (rr->status == HTTP_OK) {
  111. object_init(return_value);
  112. ADD_LONG(status);
  113. ADD_STRING(the_request);
  114. ADD_STRING(status_line);
  115. ADD_STRING(method);
  116. ADD_TIME(mtime);
  117. ADD_LONG(clength);
  118. #if MODULE_MAGIC_NUMBER < 20020506
  119. ADD_STRING(boundary);
  120. #endif
  121. ADD_STRING(range);
  122. ADD_LONG(chunked);
  123. ADD_STRING(content_type);
  124. ADD_STRING(handler);
  125. ADD_LONG(no_cache);
  126. ADD_LONG(no_local_copy);
  127. ADD_STRING(unparsed_uri);
  128. ADD_STRING(uri);
  129. ADD_STRING(filename);
  130. ADD_STRING(path_info);
  131. ADD_STRING(args);
  132. ADD_LONG(allowed);
  133. ADD_LONG(sent_bodyct);
  134. ADD_LONG(bytes_sent);
  135. ADD_LONG(mtime);
  136. ADD_TIME(request_time);
  137. ap_destroy_sub_req(rr);
  138. return;
  139. }
  140. php_error_docref(NULL, E_WARNING, "Unable to include '%s' - error finding URI", 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. if (zend_parse_parameters_none() == FAILURE) {
  152. return;
  153. }
  154. array_init(return_value);
  155. ctx = SG(server_context);
  156. arr = apr_table_elts(ctx->r->headers_in);
  157. APR_ARRAY_FOREACH_OPEN(arr, key, val)
  158. if (!val) val = "";
  159. add_assoc_string(return_value, key, val);
  160. APR_ARRAY_FOREACH_CLOSE()
  161. }
  162. /* }}} */
  163. /* {{{ proto array apache_response_headers(void)
  164. Fetch all HTTP response headers */
  165. PHP_FUNCTION(apache_response_headers)
  166. {
  167. php_struct *ctx;
  168. const apr_array_header_t *arr;
  169. char *key, *val;
  170. if (zend_parse_parameters_none() == FAILURE) {
  171. return;
  172. }
  173. array_init(return_value);
  174. ctx = SG(server_context);
  175. arr = apr_table_elts(ctx->r->headers_out);
  176. APR_ARRAY_FOREACH_OPEN(arr, key, val)
  177. if (!val) val = "";
  178. add_assoc_string(return_value, key, val);
  179. APR_ARRAY_FOREACH_CLOSE()
  180. }
  181. /* }}} */
  182. /* {{{ proto string apache_note(string note_name [, string note_value])
  183. Get and set Apache request notes */
  184. PHP_FUNCTION(apache_note)
  185. {
  186. php_struct *ctx;
  187. char *note_name, *note_val = NULL;
  188. size_t note_name_len, note_val_len;
  189. char *old_note_val=NULL;
  190. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|s", &note_name, &note_name_len, &note_val, &note_val_len) == FAILURE) {
  191. return;
  192. }
  193. ctx = SG(server_context);
  194. old_note_val = (char *) apr_table_get(ctx->r->notes, note_name);
  195. if (note_val) {
  196. apr_table_set(ctx->r->notes, note_name, note_val);
  197. }
  198. if (old_note_val) {
  199. RETURN_STRING(old_note_val);
  200. }
  201. RETURN_FALSE;
  202. }
  203. /* }}} */
  204. /* {{{ proto bool apache_setenv(string variable, string value [, bool walk_to_top])
  205. Set an Apache subprocess_env variable */
  206. /*
  207. * XXX this doesn't look right. shouldn't it be the parent ?*/
  208. PHP_FUNCTION(apache_setenv)
  209. {
  210. php_struct *ctx;
  211. char *variable=NULL, *string_val=NULL;
  212. size_t variable_len, string_val_len;
  213. zend_bool walk_to_top = 0;
  214. int arg_count = ZEND_NUM_ARGS();
  215. request_rec *r;
  216. if (zend_parse_parameters(arg_count, "ss|b", &variable, &variable_len, &string_val, &string_val_len, &walk_to_top) == FAILURE) {
  217. return;
  218. }
  219. ctx = SG(server_context);
  220. r = ctx->r;
  221. if (arg_count == 3) {
  222. if (walk_to_top) {
  223. while(r->prev) {
  224. r = r->prev;
  225. }
  226. }
  227. }
  228. apr_table_set(r->subprocess_env, variable, string_val);
  229. RETURN_TRUE;
  230. }
  231. /* }}} */
  232. /* {{{ proto bool apache_getenv(string variable [, bool walk_to_top])
  233. Get an Apache subprocess_env variable */
  234. /*
  235. * XXX: shouldn't this be the parent not the 'prev'
  236. */
  237. PHP_FUNCTION(apache_getenv)
  238. {
  239. php_struct *ctx;
  240. char *variable;
  241. size_t variable_len;
  242. zend_bool walk_to_top = 0;
  243. int arg_count = ZEND_NUM_ARGS();
  244. char *env_val=NULL;
  245. request_rec *r;
  246. if (zend_parse_parameters(arg_count, "s|b", &variable, &variable_len, &walk_to_top) == FAILURE) {
  247. return;
  248. }
  249. ctx = SG(server_context);
  250. r = ctx->r;
  251. if (arg_count == 2) {
  252. if (walk_to_top) {
  253. while(r->prev) {
  254. r = r->prev;
  255. }
  256. }
  257. }
  258. env_val = (char*) apr_table_get(r->subprocess_env, variable);
  259. if (env_val != NULL) {
  260. RETURN_STRING(env_val);
  261. }
  262. RETURN_FALSE;
  263. }
  264. /* }}} */
  265. static char *php_apache_get_version()
  266. {
  267. #if MODULE_MAGIC_NUMBER_MAJOR >= 20060905
  268. return (char *) ap_get_server_banner();
  269. #else
  270. return (char *) ap_get_server_version();
  271. #endif
  272. }
  273. /* {{{ proto string apache_get_version(void)
  274. Fetch Apache version */
  275. PHP_FUNCTION(apache_get_version)
  276. {
  277. char *apv = php_apache_get_version();
  278. if (apv && *apv) {
  279. RETURN_STRING(apv);
  280. } else {
  281. RETURN_FALSE;
  282. }
  283. }
  284. /* }}} */
  285. /* {{{ proto array apache_get_modules(void)
  286. Get a list of loaded Apache modules */
  287. PHP_FUNCTION(apache_get_modules)
  288. {
  289. int n;
  290. char *p;
  291. array_init(return_value);
  292. for (n = 0; ap_loaded_modules[n]; ++n) {
  293. char *s = (char *) ap_loaded_modules[n]->name;
  294. if ((p = strchr(s, '.'))) {
  295. add_next_index_stringl(return_value, s, (p - s));
  296. } else {
  297. add_next_index_string(return_value, s);
  298. }
  299. }
  300. }
  301. /* }}} */
  302. PHP_MINFO_FUNCTION(apache)
  303. {
  304. char *apv = php_apache_get_version();
  305. smart_str tmp1 = {0};
  306. char tmp[1024];
  307. int n, max_requests;
  308. char *p;
  309. server_rec *serv = ((php_struct *) SG(server_context))->r->server;
  310. #ifndef PHP_WIN32
  311. # if MODULE_MAGIC_NUMBER_MAJOR >= 20081201
  312. AP_DECLARE_DATA extern unixd_config_rec ap_unixd_config;
  313. # else
  314. AP_DECLARE_DATA extern unixd_config_rec unixd_config;
  315. # endif
  316. #endif
  317. for (n = 0; ap_loaded_modules[n]; ++n) {
  318. char *s = (char *) ap_loaded_modules[n]->name;
  319. if ((p = strchr(s, '.'))) {
  320. smart_str_appendl(&tmp1, s, (p - s));
  321. } else {
  322. smart_str_appends(&tmp1, s);
  323. }
  324. smart_str_appendc(&tmp1, ' ');
  325. }
  326. if (tmp1.s) {
  327. if (tmp1.s->len > 0) {
  328. tmp1.s->val[tmp1.s->len - 1] = '\0';
  329. } else {
  330. tmp1.s->val[0] = '\0';
  331. }
  332. }
  333. php_info_print_table_start();
  334. if (apv && *apv) {
  335. php_info_print_table_row(2, "Apache Version", apv);
  336. }
  337. snprintf(tmp, sizeof(tmp), "%d", MODULE_MAGIC_NUMBER);
  338. php_info_print_table_row(2, "Apache API Version", tmp);
  339. if (serv->server_admin && *(serv->server_admin)) {
  340. php_info_print_table_row(2, "Server Administrator", serv->server_admin);
  341. }
  342. snprintf(tmp, sizeof(tmp), "%s:%u", serv->server_hostname, serv->port);
  343. php_info_print_table_row(2, "Hostname:Port", tmp);
  344. #ifndef PHP_WIN32
  345. #if MODULE_MAGIC_NUMBER_MAJOR >= 20081201
  346. snprintf(tmp, sizeof(tmp), "%s(%d)/%d", ap_unixd_config.user_name, ap_unixd_config.user_id, ap_unixd_config.group_id);
  347. #else
  348. snprintf(tmp, sizeof(tmp), "%s(%d)/%d", unixd_config.user_name, unixd_config.user_id, unixd_config.group_id);
  349. #endif
  350. php_info_print_table_row(2, "User/Group", tmp);
  351. #endif
  352. ap_mpm_query(AP_MPMQ_MAX_REQUESTS_DAEMON, &max_requests);
  353. 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);
  354. php_info_print_table_row(2, "Max Requests", tmp);
  355. apr_snprintf(tmp, sizeof tmp,
  356. "Connection: %" APR_TIME_T_FMT " - Keep-Alive: %" APR_TIME_T_FMT,
  357. apr_time_sec(serv->timeout), apr_time_sec(serv->keep_alive_timeout));
  358. php_info_print_table_row(2, "Timeouts", tmp);
  359. php_info_print_table_row(2, "Virtual Server", (serv->is_virtual ? "Yes" : "No"));
  360. php_info_print_table_row(2, "Server Root", ap_server_root);
  361. php_info_print_table_row(2, "Loaded Modules", tmp1.s->val);
  362. smart_str_free(&tmp1);
  363. php_info_print_table_end();
  364. DISPLAY_INI_ENTRIES();
  365. {
  366. const apr_array_header_t *arr = apr_table_elts(((php_struct *) SG(server_context))->r->subprocess_env);
  367. char *key, *val;
  368. SECTION("Apache Environment");
  369. php_info_print_table_start();
  370. php_info_print_table_header(2, "Variable", "Value");
  371. APR_ARRAY_FOREACH_OPEN(arr, key, val)
  372. if (!val) {
  373. val = "";
  374. }
  375. php_info_print_table_row(2, key, val);
  376. APR_ARRAY_FOREACH_CLOSE()
  377. php_info_print_table_end();
  378. SECTION("HTTP Headers Information");
  379. php_info_print_table_start();
  380. php_info_print_table_colspan_header(2, "HTTP Request Headers");
  381. php_info_print_table_row(2, "HTTP Request", ((php_struct *) SG(server_context))->r->the_request);
  382. arr = apr_table_elts(((php_struct *) SG(server_context))->r->headers_in);
  383. APR_ARRAY_FOREACH_OPEN(arr, key, val)
  384. if (!val) {
  385. val = "";
  386. }
  387. php_info_print_table_row(2, key, val);
  388. APR_ARRAY_FOREACH_CLOSE()
  389. php_info_print_table_colspan_header(2, "HTTP Response Headers");
  390. arr = apr_table_elts(((php_struct *) SG(server_context))->r->headers_out);
  391. APR_ARRAY_FOREACH_OPEN(arr, key, val)
  392. if (!val) {
  393. val = "";
  394. }
  395. php_info_print_table_row(2, key, val);
  396. APR_ARRAY_FOREACH_CLOSE()
  397. php_info_print_table_end();
  398. }
  399. }
  400. /* {{{ arginfo */
  401. ZEND_BEGIN_ARG_INFO_EX(arginfo_apache2handler_lookup_uri, 0, 0, 1)
  402. ZEND_ARG_INFO(0, filename)
  403. ZEND_END_ARG_INFO()
  404. ZEND_BEGIN_ARG_INFO_EX(arginfo_apache2handler_virtual, 0, 0, 1)
  405. ZEND_ARG_INFO(0, uri)
  406. ZEND_END_ARG_INFO()
  407. ZEND_BEGIN_ARG_INFO(arginfo_apache2handler_response_headers, 0)
  408. ZEND_END_ARG_INFO()
  409. ZEND_BEGIN_ARG_INFO(arginfo_apache2handler_getallheaders, 0)
  410. ZEND_END_ARG_INFO()
  411. ZEND_BEGIN_ARG_INFO_EX(arginfo_apache2handler_note, 0, 0, 1)
  412. ZEND_ARG_INFO(0, note_name)
  413. ZEND_ARG_INFO(0, note_value)
  414. ZEND_END_ARG_INFO()
  415. ZEND_BEGIN_ARG_INFO_EX(arginfo_apache2handler_setenv, 0, 0, 2)
  416. ZEND_ARG_INFO(0, variable)
  417. ZEND_ARG_INFO(0, value)
  418. ZEND_ARG_INFO(0, walk_to_top)
  419. ZEND_END_ARG_INFO()
  420. ZEND_BEGIN_ARG_INFO_EX(arginfo_apache2handler_getenv, 0, 0, 1)
  421. ZEND_ARG_INFO(0, variable)
  422. ZEND_ARG_INFO(0, walk_to_top)
  423. ZEND_END_ARG_INFO()
  424. ZEND_BEGIN_ARG_INFO(arginfo_apache2handler_get_version, 0)
  425. ZEND_END_ARG_INFO()
  426. ZEND_BEGIN_ARG_INFO(arginfo_apache2handler_get_modules, 0)
  427. ZEND_END_ARG_INFO()
  428. /* }}} */
  429. static const zend_function_entry apache_functions[] = {
  430. PHP_FE(apache_lookup_uri, arginfo_apache2handler_lookup_uri)
  431. PHP_FE(virtual, arginfo_apache2handler_virtual)
  432. PHP_FE(apache_request_headers, arginfo_apache2handler_getallheaders)
  433. PHP_FE(apache_response_headers, arginfo_apache2handler_response_headers)
  434. PHP_FE(apache_setenv, arginfo_apache2handler_setenv)
  435. PHP_FE(apache_getenv, arginfo_apache2handler_getenv)
  436. PHP_FE(apache_note, arginfo_apache2handler_note)
  437. PHP_FE(apache_get_version, arginfo_apache2handler_get_version)
  438. PHP_FE(apache_get_modules, arginfo_apache2handler_get_modules)
  439. PHP_FALIAS(getallheaders, apache_request_headers, arginfo_apache2handler_getallheaders)
  440. {NULL, NULL, NULL}
  441. };
  442. PHP_INI_BEGIN()
  443. STD_PHP_INI_ENTRY("xbithack", "0", PHP_INI_ALL, OnUpdateBool, xbithack, php_apache2_info_struct, php_apache2_info)
  444. STD_PHP_INI_ENTRY("engine", "1", PHP_INI_ALL, OnUpdateBool, engine, php_apache2_info_struct, php_apache2_info)
  445. STD_PHP_INI_ENTRY("last_modified", "0", PHP_INI_ALL, OnUpdateBool, last_modified, php_apache2_info_struct, php_apache2_info)
  446. PHP_INI_END()
  447. static PHP_MINIT_FUNCTION(apache)
  448. {
  449. #ifdef ZTS
  450. ts_allocate_id(&php_apache2_info_id, sizeof(php_apache2_info_struct), (ts_allocate_ctor) NULL, NULL);
  451. #endif
  452. REGISTER_INI_ENTRIES();
  453. return SUCCESS;
  454. }
  455. static PHP_MSHUTDOWN_FUNCTION(apache)
  456. {
  457. UNREGISTER_INI_ENTRIES();
  458. return SUCCESS;
  459. }
  460. zend_module_entry php_apache_module = {
  461. STANDARD_MODULE_HEADER,
  462. "apache2handler",
  463. apache_functions,
  464. PHP_MINIT(apache),
  465. PHP_MSHUTDOWN(apache),
  466. NULL,
  467. NULL,
  468. PHP_MINFO(apache),
  469. NULL,
  470. STANDARD_MODULE_PROPERTIES
  471. };
  472. /*
  473. * Local variables:
  474. * tab-width: 4
  475. * c-basic-offset: 4
  476. * End:
  477. * vim600: sw=4 ts=4 fdm=marker
  478. * vim<600: sw=4 ts=4
  479. */