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.

1920 lines
54 KiB

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