php_functions.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Sascha Schumann <sascha@schumann.cx> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
  17. #include "php.h"
  18. #ifdef strcasecmp
  19. # undef strcasecmp
  20. #endif
  21. #ifdef strncasecmp
  22. # undef strncasecmp
  23. #endif
  24. #include "zend_smart_str.h"
  25. #include "ext/standard/info.h"
  26. #include "ext/standard/head.h"
  27. #include "php_ini.h"
  28. #include "SAPI.h"
  29. #define CORE_PRIVATE
  30. #include "apr_strings.h"
  31. #include "apr_time.h"
  32. #include "ap_config.h"
  33. #include "util_filter.h"
  34. #include "httpd.h"
  35. #include "http_config.h"
  36. #include "http_request.h"
  37. #include "http_core.h"
  38. #include "http_protocol.h"
  39. #include "http_log.h"
  40. #include "http_main.h"
  41. #include "util_script.h"
  42. #include "http_core.h"
  43. #include "ap_mpm.h"
  44. #ifndef PHP_WIN32
  45. #include "unixd.h"
  46. #endif
  47. #include "php_apache.h"
  48. #include "php_functions_arginfo.h"
  49. #ifdef ZTS
  50. int php_apache2_info_id;
  51. #else
  52. php_apache2_info_struct php_apache2_info;
  53. #endif
  54. #define SECTION(name) PUTS("<h2>" name "</h2>\n")
  55. static request_rec *php_apache_lookup_uri(char *filename)
  56. {
  57. php_struct *ctx = SG(server_context);
  58. if (!filename || !ctx || !ctx->r) {
  59. return NULL;
  60. }
  61. return ap_sub_req_lookup_uri(filename, ctx->r, ctx->r->output_filters);
  62. }
  63. /* {{{ Perform an apache sub-request */
  64. PHP_FUNCTION(virtual)
  65. {
  66. char *filename;
  67. size_t filename_len;
  68. request_rec *rr;
  69. if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &filename, &filename_len) == FAILURE) {
  70. RETURN_THROWS();
  71. }
  72. if (!(rr = php_apache_lookup_uri(filename))) {
  73. php_error_docref(NULL, E_WARNING, "Unable to include '%s' - URI lookup failed", filename);
  74. RETURN_FALSE;
  75. }
  76. if (rr->status != HTTP_OK) {
  77. php_error_docref(NULL, E_WARNING, "Unable to include '%s' - error finding URI", filename);
  78. ap_destroy_sub_req(rr);
  79. RETURN_FALSE;
  80. }
  81. /* Flush everything. */
  82. php_output_end_all();
  83. php_header();
  84. /* Ensure that the ap_r* layer for the main request is flushed, to
  85. * work around http://issues.apache.org/bugzilla/show_bug.cgi?id=17629 */
  86. ap_rflush(rr->main);
  87. if (ap_run_sub_req(rr)) {
  88. php_error_docref(NULL, E_WARNING, "Unable to include '%s' - request execution failed", filename);
  89. ap_destroy_sub_req(rr);
  90. RETURN_FALSE;
  91. }
  92. ap_destroy_sub_req(rr);
  93. RETURN_TRUE;
  94. }
  95. /* }}} */
  96. #define ADD_LONG(name) \
  97. add_property_long(return_value, #name, rr->name)
  98. #define ADD_TIME(name) \
  99. add_property_long(return_value, #name, apr_time_sec(rr->name));
  100. #define ADD_STRING(name) \
  101. if (rr->name) add_property_string(return_value, #name, (char *) rr->name)
  102. PHP_FUNCTION(apache_lookup_uri)
  103. {
  104. request_rec *rr;
  105. char *filename;
  106. size_t filename_len;
  107. if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &filename, &filename_len) == FAILURE) {
  108. RETURN_THROWS();
  109. }
  110. if (!(rr = php_apache_lookup_uri(filename))) {
  111. php_error_docref(NULL, E_WARNING, "Unable to include '%s' - URI lookup failed", filename);
  112. RETURN_FALSE;
  113. }
  114. if (rr->status == HTTP_OK) {
  115. object_init(return_value);
  116. ADD_LONG(status);
  117. ADD_STRING(the_request);
  118. ADD_STRING(status_line);
  119. ADD_STRING(method);
  120. ADD_TIME(mtime);
  121. ADD_LONG(clength);
  122. #if MODULE_MAGIC_NUMBER < 20020506
  123. ADD_STRING(boundary);
  124. #endif
  125. ADD_STRING(range);
  126. ADD_LONG(chunked);
  127. ADD_STRING(content_type);
  128. ADD_STRING(handler);
  129. ADD_LONG(no_cache);
  130. ADD_LONG(no_local_copy);
  131. ADD_STRING(unparsed_uri);
  132. ADD_STRING(uri);
  133. ADD_STRING(filename);
  134. ADD_STRING(path_info);
  135. ADD_STRING(args);
  136. ADD_LONG(allowed);
  137. ADD_LONG(sent_bodyct);
  138. ADD_LONG(bytes_sent);
  139. ADD_LONG(mtime);
  140. ADD_TIME(request_time);
  141. ap_destroy_sub_req(rr);
  142. return;
  143. }
  144. php_error_docref(NULL, E_WARNING, "Unable to include '%s' - error finding URI", filename);
  145. ap_destroy_sub_req(rr);
  146. RETURN_FALSE;
  147. }
  148. /* {{{ Fetch all HTTP request headers */
  149. PHP_FUNCTION(apache_request_headers)
  150. {
  151. php_struct *ctx;
  152. const apr_array_header_t *arr;
  153. char *key, *val;
  154. if (zend_parse_parameters_none() == FAILURE) {
  155. RETURN_THROWS();
  156. }
  157. array_init(return_value);
  158. ctx = SG(server_context);
  159. arr = apr_table_elts(ctx->r->headers_in);
  160. APR_ARRAY_FOREACH_OPEN(arr, key, val)
  161. if (!val) val = "";
  162. add_assoc_string(return_value, key, val);
  163. APR_ARRAY_FOREACH_CLOSE()
  164. }
  165. /* }}} */
  166. /* {{{ Fetch all HTTP response headers */
  167. PHP_FUNCTION(apache_response_headers)
  168. {
  169. php_struct *ctx;
  170. const apr_array_header_t *arr;
  171. char *key, *val;
  172. if (zend_parse_parameters_none() == FAILURE) {
  173. RETURN_THROWS();
  174. }
  175. array_init(return_value);
  176. ctx = SG(server_context);
  177. arr = apr_table_elts(ctx->r->headers_out);
  178. APR_ARRAY_FOREACH_OPEN(arr, key, val)
  179. if (!val) val = "";
  180. add_assoc_string(return_value, key, val);
  181. APR_ARRAY_FOREACH_CLOSE()
  182. }
  183. /* }}} */
  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_THROWS();
  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. /* {{{ 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. 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_THROWS();
  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. /* {{{ Get an Apache subprocess_env variable */
  233. /*
  234. * XXX: shouldn't this be the parent not the 'prev'
  235. */
  236. PHP_FUNCTION(apache_getenv)
  237. {
  238. php_struct *ctx;
  239. char *variable;
  240. size_t variable_len;
  241. bool walk_to_top = 0;
  242. int arg_count = ZEND_NUM_ARGS();
  243. char *env_val=NULL;
  244. request_rec *r;
  245. if (zend_parse_parameters(arg_count, "s|b", &variable, &variable_len, &walk_to_top) == FAILURE) {
  246. RETURN_THROWS();
  247. }
  248. ctx = SG(server_context);
  249. r = ctx->r;
  250. if (arg_count == 2) {
  251. if (walk_to_top) {
  252. while(r->prev) {
  253. r = r->prev;
  254. }
  255. }
  256. }
  257. env_val = (char*) apr_table_get(r->subprocess_env, variable);
  258. if (env_val != NULL) {
  259. RETURN_STRING(env_val);
  260. }
  261. RETURN_FALSE;
  262. }
  263. /* }}} */
  264. static char *php_apache_get_version()
  265. {
  266. #if MODULE_MAGIC_NUMBER_MAJOR >= 20060905
  267. return (char *) ap_get_server_banner();
  268. #else
  269. return (char *) ap_get_server_version();
  270. #endif
  271. }
  272. /* {{{ Fetch Apache version */
  273. PHP_FUNCTION(apache_get_version)
  274. {
  275. char *apv = php_apache_get_version();
  276. if (apv && *apv) {
  277. RETURN_STRING(apv);
  278. } else {
  279. RETURN_FALSE;
  280. }
  281. }
  282. /* }}} */
  283. /* {{{ Get a list of loaded Apache modules */
  284. PHP_FUNCTION(apache_get_modules)
  285. {
  286. int n;
  287. char *p;
  288. array_init(return_value);
  289. for (n = 0; ap_loaded_modules[n]; ++n) {
  290. char *s = (char *) ap_loaded_modules[n]->name;
  291. if ((p = strchr(s, '.'))) {
  292. add_next_index_stringl(return_value, s, (p - s));
  293. } else {
  294. add_next_index_string(return_value, s);
  295. }
  296. }
  297. }
  298. /* }}} */
  299. PHP_MINFO_FUNCTION(apache)
  300. {
  301. char *apv = php_apache_get_version();
  302. smart_str tmp1 = {0};
  303. char tmp[1024];
  304. int n, max_requests;
  305. char *p;
  306. server_rec *serv = ((php_struct *) SG(server_context))->r->server;
  307. #ifndef PHP_WIN32
  308. # if MODULE_MAGIC_NUMBER_MAJOR >= 20081201
  309. AP_DECLARE_DATA extern unixd_config_rec ap_unixd_config;
  310. # else
  311. AP_DECLARE_DATA extern unixd_config_rec unixd_config;
  312. # endif
  313. #endif
  314. for (n = 0; ap_loaded_modules[n]; ++n) {
  315. char *s = (char *) ap_loaded_modules[n]->name;
  316. if ((p = strchr(s, '.'))) {
  317. smart_str_appendl(&tmp1, s, (p - s));
  318. } else {
  319. smart_str_appends(&tmp1, s);
  320. }
  321. smart_str_appendc(&tmp1, ' ');
  322. }
  323. if (tmp1.s) {
  324. if (tmp1.s->len > 0) {
  325. tmp1.s->val[tmp1.s->len - 1] = '\0';
  326. } else {
  327. tmp1.s->val[0] = '\0';
  328. }
  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. #ifndef PHP_WIN32
  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.s->val);
  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. PHP_INI_BEGIN()
  398. STD_PHP_INI_BOOLEAN("xbithack", "0", PHP_INI_ALL, OnUpdateBool, xbithack, php_apache2_info_struct, php_apache2_info)
  399. STD_PHP_INI_BOOLEAN("engine", "1", PHP_INI_ALL, OnUpdateBool, engine, php_apache2_info_struct, php_apache2_info)
  400. STD_PHP_INI_BOOLEAN("last_modified", "0", PHP_INI_ALL, OnUpdateBool, last_modified, php_apache2_info_struct, php_apache2_info)
  401. PHP_INI_END()
  402. static PHP_MINIT_FUNCTION(apache)
  403. {
  404. #ifdef ZTS
  405. ts_allocate_id(&php_apache2_info_id, sizeof(php_apache2_info_struct), (ts_allocate_ctor) NULL, NULL);
  406. #endif
  407. REGISTER_INI_ENTRIES();
  408. return SUCCESS;
  409. }
  410. static PHP_MSHUTDOWN_FUNCTION(apache)
  411. {
  412. UNREGISTER_INI_ENTRIES();
  413. return SUCCESS;
  414. }
  415. zend_module_entry php_apache_module = {
  416. STANDARD_MODULE_HEADER,
  417. "apache2handler",
  418. ext_functions,
  419. PHP_MINIT(apache),
  420. PHP_MSHUTDOWN(apache),
  421. NULL,
  422. NULL,
  423. PHP_MINFO(apache),
  424. PHP_VERSION,
  425. STANDARD_MODULE_PROPERTIES
  426. };