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.

442 lines
13 KiB

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