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.

450 lines
13 KiB

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