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.

1963 lines
54 KiB

20 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
19 years ago
19 years ago
19 years ago
22 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
20 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2008 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.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@php.net> |
  17. | David Sklar <sklar@student.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. /* $Id$ */
  21. #include "php_apache_http.h"
  22. #if defined(PHP_WIN32) || defined(NETWARE)
  23. #include "zend.h"
  24. #include "ap_compat.h"
  25. #else
  26. #include <build-defs.h>
  27. #endif
  28. #ifdef ZTS
  29. int php_apache_info_id;
  30. #else
  31. php_apache_info_struct php_apache_info;
  32. #endif
  33. #define SECTION(name) PUTS("<H2 align=\"center\">" name "</H2>\n")
  34. #undef offsetof
  35. #define offsetof(s_type,field) ((size_t)&(((s_type*)0)->field))
  36. extern module *top_module;
  37. extern module **ap_loaded_modules;
  38. static int le_apachereq;
  39. static zend_class_entry *apacherequest_class_entry;
  40. static void apache_table_to_zval(table *, zval *return_value);
  41. PHP_FUNCTION(virtual);
  42. PHP_FUNCTION(apache_request_headers);
  43. PHP_FUNCTION(apache_response_headers);
  44. PHP_FUNCTION(apachelog);
  45. PHP_FUNCTION(apache_note);
  46. PHP_FUNCTION(apache_lookup_uri);
  47. PHP_FUNCTION(apache_child_terminate);
  48. PHP_FUNCTION(apache_setenv);
  49. PHP_FUNCTION(apache_get_version);
  50. PHP_FUNCTION(apache_get_modules);
  51. PHP_MINFO_FUNCTION(apache);
  52. ZEND_BEGIN_ARG_INFO_EX(arginfo_apachehooks_virtual, 0, 0, 1)
  53. ZEND_ARG_INFO(0, filename)
  54. ZEND_END_ARG_INFO()
  55. ZEND_BEGIN_ARG_INFO(arginfo_apachehooks_request_headers, 0)
  56. ZEND_END_ARG_INFO()
  57. ZEND_BEGIN_ARG_INFO(arginfo_apachehooks_response_headers, 0)
  58. ZEND_END_ARG_INFO()
  59. ZEND_BEGIN_ARG_INFO_EX(arginfo_apachehooks_setenv, 0, 0, 2)
  60. ZEND_ARG_INFO(0, variable)
  61. ZEND_ARG_INFO(0, value)
  62. ZEND_ARG_INFO(0, walk_to_top)
  63. ZEND_END_ARG_INFO()
  64. ZEND_BEGIN_ARG_INFO_EX(arginfo_apachehooks_lookup_uri, 0, 0, 1)
  65. ZEND_ARG_INFO(0, uri)
  66. ZEND_END_ARG_INFO()
  67. ZEND_BEGIN_ARG_INFO(arginfo_apachehooks_get_version, 0)
  68. ZEND_END_ARG_INFO()
  69. ZEND_BEGIN_ARG_INFO(arginfo_apachehooks_get_modules, 0)
  70. ZEND_END_ARG_INFO()
  71. ZEND_BEGIN_ARG_INFO(arginfo_apachehooks_request_run, 0)
  72. ZEND_END_ARG_INFO()
  73. ZEND_BEGIN_ARG_INFO(arginfo_apachehooks_child_terminate, 0)
  74. ZEND_END_ARG_INFO()
  75. ZEND_BEGIN_ARG_INFO_EX(arginfo_apachehooks_note, 0, 0, 1)
  76. ZEND_ARG_INFO(0, note_name)
  77. ZEND_ARG_INFO(0, note_value)
  78. ZEND_END_ARG_INFO()
  79. const zend_function_entry apache_functions[] = {
  80. PHP_FE(virtual, arginfo_apachehooks_virtual)
  81. PHP_FE(apache_request_headers, arginfo_apachehooks_request_headers)
  82. PHP_FE(apache_note, arginfo_apachehooks_note)
  83. PHP_FE(apache_lookup_uri, arginfo_apachehooks_lookup_uri)
  84. PHP_FE(apache_child_terminate, arginfo_apachehooks_child_terminate)
  85. PHP_FE(apache_setenv, arginfo_apachehooks_setenv)
  86. PHP_FE(apache_response_headers, arginfo_apachehooks_response_headers)
  87. PHP_FE(apache_get_version, arginfo_apachehooks_get_version)
  88. PHP_FE(apache_get_modules, arginfo_apachehooks_get_modules)
  89. PHP_FALIAS(getallheaders, apache_request_headers, arginfo_apachehooks_request_headers)
  90. {NULL, NULL, NULL}
  91. };
  92. /* {{{ php_apache ini entries
  93. */
  94. PHP_INI_BEGIN()
  95. STD_PHP_INI_ENTRY("xbithack", "0", PHP_INI_ALL, OnUpdateLong, xbithack, php_apache_info_struct, php_apache_info)
  96. STD_PHP_INI_ENTRY("engine", "1", PHP_INI_ALL, OnUpdateLong, engine, php_apache_info_struct, php_apache_info)
  97. STD_PHP_INI_ENTRY("last_modified", "0", PHP_INI_ALL, OnUpdateLong, last_modified, php_apache_info_struct, php_apache_info)
  98. STD_PHP_INI_ENTRY("child_terminate", "0", PHP_INI_ALL, OnUpdateLong, terminate_child, php_apache_info_struct, php_apache_info)
  99. PHP_INI_END()
  100. /* }}} */
  101. static void php_apache_globals_ctor(php_apache_info_struct *apache_globals TSRMLS_DC)
  102. {
  103. apache_globals->in_request = 0;
  104. }
  105. #define APREQ_GET_THIS(ZVAL) if (NULL == (ZVAL = getThis())) { \
  106. php_error(E_WARNING, "%s(): underlying ApacheRequest object missing", \
  107. get_active_function_name(TSRMLS_C)); \
  108. RETURN_FALSE; \
  109. }
  110. #define APREQ_GET_REQUEST(ZVAL, R) APREQ_GET_THIS(ZVAL); \
  111. R = get_apache_request(ZVAL TSRMLS_CC)
  112. static void php_apache_request_free(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  113. {
  114. zval *z = (zval *)rsrc->ptr;
  115. /* fprintf(stderr, "%s() %p\n", __FUNCTION__, z); */
  116. zval_ptr_dtor(&z);
  117. }
  118. static request_rec *get_apache_request(zval *z TSRMLS_DC)
  119. {
  120. request_rec *r;
  121. zval **addr;
  122. if (NULL == z) {
  123. php_error(E_WARNING, "get_apache_request() invalid wrapper passed");
  124. return NULL;
  125. }
  126. if (Z_TYPE_P(z) != IS_OBJECT) {
  127. php_error(E_WARNING, "%s(): wrapper is not an object", get_active_function_name(TSRMLS_C));
  128. return NULL;
  129. }
  130. if (zend_hash_index_find(Z_OBJPROP_P(z), 0, (void **)&addr) == FAILURE) {
  131. php_error(E_WARNING, "%s(): underlying object missing", get_active_function_name(TSRMLS_C));
  132. return NULL;
  133. }
  134. r = (request_rec *)Z_LVAL_PP(addr);
  135. if (!r) {
  136. php_error(E_WARNING, "%s(): request_rec invalid", get_active_function_name(TSRMLS_C));
  137. return NULL;
  138. }
  139. return r;
  140. }
  141. /* {{{ php_apache_request_new(request_rec *r)
  142. * create a new zval-instance for ApacheRequest that wraps request_rec
  143. */
  144. zval *php_apache_request_new(request_rec *r)
  145. {
  146. zval *req;
  147. zval *addr;
  148. TSRMLS_FETCH();
  149. MAKE_STD_ZVAL(addr);
  150. Z_TYPE_P(addr) = IS_LONG;
  151. Z_LVAL_P(addr) = (int) r;
  152. MAKE_STD_ZVAL(req);
  153. object_init_ex(req, apacherequest_class_entry);
  154. zend_hash_index_update(Z_OBJPROP_P(req), 0, &addr, sizeof(zval *), NULL);
  155. return req;
  156. }
  157. /* }}} */
  158. /* {{{ apache_request_read_string_slot()
  159. */
  160. static void apache_request_read_string_slot(int offset, INTERNAL_FUNCTION_PARAMETERS)
  161. {
  162. zval *id;
  163. request_rec *r;
  164. char *s;
  165. if (zend_parse_parameters_none() == FAILURE) {
  166. return;
  167. }
  168. APREQ_GET_REQUEST(id, r);
  169. s = *(char **)((char*)r + offset);
  170. if (s)
  171. RETURN_STRING(s, 1);
  172. RETURN_EMPTY_STRING();
  173. }
  174. /* }}} */
  175. /* {{{ apache_request_string_slot()
  176. */
  177. static void apache_request_string_slot(int offset, INTERNAL_FUNCTION_PARAMETERS)
  178. {
  179. zval *id;
  180. request_rec *r;
  181. char *old_value, *new_value;
  182. int new_value_len;
  183. char **target;
  184. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &new_value, &new_value_len) == FAILURE) {
  185. return;
  186. }
  187. APREQ_GET_REQUEST(id, r);
  188. target = (char **)((char*)r + offset);
  189. old_value = *target;
  190. switch (ZEND_NUM_ARGS()) {
  191. case 0:
  192. break;
  193. case 1:
  194. *target = ap_pstrdup(r->pool, new_value);
  195. break;
  196. default:
  197. WRONG_PARAM_COUNT;
  198. break;
  199. }
  200. if (old_value)
  201. RETURN_STRING(old_value, 1);
  202. RETURN_EMPTY_STRING();
  203. }
  204. /* }}} */
  205. /* {{{ apache_request_read_int_slot()
  206. */
  207. static void apache_request_read_int_slot(int offset, INTERNAL_FUNCTION_PARAMETERS)
  208. {
  209. zval *id;
  210. request_rec *r;
  211. long l;
  212. if (zend_parse_parameters_none() == FAILURE) {
  213. return;
  214. }
  215. APREQ_GET_REQUEST(id, r);
  216. l = *(long *)((char*)r + offset);
  217. RETURN_LONG(l);
  218. }
  219. /* }}} */
  220. /* {{{ apache_request_int_slot()
  221. */
  222. static void apache_request_int_slot(int offset, INTERNAL_FUNCTION_PARAMETERS)
  223. {
  224. zval *id;
  225. request_rec *r;
  226. long old_value, new_value;
  227. long *target;
  228. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &new_value) == FAILURE) {
  229. return;
  230. }
  231. APREQ_GET_REQUEST(id, r);
  232. target = (long *)((char*)r + offset);
  233. old_value = *target;
  234. switch (ZEND_NUM_ARGS()) {
  235. case 0:
  236. break;
  237. case 1:
  238. *target = new_value;
  239. break;
  240. default:
  241. WRONG_PARAM_COUNT;
  242. break;
  243. }
  244. RETURN_LONG(old_value);
  245. }
  246. /* }}} */
  247. /* {{{ access string slots of request rec
  248. */
  249. /* {{{ proto string ApacheRequest::filename([string new_filename])
  250. */
  251. PHP_FUNCTION(apache_request_filename)
  252. {
  253. apache_request_string_slot(offsetof(request_rec, filename), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  254. }
  255. /* }}} */
  256. /* {{{ proto string ApacheRequest::uri([string new_uri])
  257. */
  258. PHP_FUNCTION(apache_request_uri)
  259. {
  260. apache_request_string_slot(offsetof(request_rec, uri), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  261. }
  262. /* }}} */
  263. /* {{{ proto string ApacheRequest::unparsed_uri([string new_unparsed_uri])
  264. */
  265. PHP_FUNCTION(apache_request_unparsed_uri)
  266. {
  267. apache_request_string_slot(offsetof(request_rec, unparsed_uri), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  268. }
  269. /* }}} */
  270. /* {{{ proto string ApacheRequest::path_info([string new_path_info])
  271. */
  272. PHP_FUNCTION(apache_request_path_info)
  273. {
  274. apache_request_string_slot(offsetof(request_rec, path_info), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  275. }
  276. /* }}} */
  277. /* {{{ proto string ApacheRequest::args([string new_args])
  278. */
  279. PHP_FUNCTION(apache_request_args)
  280. {
  281. apache_request_string_slot(offsetof(request_rec, args), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  282. }
  283. /* }}} */
  284. /* {{{ proto string ApacheRequest::boundary()
  285. */
  286. PHP_FUNCTION(apache_request_boundary)
  287. {
  288. apache_request_read_string_slot(offsetof(request_rec, boundary), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  289. }
  290. /* }}} */
  291. /* {{{ proto string ApacheRequest::content_type([string new_type])
  292. */
  293. PHP_FUNCTION(apache_request_content_type)
  294. {
  295. apache_request_string_slot(offsetof(request_rec, content_type), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  296. }
  297. /* }}} */
  298. /* {{{ proto string ApacheRequest::content_encoding([string new_encoding])
  299. */
  300. PHP_FUNCTION(apache_request_content_encoding)
  301. {
  302. apache_request_string_slot(offsetof(request_rec, content_encoding), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  303. }
  304. /* }}} */
  305. /* {{{ proto string ApacheRequest::handler([string new_handler])
  306. */
  307. PHP_FUNCTION(apache_request_handler)
  308. {
  309. apache_request_string_slot(offsetof(request_rec, handler), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  310. }
  311. /* }}} */
  312. /* {{{ proto string ApacheRequest::the_request()
  313. */
  314. PHP_FUNCTION(apache_request_the_request)
  315. {
  316. apache_request_read_string_slot(offsetof(request_rec, the_request), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  317. }
  318. /* }}} */
  319. /* {{{ proto string ApacheRequest::protocol()
  320. */
  321. PHP_FUNCTION(apache_request_protocol)
  322. {
  323. apache_request_read_string_slot(offsetof(request_rec, protocol), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  324. }
  325. /* }}} */
  326. /* {{{ proto string ApacheRequest::hostname()
  327. */
  328. PHP_FUNCTION(apache_request_hostname)
  329. {
  330. apache_request_read_string_slot(offsetof(request_rec, hostname), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  331. }
  332. /* }}} */
  333. /* {{{ proto string ApacheRequest::status_line([string new_status_line])
  334. */
  335. PHP_FUNCTION(apache_request_status_line)
  336. {
  337. apache_request_string_slot(offsetof(request_rec, status_line), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  338. }
  339. /* }}} */
  340. /* {{{ proto string ApacheRequest::method()
  341. */
  342. PHP_FUNCTION(apache_request_method)
  343. {
  344. apache_request_read_string_slot(offsetof(request_rec, method), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  345. }
  346. /* }}} */
  347. /* }}} access string slots of request rec */
  348. /* {{{ access int slots of request_rec
  349. */
  350. /* {{{ proto int ApacheRequest::proto_num()
  351. */
  352. PHP_FUNCTION(apache_request_proto_num)
  353. {
  354. apache_request_read_int_slot(offsetof(request_rec, proto_num), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  355. }
  356. /* }}} */
  357. /* {{{ proto int ApacheRequest::assbackwards()
  358. */
  359. PHP_FUNCTION(apache_request_assbackwards)
  360. {
  361. apache_request_read_int_slot(offsetof(request_rec, assbackwards), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  362. }
  363. /* }}} */
  364. /* {{{ proto int ApacheRequest::proxyreq([int new_proxyreq])
  365. */
  366. PHP_FUNCTION(apache_request_proxyreq)
  367. {
  368. apache_request_int_slot(offsetof(request_rec, proxyreq), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  369. }
  370. /* }}} */
  371. /* {{{ proto int ApacheRequest::chunked()
  372. */
  373. PHP_FUNCTION(apache_request_chunked)
  374. {
  375. apache_request_read_int_slot(offsetof(request_rec, chunked), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  376. }
  377. /* }}} */
  378. /* {{{ proto int ApacheRequest::header_only()
  379. */
  380. PHP_FUNCTION(apache_request_header_only)
  381. {
  382. apache_request_read_int_slot(offsetof(request_rec, header_only), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  383. }
  384. /* }}} */
  385. /* {{{ proto int ApacheRequest::request_time()
  386. */
  387. PHP_FUNCTION(apache_request_request_time)
  388. {
  389. apache_request_read_int_slot(offsetof(request_rec, request_time), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  390. }
  391. /* }}} */
  392. /* {{{ proto int ApacheRequest::status([int new_status])
  393. */
  394. PHP_FUNCTION(apache_request_status)
  395. {
  396. apache_request_int_slot(offsetof(request_rec, status), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  397. }
  398. /* }}} */
  399. /* {{{ proto int ApacheRequest::method_number([int method_number])
  400. */
  401. PHP_FUNCTION(apache_request_method_number)
  402. {
  403. apache_request_read_int_slot(offsetof(request_rec, method_number), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  404. }
  405. /* }}} */
  406. /* {{{ proto int ApacheRequest::allowed([int allowed])
  407. */
  408. PHP_FUNCTION(apache_request_allowed)
  409. {
  410. apache_request_int_slot(offsetof(request_rec, allowed), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  411. }
  412. /* }}} */
  413. /* {{{ proto int ApacheRequest::bytes_sent()
  414. */
  415. PHP_FUNCTION(apache_request_bytes_sent)
  416. {
  417. apache_request_read_int_slot(offsetof(request_rec, bytes_sent), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  418. }
  419. /* }}} */
  420. /* {{{ proto int ApacheRequest::mtime()
  421. */
  422. PHP_FUNCTION(apache_request_mtime)
  423. {
  424. apache_request_read_int_slot(offsetof(request_rec, mtime), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  425. }
  426. /* }}} */
  427. /* {{{ proto int ApacheRequest::content_length([int new_content_length])
  428. */
  429. PHP_FUNCTION(apache_request_content_length)
  430. {
  431. zval *id;
  432. long zlen;
  433. request_rec *r;
  434. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &zlen) == FAILURE) {
  435. return;
  436. }
  437. if (ZEND_NUM_ARGS() == 0) {
  438. apache_request_read_int_slot(offsetof(request_rec, clength), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  439. } else {
  440. APREQ_GET_REQUEST(id, r);
  441. (void)ap_set_content_length(r, zlen);
  442. RETURN_TRUE;
  443. }
  444. }
  445. /* }}} */
  446. /* {{{ proto int ApacheRequest::remaining()
  447. */
  448. PHP_FUNCTION(apache_request_remaining)
  449. {
  450. apache_request_read_int_slot(offsetof(request_rec, remaining), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  451. }
  452. /* }}} */
  453. /* {{{ proto int ApacheRequest::no_cache()
  454. */
  455. PHP_FUNCTION(apache_request_no_cache)
  456. {
  457. apache_request_int_slot(offsetof(request_rec, no_cache), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  458. }
  459. /* }}} */
  460. /* {{{ proto int ApacheRequest::no_local_copy()
  461. */
  462. PHP_FUNCTION(apache_request_no_local_copy)
  463. {
  464. apache_request_int_slot(offsetof(request_rec, no_local_copy), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  465. }
  466. /* }}} */
  467. /* {{{ proto int ApacheRequest::read_body()
  468. */
  469. PHP_FUNCTION(apache_request_read_body)
  470. {
  471. apache_request_int_slot(offsetof(request_rec, read_body), INTERNAL_FUNCTION_PARAM_PASSTHRU);
  472. }
  473. /* }}} */
  474. /* }}} access int slots of request_rec */
  475. /* {{{ proto array apache_request_headers_in()
  476. * fetch all incoming request headers
  477. */
  478. PHP_FUNCTION(apache_request_headers_in)
  479. {
  480. zval *id;
  481. request_rec *r;
  482. APREQ_GET_REQUEST(id, r);
  483. apache_table_to_zval(r->headers_in, return_value);
  484. }
  485. /* }}} */
  486. /* {{{ add_header_to_table
  487. */
  488. static void add_header_to_table(table *t, INTERNAL_FUNCTION_PARAMETERS)
  489. {
  490. zval *first = NULL;
  491. zval *second = NULL;
  492. zval **entry, **value;
  493. zstr string_key;
  494. uint string_key_len;
  495. ulong num_key;
  496. zend_bool replace = 0;
  497. HashPosition pos;
  498. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|zb", &first, &second, &replace) == FAILURE)
  499. RETURN_FALSE;
  500. if (Z_TYPE_P(first) == IS_ARRAY) {
  501. switch(ZEND_NUM_ARGS()) {
  502. case 1:
  503. case 3:
  504. zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(first), &pos);
  505. while (zend_hash_get_current_data_ex(Z_ARRVAL_P(first), (void **)&entry, &pos) == SUCCESS) {
  506. switch(zend_hash_get_current_key_ex(Z_ARRVAL_P(first), &string_key, &string_key_len, &num_key, 0, &pos)) {
  507. case HASH_KEY_IS_STRING:
  508. if (zend_hash_find(Z_ARRVAL_P(first), string_key.s, string_key_len, (void **)&value) == FAILURE) {
  509. zend_hash_move_forward_ex(Z_ARRVAL_P(first), &pos);
  510. continue;
  511. }
  512. if (!value) {
  513. zend_hash_move_forward_ex(Z_ARRVAL_P(first), &pos);
  514. continue;
  515. }
  516. convert_to_string_ex(value);
  517. if (replace)
  518. ap_table_set(t, string_key.s, Z_STRVAL_PP(value));
  519. else
  520. ap_table_merge(t, string_key.s, Z_STRVAL_PP(value));
  521. break;
  522. case HASH_KEY_IS_LONG:
  523. default:
  524. php_error(E_WARNING, "%s(): Can only add STRING keys to headers!", get_active_function_name(TSRMLS_C));
  525. break;
  526. }
  527. zend_hash_move_forward_ex(Z_ARRVAL_P(first), &pos);
  528. }
  529. break;
  530. default:
  531. WRONG_PARAM_COUNT;
  532. break;
  533. }
  534. }
  535. else if (Z_TYPE_P(first) == IS_STRING) {
  536. switch(ZEND_NUM_ARGS()) {
  537. case 2:
  538. case 3:
  539. convert_to_string_ex(&second);
  540. if (replace)
  541. ap_table_set(t, Z_STRVAL_P(first), Z_STRVAL_P(second));
  542. else
  543. ap_table_merge(t, Z_STRVAL_P(first), Z_STRVAL_P(second));
  544. break;
  545. default:
  546. WRONG_PARAM_COUNT;
  547. break;
  548. }
  549. }
  550. else {
  551. RETURN_FALSE;
  552. }
  553. }
  554. /* }}} */
  555. /* {{{ proto array apache_request_headers_out([{string name|array list} [, string value [, bool replace = false]]])
  556. * fetch all outgoing request headers
  557. */
  558. PHP_FUNCTION(apache_request_headers_out)
  559. {
  560. zval *id;
  561. request_rec *r;
  562. APREQ_GET_REQUEST(id, r);
  563. if (ZEND_NUM_ARGS() > 0)
  564. add_header_to_table(r->headers_out, INTERNAL_FUNCTION_PARAM_PASSTHRU);
  565. apache_table_to_zval(r->headers_out, return_value);
  566. }
  567. /* }}} */
  568. /* {{{ proto array apache_request_err_headers_out([{string name|array list} [, string value [, bool replace = false]]])
  569. * fetch all headers that go out in case of an error or a subrequest
  570. */
  571. PHP_FUNCTION(apache_request_err_headers_out)
  572. {
  573. zval *id;
  574. request_rec *r;
  575. APREQ_GET_REQUEST(id, r);
  576. if (ZEND_NUM_ARGS() > 0)
  577. add_header_to_table(r->err_headers_out, INTERNAL_FUNCTION_PARAM_PASSTHRU);
  578. apache_table_to_zval(r->err_headers_out, return_value);
  579. }
  580. /* }}} */
  581. /* {{{ proxy functions for the ap_* functions family
  582. */
  583. /* {{{ proto int apache_request_server_port()
  584. */
  585. PHP_FUNCTION(apache_request_server_port)
  586. {
  587. zval *id;
  588. request_rec *r;
  589. if (zend_parse_parameters_none() == FAILURE) {
  590. return;
  591. }
  592. APREQ_GET_REQUEST(id, r);
  593. RETURN_LONG(ap_get_server_port(r));
  594. }
  595. /* }}} */
  596. /* {{{ proto int apache_request_remote_host([int type])
  597. */
  598. PHP_FUNCTION(apache_request_remote_host)
  599. {
  600. zval *id;
  601. long ztype;
  602. request_rec *r;
  603. char *res;
  604. int type = REMOTE_NAME;
  605. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &ztype) == FAILURE) {
  606. return;
  607. }
  608. switch (ZEND_NUM_ARGS()) {
  609. case 0:
  610. break;
  611. case 1:
  612. type = ztype;
  613. break;
  614. }
  615. APREQ_GET_REQUEST(id, r);
  616. res = (char *)ap_get_remote_host(r->connection, r->per_dir_config, type);
  617. if (res)
  618. RETURN_STRING(res, 1);
  619. RETURN_EMPTY_STRING();
  620. }
  621. /* }}} */
  622. /* {{{ proto long apache_request_update_mtime([int dependency_mtime])
  623. */
  624. PHP_FUNCTION(apache_request_update_mtime)
  625. {
  626. zval *id;
  627. request_rec *r;
  628. int mtime = 0;
  629. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &mtime) == FAILURE) {
  630. return;
  631. }
  632. APREQ_GET_REQUEST(id, r);
  633. RETURN_LONG(ap_update_mtime(r, mtime));
  634. }
  635. /* }}} */
  636. /* {{{ proto void apache_request_set_etag()
  637. */
  638. PHP_FUNCTION(apache_request_set_etag)
  639. {
  640. zval *id;
  641. request_rec *r;
  642. if (zend_parse_parameters_none() == FAILURE) {
  643. return;
  644. }
  645. APREQ_GET_REQUEST(id, r);
  646. ap_set_etag(r);
  647. RETURN_TRUE;
  648. }
  649. /* }}} */
  650. /* {{{ proto void apache_request_set_last_modified()
  651. */
  652. PHP_FUNCTION(apache_request_set_last_modified)
  653. {
  654. zval *id;
  655. request_rec *r;
  656. if (zend_parse_parameters_none() == FAILURE) {
  657. return;
  658. }
  659. APREQ_GET_REQUEST(id, r);
  660. ap_set_last_modified(r);
  661. RETURN_TRUE;
  662. }
  663. /* }}} */
  664. /* {{{ proto long apache_request_meets_conditions()
  665. */
  666. PHP_FUNCTION(apache_request_meets_conditions)
  667. {
  668. zval *id;
  669. request_rec *r;
  670. if (zend_parse_parameters_none() == FAILURE) {
  671. return;
  672. }
  673. APREQ_GET_REQUEST(id, r);
  674. RETURN_LONG(ap_meets_conditions(r));
  675. }
  676. /* }}} */
  677. /* {{{ proto long apache_request_discard_request_body()
  678. */
  679. PHP_FUNCTION(apache_request_discard_request_body)
  680. {
  681. zval *id;
  682. request_rec *r;
  683. if (zend_parse_parameters_none() == FAILURE) {
  684. return;
  685. }
  686. APREQ_GET_REQUEST(id, r);
  687. RETURN_LONG(ap_discard_request_body(r));
  688. }
  689. /* }}} */
  690. /* {{{ proto long apache_request_satisfies()
  691. */
  692. PHP_FUNCTION(apache_request_satisfies)
  693. {
  694. zval *id;
  695. request_rec *r;
  696. if (zend_parse_parameters_none() == FAILURE) {
  697. return;
  698. }
  699. APREQ_GET_REQUEST(id, r);
  700. RETURN_LONG(ap_satisfies(r));
  701. }
  702. /* }}} */
  703. /* {{{ proto bool apache_request_is_initial_req()
  704. */
  705. PHP_FUNCTION(apache_request_is_initial_req)
  706. {
  707. zval *id;
  708. request_rec *r;
  709. if (zend_parse_parameters_none() == FAILURE) {
  710. return;
  711. }
  712. APREQ_GET_REQUEST(id, r);
  713. RETURN_BOOL(ap_is_initial_req(r));
  714. }
  715. /* }}} */
  716. /* {{{ proto bool apache_request_some_auth_required()
  717. */
  718. PHP_FUNCTION(apache_request_some_auth_required)
  719. {
  720. zval *id;
  721. request_rec *r;
  722. if (zend_parse_parameters_none() == FAILURE) {
  723. return;
  724. }
  725. APREQ_GET_REQUEST(id, r);
  726. RETURN_BOOL(ap_some_auth_required(r));
  727. }
  728. /* }}} */
  729. /* {{{ proto string apache_request_auth_type()
  730. */
  731. PHP_FUNCTION(apache_request_auth_type)
  732. {
  733. zval *id;
  734. request_rec *r;
  735. char *t;
  736. if (zend_parse_parameters_none() == FAILURE) {
  737. return;
  738. }
  739. APREQ_GET_REQUEST(id, r);
  740. t = (char *)ap_auth_type(r);
  741. if (!t)
  742. RETURN_NULL();
  743. RETURN_STRING(t, 1);
  744. }
  745. /* }}} */
  746. /* {{{ proto string apache_request_auth_name()
  747. */
  748. PHP_FUNCTION(apache_request_auth_name)
  749. {
  750. zval *id;
  751. request_rec *r;
  752. char *t;
  753. if (zend_parse_parameters_none() == FAILURE) {
  754. return;
  755. }
  756. APREQ_GET_REQUEST(id, r);
  757. t = (char *)ap_auth_name(r);
  758. if (!t)
  759. RETURN_NULL();
  760. RETURN_STRING(t, 1);
  761. }
  762. /* }}} */
  763. /* {{{ proto apache_request_basic_auth_pw()
  764. */
  765. PHP_FUNCTION(apache_request_basic_auth_pw)
  766. {
  767. zval *id, *zpw;
  768. request_rec *r;
  769. const char *pw;
  770. long status;
  771. if (ZEND_NUM_ARGS() != 1) {
  772. WRONG_PARAM_COUNT;
  773. }
  774. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zpw) == FAILURE) {
  775. RETURN_NULL();
  776. }
  777. if (!PZVAL_IS_REF(zpw)) {
  778. zend_error(E_WARNING, "Parameter wasn't passed by reference");
  779. RETURN_NULL();
  780. }
  781. APREQ_GET_REQUEST(id, r);
  782. pw = NULL;
  783. status = ap_get_basic_auth_pw(r, &pw);
  784. if (status == OK && pw) {
  785. ZVAL_STRING(zpw, (char *)pw, 1);
  786. }
  787. else
  788. ZVAL_NULL(zpw);
  789. RETURN_LONG(status);
  790. }
  791. /* }}} */
  792. /* http_protocol.h */
  793. PHP_FUNCTION(apache_request_send_http_header)
  794. {
  795. zval *id;
  796. request_rec *r;
  797. char *type = NULL;
  798. int typelen;
  799. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &type, &typelen) == FAILURE)
  800. return;
  801. APREQ_GET_REQUEST(id, r);
  802. if(type) {
  803. r->content_type = pstrdup(r->pool, type);
  804. }
  805. ap_send_http_header(r);
  806. SG(headers_sent) = 1;
  807. AP(headers_sent) = 1;
  808. RETURN_TRUE;
  809. }
  810. PHP_FUNCTION(apache_request_basic_http_header)
  811. {
  812. zval *id;
  813. request_rec *r;
  814. APREQ_GET_REQUEST(id, r);
  815. ap_basic_http_header((request_rec *)SG(server_context));
  816. SG(headers_sent) = 1;
  817. AP(headers_sent) = 1;
  818. RETURN_TRUE;
  819. }
  820. PHP_FUNCTION(apache_request_send_http_trace)
  821. {
  822. zval *id;
  823. request_rec *r;
  824. APREQ_GET_REQUEST(id, r);
  825. ap_send_http_trace((request_rec *)SG(server_context));
  826. SG(headers_sent) = 1;
  827. AP(headers_sent) = 1;
  828. RETURN_TRUE;
  829. }
  830. PHP_FUNCTION(apache_request_send_http_options)
  831. {
  832. zval *id;
  833. request_rec *r;
  834. APREQ_GET_REQUEST(id, r);
  835. ap_send_http_options((request_rec *)SG(server_context));
  836. SG(headers_sent) = 1;
  837. AP(headers_sent) = 1;
  838. RETURN_TRUE;
  839. }
  840. PHP_FUNCTION(apache_request_send_error_response)
  841. {
  842. zval *id;
  843. request_rec *r;
  844. int rec = 0;
  845. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &rec) == FAILURE) {
  846. return;
  847. }
  848. APREQ_GET_REQUEST(id, r);
  849. ap_send_error_response(r, rec);
  850. RETURN_TRUE;
  851. }
  852. PHP_FUNCTION(apache_request_set_content_length)
  853. {
  854. int length;
  855. zval *id;
  856. request_rec *r;
  857. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &length) == FAILURE) {
  858. return;
  859. }
  860. APREQ_GET_REQUEST(id, r);
  861. ap_set_content_length(r, length);
  862. RETURN_TRUE;
  863. }
  864. PHP_FUNCTION(apache_request_set_keepalive)
  865. {
  866. zval *id;
  867. request_rec *r;
  868. APREQ_GET_REQUEST(id, r);
  869. ap_set_keepalive(r);
  870. RETURN_TRUE;
  871. }
  872. /* This stuff should use streams or however this is implemented now
  873. PHP_FUNCTION(apache_request_send_fd)
  874. {
  875. }
  876. PHP_FUNCTION(apache_request_send_fd_length)
  877. {
  878. }
  879. */
  880. /* These are for overriding default output behaviour */
  881. PHP_FUNCTION(apache_request_rputs)
  882. {
  883. char *buffer;
  884. int buffer_len;
  885. zval *id;
  886. request_rec *r;
  887. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &buffer, &buffer_len) == FAILURE) {
  888. return;
  889. }
  890. APREQ_GET_REQUEST(id, r);
  891. ap_rwrite(buffer, buffer_len, (request_rec*)SG(server_context));
  892. }
  893. /* This stuff would be useful for custom POST handlers,
  894. which should be supported. Probably by not using
  895. sapi_activate at all inside a phpResponseHandler
  896. and instead using a builtin composed of the below
  897. calls as a apache_read_request_body() and allow
  898. people to custom craft their own.
  899. PHP_FUNCTION(apache_request_setup_client_block)
  900. {
  901. }
  902. PHP_FUNCTION(apache_request_should_client_block)
  903. {
  904. }
  905. PHP_FUNCTION(apache_request_get_client_block)
  906. {
  907. }
  908. PHP_FUNCTION(apache_request_discard_request_body)
  909. {
  910. }
  911. */
  912. /* http_log.h */
  913. /* {{{ proto boolean apache_request_log_error(string message, [long facility])
  914. */
  915. PHP_FUNCTION(apache_request_log_error)
  916. {
  917. zval *id;
  918. char *z_errstr;
  919. int z_errstr_len;
  920. int facility = APLOG_ERR;
  921. request_rec *r;
  922. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &z_errstr, &z_errstr_len, &facility) == FAILURE) {
  923. return;
  924. }
  925. APREQ_GET_REQUEST(id, r);
  926. ap_log_error(APLOG_MARK, facility, r->server, "%s", z_errstr);
  927. RETURN_TRUE;
  928. }
  929. /* }}} */
  930. /* http_main.h */
  931. /* {{{ proto object apache_request_sub_req_lookup_uri(string uri)
  932. Returns sub-request for the specified uri. You would
  933. need to run it yourself with run()
  934. */
  935. PHP_FUNCTION(apache_request_sub_req_lookup_uri)
  936. {
  937. zval *id;
  938. char *file;
  939. int file_len;
  940. request_rec *r, *sub_r;
  941. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &file, &file_len) == FAILURE) {
  942. return;
  943. }
  944. APREQ_GET_REQUEST(id, r);
  945. sub_r = ap_sub_req_lookup_uri(file, r);
  946. if (!sub_r) {
  947. RETURN_FALSE;
  948. }
  949. return_value = php_apache_request_new(sub_r);
  950. }
  951. /* }}} */
  952. /* {{{ proto object apache_request_sub_req_lookup_file(string file)
  953. Returns sub-request for the specified file. You would
  954. need to run it yourself with run().
  955. */
  956. PHP_FUNCTION(apache_request_sub_req_lookup_file)
  957. {
  958. zval *id;
  959. char *file;
  960. int file_len;
  961. request_rec *r, *sub_r;
  962. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &file, &file_len) == FAILURE) {
  963. return;
  964. }
  965. APREQ_GET_REQUEST(id, r);
  966. sub_r = ap_sub_req_lookup_file(file, r);
  967. if (!sub_r) {
  968. RETURN_FALSE;
  969. }
  970. return_value = php_apache_request_new(sub_r);
  971. }
  972. /* }}} */
  973. /* {{{ proto object apache_request_sub_req_method_uri(string method, string uri)
  974. Returns sub-request for the specified file. You would
  975. need to run it yourself with run().
  976. */
  977. PHP_FUNCTION(apache_request_sub_req_method_uri)
  978. {
  979. zval *id;
  980. char *file, *method;
  981. int file_len, method_len;
  982. request_rec *r, *sub_r;
  983. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &method, &method_len, &file, &file_len) == FAILURE) {
  984. return;
  985. }
  986. APREQ_GET_REQUEST(id, r);
  987. sub_r = ap_sub_req_method_uri(method, file, r);
  988. if (!sub_r) {
  989. RETURN_FALSE;
  990. }
  991. return_value = php_apache_request_new(sub_r);
  992. }
  993. /* }}} */
  994. /* {{{ proto long apache_request_run()
  995. This is a wrapper for ap_sub_run_req and ap_destory_sub_req. It takes
  996. sub_request, runs it, destroys it, and returns it's status.
  997. */
  998. PHP_FUNCTION(apache_request_run)
  999. {
  1000. zval *id;
  1001. request_rec *r;
  1002. int status;
  1003. APREQ_GET_REQUEST(id, r);
  1004. if(!r || ap_is_initial_req(r))
  1005. RETURN_FALSE;
  1006. status = ap_run_sub_req(r);
  1007. ap_destroy_sub_req(r);
  1008. RETURN_LONG(status);
  1009. }
  1010. /* }}} */
  1011. PHP_FUNCTION(apache_request_internal_redirect)
  1012. {
  1013. zval *id;
  1014. char *new_uri;
  1015. int new_uri_len;
  1016. request_rec *r;
  1017. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &new_uri, &new_uri_len) == FAILURE) {
  1018. return;
  1019. }
  1020. APREQ_GET_REQUEST(id, r);
  1021. ap_internal_redirect(new_uri, r);
  1022. }
  1023. PHP_FUNCTION(apache_request_send_header_field)
  1024. {
  1025. char *fieldname, *fieldval;
  1026. int fieldname_len, fieldval_len;
  1027. zval *id;
  1028. request_rec *r;
  1029. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &fieldname, &fieldname_len, &fieldval, &fieldval_len) == FAILURE) {
  1030. return;
  1031. }
  1032. APREQ_GET_REQUEST(id, r);
  1033. ap_send_header_field(r, fieldname, fieldval);
  1034. SG(headers_sent) = 1;
  1035. AP(headers_sent) = 1;
  1036. }
  1037. /* }}} */
  1038. /* {{{ php_apache_request_class_functions
  1039. */
  1040. static const zend_function_entry php_apache_request_class_functions[] = {
  1041. /* string slots */
  1042. PHP_FALIAS(args, apache_request_args, NULL)
  1043. PHP_FALIAS(boundary, apache_request_boundary, NULL)
  1044. PHP_FALIAS(content_encoding, apache_request_content_encoding, NULL)
  1045. PHP_FALIAS(content_type, apache_request_content_type, NULL)
  1046. PHP_FALIAS(filename, apache_request_filename, NULL)
  1047. PHP_FALIAS(handler, apache_request_handler, NULL)
  1048. PHP_FALIAS(hostname, apache_request_hostname, NULL)
  1049. PHP_FALIAS(method, apache_request_method, NULL)
  1050. PHP_FALIAS(path_info, apache_request_path_info, NULL)
  1051. PHP_FALIAS(protocol, apache_request_protocol, NULL)
  1052. PHP_FALIAS(status_line, apache_request_status_line, NULL)
  1053. PHP_FALIAS(the_request, apache_request_the_request, NULL)
  1054. PHP_FALIAS(unparsed_uri, apache_request_unparsed_uri, NULL)
  1055. PHP_FALIAS(uri, apache_request_uri, NULL)
  1056. /* int slots */
  1057. PHP_FALIAS(allowed, apache_request_allowed, NULL)
  1058. PHP_FALIAS(bytes_sent, apache_request_bytes_sent, NULL)
  1059. PHP_FALIAS(chunked, apache_request_chunked, NULL)
  1060. PHP_FALIAS(content_length, apache_request_content_length, NULL)
  1061. PHP_FALIAS(header_only, apache_request_header_only, NULL)
  1062. PHP_FALIAS(method_number, apache_request_method_number, NULL)
  1063. PHP_FALIAS(mtime, apache_request_mtime, NULL)
  1064. PHP_FALIAS(no_cache, apache_request_no_cache, NULL)
  1065. PHP_FALIAS(no_local_copy, apache_request_no_local_copy, NULL)
  1066. PHP_FALIAS(proto_num, apache_request_proto_num, NULL)
  1067. PHP_FALIAS(proxyreq, apache_request_proxyreq, NULL)
  1068. PHP_FALIAS(read_body, apache_request_read_body, NULL)
  1069. PHP_FALIAS(remaining, apache_request_remaining, NULL)
  1070. PHP_FALIAS(request_time, apache_request_request_time, NULL)
  1071. PHP_FALIAS(status, apache_request_status, NULL)
  1072. /* tables & arrays */
  1073. PHP_FALIAS(headers_in, apache_request_headers_in, NULL)
  1074. PHP_FALIAS(headers_out, apache_request_headers_out, NULL)
  1075. PHP_FALIAS(err_headers_out, apache_request_err_headers_out, NULL)
  1076. /* proxy functions for the ap_* functions family */
  1077. #undef auth_name
  1078. #undef auth_type
  1079. #undef discard_request_body
  1080. #undef is_initial_req
  1081. #undef meets_conditions
  1082. #undef satisfies
  1083. #undef set_etag
  1084. #undef set_last_modified
  1085. #undef some_auth_required
  1086. #undef update_mtime
  1087. #undef send_http_header
  1088. #undef send_header_field
  1089. #undef basic_http_header
  1090. #undef send_http_trace
  1091. #undef send_http_options
  1092. #undef send_error_response
  1093. #undef set_content_length
  1094. #undef set_keepalive
  1095. #undef rputs
  1096. #undef log_error
  1097. #undef lookup_uri
  1098. #undef lookup_file
  1099. #undef method_uri
  1100. #undef run
  1101. #undef internal_redirect
  1102. PHP_FALIAS(auth_name, apache_request_auth_name, NULL)
  1103. PHP_FALIAS(auth_type, apache_request_auth_type, NULL)
  1104. PHP_FALIAS(basic_auth_pw, apache_request_basic_auth_pw, NULL)
  1105. PHP_FALIAS(discard_request_body, apache_request_discard_request_body, NULL)
  1106. PHP_FALIAS(is_initial_req, apache_request_is_initial_req, NULL)
  1107. PHP_FALIAS(meets_conditions, apache_request_meets_conditions, NULL)
  1108. PHP_FALIAS(remote_host, apache_request_remote_host, NULL)
  1109. PHP_FALIAS(satisfies, apache_request_satisfies, NULL)
  1110. PHP_FALIAS(server_port, apache_request_server_port, NULL)
  1111. PHP_FALIAS(set_etag, apache_request_set_etag, NULL)
  1112. PHP_FALIAS(set_last_modified, apache_request_set_last_modified, NULL)
  1113. PHP_FALIAS(some_auth_required, apache_request_some_auth_required, NULL)
  1114. PHP_FALIAS(update_mtime, apache_request_update_mtime, NULL)
  1115. PHP_FALIAS(send_http_header, apache_request_send_http_header, NULL)
  1116. PHP_FALIAS(basic_http_header, apache_request_basic_http_header, NULL)
  1117. PHP_FALIAS(send_header_field, apache_request_send_header_field, NULL)
  1118. PHP_FALIAS(send_http_trace, apache_request_send_http_trace, NULL)
  1119. PHP_FALIAS(send_http_options, apache_request_send_http_trace, NULL)
  1120. PHP_FALIAS(send_error_response, apache_request_send_error_response, NULL)
  1121. PHP_FALIAS(set_content_length, apache_request_set_content_length, NULL)
  1122. PHP_FALIAS(set_keepalive, apache_request_set_keepalive, NULL)
  1123. PHP_FALIAS(rputs, apache_request_rputs, NULL)
  1124. PHP_FALIAS(log_error, apache_request_log_error, NULL)
  1125. PHP_FALIAS(lookup_uri, apache_request_sub_req_lookup_uri, NULL)
  1126. PHP_FALIAS(lookup_file, apache_request_sub_req_lookup_file, NULL)
  1127. PHP_FALIAS(method_uri, apache_request_sub_req_method_uri, NULL)
  1128. PHP_FALIAS(run, apache_request_run, NULL)
  1129. PHP_FALIAS(internal_redirect, apache_request_internal_redirect, NULL)
  1130. { NULL, NULL, NULL }
  1131. };
  1132. /* }}} */
  1133. static PHP_MINIT_FUNCTION(apache)
  1134. {
  1135. zend_class_entry ce;
  1136. #ifdef ZTS
  1137. ts_allocate_id(&php_apache_info_id, sizeof(php_apache_info_struct), (ts_allocate_ctor) php_apache_globals_ctor, NULL);
  1138. #else
  1139. php_apache_globals_ctor(&php_apache_info TSRMLS_CC);
  1140. #endif
  1141. REGISTER_INI_ENTRIES();
  1142. le_apachereq = zend_register_list_destructors_ex(php_apache_request_free, NULL, "ApacheRequest", module_number);
  1143. INIT_OVERLOADED_CLASS_ENTRY(ce, "ApacheRequest", php_apache_request_class_functions, NULL, NULL, NULL);
  1144. apacherequest_class_entry = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);
  1145. REGISTER_LONG_CONSTANT("OK", OK, CONST_CS | CONST_PERSISTENT);
  1146. REGISTER_LONG_CONSTANT("DECLINED", DECLINED, CONST_CS | CONST_PERSISTENT);
  1147. REGISTER_LONG_CONSTANT("FORBIDDEN", FORBIDDEN, CONST_CS | CONST_PERSISTENT);
  1148. REGISTER_LONG_CONSTANT("AUTH_REQUIRED", AUTH_REQUIRED, CONST_CS | CONST_PERSISTENT);
  1149. REGISTER_LONG_CONSTANT("DONE", DONE, CONST_CS | CONST_PERSISTENT);
  1150. REGISTER_LONG_CONSTANT("SERVER_ERROR", SERVER_ERROR, CONST_CS | CONST_PERSISTENT);
  1151. REGISTER_LONG_CONSTANT("REDIRECT", REDIRECT, CONST_CS | CONST_PERSISTENT);
  1152. REGISTER_LONG_CONSTANT("BAD_REQUEST", BAD_REQUEST, CONST_CS | CONST_PERSISTENT);
  1153. REGISTER_LONG_CONSTANT("NOT_FOUND", NOT_FOUND, CONST_CS | CONST_PERSISTENT);
  1154. REGISTER_LONG_CONSTANT("HTTP_CONTINUE", HTTP_CONTINUE, CONST_CS | CONST_PERSISTENT);
  1155. REGISTER_LONG_CONSTANT("HTTP_SWITCHING_PROTOCOLS", HTTP_SWITCHING_PROTOCOLS, CONST_CS | CONST_PERSISTENT);
  1156. REGISTER_LONG_CONSTANT("HTTP_PROCESSING", HTTP_PROCESSING, CONST_CS | CONST_PERSISTENT);
  1157. REGISTER_LONG_CONSTANT("HTTP_OK", HTTP_OK, CONST_CS | CONST_PERSISTENT);
  1158. REGISTER_LONG_CONSTANT("HTTP_CREATED", HTTP_CREATED, CONST_CS | CONST_PERSISTENT);
  1159. REGISTER_LONG_CONSTANT("HTTP_ACCEPTED", HTTP_ACCEPTED, CONST_CS | CONST_PERSISTENT);
  1160. REGISTER_LONG_CONSTANT("HTTP_NON_AUTHORITATIVE", HTTP_NON_AUTHORITATIVE, CONST_CS | CONST_PERSISTENT);
  1161. REGISTER_LONG_CONSTANT("HTTP_NO_CONTENT", HTTP_NO_CONTENT, CONST_CS | CONST_PERSISTENT);
  1162. REGISTER_LONG_CONSTANT("HTTP_RESET_CONTENT", HTTP_RESET_CONTENT, CONST_CS | CONST_PERSISTENT);
  1163. REGISTER_LONG_CONSTANT("HTTP_PARTIAL_CONTENT", HTTP_PARTIAL_CONTENT, CONST_CS | CONST_PERSISTENT);
  1164. REGISTER_LONG_CONSTANT("HTTP_MULTI_STATUS", HTTP_MULTI_STATUS, CONST_CS | CONST_PERSISTENT);
  1165. REGISTER_LONG_CONSTANT("HTTP_MULTIPLE_CHOICES", HTTP_MULTIPLE_CHOICES, CONST_CS | CONST_PERSISTENT);
  1166. REGISTER_LONG_CONSTANT("HTTP_MOVED_PERMANENTLY", HTTP_MOVED_PERMANENTLY, CONST_CS | CONST_PERSISTENT);
  1167. REGISTER_LONG_CONSTANT("HTTP_MOVED_TEMPORARILY", HTTP_MOVED_TEMPORARILY, CONST_CS | CONST_PERSISTENT);
  1168. REGISTER_LONG_CONSTANT("HTTP_SEE_OTHER", HTTP_SEE_OTHER, CONST_CS | CONST_PERSISTENT);
  1169. REGISTER_LONG_CONSTANT("HTTP_NOT_MODIFIED", HTTP_NOT_MODIFIED, CONST_CS | CONST_PERSISTENT);
  1170. REGISTER_LONG_CONSTANT("HTTP_USE_PROXY", HTTP_USE_PROXY, CONST_CS | CONST_PERSISTENT);
  1171. REGISTER_LONG_CONSTANT("HTTP_TEMPORARY_REDIRECT", HTTP_TEMPORARY_REDIRECT, CONST_CS | CONST_PERSISTENT);
  1172. REGISTER_LONG_CONSTANT("HTTP_BAD_REQUEST", HTTP_BAD_REQUEST, CONST_CS | CONST_PERSISTENT);
  1173. REGISTER_LONG_CONSTANT("HTTP_UNAUTHORIZED", HTTP_UNAUTHORIZED, CONST_CS | CONST_PERSISTENT);
  1174. REGISTER_LONG_CONSTANT("HTTP_PAYMENT_REQUIRED", HTTP_PAYMENT_REQUIRED, CONST_CS | CONST_PERSISTENT);
  1175. REGISTER_LONG_CONSTANT("HTTP_FORBIDDEN", HTTP_FORBIDDEN, CONST_CS | CONST_PERSISTENT);
  1176. REGISTER_LONG_CONSTANT("HTTP_NOT_FOUND", HTTP_NOT_FOUND, CONST_CS | CONST_PERSISTENT);
  1177. REGISTER_LONG_CONSTANT("HTTP_METHOD_NOT_ALLOWED", HTTP_METHOD_NOT_ALLOWED, CONST_CS | CONST_PERSISTENT);
  1178. REGISTER_LONG_CONSTANT("HTTP_NOT_ACCEPTABLE", HTTP_NOT_ACCEPTABLE, CONST_CS | CONST_PERSISTENT);
  1179. REGISTER_LONG_CONSTANT("HTTP_PROXY_AUTHENTICATION_REQUIRED", HTTP_PROXY_AUTHENTICATION_REQUIRED, CONST_CS | CONST_PERSISTENT);
  1180. REGISTER_LONG_CONSTANT("HTTP_REQUEST_TIME_OUT", HTTP_REQUEST_TIME_OUT, CONST_CS | CONST_PERSISTENT);
  1181. REGISTER_LONG_CONSTANT("HTTP_CONFLICT", HTTP_CONFLICT, CONST_CS | CONST_PERSISTENT);
  1182. REGISTER_LONG_CONSTANT("HTTP_GONE", HTTP_GONE, CONST_CS | CONST_PERSISTENT);REGISTER_LONG_CONSTANT("HTTP_LENGTH_REQUIRED", HTTP_LENGTH_REQUIRED, CONST_CS | CONST_PERSISTENT);
  1183. REGISTER_LONG_CONSTANT("HTTP_PRECONDITION_FAILED", HTTP_PRECONDITION_FAILED, CONST_CS | CONST_PERSISTENT);
  1184. REGISTER_LONG_CONSTANT("HTTP_REQUEST_ENTITY_TOO_LARGE", HTTP_REQUEST_ENTITY_TOO_LARGE, CONST_CS | CONST_PERSISTENT);
  1185. REGISTER_LONG_CONSTANT("HTTP_REQUEST_URI_TOO_LARGE", HTTP_REQUEST_URI_TOO_LARGE, CONST_CS | CONST_PERSISTENT);
  1186. REGISTER_LONG_CONSTANT("HTTP_UNSUPPORTED_MEDIA_TYPE", HTTP_UNSUPPORTED_MEDIA_TYPE, CONST_CS | CONST_PERSISTENT);
  1187. REGISTER_LONG_CONSTANT("HTTP_RANGE_NOT_SATISFIABLE", HTTP_RANGE_NOT_SATISFIABLE, CONST_CS | CONST_PERSISTENT);
  1188. REGISTER_LONG_CONSTANT("HTTP_EXPECTATION_FAILED", HTTP_EXPECTATION_FAILED, CONST_CS | CONST_PERSISTENT);
  1189. REGISTER_LONG_CONSTANT("HTTP_UNPROCESSABLE_ENTITY", HTTP_UNPROCESSABLE_ENTITY, CONST_CS | CONST_PERSISTENT);
  1190. REGISTER_LONG_CONSTANT("HTTP_LOCKED", HTTP_LOCKED, CONST_CS | CONST_PERSISTENT);
  1191. REGISTER_LONG_CONSTANT("HTTP_FAILED_DEPENDENCY", HTTP_FAILED_DEPENDENCY, CONST_CS | CONST_PERSISTENT);
  1192. REGISTER_LONG_CONSTANT("HTTP_INTERNAL_SERVER_ERROR", HTTP_INTERNAL_SERVER_ERROR, CONST_CS | CONST_PERSISTENT);
  1193. REGISTER_LONG_CONSTANT("HTTP_NOT_IMPLEMENTED", HTTP_NOT_IMPLEMENTED, CONST_CS | CONST_PERSISTENT);
  1194. REGISTER_LONG_CONSTANT("HTTP_BAD_GATEWAY", HTTP_BAD_GATEWAY, CONST_CS | CONST_PERSISTENT);
  1195. REGISTER_LONG_CONSTANT("HTTP_SERVICE_UNAVAILABLE", HTTP_SERVICE_UNAVAILABLE, CONST_CS | CONST_PERSISTENT);
  1196. REGISTER_LONG_CONSTANT("HTTP_GATEWAY_TIME_OUT", HTTP_GATEWAY_TIME_OUT, CONST_CS | CONST_PERSISTENT);
  1197. REGISTER_LONG_CONSTANT("HTTP_VERSION_NOT_SUPPORTED", HTTP_VERSION_NOT_SUPPORTED, CONST_CS | CONST_PERSISTENT);
  1198. REGISTER_LONG_CONSTANT("HTTP_VARIANT_ALSO_VARIES", HTTP_VARIANT_ALSO_VARIES, CONST_CS | CONST_PERSISTENT);
  1199. REGISTER_LONG_CONSTANT("HTTP_INSUFFICIENT_STORAGE", HTTP_INSUFFICIENT_STORAGE, CONST_CS | CONST_PERSISTENT);
  1200. REGISTER_LONG_CONSTANT("HTTP_NOT_EXTENDED", HTTP_NOT_EXTENDED, CONST_CS | CONST_PERSISTENT);
  1201. REGISTER_LONG_CONSTANT("APLOG_EMERG", APLOG_EMERG, CONST_CS | CONST_PERSISTENT);
  1202. REGISTER_LONG_CONSTANT("APLOG_ALERT", APLOG_ALERT, CONST_CS | CONST_PERSISTENT);
  1203. REGISTER_LONG_CONSTANT("APLOG_CRIT", APLOG_CRIT, CONST_CS | CONST_PERSISTENT);
  1204. REGISTER_LONG_CONSTANT("APLOG_ERR", APLOG_ERR, CONST_CS | CONST_PERSISTENT);
  1205. REGISTER_LONG_CONSTANT("APLOG_WARNING", APLOG_WARNING, CONST_CS | CONST_PERSISTENT);
  1206. REGISTER_LONG_CONSTANT("APLOG_NOTICE", APLOG_NOTICE, CONST_CS | CONST_PERSISTENT);
  1207. REGISTER_LONG_CONSTANT("APLOG_INFO", APLOG_INFO, CONST_CS | CONST_PERSISTENT);
  1208. REGISTER_LONG_CONSTANT("APLOG_DEBUG", APLOG_DEBUG, CONST_CS | CONST_PERSISTENT);
  1209. REGISTER_LONG_CONSTANT("M_GET", M_GET, CONST_CS | CONST_PERSISTENT);
  1210. REGISTER_LONG_CONSTANT("M_PUT", M_PUT, CONST_CS | CONST_PERSISTENT);
  1211. REGISTER_LONG_CONSTANT("M_POST", M_POST, CONST_CS | CONST_PERSISTENT);
  1212. REGISTER_LONG_CONSTANT("M_DELETE", M_DELETE, CONST_CS | CONST_PERSISTENT);
  1213. REGISTER_LONG_CONSTANT("M_CONNECT", M_CONNECT, CONST_CS | CONST_PERSISTENT);
  1214. REGISTER_LONG_CONSTANT("M_OPTIONS", M_OPTIONS, CONST_CS | CONST_PERSISTENT);
  1215. REGISTER_LONG_CONSTANT("M_TRACE", M_TRACE, CONST_CS | CONST_PERSISTENT);
  1216. REGISTER_LONG_CONSTANT("M_PATCH", M_PATCH, CONST_CS | CONST_PERSISTENT);
  1217. REGISTER_LONG_CONSTANT("M_PROPFIND", M_PROPFIND, CONST_CS | CONST_PERSISTENT);
  1218. REGISTER_LONG_CONSTANT("M_PROPPATCH", M_PROPPATCH, CONST_CS | CONST_PERSISTENT);
  1219. REGISTER_LONG_CONSTANT("M_MKCOL", M_MKCOL, CONST_CS | CONST_PERSISTENT);
  1220. REGISTER_LONG_CONSTANT("M_COPY", M_COPY, CONST_CS | CONST_PERSISTENT);
  1221. REGISTER_LONG_CONSTANT("M_MOVE", M_MOVE, CONST_CS | CONST_PERSISTENT);
  1222. REGISTER_LONG_CONSTANT("M_LOCK", M_LOCK, CONST_CS | CONST_PERSISTENT);
  1223. REGISTER_LONG_CONSTANT("M_UNLOCK", M_UNLOCK, CONST_CS | CONST_PERSISTENT);
  1224. REGISTER_LONG_CONSTANT("M_INVALID", M_INVALID, CONST_CS | CONST_PERSISTENT);
  1225. /* Possible values for request_rec.read_body (set by handling module):
  1226. * REQUEST_NO_BODY Send 413 error if message has any body
  1227. * REQUEST_CHUNKED_ERROR Send 411 error if body without Content-Length
  1228. * REQUEST_CHUNKED_DECHUNK If chunked, remove the chunks for me.
  1229. * REQUEST_CHUNKED_PASS Pass the chunks to me without removal.
  1230. */
  1231. REGISTER_LONG_CONSTANT("REQUEST_NO_BODY", REQUEST_NO_BODY, CONST_CS | CONST_PERSISTENT);
  1232. REGISTER_LONG_CONSTANT("REQUEST_CHUNKED_ERROR", REQUEST_CHUNKED_ERROR, CONST_CS | CONST_PERSISTENT);
  1233. REGISTER_LONG_CONSTANT("REQUEST_CHUNKED_DECHUNK", REQUEST_CHUNKED_DECHUNK, CONST_CS | CONST_PERSISTENT);
  1234. REGISTER_LONG_CONSTANT("REQUEST_CHUNKED_PASS", REQUEST_CHUNKED_PASS, CONST_CS | CONST_PERSISTENT);
  1235. /* resolve types for remote_host() */
  1236. REGISTER_LONG_CONSTANT("REMOTE_HOST", REMOTE_HOST, CONST_CS | CONST_PERSISTENT);
  1237. REGISTER_LONG_CONSTANT("REMOTE_NAME", REMOTE_NAME, CONST_CS | CONST_PERSISTENT);
  1238. REGISTER_LONG_CONSTANT("REMOTE_NOLOOKUP", REMOTE_NOLOOKUP, CONST_CS | CONST_PERSISTENT);
  1239. REGISTER_LONG_CONSTANT("REMOTE_DOUBLE_REV", REMOTE_DOUBLE_REV, CONST_CS | CONST_PERSISTENT);
  1240. return SUCCESS;
  1241. }
  1242. static PHP_MSHUTDOWN_FUNCTION(apache)
  1243. {
  1244. UNREGISTER_INI_ENTRIES();
  1245. return SUCCESS;
  1246. }
  1247. zend_module_entry apache_module_entry = {
  1248. STANDARD_MODULE_HEADER,
  1249. "apache",
  1250. apache_functions,
  1251. PHP_MINIT(apache),
  1252. PHP_MSHUTDOWN(apache),
  1253. NULL,
  1254. NULL,
  1255. PHP_MINFO(apache),
  1256. NO_VERSION_YET,
  1257. STANDARD_MODULE_PROPERTIES
  1258. };
  1259. /* {{{ proto bool apache_child_terminate(void)
  1260. Terminate apache process after this request */
  1261. PHP_FUNCTION(apache_child_terminate)
  1262. {
  1263. #ifndef MULTITHREAD
  1264. if (AP(terminate_child)) {
  1265. ap_child_terminate( ((request_rec *)SG(server_context)) );
  1266. RETURN_TRUE;
  1267. } else { /* tell them to get lost! */
  1268. php_error(E_WARNING, "apache.child_terminate is disabled");
  1269. RETURN_FALSE;
  1270. }
  1271. #else
  1272. php_error(E_WARNING, "apache_child_terminate() is not supported in this build");
  1273. RETURN_FALSE;
  1274. #endif
  1275. }
  1276. /* }}} */
  1277. /* {{{ proto string apache_note(string note_name [, string note_value])
  1278. Get and set Apache request notes */
  1279. PHP_FUNCTION(apache_note)
  1280. {
  1281. char *arg_name, *arg_val;
  1282. int arg_name_len, arg_val_len;
  1283. char *note_val;
  1284. int arg_count = ZEND_NUM_ARGS();
  1285. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &arg_name, &arg_name_len, &arg_val, &arg_val_len) == FAILURE) {
  1286. return;
  1287. }
  1288. note_val = (char *) table_get(((request_rec *)SG(server_context))->notes, arg_name);
  1289. if (arg_count == 2) {
  1290. table_set(((request_rec *)SG(server_context))->notes, arg_name, arg_val);
  1291. }
  1292. if (!note_val) {
  1293. RETURN_FALSE;
  1294. }
  1295. RETURN_STRING(note_val, 1);
  1296. }
  1297. /* }}} */
  1298. /* {{{ PHP_MINFO_FUNCTION
  1299. */
  1300. PHP_MINFO_FUNCTION(apache)
  1301. {
  1302. module *modp = NULL;
  1303. char output_buf[128];
  1304. #if !defined(WIN32) && !defined(WINNT)
  1305. char name[64];
  1306. char modulenames[1024];
  1307. char *p;
  1308. #endif
  1309. server_rec *serv;
  1310. extern char server_root[MAX_STRING_LEN];
  1311. extern uid_t user_id;
  1312. extern char *user_name;
  1313. extern gid_t group_id;
  1314. extern int max_requests_per_child;
  1315. serv = ((request_rec *) SG(server_context))->server;
  1316. php_info_print_table_start();
  1317. #ifdef PHP_WIN32
  1318. php_info_print_table_row(1, "Apache for Windows 95/NT");
  1319. php_info_print_table_end();
  1320. php_info_print_table_start();
  1321. #elif defined(NETWARE)
  1322. php_info_print_table_row(1, "Apache for NetWare");
  1323. php_info_print_table_end();
  1324. php_info_print_table_start();
  1325. #else
  1326. php_info_print_table_row(2, "APACHE_INCLUDE", PHP_APACHE_INCLUDE);
  1327. php_info_print_table_row(2, "APACHE_TARGET", PHP_APACHE_TARGET);
  1328. #endif
  1329. php_info_print_table_row(2, "Apache Version", SERVER_VERSION);
  1330. #ifdef APACHE_RELEASE
  1331. sprintf(output_buf, "%d", APACHE_RELEASE);
  1332. php_info_print_table_row(2, "Apache Release", output_buf);
  1333. #endif
  1334. sprintf(output_buf, "%d", MODULE_MAGIC_NUMBER);
  1335. php_info_print_table_row(2, "Apache API Version", output_buf);
  1336. sprintf(output_buf, "%s:%u", serv->server_hostname, serv->port);
  1337. php_info_print_table_row(2, "Hostname:Port", output_buf);
  1338. #if !defined(WIN32) && !defined(WINNT)
  1339. sprintf(output_buf, "%s(%d)/%d", user_name, (int)user_id, (int)group_id);
  1340. php_info_print_table_row(2, "User/Group", output_buf);
  1341. 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);
  1342. php_info_print_table_row(2, "Max Requests", output_buf);
  1343. #endif
  1344. sprintf(output_buf, "Connection: %d - Keep-Alive: %d", serv->timeout, serv->keep_alive_timeout);
  1345. php_info_print_table_row(2, "Timeouts", output_buf);
  1346. #if !defined(WIN32) && !defined(WINNT)
  1347. /*
  1348. This block seems to be working on NetWare; But it seems to be showing
  1349. all modules instead of just the loaded ones
  1350. */
  1351. php_info_print_table_row(2, "Server Root", server_root);
  1352. strcpy(modulenames, "");
  1353. for(modp = top_module; modp; modp = modp->next) {
  1354. strlcpy(name, modp->name, sizeof(name));
  1355. if ((p = strrchr(name, '.'))) {
  1356. *p='\0'; /* Cut off ugly .c extensions on module names */
  1357. }
  1358. strlcat(modulenames, name, sizeof(modulenames));
  1359. if (modp->next) {
  1360. strlcat(modulenames, ", ", sizeof(modulenames));
  1361. }
  1362. }
  1363. php_info_print_table_row(2, "Loaded Modules", modulenames);
  1364. #endif
  1365. php_info_print_table_end();
  1366. DISPLAY_INI_ENTRIES();
  1367. {
  1368. register int i;
  1369. array_header *arr;
  1370. table_entry *elts;
  1371. request_rec *r;
  1372. r = ((request_rec *) SG(server_context));
  1373. arr = table_elts(r->subprocess_env);
  1374. elts = (table_entry *)arr->elts;
  1375. SECTION("Apache Environment");
  1376. php_info_print_table_start();
  1377. php_info_print_table_header(2, "Variable", "Value");
  1378. for (i=0; i < arr->nelts; i++) {
  1379. php_info_print_table_row(2, elts[i].key, elts[i].val);
  1380. }
  1381. php_info_print_table_end();
  1382. }
  1383. {
  1384. array_header *env_arr;
  1385. table_entry *env;
  1386. int i;
  1387. request_rec *r;
  1388. r = ((request_rec *) SG(server_context));
  1389. SECTION("HTTP Headers Information");
  1390. php_info_print_table_start();
  1391. php_info_print_table_colspan_header(2, "HTTP Request Headers");
  1392. php_info_print_table_row(2, "HTTP Request", r->the_request);
  1393. env_arr = table_elts(r->headers_in);
  1394. env = (table_entry *)env_arr->elts;
  1395. for (i = 0; i < env_arr->nelts; ++i) {
  1396. if (env[i].key) {
  1397. php_info_print_table_row(2, env[i].key, env[i].val);
  1398. }
  1399. }
  1400. php_info_print_table_colspan_header(2, "HTTP Response Headers");
  1401. env_arr = table_elts(r->headers_out);
  1402. env = (table_entry *)env_arr->elts;
  1403. for(i = 0; i < env_arr->nelts; ++i) {
  1404. if (env[i].key) {
  1405. php_info_print_table_row(2, env[i].key, env[i].val);
  1406. }
  1407. }
  1408. php_info_print_table_end();
  1409. }
  1410. }
  1411. /* }}} */
  1412. /* {{{ proto bool virtual(string filename)
  1413. Perform an Apache sub-request */
  1414. /* This function is equivalent to <!--#include virtual...-->
  1415. * in mod_include. It does an Apache sub-request. It is useful
  1416. * for including CGI scripts or .shtml files, or anything else
  1417. * that you'd parse through Apache (for .phtml files, you'd probably
  1418. * want to use <?Include>. This only works when PHP is compiled
  1419. * as an Apache module, since it uses the Apache API for doing
  1420. * sub requests.
  1421. */
  1422. PHP_FUNCTION(virtual)
  1423. {
  1424. char *filename;
  1425. int filename_len;
  1426. request_rec *rr = NULL;
  1427. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE) {
  1428. return;
  1429. }
  1430. if (!(rr = sub_req_lookup_uri (filename, ((request_rec *) SG(server_context))))) {
  1431. php_error(E_WARNING, "Unable to include '%s' - URI lookup failed", filename);
  1432. if (rr)
  1433. destroy_sub_req (rr);
  1434. RETURN_FALSE;
  1435. }
  1436. if (rr->status != 200) {
  1437. php_error(E_WARNING, "Unable to include '%s' - error finding URI", filename);
  1438. if (rr)
  1439. destroy_sub_req (rr);
  1440. RETURN_FALSE;
  1441. }
  1442. php_output_end_all(TSRMLS_C);
  1443. php_header(TSRMLS_C);
  1444. if (run_sub_req(rr)) {
  1445. php_error(E_WARNING, "Unable to include '%s' - request execution failed", filename);
  1446. if (rr)
  1447. destroy_sub_req (rr);
  1448. RETURN_FALSE;
  1449. }
  1450. if (rr)
  1451. destroy_sub_req (rr);
  1452. RETURN_TRUE;
  1453. }
  1454. /* }}} */
  1455. /* {{{ apache_table_to_zval(table *, zval *return_value)
  1456. Fetch all HTTP request headers */
  1457. static void apache_table_to_zval(table *t, zval *return_value)
  1458. {
  1459. array_header *env_arr;
  1460. table_entry *tenv;
  1461. int i;
  1462. array_init(return_value);
  1463. env_arr = table_elts(t);
  1464. tenv = (table_entry *)env_arr->elts;
  1465. for (i = 0; i < env_arr->nelts; ++i) {
  1466. if (tenv[i].key) {
  1467. continue;
  1468. }
  1469. add_assoc_string(return_value, tenv[i].key, (tenv[i].val==NULL) ? "" : tenv[i].val, 1);
  1470. }
  1471. }
  1472. /* }}} */
  1473. /* {{{ proto array getallheaders(void)
  1474. */
  1475. /* Alias for apache_request_headers() */
  1476. /* }}} */
  1477. /* {{{ proto array apache_request_headers(void)
  1478. Fetch all HTTP request headers */
  1479. PHP_FUNCTION(apache_request_headers)
  1480. {
  1481. apache_table_to_zval(((request_rec *)SG(server_context))->headers_in, return_value);
  1482. }
  1483. /* }}} */
  1484. /* {{{ proto array apache_response_headers(void)
  1485. Fetch all HTTP response headers */
  1486. PHP_FUNCTION(apache_response_headers)
  1487. {
  1488. apache_table_to_zval(((request_rec *) SG(server_context))->headers_out, return_value);
  1489. }
  1490. /* }}} */
  1491. /* {{{ proto bool apache_setenv(string variable, string value [, bool walk_to_top])
  1492. Set an Apache subprocess_env variable */
  1493. PHP_FUNCTION(apache_setenv)
  1494. {
  1495. int var_len, val_len;
  1496. zend_bool top=0;
  1497. char *var = NULL, *val = NULL;
  1498. request_rec *r = (request_rec *) SG(server_context);
  1499. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|b", &var, &var_len, &val, &val_len, &top) == FAILURE) {
  1500. RETURN_FALSE;
  1501. }
  1502. while(top) {
  1503. if(r->prev) r = r->prev;
  1504. else break;
  1505. }
  1506. ap_table_setn(r->subprocess_env, ap_pstrndup(r->pool, var, var_len), ap_pstrndup(r->pool, val, val_len));
  1507. RETURN_TRUE;
  1508. }
  1509. /* }}} */
  1510. /* {{{ proto object apache_lookup_uri(string URI)
  1511. Perform a partial request of the given URI to obtain information about it */
  1512. PHP_FUNCTION(apache_lookup_uri)
  1513. {
  1514. char *filename;
  1515. int filename_len;
  1516. request_rec *rr=NULL;
  1517. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE) {
  1518. return;
  1519. }
  1520. if(!(rr = sub_req_lookup_uri(filename, ((request_rec *) SG(server_context))))) {
  1521. php_error(E_WARNING, "URI lookup failed", filename);
  1522. RETURN_FALSE;
  1523. }
  1524. object_init(return_value);
  1525. add_property_long(return_value,"status", rr->status);
  1526. if (rr->the_request) {
  1527. add_property_string(return_value,"the_request", rr->the_request, 1);
  1528. }
  1529. if (rr->status_line) {
  1530. add_property_string(return_value,"status_line", (char *)rr->status_line, 1);
  1531. }
  1532. if (rr->method) {
  1533. add_property_string(return_value,"method", (char *)rr->method, 1);
  1534. }
  1535. if (rr->content_type) {
  1536. add_property_string(return_value,"content_type", (char *)rr->content_type, 1);
  1537. }
  1538. if (rr->handler) {
  1539. add_property_string(return_value,"handler", (char *)rr->handler, 1);
  1540. }
  1541. if (rr->uri) {
  1542. add_property_string(return_value,"uri", rr->uri, 1);
  1543. }
  1544. if (rr->filename) {
  1545. add_property_string(return_value,"filename", rr->filename, 1);
  1546. }
  1547. if (rr->path_info) {
  1548. add_property_string(return_value,"path_info", rr->path_info, 1);
  1549. }
  1550. if (rr->args) {
  1551. add_property_string(return_value,"args", rr->args, 1);
  1552. }
  1553. if (rr->boundary) {
  1554. add_property_string(return_value,"boundary", rr->boundary, 1);
  1555. }
  1556. add_property_long(return_value,"no_cache", rr->no_cache);
  1557. add_property_long(return_value,"no_local_copy", rr->no_local_copy);
  1558. add_property_long(return_value,"allowed", rr->allowed);
  1559. add_property_long(return_value,"sent_bodyct", rr->sent_bodyct);
  1560. add_property_long(return_value,"bytes_sent", rr->bytes_sent);
  1561. add_property_long(return_value,"byterange", rr->byterange);
  1562. add_property_long(return_value,"clength", rr->clength);
  1563. #if MODULE_MAGIC_NUMBER >= 19980324
  1564. if (rr->unparsed_uri) {
  1565. add_property_string(return_value,"unparsed_uri", rr->unparsed_uri, 1);
  1566. }
  1567. if(rr->mtime) {
  1568. add_property_long(return_value,"mtime", rr->mtime);
  1569. }
  1570. #endif
  1571. if(rr->request_time) {
  1572. add_property_long(return_value,"request_time", rr->request_time);
  1573. }
  1574. destroy_sub_req(rr);
  1575. }
  1576. /* }}} */
  1577. #if 0
  1578. This function is most likely a bad idea. Just playing with it for now.
  1579. PHP_FUNCTION(apache_exec_uri)
  1580. {
  1581. zval **filename;
  1582. request_rec *rr=NULL;
  1583. TSRMLS_FETCH();
  1584. if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &filename) == FAILURE) {
  1585. WRONG_PARAM_COUNT;
  1586. }
  1587. convert_to_string_ex(filename);
  1588. if(!(rr = ap_sub_req_lookup_uri((*filename)->value.str.val, ((request_rec *) SG(server_context))))) {
  1589. php_error(E_WARNING, "URI lookup failed", (*filename)->value.str.val);
  1590. RETURN_FALSE;
  1591. }
  1592. RETVAL_LONG(ap_run_sub_req(rr));
  1593. ap_destroy_sub_req(rr);
  1594. }
  1595. #endif
  1596. /* {{{ proto string apache_get_version(void)
  1597. Fetch Apache version */
  1598. PHP_FUNCTION(apache_get_version)
  1599. {
  1600. char *apv = (char *) ap_get_server_version();
  1601. if (apv && *apv) {
  1602. RETURN_STRING(apv, 1);
  1603. } else {
  1604. RETURN_FALSE;
  1605. }
  1606. }
  1607. /* }}} */
  1608. /* {{{ proto array apache_get_modules(void)
  1609. Get a list of loaded Apache modules */
  1610. PHP_FUNCTION(apache_get_modules)
  1611. {
  1612. int n;
  1613. char *p;
  1614. array_init(return_value);
  1615. for (n = 0; ap_loaded_modules[n]; ++n) {
  1616. char *s = (char *) ap_loaded_modules[n]->name;
  1617. if ((p = strchr(s, '.'))) {
  1618. add_next_index_stringl(return_value, s, (p - s), 1);
  1619. } else {
  1620. add_next_index_string(return_value, s, 1);
  1621. }
  1622. }
  1623. }
  1624. /* }}} */
  1625. /*
  1626. * Local variables:
  1627. * tab-width: 4
  1628. * c-basic-offset: 4
  1629. * End:
  1630. * vim600: sw=4 ts=4 fdm=marker
  1631. * vim<600: sw=4 ts=4
  1632. */