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.

1970 lines
54 KiB

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