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.

468 lines
13 KiB

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