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.

467 lines
13 KiB

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