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.

414 lines
10 KiB

  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 4 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2003 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. | Author: Sascha Schumann <sascha@schumann.cx> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #include "php.h"
  20. #include "ext/standard/php_smart_str.h"
  21. #include "ext/standard/info.h"
  22. #include "SAPI.h"
  23. #define CORE_PRIVATE
  24. #include "apr_strings.h"
  25. #include "apr_time.h"
  26. #include "ap_config.h"
  27. #include "util_filter.h"
  28. #include "httpd.h"
  29. #include "http_config.h"
  30. #include "http_request.h"
  31. #include "http_core.h"
  32. #include "http_protocol.h"
  33. #include "http_log.h"
  34. #include "http_main.h"
  35. #include "util_script.h"
  36. #include "http_core.h"
  37. #include "php_apache.h"
  38. static request_rec *php_apache_lookup_uri(char *filename TSRMLS_DC)
  39. {
  40. php_struct *ctx;
  41. if (!filename) {
  42. return NULL;
  43. }
  44. ctx = SG(server_context);
  45. return ap_sub_req_lookup_uri(filename, ctx->r, ctx->r->output_filters);
  46. }
  47. /* proto bool virtual(string uri)
  48. Perform an apache sub-request */
  49. PHP_FUNCTION(virtual)
  50. {
  51. zval **filename;
  52. request_rec *rr;
  53. if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &filename) == FAILURE) {
  54. WRONG_PARAM_COUNT;
  55. }
  56. convert_to_string_ex(filename);
  57. php_error_docref(NULL TSRMLS_CC, E_ERROR,
  58. "Virtual Function is not implemented in Apache2Handler '%s' "
  59. "use SSI instead http://httpd.apache.org/docs-2.0/mod/mod_include.html",
  60. Z_STRVAL_PP(filename));
  61. ap_destroy_sub_req(rr);
  62. RETURN_FALSE;
  63. /* ### this function is unsafe until PHP becomes re-entrant
  64. * as running virtual('foo.php') from inside another php script
  65. * can cause bad things to happen.
  66. * Workaround:
  67. * Use <!--#include virtual="filename" --> instead, and add the INCLUDES filter
  68. */
  69. #ifdef NEVER
  70. if (!(rr = php_apache_lookup_uri(Z_STRVAL_PP(filename) TSRMLS_CC))) {
  71. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to include '%s' - URI lookup failed", Z_STRVAL_PP(filename));
  72. RETURN_FALSE;
  73. }
  74. if (rr->status == HTTP_OK) {
  75. if (ap_run_sub_req(rr)) {
  76. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to include '%s' - request execution failed", Z_STRVAL_PP(filename));
  77. ap_destroy_sub_req(rr);
  78. RETURN_FALSE;
  79. }
  80. ap_destroy_sub_req(rr);
  81. RETURN_TRUE;
  82. }
  83. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to include '%s' - error finding URI", Z_STRVAL_PP(filename));
  84. ap_destroy_sub_req(rr);
  85. RETURN_FALSE;
  86. #endif
  87. }
  88. /* */
  89. #define ADD_LONG(name) \
  90. add_property_long(return_value, #name, rr->name)
  91. #define ADD_TIME(name) \
  92. add_property_long(return_value, #name, rr->name / APR_USEC_PER_SEC);
  93. #define ADD_STRING(name) \
  94. if (rr->name) add_property_string(return_value, #name, (char *) rr->name, 1)
  95. PHP_FUNCTION(apache_lookup_uri)
  96. {
  97. request_rec *rr;
  98. zval **filename;
  99. if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &filename) == FAILURE) {
  100. WRONG_PARAM_COUNT;
  101. }
  102. convert_to_string_ex(filename);
  103. if (!(rr = php_apache_lookup_uri(Z_STRVAL_PP(filename) TSRMLS_CC))) {
  104. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to include '%s' - URI lookup failed", Z_STRVAL_PP(filename));
  105. RETURN_FALSE;
  106. }
  107. if (rr->status == HTTP_OK) {
  108. object_init(return_value);
  109. ADD_LONG(status);
  110. ADD_STRING(the_request);
  111. ADD_STRING(status_line);
  112. ADD_STRING(method);
  113. ADD_TIME(mtime);
  114. ADD_LONG(clength);
  115. #if MODULE_MAGIC_NUMBER < 20020506
  116. ADD_STRING(boundary);
  117. #endif
  118. ADD_STRING(range);
  119. ADD_LONG(chunked);
  120. ADD_STRING(content_type);
  121. ADD_STRING(handler);
  122. ADD_LONG(no_cache);
  123. ADD_LONG(no_local_copy);
  124. ADD_STRING(unparsed_uri);
  125. ADD_STRING(uri);
  126. ADD_STRING(filename);
  127. ADD_STRING(path_info);
  128. ADD_STRING(args);
  129. ADD_LONG(allowed);
  130. ADD_LONG(sent_bodyct);
  131. ADD_LONG(bytes_sent);
  132. ADD_LONG(request_time);
  133. ADD_LONG(mtime);
  134. ADD_TIME(request_time);
  135. ap_destroy_sub_req(rr);
  136. return;
  137. }
  138. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to include '%s' - error finding URI", Z_STRVAL_PP(filename));
  139. ap_destroy_sub_req(rr);
  140. RETURN_FALSE;
  141. }
  142. /* proto array getallheaders(void)
  143. Fetch all HTTP request headers */
  144. PHP_FUNCTION(apache_request_headers)
  145. {
  146. php_struct *ctx;
  147. const apr_array_header_t *arr;
  148. char *key, *val;
  149. array_init(return_value);
  150. ctx = SG(server_context);
  151. arr = apr_table_elts(ctx->r->headers_in);
  152. APR_ARRAY_FOREACH_OPEN(arr, key, val)
  153. if (!val) val = empty_string;
  154. add_assoc_string(return_value, key, val, 1);
  155. APR_ARRAY_FOREACH_CLOSE()
  156. }
  157. /* */
  158. /* {{{ proto array apache_response_headers(void)
  159. Fetch all HTTP response headers */
  160. PHP_FUNCTION(apache_response_headers)
  161. {
  162. php_struct *ctx;
  163. const apr_array_header_t *arr;
  164. char *key, *val;
  165. array_init(return_value);
  166. ctx = SG(server_context);
  167. arr = apr_table_elts(ctx->r->headers_out);
  168. APR_ARRAY_FOREACH_OPEN(arr, key, val)
  169. if (!val) val = empty_string;
  170. add_assoc_string(return_value, key, val, 1);
  171. APR_ARRAY_FOREACH_CLOSE()
  172. }
  173. /* }}} */
  174. /* {{{ proto string apache_note(string note_name [, string note_value])
  175. Get and set Apache request notes */
  176. PHP_FUNCTION(apache_note)
  177. {
  178. php_struct *ctx;
  179. zval **note_name, **note_val;
  180. char *old_note_val=NULL;
  181. int arg_count = ZEND_NUM_ARGS();
  182. if (arg_count<1 || arg_count>2 ||
  183. zend_get_parameters_ex(arg_count, &note_name, &note_val) == FAILURE) {
  184. WRONG_PARAM_COUNT;
  185. }
  186. ctx = SG(server_context);
  187. convert_to_string_ex(note_name);
  188. old_note_val = (char *) apr_table_get(ctx->r->notes, Z_STRVAL_PP(note_name));
  189. if (arg_count == 2) {
  190. convert_to_string_ex(note_val);
  191. apr_table_set(ctx->r->notes, Z_STRVAL_PP(note_name), Z_STRVAL_PP(note_val));
  192. }
  193. if (old_note_val) {
  194. RETURN_STRING(old_note_val, 1);
  195. } else {
  196. RETURN_FALSE;
  197. }
  198. }
  199. /* }}} */
  200. /* {{{ proto bool apache_setenv(string variable, string value [, bool walk_to_top])
  201. Set an Apache subprocess_env variable */
  202. /*
  203. * XXX this doesn't look right. shouldn't it be the parent ?*/
  204. PHP_FUNCTION(apache_setenv)
  205. {
  206. php_struct *ctx;
  207. zval **variable=NULL, **string_val=NULL, **walk_to_top=NULL;
  208. int arg_count = ZEND_NUM_ARGS();
  209. request_rec *r;
  210. if (arg_count<1 || arg_count>3 ||
  211. zend_get_parameters_ex(arg_count, &variable, &string_val, &walk_to_top) == FAILURE) {
  212. WRONG_PARAM_COUNT;
  213. }
  214. ctx = SG(server_context);
  215. if (arg_count == 3 && Z_STRVAL_PP(walk_to_top)) {
  216. r = ctx->r;
  217. while(r->prev) {
  218. r = r->prev;
  219. }
  220. }
  221. convert_to_string_ex(variable);
  222. convert_to_string_ex(string_val);
  223. apr_table_set(r->subprocess_env, Z_STRVAL_PP(variable), Z_STRVAL_PP(string_val));
  224. RETURN_TRUE;
  225. }
  226. /* }}} */
  227. /* {{{ proto bool apache_getenv(string variable [, bool walk_to_top])
  228. Get an Apache subprocess_env variable */
  229. /*
  230. * XXX: shouldn't this be the parent not the 'prev'
  231. */
  232. PHP_FUNCTION(apache_getenv)
  233. {
  234. php_struct *ctx;
  235. zval **variable=NULL, **walk_to_top=NULL;
  236. int arg_count = ZEND_NUM_ARGS();
  237. char *env_val=NULL;
  238. request_rec *r;
  239. if (arg_count<1 || arg_count>2 ||
  240. zend_get_parameters_ex(arg_count, &variable, &walk_to_top) == FAILURE) {
  241. WRONG_PARAM_COUNT;
  242. }
  243. ctx = SG(server_context);
  244. r = ctx->r;
  245. if (arg_count == 2 && Z_STRVAL_PP(walk_to_top)) {
  246. while(r->prev) {
  247. r = r->prev;
  248. }
  249. }
  250. convert_to_string_ex(variable);
  251. env_val = (char*) apr_table_get(r->subprocess_env, Z_STRVAL_PP(variable));
  252. if (env_val != NULL) {
  253. RETURN_STRING(env_val, 1);
  254. } else {
  255. RETURN_FALSE;
  256. }
  257. }
  258. /* }}} */
  259. static char *php_apache_get_version()
  260. {
  261. return (char *) ap_get_server_version();
  262. }
  263. /* {{{ proto string apache_get_version(void)
  264. Fetch Apache version */
  265. PHP_FUNCTION(apache_get_version)
  266. {
  267. char *apv = php_apache_get_version();
  268. if (apv && *apv) {
  269. RETURN_STRING(apv, 1);
  270. } else {
  271. RETURN_FALSE;
  272. }
  273. }
  274. /* }}} */
  275. /* {{{ proto array apache_get_modules(void)
  276. Get a list of loaded Apache modules */
  277. PHP_FUNCTION(apache_get_modules)
  278. {
  279. int n;
  280. char *p;
  281. array_init(return_value);
  282. for (n = 0; ap_loaded_modules[n]; ++n) {
  283. char *s = (char *) ap_loaded_modules[n]->name;
  284. if ((p = strchr(s, '.'))) {
  285. add_next_index_stringl(return_value, s, (p - s), 1);
  286. } else {
  287. add_next_index_string(return_value, s, 1);
  288. }
  289. }
  290. }
  291. /* }}} */
  292. PHP_MINFO_FUNCTION(apache)
  293. {
  294. char *apv = php_apache_get_version();
  295. smart_str tmp1 = {0};
  296. int n;
  297. char *p;
  298. for (n = 0; ap_loaded_modules[n]; ++n) {
  299. char *s = (char *) ap_loaded_modules[n]->name;
  300. if ((p = strchr(s, '.'))) {
  301. smart_str_appendl(&tmp1, s, (p - s));
  302. } else {
  303. smart_str_appends(&tmp1, s);
  304. }
  305. smart_str_appendc(&tmp1, ' ');
  306. }
  307. if ((tmp1.len - 1) >= 0) {
  308. tmp1.c[tmp1.len - 1] = '\0';
  309. }
  310. php_info_print_table_start();
  311. if (apv && *apv) {
  312. php_info_print_table_row(2, "Apache Version", apv);
  313. }
  314. php_info_print_table_row(2, "Loaded Modules", tmp1.c);
  315. smart_str_free(&tmp1);
  316. php_info_print_table_end();
  317. }
  318. static function_entry apache_functions[] = {
  319. PHP_FE(apache_lookup_uri, NULL)
  320. PHP_FE(virtual, NULL)
  321. PHP_FE(apache_request_headers, NULL)
  322. PHP_FE(apache_response_headers, NULL)
  323. PHP_FE(apache_setenv, NULL)
  324. PHP_FE(apache_getenv, NULL)
  325. PHP_FE(apache_note, NULL)
  326. PHP_FE(apache_get_version, NULL)
  327. PHP_FE(apache_get_modules, NULL)
  328. PHP_FALIAS(getallheaders, apache_request_headers, NULL)
  329. {NULL, NULL, NULL}
  330. };
  331. zend_module_entry php_apache_module = {
  332. STANDARD_MODULE_HEADER,
  333. "Apache 2.0",
  334. apache_functions,
  335. NULL,
  336. NULL,
  337. NULL,
  338. NULL,
  339. PHP_MINFO(apache),
  340. NULL,
  341. STANDARD_MODULE_PROPERTIES
  342. };
  343. /*
  344. * Local variables:
  345. * tab-width: 4
  346. * c-basic-offset: 4
  347. * End:
  348. * vim600: sw=4 ts=4 fdm=marker
  349. * vim<600: sw=4 ts=4
  350. */