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.

474 lines
13 KiB

26 years ago
25 years ago
26 years ago
25 years ago
26 years ago
26 years ago
26 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
  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)
  77. {
  78. apache_globals->in_request = 0;
  79. }
  80. static PHP_MINIT_FUNCTION(apache)
  81. {
  82. #ifdef ZTS
  83. php_apache_info_id = ts_allocate_id(sizeof(php_apache_info_struct), php_apache_globals_ctor, NULL);
  84. #else
  85. php_apache_globals_ctor(&php_apache_info);
  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. APLS_FETCH();
  104. SLS_FETCH();
  105. if (AP(terminate_child)) {
  106. ap_child_terminate( ((request_rec *)SG(server_context)) );
  107. } else { /* tell them to get lost! */
  108. php_error(E_WARNING, "apache.child_terminate is disabled");
  109. }
  110. #else
  111. php_error(E_WARNING, "apache_child_terminate() is not supported in this build");
  112. #endif
  113. }
  114. /* }}} */
  115. /* {{{ proto string apache_note(string note_name [, string note_value])
  116. Get and set Apache request notes */
  117. PHP_FUNCTION(apache_note)
  118. {
  119. pval **arg_name,**arg_val;
  120. char *note_val;
  121. int arg_count = ARG_COUNT(ht);
  122. SLS_FETCH();
  123. if (arg_count<1 || arg_count>2 ||
  124. zend_get_parameters_ex(arg_count,&arg_name,&arg_val) ==FAILURE ) {
  125. WRONG_PARAM_COUNT;
  126. }
  127. convert_to_string_ex(arg_name);
  128. note_val = (char *) table_get(((request_rec *)SG(server_context))->notes,(*arg_name)->value.str.val);
  129. if (arg_count == 2) {
  130. convert_to_string_ex(arg_val);
  131. table_set(((request_rec *)SG(server_context))->notes,(*arg_name)->value.str.val,(*arg_val)->value.str.val);
  132. }
  133. if (note_val) {
  134. RETURN_STRING(note_val,1);
  135. } else {
  136. RETURN_FALSE;
  137. }
  138. }
  139. /* }}} */
  140. /* {{{ PHP_MINFO_FUNCTION
  141. */
  142. PHP_MINFO_FUNCTION(apache)
  143. {
  144. module *modp = NULL;
  145. char output_buf[128];
  146. #if !defined(WIN32) && !defined(WINNT)
  147. char name[64];
  148. char modulenames[1024];
  149. char *p;
  150. #endif
  151. server_rec *serv;
  152. extern char server_root[MAX_STRING_LEN];
  153. extern uid_t user_id;
  154. extern char *user_name;
  155. extern gid_t group_id;
  156. extern int max_requests_per_child;
  157. SLS_FETCH();
  158. serv = ((request_rec *) SG(server_context))->server;
  159. php_info_print_table_start();
  160. #ifdef PHP_WIN32
  161. php_info_print_table_row(1, "Apache for Windows 95/NT");
  162. php_info_print_table_end();
  163. php_info_print_table_start();
  164. #else
  165. php_info_print_table_row(2, "APACHE_INCLUDE", PHP_APACHE_INCLUDE);
  166. php_info_print_table_row(2, "APACHE_TARGET", PHP_APACHE_TARGET);
  167. #endif
  168. php_info_print_table_row(2, "Apache Version", SERVER_VERSION);
  169. #ifdef APACHE_RELEASE
  170. sprintf(output_buf, "%d", APACHE_RELEASE);
  171. php_info_print_table_row(2, "Apache Release", output_buf);
  172. #endif
  173. sprintf(output_buf, "%d", MODULE_MAGIC_NUMBER);
  174. php_info_print_table_row(2, "Apache API Version", output_buf);
  175. sprintf(output_buf, "%s:%u", serv->server_hostname,serv->port);
  176. php_info_print_table_row(2, "Hostname:Port", output_buf);
  177. #if !defined(WIN32) && !defined(WINNT)
  178. sprintf(output_buf, "%s(%d)/%d", user_name,(int)user_id,(int)group_id);
  179. php_info_print_table_row(2, "User/Group", output_buf);
  180. 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);
  181. php_info_print_table_row(2, "Max Requests", output_buf);
  182. #endif
  183. sprintf(output_buf, "Connection: %d<br>Keep-Alive: %d",serv->timeout,serv->keep_alive_timeout);
  184. php_info_print_table_row(2, "Timeouts", output_buf);
  185. #if !defined(WIN32) && !defined(WINNT)
  186. php_info_print_table_row(2, "Server Root", server_root);
  187. strcpy(modulenames, "");
  188. for(modp = top_module; modp; modp = modp->next) {
  189. strlcpy(name, modp->name, sizeof(name));
  190. if ((p = strrchr(name, '.'))) {
  191. *p='\0'; /* Cut off ugly .c extensions on module names */
  192. }
  193. strcat(modulenames, name);
  194. if (modp->next) {
  195. strcat(modulenames, ", ");
  196. }
  197. }
  198. php_info_print_table_row(2, "Loaded Modules", modulenames);
  199. #endif
  200. php_info_print_table_end();
  201. DISPLAY_INI_ENTRIES();
  202. {
  203. register int i;
  204. array_header *arr;
  205. table_entry *elts;
  206. request_rec *r;
  207. SLS_FETCH();
  208. r = ((request_rec *) SG(server_context));
  209. arr = table_elts(r->subprocess_env);
  210. elts = (table_entry *)arr->elts;
  211. SECTION("Apache Environment");
  212. php_info_print_table_start();
  213. php_info_print_table_header(2, "Variable", "Value");
  214. for (i=0; i < arr->nelts; i++) {
  215. php_info_print_table_row(2, elts[i].key, elts[i].val);
  216. }
  217. php_info_print_table_end();
  218. }
  219. {
  220. array_header *env_arr;
  221. table_entry *env;
  222. int i;
  223. request_rec *r;
  224. SLS_FETCH();
  225. r = ((request_rec *) SG(server_context));
  226. SECTION("HTTP Headers Information");
  227. php_info_print_table_start();
  228. php_info_print_table_colspan_header(2, "HTTP Request Headers");
  229. php_info_print_table_row(2, "HTTP Request", r->the_request);
  230. env_arr = table_elts(r->headers_in);
  231. env = (table_entry *)env_arr->elts;
  232. for (i = 0; i < env_arr->nelts; ++i) {
  233. if (env[i].key) {
  234. php_info_print_table_row(2, env[i].key, env[i].val);
  235. }
  236. }
  237. php_info_print_table_colspan_header(2, "HTTP Response Headers");
  238. env_arr = table_elts(r->headers_out);
  239. env = (table_entry *)env_arr->elts;
  240. for(i = 0; i < env_arr->nelts; ++i) {
  241. if (env[i].key) {
  242. php_info_print_table_row(2, env[i].key, env[i].val);
  243. }
  244. }
  245. php_info_print_table_end();
  246. }
  247. }
  248. /* }}} */
  249. /* {{{ proto int virtual(string filename)
  250. Perform an Apache sub-request */
  251. /* This function is equivalent to <!--#include virtual...-->
  252. * in mod_include. It does an Apache sub-request. It is useful
  253. * for including CGI scripts or .shtml files, or anything else
  254. * that you'd parse through Apache (for .phtml files, you'd probably
  255. * want to use <?Include>. This only works when PHP is compiled
  256. * as an Apache module, since it uses the Apache API for doing
  257. * sub requests.
  258. */
  259. PHP_FUNCTION(virtual)
  260. {
  261. pval **filename;
  262. request_rec *rr = NULL;
  263. SLS_FETCH();
  264. if (ARG_COUNT(ht) != 1 || zend_get_parameters_ex(1,&filename) == FAILURE) {
  265. WRONG_PARAM_COUNT;
  266. }
  267. convert_to_string_ex(filename);
  268. if (!(rr = sub_req_lookup_uri ((*filename)->value.str.val,((request_rec *) SG(server_context))))) {
  269. php_error(E_WARNING, "Unable to include '%s' - URI lookup failed", (*filename)->value.str.val);
  270. if (rr) destroy_sub_req (rr);
  271. RETURN_FALSE;
  272. }
  273. if (rr->status != 200) {
  274. php_error(E_WARNING, "Unable to include '%s' - error finding URI", (*filename)->value.str.val);
  275. if (rr) destroy_sub_req (rr);
  276. RETURN_FALSE;
  277. }
  278. php_end_ob_buffers(1);
  279. php_header();
  280. if (run_sub_req(rr)) {
  281. php_error(E_WARNING, "Unable to include '%s' - request execution failed", (*filename)->value.str.val);
  282. if (rr) destroy_sub_req (rr);
  283. RETURN_FALSE;
  284. } else {
  285. if (rr) destroy_sub_req (rr);
  286. RETURN_TRUE;
  287. }
  288. }
  289. /* }}} */
  290. /* {{{ proto array getallheaders(void)
  291. Fetch all HTTP request headers */
  292. PHP_FUNCTION(getallheaders)
  293. {
  294. array_header *env_arr;
  295. table_entry *tenv;
  296. int i;
  297. SLS_FETCH();
  298. PLS_FETCH();
  299. if (array_init(return_value) == FAILURE) {
  300. RETURN_FALSE;
  301. }
  302. env_arr = table_elts(((request_rec *) SG(server_context))->headers_in);
  303. tenv = (table_entry *)env_arr->elts;
  304. for (i = 0; i < env_arr->nelts; ++i) {
  305. if (!tenv[i].key ||
  306. (PG(safe_mode) &&
  307. !strncasecmp(tenv[i].key, "authorization", 13))) {
  308. continue;
  309. }
  310. if (add_assoc_string(return_value, tenv[i].key,(tenv[i].val==NULL) ? "" : tenv[i].val, 1)==FAILURE) {
  311. RETURN_FALSE;
  312. }
  313. }
  314. }
  315. /* }}} */
  316. /* {{{ proto class apache_lookup_uri(string URI)
  317. Perform a partial request of the given URI to obtain information about it */
  318. PHP_FUNCTION(apache_lookup_uri)
  319. {
  320. pval **filename;
  321. request_rec *rr=NULL;
  322. SLS_FETCH();
  323. if (ARG_COUNT(ht) != 1 || zend_get_parameters_ex(1,&filename) == FAILURE) {
  324. WRONG_PARAM_COUNT;
  325. }
  326. convert_to_string_ex(filename);
  327. if(!(rr = sub_req_lookup_uri((*filename)->value.str.val,((request_rec *) SG(server_context))))) {
  328. php_error(E_WARNING, "URI lookup failed",(*filename)->value.str.val);
  329. RETURN_FALSE;
  330. }
  331. object_init(return_value);
  332. add_property_long(return_value,"status",rr->status);
  333. if (rr->the_request) {
  334. add_property_string(return_value,"the_request",rr->the_request,1);
  335. }
  336. if (rr->status_line) {
  337. add_property_string(return_value,"status_line",(char *)rr->status_line,1);
  338. }
  339. if (rr->method) {
  340. add_property_string(return_value,"method",(char *)rr->method,1);
  341. }
  342. if (rr->content_type) {
  343. add_property_string(return_value,"content_type",(char *)rr->content_type,1);
  344. }
  345. if (rr->handler) {
  346. add_property_string(return_value,"handler",(char *)rr->handler,1);
  347. }
  348. if (rr->uri) {
  349. add_property_string(return_value,"uri",rr->uri,1);
  350. }
  351. if (rr->filename) {
  352. add_property_string(return_value,"filename",rr->filename,1);
  353. }
  354. if (rr->path_info) {
  355. add_property_string(return_value,"path_info",rr->path_info,1);
  356. }
  357. if (rr->args) {
  358. add_property_string(return_value,"args",rr->args,1);
  359. }
  360. if (rr->boundary) {
  361. add_property_string(return_value,"boundary",rr->boundary,1);
  362. }
  363. add_property_long(return_value,"no_cache",rr->no_cache);
  364. add_property_long(return_value,"no_local_copy",rr->no_local_copy);
  365. add_property_long(return_value,"allowed",rr->allowed);
  366. add_property_long(return_value,"sent_bodyct",rr->sent_bodyct);
  367. add_property_long(return_value,"bytes_sent",rr->bytes_sent);
  368. add_property_long(return_value,"byterange",rr->byterange);
  369. add_property_long(return_value,"clength",rr->clength);
  370. #if MODULE_MAGIC_NUMBER >= 19980324
  371. if (rr->unparsed_uri) {
  372. add_property_string(return_value,"unparsed_uri",rr->unparsed_uri,1);
  373. }
  374. if(rr->mtime) {
  375. add_property_long(return_value,"mtime",rr->mtime);
  376. }
  377. #endif
  378. if(rr->request_time) {
  379. add_property_long(return_value,"request_time",rr->request_time);
  380. }
  381. destroy_sub_req(rr);
  382. }
  383. /* }}} */
  384. #if 0
  385. This function is most likely a bad idea. Just playing with it for now.
  386. PHP_FUNCTION(apache_exec_uri)
  387. {
  388. pval **filename;
  389. request_rec *rr=NULL;
  390. SLS_FETCH();
  391. if (ARG_COUNT(ht) != 1 || zend_get_parameters_ex(1,&filename) == FAILURE) {
  392. WRONG_PARAM_COUNT;
  393. }
  394. convert_to_string_ex(filename);
  395. if(!(rr = ap_sub_req_lookup_uri((*filename)->value.str.val,((request_rec *) SG(server_context))))) {
  396. php_error(E_WARNING, "URI lookup failed",(*filename)->value.str.val);
  397. RETURN_FALSE;
  398. }
  399. RETVAL_LONG(ap_run_sub_req(rr));
  400. ap_destroy_sub_req(rr);
  401. }
  402. #endif
  403. /*
  404. * Local variables:
  405. * tab-width: 4
  406. * c-basic-offset: 4
  407. * End:
  408. * vim600: sw=4 ts=4 tw=78 fdm=marker
  409. * vim<600: sw=4 ts=4 tw=78
  410. */