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.

463 lines
13 KiB

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