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.

567 lines
16 KiB

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