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.

508 lines
15 KiB

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