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.

604 lines
17 KiB

25 years ago
24 years ago
25 years ago
24 years ago
24 years ago
25 years ago
23 years ago
23 years ago
23 years ago
26 years ago
25 years ago
26 years ago
24 years ago
26 years ago
26 years ago
26 years ago
19 years ago
19 years ago
26 years ago
26 years ago
25 years ago
26 years ago
26 years ago
26 years ago
21 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2008 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. | Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
  16. | Stig Sther Bakken <ssb@php.net> |
  17. | David Sklar <sklar@student.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. /* $Id$ */
  21. #include "php_apache_http.h"
  22. #if defined(PHP_WIN32) || defined(NETWARE)
  23. #include "zend.h"
  24. #include "ap_compat.h"
  25. #endif
  26. #ifdef ZTS
  27. int php_apache_info_id;
  28. #else
  29. php_apache_info_struct php_apache_info;
  30. #endif
  31. #define SECTION(name) PUTS("<h2>" name "</h2>\n")
  32. #ifndef PHP_WIN32
  33. extern module *top_module;
  34. extern module **ap_loaded_modules;
  35. #else
  36. extern __declspec(dllimport) module *top_module;
  37. extern __declspec(dllimport) module **ap_loaded_modules;
  38. #endif
  39. PHP_FUNCTION(virtual);
  40. PHP_FUNCTION(apache_request_headers);
  41. PHP_FUNCTION(apache_response_headers);
  42. PHP_FUNCTION(apachelog);
  43. PHP_FUNCTION(apache_note);
  44. PHP_FUNCTION(apache_lookup_uri);
  45. PHP_FUNCTION(apache_child_terminate);
  46. PHP_FUNCTION(apache_setenv);
  47. PHP_FUNCTION(apache_get_version);
  48. PHP_FUNCTION(apache_get_modules);
  49. PHP_FUNCTION(apache_reset_timeout);
  50. PHP_MINFO_FUNCTION(apache);
  51. ZEND_BEGIN_ARG_INFO(arginfo_apache_child_terminate, 0)
  52. ZEND_END_ARG_INFO()
  53. ZEND_BEGIN_ARG_INFO_EX(arginfo_apache_note, 0, 0, 1)
  54. ZEND_ARG_INFO(0, note_name)
  55. ZEND_ARG_INFO(0, note_value)
  56. ZEND_END_ARG_INFO()
  57. ZEND_BEGIN_ARG_INFO_EX(arginfo_apache_virtual, 0, 0, 1)
  58. ZEND_ARG_INFO(0, filename)
  59. ZEND_END_ARG_INFO()
  60. ZEND_BEGIN_ARG_INFO(arginfo_apache_request_headers, 0)
  61. ZEND_END_ARG_INFO()
  62. ZEND_BEGIN_ARG_INFO(arginfo_apache_response_headers, 0)
  63. ZEND_END_ARG_INFO()
  64. ZEND_BEGIN_ARG_INFO_EX(arginfo_apache_setenv, 0, 0, 2)
  65. ZEND_ARG_INFO(0, variable)
  66. ZEND_ARG_INFO(0, value)
  67. ZEND_ARG_INFO(0, walk_to_top)
  68. ZEND_END_ARG_INFO()
  69. ZEND_BEGIN_ARG_INFO_EX(arginfo_apache_lookup_uri, 0, 0, 1)
  70. ZEND_ARG_INFO(0, uri)
  71. ZEND_END_ARG_INFO()
  72. ZEND_BEGIN_ARG_INFO(arginfo_apache_get_version, 0)
  73. ZEND_END_ARG_INFO()
  74. ZEND_BEGIN_ARG_INFO(arginfo_apache_get_modules, 0)
  75. ZEND_END_ARG_INFO()
  76. ZEND_BEGIN_ARG_INFO(arginfo_apache_reset_timeout, 0)
  77. ZEND_END_ARG_INFO()
  78. const zend_function_entry apache_functions[] = {
  79. PHP_FE(virtual, arginfo_apache_virtual)
  80. PHP_FE(apache_request_headers, arginfo_apache_request_headers)
  81. PHP_FE(apache_note, arginfo_apache_note)
  82. PHP_FE(apache_lookup_uri, arginfo_apache_lookup_uri)
  83. PHP_FE(apache_child_terminate, arginfo_apache_child_terminate)
  84. PHP_FE(apache_setenv, arginfo_apache_setenv)
  85. PHP_FE(apache_response_headers, arginfo_apache_response_headers)
  86. PHP_FE(apache_get_version, arginfo_apache_get_version)
  87. PHP_FE(apache_get_modules, arginfo_apache_get_modules)
  88. PHP_FE(apache_reset_timeout, arginfo_apache_reset_timeout)
  89. PHP_FALIAS(getallheaders, apache_request_headers, arginfo_apache_request_headers)
  90. {NULL, NULL, NULL}
  91. };
  92. PHP_INI_BEGIN()
  93. STD_PHP_INI_ENTRY("xbithack", "0", PHP_INI_ALL, OnUpdateLong, xbithack, php_apache_info_struct, php_apache_info)
  94. STD_PHP_INI_ENTRY("engine", "1", PHP_INI_ALL, OnUpdateLong, engine, php_apache_info_struct, php_apache_info)
  95. STD_PHP_INI_ENTRY("last_modified", "0", PHP_INI_ALL, OnUpdateLong, last_modified, php_apache_info_struct, php_apache_info)
  96. STD_PHP_INI_ENTRY("child_terminate", "0", PHP_INI_ALL, OnUpdateLong, terminate_child, php_apache_info_struct, php_apache_info)
  97. PHP_INI_END()
  98. static void php_apache_globals_ctor(php_apache_info_struct *apache_globals TSRMLS_DC)
  99. {
  100. apache_globals->in_request = 0;
  101. }
  102. static PHP_MINIT_FUNCTION(apache)
  103. {
  104. #ifdef ZTS
  105. ts_allocate_id(&php_apache_info_id, sizeof(php_apache_info_struct), (ts_allocate_ctor) php_apache_globals_ctor, NULL);
  106. #else
  107. php_apache_globals_ctor(&php_apache_info TSRMLS_CC);
  108. #endif
  109. REGISTER_INI_ENTRIES();
  110. return SUCCESS;
  111. }
  112. static PHP_MSHUTDOWN_FUNCTION(apache)
  113. {
  114. UNREGISTER_INI_ENTRIES();
  115. return SUCCESS;
  116. }
  117. zend_module_entry apache_module_entry = {
  118. STANDARD_MODULE_HEADER,
  119. "apache",
  120. apache_functions,
  121. PHP_MINIT(apache),
  122. PHP_MSHUTDOWN(apache),
  123. NULL,
  124. NULL,
  125. PHP_MINFO(apache),
  126. NO_VERSION_YET,
  127. STANDARD_MODULE_PROPERTIES
  128. };
  129. /* {{{ proto bool apache_child_terminate(void)
  130. Terminate apache process after this request */
  131. PHP_FUNCTION(apache_child_terminate)
  132. {
  133. #ifndef MULTITHREAD
  134. if (AP(terminate_child)) {
  135. ap_child_terminate( ((request_rec *)SG(server_context)) );
  136. RETURN_TRUE;
  137. } else { /* tell them to get lost! */
  138. php_error_docref(NULL TSRMLS_CC, E_WARNING, "This function is disabled");
  139. RETURN_FALSE;
  140. }
  141. #else
  142. php_error_docref(NULL TSRMLS_CC, E_WARNING, "This function is not supported in this build");
  143. RETURN_FALSE;
  144. #endif
  145. }
  146. /* }}} */
  147. /* {{{ proto string apache_note(string note_name [, string note_value])
  148. Get and set Apache request notes */
  149. PHP_FUNCTION(apache_note)
  150. {
  151. char *note_name, *note_val;
  152. int note_name_len, note_val_len;
  153. char *old_val;
  154. int arg_count = ZEND_NUM_ARGS();
  155. if (zend_parse_parameters(arg_count TSRMLS_CC, "s|s", &note_name, &note_name_len, &note_val, &note_val_len) == FAILURE) {
  156. return;
  157. }
  158. old_val = (char *) table_get(((request_rec *)SG(server_context))->notes, note_name);
  159. if (arg_count == 2) {
  160. table_set(((request_rec *)SG(server_context))->notes, note_name, note_val);
  161. }
  162. if (old_val) {
  163. RETURN_STRING(old_val, 1);
  164. }
  165. RETURN_FALSE;
  166. }
  167. /* }}} */
  168. /* {{{ PHP_MINFO_FUNCTION
  169. */
  170. PHP_MINFO_FUNCTION(apache)
  171. {
  172. char *apv = (char *) ap_get_server_version();
  173. module *modp = NULL;
  174. char output_buf[128];
  175. #if !defined(WIN32) && !defined(WINNT)
  176. char name[64];
  177. char modulenames[1024];
  178. char *p;
  179. #endif
  180. server_rec *serv;
  181. extern char server_root[MAX_STRING_LEN];
  182. extern uid_t user_id;
  183. extern char *user_name;
  184. extern gid_t group_id;
  185. extern int max_requests_per_child;
  186. serv = ((request_rec *) SG(server_context))->server;
  187. php_info_print_table_start();
  188. #ifdef PHP_WIN32
  189. php_info_print_table_row(1, "Apache for Windows 95/NT");
  190. php_info_print_table_end();
  191. php_info_print_table_start();
  192. #elif defined(NETWARE)
  193. php_info_print_table_row(1, "Apache for NetWare");
  194. php_info_print_table_end();
  195. php_info_print_table_start();
  196. #else
  197. php_info_print_table_row(2, "APACHE_INCLUDE", PHP_APACHE_INCLUDE);
  198. php_info_print_table_row(2, "APACHE_TARGET", PHP_APACHE_TARGET);
  199. #endif
  200. if (apv && *apv) {
  201. php_info_print_table_row(2, "Apache Version", apv);
  202. }
  203. #ifdef APACHE_RELEASE
  204. sprintf(output_buf, "%d", APACHE_RELEASE);
  205. php_info_print_table_row(2, "Apache Release", output_buf);
  206. #endif
  207. sprintf(output_buf, "%d", MODULE_MAGIC_NUMBER);
  208. php_info_print_table_row(2, "Apache API Version", output_buf);
  209. sprintf(output_buf, "%s:%u", serv->server_hostname, serv->port);
  210. php_info_print_table_row(2, "Hostname:Port", output_buf);
  211. #if !defined(WIN32) && !defined(WINNT)
  212. sprintf(output_buf, "%s(%d)/%d", user_name, (int)user_id, (int)group_id);
  213. php_info_print_table_row(2, "User/Group", output_buf);
  214. sprintf(output_buf, "Per Child: %d - Keep Alive: %s - Max Per Connection: %d", max_requests_per_child, serv->keep_alive ? "on":"off", serv->keep_alive_max);
  215. php_info_print_table_row(2, "Max Requests", output_buf);
  216. #endif
  217. sprintf(output_buf, "Connection: %d - Keep-Alive: %d", serv->timeout, serv->keep_alive_timeout);
  218. php_info_print_table_row(2, "Timeouts", output_buf);
  219. #if !defined(WIN32) && !defined(WINNT)
  220. /*
  221. This block seems to be working on NetWare; But it seems to be showing
  222. all modules instead of just the loaded ones
  223. */
  224. php_info_print_table_row(2, "Server Root", server_root);
  225. strcpy(modulenames, "");
  226. for(modp = top_module; modp; modp = modp->next) {
  227. strlcpy(name, modp->name, sizeof(name));
  228. if ((p = strrchr(name, '.'))) {
  229. *p='\0'; /* Cut off ugly .c extensions on module names */
  230. }
  231. strlcat(modulenames, name, sizeof(modulenames));
  232. if (modp->next) {
  233. strlcat(modulenames, ", ", sizeof(modulenames));
  234. }
  235. }
  236. php_info_print_table_row(2, "Loaded Modules", modulenames);
  237. #endif
  238. php_info_print_table_end();
  239. DISPLAY_INI_ENTRIES();
  240. {
  241. register int i;
  242. array_header *arr;
  243. table_entry *elts;
  244. request_rec *r;
  245. r = ((request_rec *) SG(server_context));
  246. arr = table_elts(r->subprocess_env);
  247. elts = (table_entry *)arr->elts;
  248. SECTION("Apache Environment");
  249. php_info_print_table_start();
  250. php_info_print_table_header(2, "Variable", "Value");
  251. for (i=0; i < arr->nelts; i++) {
  252. php_info_print_table_row(2, elts[i].key, elts[i].val);
  253. }
  254. php_info_print_table_end();
  255. }
  256. {
  257. array_header *env_arr;
  258. table_entry *env;
  259. int i;
  260. request_rec *r;
  261. r = ((request_rec *) SG(server_context));
  262. SECTION("HTTP Headers Information");
  263. php_info_print_table_start();
  264. php_info_print_table_colspan_header(2, "HTTP Request Headers");
  265. php_info_print_table_row(2, "HTTP Request", r->the_request);
  266. env_arr = table_elts(r->headers_in);
  267. env = (table_entry *)env_arr->elts;
  268. for (i = 0; i < env_arr->nelts; ++i) {
  269. if (env[i].key) {
  270. php_info_print_table_row(2, env[i].key, env[i].val);
  271. }
  272. }
  273. php_info_print_table_colspan_header(2, "HTTP Response Headers");
  274. env_arr = table_elts(r->headers_out);
  275. env = (table_entry *)env_arr->elts;
  276. for(i = 0; i < env_arr->nelts; ++i) {
  277. if (env[i].key) {
  278. php_info_print_table_row(2, env[i].key, env[i].val);
  279. }
  280. }
  281. php_info_print_table_end();
  282. }
  283. }
  284. /* }}} */
  285. /* {{{ proto bool virtual(string filename)
  286. Perform an Apache sub-request */
  287. /* This function is equivalent to <!--#include virtual...-->
  288. * in mod_include. It does an Apache sub-request. It is useful
  289. * for including CGI scripts or .shtml files, or anything else
  290. * that you'd parse through Apache (for .phtml files, you'd probably
  291. * want to use <?Include>. This only works when PHP is compiled
  292. * as an Apache module, since it uses the Apache API for doing
  293. * sub requests.
  294. */
  295. PHP_FUNCTION(virtual)
  296. {
  297. char *filename;
  298. int filename_len;
  299. request_rec *rr = NULL;
  300. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE) {
  301. return;
  302. }
  303. if (!(rr = sub_req_lookup_uri (filename, ((request_rec *) SG(server_context))))) {
  304. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to include '%s' - URI lookup failed", filename);
  305. if (rr)
  306. destroy_sub_req (rr);
  307. RETURN_FALSE;
  308. }
  309. if (rr->status != 200) {
  310. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to include '%s' - error finding URI", filename);
  311. if (rr)
  312. destroy_sub_req (rr);
  313. RETURN_FALSE;
  314. }
  315. php_output_end_all(TSRMLS_C);
  316. php_header(TSRMLS_C);
  317. if (run_sub_req(rr)) {
  318. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to include '%s' - request execution failed", filename);
  319. if (rr)
  320. destroy_sub_req (rr);
  321. RETURN_FALSE;
  322. }
  323. if (rr)
  324. destroy_sub_req (rr);
  325. RETURN_TRUE;
  326. }
  327. /* }}} */
  328. /* {{{ proto array getallheaders(void)
  329. Alias for apache_request_headers() */
  330. /* }}} */
  331. /* {{{ proto array apache_request_headers(void)
  332. Fetch all HTTP request headers */
  333. PHP_FUNCTION(apache_request_headers)
  334. {
  335. array_header *env_arr;
  336. table_entry *tenv;
  337. int i;
  338. array_init(return_value);
  339. env_arr = table_elts(((request_rec *) SG(server_context))->headers_in);
  340. tenv = (table_entry *)env_arr->elts;
  341. for (i = 0; i < env_arr->nelts; ++i) {
  342. if (!tenv[i].key) {
  343. continue;
  344. }
  345. add_assoc_string(return_value, tenv[i].key, (tenv[i].val==NULL) ? "" : tenv[i].val, 1);
  346. }
  347. }
  348. /* }}} */
  349. /* {{{ proto array apache_response_headers(void)
  350. Fetch all HTTP response headers */
  351. PHP_FUNCTION(apache_response_headers)
  352. {
  353. array_header *env_arr;
  354. table_entry *tenv;
  355. int i;
  356. array_init(return_value);
  357. env_arr = table_elts(((request_rec *) SG(server_context))->headers_out);
  358. tenv = (table_entry *)env_arr->elts;
  359. for (i = 0; i < env_arr->nelts; ++i) {
  360. if (!tenv[i].key) continue;
  361. add_assoc_string(return_value, tenv[i].key, (tenv[i].val==NULL) ? "" : tenv[i].val, 1);
  362. }
  363. }
  364. /* }}} */
  365. /* {{{ proto bool apache_setenv(string variable, string value [, bool walk_to_top])
  366. Set an Apache subprocess_env variable */
  367. PHP_FUNCTION(apache_setenv)
  368. {
  369. int var_len, val_len;
  370. zend_bool top=0;
  371. char *var = NULL, *val = NULL;
  372. request_rec *r = (request_rec *) SG(server_context);
  373. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|b", &var, &var_len, &val, &val_len, &top) == FAILURE) {
  374. return;
  375. }
  376. while(top) {
  377. if(r->prev) r = r->prev;
  378. else break;
  379. }
  380. ap_table_setn(r->subprocess_env, ap_pstrndup(r->pool, var, var_len), ap_pstrndup(r->pool, val, val_len));
  381. RETURN_TRUE;
  382. }
  383. /* }}} */
  384. /* {{{ proto object apache_lookup_uri(string URI)
  385. Perform a partial request of the given URI to obtain information about it */
  386. PHP_FUNCTION(apache_lookup_uri)
  387. {
  388. char *filename;
  389. int filename_len;
  390. request_rec *rr=NULL;
  391. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE) {
  392. return;
  393. }
  394. if (!(rr = sub_req_lookup_uri(filename, ((request_rec *) SG(server_context))))) {
  395. php_error_docref(NULL TSRMLS_CC, E_WARNING, "URI lookup failed '%s'", filename);
  396. RETURN_FALSE;
  397. }
  398. object_init(return_value);
  399. add_property_long(return_value,"status", rr->status);
  400. if (rr->the_request) {
  401. add_property_string(return_value,"the_request", rr->the_request, 1);
  402. }
  403. if (rr->status_line) {
  404. add_property_string(return_value,"status_line", (char *)rr->status_line, 1);
  405. }
  406. if (rr->method) {
  407. add_property_string(return_value,"method", (char *)rr->method, 1);
  408. }
  409. if (rr->content_type) {
  410. add_property_string(return_value,"content_type", (char *)rr->content_type, 1);
  411. }
  412. if (rr->handler) {
  413. add_property_string(return_value,"handler", (char *)rr->handler, 1);
  414. }
  415. if (rr->uri) {
  416. add_property_string(return_value,"uri", rr->uri, 1);
  417. }
  418. if (rr->filename) {
  419. add_property_string(return_value,"filename", rr->filename, 1);
  420. }
  421. if (rr->path_info) {
  422. add_property_string(return_value,"path_info", rr->path_info, 1);
  423. }
  424. if (rr->args) {
  425. add_property_string(return_value,"args", rr->args, 1);
  426. }
  427. if (rr->boundary) {
  428. add_property_string(return_value,"boundary", rr->boundary, 1);
  429. }
  430. add_property_long(return_value,"no_cache", rr->no_cache);
  431. add_property_long(return_value,"no_local_copy", rr->no_local_copy);
  432. add_property_long(return_value,"allowed", rr->allowed);
  433. add_property_long(return_value,"sent_bodyct", rr->sent_bodyct);
  434. add_property_long(return_value,"bytes_sent", rr->bytes_sent);
  435. add_property_long(return_value,"byterange", rr->byterange);
  436. add_property_long(return_value,"clength", rr->clength);
  437. #if MODULE_MAGIC_NUMBER >= 19980324
  438. if (rr->unparsed_uri) {
  439. add_property_string(return_value,"unparsed_uri", rr->unparsed_uri, 1);
  440. }
  441. if(rr->mtime) {
  442. add_property_long(return_value,"mtime", rr->mtime);
  443. }
  444. #endif
  445. if(rr->request_time) {
  446. add_property_long(return_value,"request_time", rr->request_time);
  447. }
  448. destroy_sub_req(rr);
  449. }
  450. /* }}} */
  451. #if 0
  452. This function is most likely a bad idea. Just playing with it for now.
  453. PHP_FUNCTION(apache_exec_uri)
  454. {
  455. char *filename;
  456. int filename_len;
  457. request_rec *rr=NULL;
  458. TSRMLS_FETCH();
  459. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE) {
  460. return;
  461. }
  462. if(!(rr = ap_sub_req_lookup_uri(filename, ((request_rec *) SG(server_context))))) {
  463. php_error_docref(NULL TSRMLS_CC, E_WARNING, "URI lookup failed", filename);
  464. RETURN_FALSE;
  465. }
  466. RETVAL_LONG(ap_run_sub_req(rr));
  467. ap_destroy_sub_req(rr);
  468. }
  469. #endif
  470. /* {{{ proto string apache_get_version(void)
  471. Fetch Apache version */
  472. PHP_FUNCTION(apache_get_version)
  473. {
  474. char *apv = (char *) ap_get_server_version();
  475. if (apv && *apv) {
  476. RETURN_STRING(apv, 1);
  477. }
  478. RETURN_FALSE;
  479. }
  480. /* }}} */
  481. /* {{{ proto array apache_get_modules(void)
  482. Get a list of loaded Apache modules */
  483. PHP_FUNCTION(apache_get_modules)
  484. {
  485. int n;
  486. char *p;
  487. array_init(return_value);
  488. for (n = 0; ap_loaded_modules[n]; ++n) {
  489. char *s = (char *) ap_loaded_modules[n]->name;
  490. if ((p = strchr(s, '.'))) {
  491. add_next_index_stringl(return_value, s, (p - s), 1);
  492. } else {
  493. add_next_index_string(return_value, s, 1);
  494. }
  495. }
  496. }
  497. /* }}} */
  498. /* {{{ proto bool apache_reset_timeout(void)
  499. Reset the Apache write timer */
  500. PHP_FUNCTION(apache_reset_timeout)
  501. {
  502. ap_reset_timeout((request_rec *)SG(server_context));
  503. RETURN_TRUE;
  504. }
  505. /* }}} */
  506. /*
  507. * Local variables:
  508. * tab-width: 4
  509. * c-basic-offset: 4
  510. * End:
  511. * vim600: sw=4 ts=4 fdm=marker
  512. * vim<600: sw=4 ts=4
  513. */