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.

495 lines
14 KiB

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