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.

1454 lines
42 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@php.net> |
  16. | (with helpful hints from Dean Gaudet <dgaudet@arctic.org> |
  17. | PHP 4.0 patches by Zeev Suraski <zeev@zend.com> |
  18. +----------------------------------------------------------------------+
  19. */
  20. /* $Id$ */
  21. #include "php_apache_http.h"
  22. #ifdef NETWARE
  23. #define SIGPIPE SIGINT
  24. #endif
  25. #if defined(ZEND_MULTIBYTE) && defined(HAVE_MBSTRING)
  26. #include "ext/mbstring/mbstring.h"
  27. #endif /* defined(ZEND_MULTIBYTE) && defined(HAVE_MBSTRING) */
  28. #undef shutdown
  29. /* {{{ Prototypes
  30. */
  31. int apache_php_module_main(request_rec *r, int display_source_mode TSRMLS_DC);
  32. static void php_save_umask(void);
  33. static void php_restore_umask(void);
  34. static int sapi_apache_read_post(char *buffer, uint count_bytes TSRMLS_DC);
  35. static char *sapi_apache_read_cookies(TSRMLS_D);
  36. static int sapi_apache_header_handler(sapi_header_struct *sapi_header, sapi_headers_struct *sapi_headers TSRMLS_DC);
  37. static int sapi_apache_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC);
  38. static int send_php(request_rec *r, int display_source_mode, char *filename);
  39. static int send_parsed_php(request_rec * r);
  40. static int send_parsed_php_source(request_rec * r);
  41. static int php_xbithack_handler(request_rec * r);
  42. static void php_init_handler(server_rec *s, pool *p);
  43. /* }}} */
  44. #if MODULE_MAGIC_NUMBER >= 19970728
  45. static void php_child_exit_handler(server_rec *s, pool *p);
  46. #endif
  47. #if MODULE_MAGIC_NUMBER > 19961007
  48. #define CONST_PREFIX const
  49. #else
  50. #define CONST_PREFIX
  51. #endif
  52. typedef struct _sapi_stack {
  53. int top, max, persistent;
  54. void **elements;
  55. } sapi_stack;
  56. typedef struct _php_per_dir_config {
  57. HashTable *ini_settings;
  58. sapi_stack headers_handlers;
  59. sapi_stack auth_handlers;
  60. sapi_stack access_handlers;
  61. sapi_stack type_handlers;
  62. sapi_stack fixup_handlers;
  63. sapi_stack logger_handlers;
  64. sapi_stack post_read_handlers;
  65. sapi_stack response_handlers;
  66. } php_per_dir_config;
  67. typedef struct _php_per_server_config {
  68. sapi_stack uri_handlers;
  69. sapi_stack requires;
  70. } php_per_server_config;
  71. static CONST_PREFIX char *php_apache_value_handler_ex(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2, int mode);
  72. static CONST_PREFIX char *php_apache_value_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1, char *arg2);
  73. static CONST_PREFIX char *php_apache_admin_value_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1, char *arg2);
  74. static CONST_PREFIX char *php_apache_flag_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1, char *arg2);
  75. static CONST_PREFIX char *php_apache_flag_handler_ex(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2, int mode);
  76. static CONST_PREFIX char *php_apache_admin_flag_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1, char *arg2);
  77. /* ### these should be defined in mod_php4.h or somewhere else */
  78. #define USE_PATH 1
  79. #define IGNORE_URL 2
  80. module MODULE_VAR_EXPORT php4_module;
  81. int saved_umask;
  82. //static int setup_env = 0;
  83. static unsigned char apache_php_initialized;
  84. typedef struct _php_per_dir_entry {
  85. char *key;
  86. char *value;
  87. uint key_length;
  88. uint value_length;
  89. int type;
  90. } php_per_dir_entry;
  91. /* some systems are missing these from their header files */
  92. /* {{{ zend stack utility functions
  93. */
  94. /* This code is ripped part and parcel from zend_stack.[ch]. Assuming that the
  95. patch supporting zend_stack_init_ex is applied, all but the bottom two
  96. module-specific iterators will be removed
  97. */
  98. int sapi_stack_init_ex(sapi_stack *stack, int persistent)
  99. {
  100. stack->top = 0;
  101. stack->persistent = persistent;
  102. stack->elements = (void **) pemalloc(sizeof(void **) * STACK_BLOCK_SIZE, persistent);
  103. if (!stack->elements) {
  104. return FAILURE;
  105. } else {
  106. stack->max = STACK_BLOCK_SIZE;
  107. return SUCCESS;
  108. }
  109. }
  110. int sapi_stack_push(sapi_stack *stack, void *element)
  111. {
  112. if (stack->top >= stack->max) { /* we need to allocate more memory */
  113. stack->elements = (void **) perealloc(stack->elements,
  114. (sizeof(void **) * (stack->max += STACK_BLOCK_SIZE)), stack->persistent);
  115. if (!stack->elements) {
  116. return FAILURE;
  117. }
  118. }
  119. stack->elements[stack->top] = (void *) element;
  120. return stack->top++;
  121. }
  122. void* sapi_stack_pop(sapi_stack *stack) {
  123. if(stack->top == 0) {
  124. return NULL;
  125. }
  126. else {
  127. return stack->elements[--stack->top];
  128. }
  129. }
  130. int sapi_stack_destroy(sapi_stack *stack)
  131. {
  132. return SUCCESS;
  133. }
  134. int sapi_stack_apply_with_argument_all(sapi_stack *stack, int type, int (*apply_function)(void *element, void *arg), void *arg)
  135. {
  136. int i, retval;
  137. switch (type) {
  138. case ZEND_STACK_APPLY_TOPDOWN:
  139. for (i=stack->top-1; i>=0; i--) {
  140. retval = apply_function(stack->elements[i], arg);
  141. }
  142. break;
  143. case ZEND_STACK_APPLY_BOTTOMUP:
  144. for (i=0; i<stack->top; i++) {
  145. retval = apply_function(stack->elements[i], arg);
  146. }
  147. break;
  148. }
  149. return retval;
  150. }
  151. int sapi_stack_apply_with_argument_stop_if_equals(sapi_stack *stack, int type, int (*apply_function)(void *element, void *arg), void *arg, int stopval)
  152. {
  153. int i;
  154. int ret = DECLINED;
  155. switch (type) {
  156. case ZEND_STACK_APPLY_TOPDOWN:
  157. for (i=stack->top-1; i>=0; i--) {
  158. if ((ret = apply_function(stack->elements[i], arg)) == stopval) {
  159. break;
  160. }
  161. }
  162. break;
  163. case ZEND_STACK_APPLY_BOTTOMUP:
  164. for (i=0; i<stack->top; i++) {
  165. if ((ret = apply_function(stack->elements[i], arg)) == stopval) {
  166. break;
  167. }
  168. }
  169. break;
  170. }
  171. return ret;
  172. }
  173. int sapi_stack_apply_with_argument_stop_if_http_error(sapi_stack *stack, int type, int (*apply_function)(void *element, void *arg), void *arg)
  174. {
  175. int i;
  176. int ret = DECLINED;
  177. switch (type) {
  178. case ZEND_STACK_APPLY_TOPDOWN:
  179. for (i=stack->top-1; i>=0; i--) {
  180. if ((ret = apply_function(stack->elements[i], arg)) > 0) {
  181. break;
  182. }
  183. }
  184. break;
  185. case ZEND_STACK_APPLY_BOTTOMUP:
  186. for (i=0; i<stack->top; i++) {
  187. if ((ret = apply_function(stack->elements[i], arg)) > 0) {
  188. break;
  189. }
  190. }
  191. break;
  192. }
  193. return ret;
  194. }
  195. void php_handler_stack_destroy(sapi_stack *stack)
  196. {
  197. php_handler *ph;
  198. while((ph = (php_handler *)sapi_stack_pop(stack)) != NULL) {
  199. free(ph->name);
  200. free(ph);
  201. }
  202. }
  203. /* }}} */
  204. /* {{{ php_save_umask
  205. */
  206. static void php_save_umask(void)
  207. {
  208. saved_umask = umask(077);
  209. umask(saved_umask);
  210. }
  211. /* }}} */
  212. /* {{{ sapi_apache_ub_write
  213. */
  214. static int sapi_apache_ub_write(const char *str, uint str_length TSRMLS_DC)
  215. {
  216. int ret=0;
  217. if (SG(server_context)) {
  218. ret = rwrite(str, str_length, (request_rec *) SG(server_context));
  219. }
  220. if (ret != str_length) {
  221. php_handle_aborted_connection();
  222. }
  223. return ret;
  224. }
  225. /* }}} */
  226. /* {{{ sapi_apache_flush
  227. */
  228. static void sapi_apache_flush(void *server_context)
  229. {
  230. if (server_context) {
  231. #if MODULE_MAGIC_NUMBER > 19970110
  232. rflush((request_rec *) server_context);
  233. #else
  234. bflush((request_rec *) server_context->connection->client);
  235. #endif
  236. }
  237. }
  238. /* }}} */
  239. /* {{{ sapi_apache_read_post
  240. */
  241. static int sapi_apache_read_post(char *buffer, uint count_bytes TSRMLS_DC)
  242. {
  243. uint total_read_bytes=0, read_bytes;
  244. request_rec *r = (request_rec *) SG(server_context);
  245. void (*handler)(int);
  246. /*
  247. * This handles the situation where the browser sends a Expect: 100-continue header
  248. * and needs to recieve confirmation from the server on whether or not it can send
  249. * the rest of the request. RFC 2616
  250. *
  251. */
  252. if (!SG(read_post_bytes) && !ap_should_client_block(r)) {
  253. return total_read_bytes;
  254. }
  255. handler = signal(SIGPIPE, SIG_IGN);
  256. while (total_read_bytes<count_bytes) {
  257. hard_timeout("Read POST information", r); /* start timeout timer */
  258. read_bytes = get_client_block(r, buffer+total_read_bytes, count_bytes-total_read_bytes);
  259. reset_timeout(r);
  260. if (read_bytes<=0) {
  261. break;
  262. }
  263. total_read_bytes += read_bytes;
  264. }
  265. signal(SIGPIPE, handler);
  266. return total_read_bytes;
  267. }
  268. /* }}} */
  269. /* {{{ sapi_apache_read_cookies
  270. */
  271. static char *sapi_apache_read_cookies(TSRMLS_D)
  272. {
  273. return (char *) table_get(((request_rec *) SG(server_context))->subprocess_env, "HTTP_COOKIE");
  274. }
  275. /* }}} */
  276. /* {{{ sapi_apache_header_handler
  277. */
  278. static int sapi_apache_header_handler(sapi_header_struct *sapi_header, sapi_headers_struct *sapi_headers TSRMLS_DC)
  279. {
  280. char *header_name, *header_content, *p;
  281. request_rec *r = (request_rec *) SG(server_context);
  282. header_name = sapi_header->header;
  283. header_content = p = strchr(header_name, ':');
  284. if (!p) {
  285. efree(sapi_header->header);
  286. return 0;
  287. }
  288. *p = 0;
  289. do {
  290. header_content++;
  291. } while (*header_content==' ');
  292. if (!strcasecmp(header_name, "Content-Type")) {
  293. r->content_type = pstrdup(r->pool, header_content);
  294. } else if (!strcasecmp(header_name, "Set-Cookie")) {
  295. table_add(r->headers_out, header_name, header_content);
  296. } else {
  297. table_set(r->headers_out, header_name, header_content);
  298. }
  299. *p = ':'; /* a well behaved header handler shouldn't change its original arguments */
  300. efree(sapi_header->header);
  301. return 0; /* don't use the default SAPI mechanism, Apache duplicates this functionality */
  302. }
  303. /* }}} */
  304. /* {{{ sapi_apache_send_headers
  305. */
  306. static int sapi_apache_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC)
  307. {
  308. if(SG(server_context) == NULL) { /* server_context is not here anymore */
  309. return SAPI_HEADER_SEND_FAILED;
  310. }
  311. ((request_rec *) SG(server_context))->status = SG(sapi_headers).http_response_code;
  312. /* check that we haven't sent headers already, we use our own
  313. * headers_sent since we may send headers at anytime
  314. */
  315. if(!AP(headers_sent)) {
  316. send_http_header((request_rec *) SG(server_context));
  317. AP(headers_sent) = 1;
  318. }
  319. return SAPI_HEADER_SENT_SUCCESSFULLY;
  320. }
  321. /* }}} */
  322. /* {{{ sapi_apache_register_server_variables
  323. */
  324. static void sapi_apache_register_server_variables(zval *track_vars_array TSRMLS_DC)
  325. {
  326. register int i;
  327. array_header *arr = table_elts(((request_rec *) SG(server_context))->subprocess_env);
  328. table_entry *elts = (table_entry *) arr->elts;
  329. zval **path_translated;
  330. HashTable *symbol_table;
  331. for (i = 0; i < arr->nelts; i++) {
  332. char *val;
  333. if (elts[i].val) {
  334. val = elts[i].val;
  335. } else {
  336. val = empty_string;
  337. }
  338. php_register_variable(elts[i].key, val, track_vars_array TSRMLS_CC);
  339. }
  340. /* If PATH_TRANSLATED doesn't exist, copy it from SCRIPT_FILENAME */
  341. if (track_vars_array) {
  342. symbol_table = track_vars_array->value.ht;
  343. } else if (PG(register_globals)) {
  344. /* should never happen nowadays */
  345. symbol_table = EG(active_symbol_table);
  346. } else {
  347. symbol_table = NULL;
  348. }
  349. if (symbol_table
  350. && !zend_hash_exists(symbol_table, "PATH_TRANSLATED", sizeof("PATH_TRANSLATED"))
  351. && zend_hash_find(symbol_table, "SCRIPT_FILENAME", sizeof("SCRIPT_FILENAME"), (void **) &path_translated)==SUCCESS) {
  352. php_register_variable("PATH_TRANSLATED", Z_STRVAL_PP(path_translated), track_vars_array TSRMLS_CC);
  353. }
  354. php_register_variable("PHP_SELF", ((request_rec *) SG(server_context))->uri, track_vars_array TSRMLS_CC);
  355. }
  356. /* }}} */
  357. /* {{{ php_apache_startup
  358. */
  359. static int php_apache_startup(sapi_module_struct *sapi_module)
  360. {
  361. if (php_module_startup(sapi_module, &apache_module_entry, 1) == FAILURE) {
  362. return FAILURE;
  363. } else {
  364. return SUCCESS;
  365. }
  366. }
  367. /* }}} */
  368. /* {{{ php_apache_log_message
  369. */
  370. static void php_apache_log_message(char *message)
  371. {
  372. TSRMLS_FETCH();
  373. if (SG(server_context)) {
  374. #if MODULE_MAGIC_NUMBER >= 19970831
  375. aplog_error(NULL, 0, APLOG_ERR | APLOG_NOERRNO, ((request_rec *) SG(server_context))->server, "%s", message);
  376. #else
  377. log_error(message, ((request_rec *) SG(server_context))->server);
  378. #endif
  379. } else {
  380. fprintf(stderr, "%s", message);
  381. fprintf(stderr, "\n");
  382. }
  383. }
  384. /* }}} */
  385. /* {{{ php_apache_request_shutdown
  386. */
  387. static void php_apache_request_shutdown(void *dummy)
  388. {
  389. TSRMLS_FETCH();
  390. AP(current_hook) = AP_CLEANUP;
  391. php_output_set_status(0 TSRMLS_CC);
  392. SG(server_context) = NULL; /* The server context (request) is invalid by the time run_cleanups() is called */
  393. if(SG(sapi_started)) {
  394. php_request_shutdown(dummy);
  395. SG(sapi_started) = 0;
  396. }
  397. AP(in_request) = 0;
  398. if(AP(setup_env)) {
  399. AP(setup_env) = 0;
  400. }
  401. AP(current_hook) = AP_WAITING_FOR_REQUEST;
  402. AP(headers_sent) = 0;
  403. }
  404. /* }}} */
  405. /* {{{ php_apache_sapi_activate
  406. */
  407. static int php_apache_sapi_activate(TSRMLS_D)
  408. {
  409. request_rec *r = (request_rec *) SG(server_context);
  410. /*
  411. * For the Apache module version, this bit of code registers a cleanup
  412. * function that gets triggered when our request pool is destroyed.
  413. * We need this because at any point in our code we can be interrupted
  414. * and that may happen before we have had time to free our memory.
  415. * The php_request_shutdown function needs to free all outstanding allocated
  416. * memory.
  417. */
  418. block_alarms();
  419. register_cleanup(r->pool, NULL, php_apache_request_shutdown, php_request_shutdown_for_exec);
  420. AP(in_request)=1;
  421. unblock_alarms();
  422. /* Override the default headers_only value - sometimes "GET" requests should actually only
  423. * send headers.
  424. */
  425. SG(request_info).headers_only = r->header_only;
  426. return SUCCESS;
  427. }
  428. /* }}} */
  429. /* {{{ php_apache_get_stat
  430. */
  431. static struct stat *php_apache_get_stat(TSRMLS_D)
  432. {
  433. return &((request_rec *) SG(server_context))->finfo;
  434. }
  435. /* }}} */
  436. /* {{{ php_apache_getenv
  437. */
  438. static char *php_apache_getenv(char *name, size_t name_len TSRMLS_DC)
  439. {
  440. return (char *) table_get(((request_rec *) SG(server_context))->subprocess_env, name);
  441. }
  442. /* }}} */
  443. /* {{{ sapi_module_struct apache_sapi_module
  444. */
  445. static sapi_module_struct apache_sapi_module = {
  446. "apache", /* name */
  447. "Apache", /* pretty name */
  448. php_apache_startup, /* startup */
  449. php_module_shutdown_wrapper, /* shutdown */
  450. php_apache_sapi_activate, /* activate */
  451. NULL, /* deactivate */
  452. sapi_apache_ub_write, /* unbuffered write */
  453. sapi_apache_flush, /* flush */
  454. php_apache_get_stat, /* get uid */
  455. php_apache_getenv, /* getenv */
  456. php_error, /* error handler */
  457. sapi_apache_header_handler, /* header handler */
  458. sapi_apache_send_headers, /* send headers handler */
  459. NULL, /* send header handler */
  460. sapi_apache_read_post, /* read POST data */
  461. sapi_apache_read_cookies, /* read Cookies */
  462. sapi_apache_register_server_variables, /* register server variables */
  463. php_apache_log_message, /* Log message */
  464. NULL, /* php.ini path override */
  465. #ifdef PHP_WIN32
  466. NULL,
  467. NULL,
  468. #else
  469. block_alarms, /* Block interruptions */
  470. unblock_alarms, /* Unblock interruptions */
  471. #endif
  472. STANDARD_SAPI_MODULE_PROPERTIES
  473. };
  474. /* }}} */
  475. /* {{{ php_restore_umask
  476. */
  477. static void php_restore_umask(void)
  478. {
  479. umask(saved_umask);
  480. }
  481. /* }}} */
  482. /* {{{ init_request_info
  483. */
  484. static void init_request_info(TSRMLS_D)
  485. {
  486. request_rec *r = ((request_rec *) SG(server_context));
  487. char *content_length = (char *) table_get(r->subprocess_env, "CONTENT_LENGTH");
  488. const char *authorization=NULL;
  489. char *tmp;
  490. SG(request_info).query_string = r->args;
  491. SG(request_info).path_translated = r->filename;
  492. SG(request_info).request_uri = r->uri;
  493. SG(request_info).request_method = (char *)r->method;
  494. SG(request_info).content_type = (char *) table_get(r->subprocess_env, "CONTENT_TYPE");
  495. SG(request_info).content_length = (content_length ? atoi(content_length) : 0);
  496. SG(sapi_headers).http_response_code = r->status;
  497. if (r->headers_in) {
  498. authorization = table_get(r->headers_in, "Authorization");
  499. }
  500. if (authorization
  501. && !auth_type(r)
  502. && !strcasecmp(getword(r->pool, &authorization, ' '), "Basic")) {
  503. tmp = uudecode(r->pool, authorization);
  504. SG(request_info).auth_user = getword_nulls_nc(r->pool, &tmp, ':');
  505. if (SG(request_info).auth_user) {
  506. r->connection->user = pstrdup(r->connection->pool, SG(request_info).auth_user);
  507. r->connection->ap_auth_type = "Basic";
  508. SG(request_info).auth_user = estrdup(SG(request_info).auth_user);
  509. }
  510. SG(request_info).auth_password = tmp;
  511. if (SG(request_info).auth_password) {
  512. SG(request_info).auth_password = estrdup(SG(request_info).auth_password);
  513. }
  514. } else {
  515. SG(request_info).auth_user = NULL;
  516. SG(request_info).auth_password = NULL;
  517. }
  518. }
  519. /* }}} */
  520. /* {{{ php_apache_alter_ini_entries
  521. */
  522. static int php_apache_alter_ini_entries(php_per_dir_entry *per_dir_entry TSRMLS_DC)
  523. {
  524. zend_alter_ini_entry(per_dir_entry->key, per_dir_entry->key_length+1, per_dir_entry->value, per_dir_entry->value_length, per_dir_entry->type, PHP_INI_STAGE_ACTIVATE);
  525. return 0;
  526. }
  527. /* }}} */
  528. /* {{{ php_apache_get_default_mimetype
  529. */
  530. static char *php_apache_get_default_mimetype(request_rec *r TSRMLS_DC)
  531. {
  532. char *mimetype;
  533. if (SG(default_mimetype) || SG(default_charset)) {
  534. /* Assume output will be of the default MIME type. Individual
  535. scripts may change this later. */
  536. char *tmpmimetype;
  537. tmpmimetype = sapi_get_default_content_type(TSRMLS_C);
  538. mimetype = pstrdup(r->pool, tmpmimetype);
  539. efree(tmpmimetype);
  540. } else {
  541. mimetype = SAPI_DEFAULT_MIMETYPE "; charset=" SAPI_DEFAULT_CHARSET;
  542. }
  543. return mimetype;
  544. }
  545. /* }}} */
  546. /* {{{ send_php
  547. */
  548. static int send_php(request_rec *r, int display_source_mode, char *filename)
  549. {
  550. int retval;
  551. php_per_dir_config *per_dir_conf;
  552. TSRMLS_FETCH();
  553. if (AP(in_request)) {
  554. zend_file_handle fh;
  555. fh.filename = r->filename;
  556. fh.opened_path = NULL;
  557. fh.free_filename = 0;
  558. fh.type = ZEND_HANDLE_FILENAME;
  559. #if defined(ZEND_MULTIBYTE) && defined(HAVE_MBSTRING)
  560. php_mbstring_set_zend_encoding(TSRMLS_C);
  561. #endif /* defined(ZEND_MULTIBYTE) && defined(HAVE_MBSTRING) */
  562. zend_execute_scripts(ZEND_INCLUDE TSRMLS_CC, NULL, 1, &fh);
  563. return OK;
  564. }
  565. zend_first_try {
  566. /* Make sure file exists */
  567. if (filename == NULL && r->finfo.st_mode == 0) {
  568. return DECLINED;
  569. }
  570. per_dir_conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php4_module);
  571. if (per_dir_conf) {
  572. zend_hash_apply((HashTable *) per_dir_conf->ini_settings, (apply_func_t) php_apache_alter_ini_entries TSRMLS_CC);
  573. }
  574. /* If PHP parser engine has been turned off with an "engine off"
  575. * directive, then decline to handle this request
  576. */
  577. if (!AP(engine)) {
  578. r->content_type = php_apache_get_default_mimetype(r TSRMLS_CC);
  579. r->allowed |= (1 << METHODS) - 1;
  580. zend_try {
  581. zend_ini_deactivate(TSRMLS_C);
  582. } zend_end_try();
  583. return DECLINED;
  584. }
  585. if (filename == NULL) {
  586. filename = r->filename;
  587. }
  588. /* Apache 1.2 has a more complex mechanism for reading POST data */
  589. #if MODULE_MAGIC_NUMBER > 19961007
  590. if ((retval = setup_client_block(r, REQUEST_CHUNKED_ERROR))) {
  591. zend_try {
  592. zend_ini_deactivate(TSRMLS_C);
  593. } zend_end_try();
  594. return retval;
  595. }
  596. #endif
  597. if (AP(last_modified)) {
  598. #if MODULE_MAGIC_NUMBER < 19970912
  599. if ((retval = set_last_modified(r, r->finfo.st_mtime))) {
  600. zend_try {
  601. zend_ini_deactivate(TSRMLS_C);
  602. } zend_end_try();
  603. return retval;
  604. }
  605. #else
  606. update_mtime (r, r->finfo.st_mtime);
  607. set_last_modified(r);
  608. set_etag(r);
  609. #endif
  610. }
  611. /* Assume output will be of the default MIME type. Individual
  612. scripts may change this later in the request. */
  613. r->content_type = php_apache_get_default_mimetype(r TSRMLS_CC);
  614. /* Init timeout */
  615. hard_timeout("send", r);
  616. SG(server_context) = r;
  617. php_save_umask();
  618. if(!AP(setup_env)) {
  619. add_common_vars(r);
  620. add_cgi_vars(r);
  621. AP(setup_env) = 1;
  622. }
  623. init_request_info(TSRMLS_C);
  624. fprintf(stderr, "%s:%d\n", __FILE__,__LINE__);
  625. apache_php_module_main(r, display_source_mode TSRMLS_CC);
  626. /* Done, restore umask, turn off timeout, close file and return */
  627. php_restore_umask();
  628. kill_timeout(r);
  629. } zend_end_try();
  630. return OK;
  631. }
  632. /* }}} */
  633. /* {{{ send_parsed_php
  634. */
  635. static int send_parsed_php(request_rec * r)
  636. {
  637. int result = send_php(r, 0, NULL);
  638. #if MEMORY_LIMIT
  639. {
  640. char *mem_usage;
  641. TSRMLS_FETCH();
  642. mem_usage = ap_psprintf(r->pool, "%u", AG(allocated_memory_peak));
  643. AG(allocated_memory_peak) = 0;
  644. ap_table_setn(r->notes, "mod_php_memory_usage", mem_usage);
  645. }
  646. #endif
  647. return result;
  648. }
  649. /* }}} */
  650. /* {{{ send_parsed_php_source
  651. */
  652. static int send_parsed_php_source(request_rec * r)
  653. {
  654. return send_php(r, 1, NULL);
  655. }
  656. /* }}} */
  657. /* {{{ destroy_per_dir_entry
  658. */
  659. static void destroy_per_dir_entry(php_per_dir_entry *per_dir_entry)
  660. {
  661. free(per_dir_entry->key);
  662. free(per_dir_entry->value);
  663. }
  664. /* }}} */
  665. /* {{{ copy_per_dir_entry
  666. */
  667. static void copy_per_dir_entry(php_per_dir_entry *per_dir_entry)
  668. {
  669. php_per_dir_entry tmp = *per_dir_entry;
  670. per_dir_entry->key = (char *) malloc(tmp.key_length+1);
  671. memcpy(per_dir_entry->key, tmp.key, tmp.key_length);
  672. per_dir_entry->key[per_dir_entry->key_length] = 0;
  673. per_dir_entry->value = (char *) malloc(tmp.value_length+1);
  674. memcpy(per_dir_entry->value, tmp.value, tmp.value_length);
  675. per_dir_entry->value[per_dir_entry->value_length] = 0;
  676. }
  677. /* }}} */
  678. /* {{{ should_overwrite_per_dir_entry
  679. */
  680. static zend_bool should_overwrite_per_dir_entry(php_per_dir_entry *orig_per_dir_entry, php_per_dir_entry *new_per_dir_entry)
  681. {
  682. if (new_per_dir_entry->type==PHP_INI_SYSTEM
  683. && orig_per_dir_entry->type!=PHP_INI_SYSTEM) {
  684. return 1;
  685. } else {
  686. return 0;
  687. }
  688. }
  689. /* }}} */
  690. /* {{{ php_destroy_per_server_info
  691. */
  692. static void php_destroy_per_server_info(php_per_server_config *conf)
  693. {
  694. php_handler_stack_destroy(&conf->requires);
  695. php_handler_stack_destroy(&conf->uri_handlers);
  696. }
  697. /* }}} */
  698. /* {{{ php_destroy_per_dir_info
  699. */
  700. static void php_destroy_per_dir_info(php_per_dir_config *conf)
  701. {
  702. zend_hash_destroy(conf->ini_settings);
  703. php_handler_stack_destroy(&conf->response_handlers);
  704. php_handler_stack_destroy(&conf->auth_handlers);
  705. php_handler_stack_destroy(&conf->access_handlers);
  706. php_handler_stack_destroy(&conf->type_handlers);
  707. php_handler_stack_destroy(&conf->fixup_handlers);
  708. php_handler_stack_destroy(&conf->logger_handlers);
  709. php_handler_stack_destroy(&conf->post_read_handlers);
  710. php_handler_stack_destroy(&conf->headers_handlers);
  711. free(conf->ini_settings);
  712. }
  713. /* }}} */
  714. /* {{{ php_create_server
  715. */
  716. static void *php_create_server(pool *p, char *dummy)
  717. {
  718. php_per_server_config *conf;
  719. conf = (php_per_server_config *) malloc(sizeof(php_per_server_config));
  720. register_cleanup(p, (void *) conf, (void (*)(void *)) php_destroy_per_server_info, (void (*)(void *)) php_destroy_per_server_info);
  721. sapi_stack_init_ex(&conf->requires, 1);
  722. sapi_stack_init_ex(&conf->uri_handlers, 1);
  723. return conf;
  724. }
  725. /* }}} */
  726. /* {{{ php_create_dir
  727. */
  728. static void *php_create_dir(pool *p, char *dummy)
  729. {
  730. php_per_dir_config *conf;
  731. conf = (php_per_dir_config *) malloc(sizeof(php_per_dir_config));
  732. conf->ini_settings = (HashTable *) malloc(sizeof(HashTable));
  733. zend_hash_init_ex(conf->ini_settings, 5, NULL, (void (*)(void *)) destroy_per_dir_entry, 1, 0);
  734. sapi_stack_init_ex(&conf->response_handlers, 1);
  735. sapi_stack_init_ex(&conf->headers_handlers, 1);
  736. sapi_stack_init_ex(&conf->auth_handlers, 1);
  737. sapi_stack_init_ex(&conf->access_handlers, 1);
  738. sapi_stack_init_ex(&conf->type_handlers, 1);
  739. sapi_stack_init_ex(&conf->fixup_handlers, 1);
  740. sapi_stack_init_ex(&conf->logger_handlers, 1);
  741. sapi_stack_init_ex(&conf->post_read_handlers, 1);
  742. register_cleanup(p, (void *) conf, (void (*)(void *)) php_destroy_per_dir_info, (void (*)(void *)) php_destroy_per_dir_info);
  743. return conf;
  744. }
  745. /* }}} */
  746. /* {{{ php_merge_dir
  747. */
  748. static void *php_merge_dir(pool *p, void *basev, void *addv)
  749. {
  750. php_per_dir_config *a = (php_per_dir_config *) addv;
  751. php_per_dir_config *b = (php_per_dir_config *) basev;
  752. /* This function *must* return addv, and not modify basev */
  753. zend_hash_merge_ex((HashTable *) a->ini_settings, (HashTable *) b->ini_settings, (copy_ctor_func_t) copy_per_dir_entry, sizeof(php_per_dir_entry), (zend_bool (*)(void *, void *)) should_overwrite_per_dir_entry);
  754. a->headers_handlers = (a->headers_handlers.top)?a->headers_handlers:b->headers_handlers;
  755. a->auth_handlers = (a->auth_handlers.top)?a->auth_handlers:b->auth_handlers;
  756. a->access_handlers = (a->access_handlers.top)?a->access_handlers:b->access_handlers;
  757. a->type_handlers = (a->type_handlers.top)?a->type_handlers:b->type_handlers;
  758. a->fixup_handlers = (a->fixup_handlers.top)?a->fixup_handlers:b->fixup_handlers;
  759. a->logger_handlers = (a->logger_handlers.top)?a->logger_handlers:b->logger_handlers;
  760. a->post_read_handlers = (a->post_read_handlers.top)?a->post_read_handlers:b->post_read_handlers;
  761. a->response_handlers = (a->response_handlers.top)?a->response_handlers:b->response_handlers;
  762. return a;
  763. }
  764. /* }}} */
  765. /* {{{ php_apache_value_handler_ex
  766. */
  767. static CONST_PREFIX char *php_apache_value_handler_ex(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2, int mode)
  768. {
  769. php_per_dir_entry per_dir_entry;
  770. if (!apache_php_initialized) {
  771. apache_php_initialized = 1;
  772. #ifdef ZTS
  773. tsrm_startup(1, 1, 0, NULL);
  774. #endif
  775. sapi_startup(&apache_sapi_module);
  776. php_apache_startup(&apache_sapi_module);
  777. }
  778. per_dir_entry.type = mode;
  779. if (strcasecmp(arg2, "none") == 0) {
  780. arg2 = "";
  781. }
  782. per_dir_entry.key_length = strlen(arg1);
  783. per_dir_entry.value_length = strlen(arg2);
  784. per_dir_entry.key = (char *) malloc(per_dir_entry.key_length+1);
  785. memcpy(per_dir_entry.key, arg1, per_dir_entry.key_length);
  786. per_dir_entry.key[per_dir_entry.key_length] = 0;
  787. per_dir_entry.value = (char *) malloc(per_dir_entry.value_length+1);
  788. memcpy(per_dir_entry.value, arg2, per_dir_entry.value_length);
  789. per_dir_entry.value[per_dir_entry.value_length] = 0;
  790. zend_hash_update(conf, per_dir_entry.key, per_dir_entry.key_length, &per_dir_entry, sizeof(php_per_dir_entry), NULL);
  791. return NULL;
  792. }
  793. /* }}} */
  794. static CONST_PREFIX char *php_set_server_handler(server_rec *s, char *arg1, long handler_stage, long handler_type)
  795. {
  796. php_per_server_config *conf;
  797. php_handler *handler;
  798. handler = (php_handler *) malloc(sizeof(php_handler));
  799. handler->type = handler_type;
  800. handler->stage = handler_stage;
  801. handler->name = strdup(arg1);
  802. conf = get_module_config(s->module_config, &php4_module);
  803. switch(handler_stage) {
  804. case AP_URI_TRANS:
  805. sapi_stack_push(&conf->uri_handlers, handler);
  806. break;
  807. default:
  808. sapi_stack_push(&conf->requires, handler);
  809. break;
  810. }
  811. return NULL;
  812. }
  813. static CONST_PREFIX char *php_set_dir_handler(php_per_dir_config *conf, char *arg1, long handler_stage, long handler_type)
  814. {
  815. php_handler *handler;
  816. handler = (php_handler *) malloc(sizeof(php_handler));
  817. handler->type = handler_type;
  818. handler->stage = handler_stage;
  819. handler->name = strdup(arg1);
  820. switch(handler_stage) {
  821. case AP_POST_READ:
  822. sapi_stack_push(&conf->post_read_handlers, handler);
  823. break;
  824. case AP_HEADER_PARSE:
  825. sapi_stack_push(&conf->headers_handlers, handler);
  826. break;
  827. case AP_ACCESS_CONTROL:
  828. sapi_stack_push(&conf->access_handlers, handler);
  829. break;
  830. case AP_AUTHENTICATION:
  831. sapi_stack_push(&conf->auth_handlers, handler);
  832. break;
  833. case AP_AUTHORIZATION:
  834. break;
  835. case AP_TYPE_CHECKING:
  836. sapi_stack_push(&conf->type_handlers, handler);
  837. break;
  838. case AP_FIXUP:
  839. sapi_stack_push(&conf->fixup_handlers, handler);
  840. break;
  841. case AP_RESPONSE:
  842. sapi_stack_push(&conf->response_handlers, handler);
  843. break;
  844. case AP_LOGGING:
  845. sapi_stack_push(&conf->logger_handlers, handler);
  846. break;
  847. default:
  848. break;
  849. }
  850. return NULL;
  851. }
  852. /* {{{ php_set_uri_handler
  853. */
  854. static CONST_PREFIX char *php_set_uri_handler(cmd_parms *cmd, void *dummy, char *arg1)
  855. {
  856. return php_set_server_handler(cmd->server, arg1, AP_URI_TRANS, AP_HANDLER_TYPE_FILE);
  857. }
  858. /* }}} */
  859. /* {{{ php_set_uri_handler_code */
  860. static CONST_PREFIX char *php_set_uri_handler_code(cmd_parms *cmd, void *dummy, char *arg1)
  861. {
  862. return php_set_server_handler(cmd->server, arg1, AP_URI_TRANS, AP_HANDLER_TYPE_METHOD);
  863. }
  864. /* }}} */
  865. /* {{{ php_set_header_handler
  866. */
  867. static CONST_PREFIX char *php_set_header_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  868. {
  869. return php_set_dir_handler(conf, arg1, AP_HEADER_PARSE, AP_HANDLER_TYPE_FILE);
  870. }
  871. static CONST_PREFIX char *php_set_header_handler_code(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  872. {
  873. return php_set_dir_handler(conf, arg1, AP_HEADER_PARSE, AP_HANDLER_TYPE_METHOD);
  874. }
  875. /* }}} */
  876. /* {{{ php_set_auth_handler
  877. */
  878. static CONST_PREFIX char *php_set_auth_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  879. {
  880. return php_set_dir_handler(conf, arg1, AP_AUTHENTICATION, AP_HANDLER_TYPE_FILE);
  881. }
  882. static CONST_PREFIX char *php_set_auth_handler_code(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  883. {
  884. return php_set_dir_handler(conf, arg1, AP_AUTHENTICATION, AP_HANDLER_TYPE_METHOD);
  885. }
  886. /* }}} */
  887. /* {{{ php_set_access_handler
  888. */
  889. static CONST_PREFIX char *php_set_access_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  890. {
  891. return php_set_dir_handler(conf, arg1, AP_ACCESS_CONTROL, AP_HANDLER_TYPE_FILE);
  892. }
  893. static CONST_PREFIX char *php_set_access_handler_code(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  894. {
  895. return php_set_dir_handler(conf, arg1, AP_ACCESS_CONTROL, AP_HANDLER_TYPE_METHOD);
  896. }
  897. /* }}} */
  898. /* {{{ php_set_type_handler
  899. */
  900. static CONST_PREFIX char *php_set_type_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  901. {
  902. return php_set_dir_handler(conf, arg1, AP_TYPE_CHECKING, AP_HANDLER_TYPE_FILE);
  903. }
  904. static CONST_PREFIX char *php_set_type_handler_code(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  905. {
  906. return php_set_dir_handler(conf, arg1, AP_TYPE_CHECKING, AP_HANDLER_TYPE_METHOD);
  907. }
  908. /* }}} */
  909. /* {{{ php_set_fixup_handler
  910. */
  911. static CONST_PREFIX char *php_set_fixup_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  912. {
  913. return php_set_dir_handler(conf, arg1, AP_FIXUP, AP_HANDLER_TYPE_FILE);
  914. }
  915. static CONST_PREFIX char *php_set_fixup_handler_code(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  916. {
  917. return php_set_dir_handler(conf, arg1, AP_FIXUP, AP_HANDLER_TYPE_METHOD);
  918. }
  919. /* }}} */
  920. /* {{{ php_set_logger_handler
  921. */
  922. static CONST_PREFIX char *php_set_logger_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  923. {
  924. return php_set_dir_handler(conf, arg1, AP_LOGGING, AP_HANDLER_TYPE_FILE);
  925. }
  926. static CONST_PREFIX char *php_set_logger_handler_code(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  927. {
  928. return php_set_dir_handler(conf, arg1, AP_LOGGING, AP_HANDLER_TYPE_METHOD);
  929. }
  930. /* }}} */
  931. /* {{{ php_set_post_read_handler
  932. */
  933. static CONST_PREFIX char *php_set_post_read_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  934. {
  935. return php_set_dir_handler(conf, arg1, AP_POST_READ, AP_HANDLER_TYPE_FILE);
  936. }
  937. static CONST_PREFIX char *php_set_post_read_handler_code(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  938. {
  939. return php_set_dir_handler(conf, arg1, AP_POST_READ, AP_HANDLER_TYPE_METHOD);
  940. }
  941. /* }}} */
  942. /* {{{ php_set_require
  943. */
  944. static CONST_PREFIX char *php_set_require(cmd_parms *cmd, void *dummy, char *arg1)
  945. {
  946. php_per_server_config *conf;
  947. return php_set_server_handler(cmd->server, arg1, 0, AP_HANDLER_TYPE_FILE);
  948. }
  949. /* }}} */
  950. /* {{{ php_set_response_handler
  951. */
  952. static CONST_PREFIX char *php_set_response_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  953. {
  954. return php_set_dir_handler(conf, arg1, AP_RESPONSE, AP_HANDLER_TYPE_FILE);
  955. }
  956. static CONST_PREFIX char *php_set_response_handler_code(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  957. {
  958. return php_set_dir_handler(conf, arg1, AP_RESPONSE, AP_HANDLER_TYPE_METHOD);
  959. }
  960. /* }}} */
  961. /* {{{ php_apache_value_handler
  962. */
  963. static CONST_PREFIX char *php_apache_value_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1, char *arg2)
  964. {
  965. return php_apache_value_handler_ex(cmd, conf->ini_settings, arg1, arg2, PHP_INI_PERDIR);
  966. }
  967. /* }}} */
  968. /* {{{ php_apache_admin_value_handler
  969. */
  970. static CONST_PREFIX char *php_apache_admin_value_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1, char *arg2)
  971. {
  972. return php_apache_value_handler_ex(cmd, conf->ini_settings, arg1, arg2, PHP_INI_SYSTEM);
  973. }
  974. /* }}} */
  975. /* {{{ php_apache_flag_handler_ex
  976. */
  977. static CONST_PREFIX char *php_apache_flag_handler_ex(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2, int mode)
  978. {
  979. char bool_val[2];
  980. if (!strcasecmp(arg2, "On")) {
  981. bool_val[0] = '1';
  982. } else {
  983. bool_val[0] = '0';
  984. }
  985. bool_val[1] = 0;
  986. return php_apache_value_handler_ex(cmd, conf, arg1, bool_val, mode);
  987. }
  988. /* }}} */
  989. /* {{{ php_apache_flag_handler
  990. */
  991. static CONST_PREFIX char *php_apache_flag_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1, char *arg2)
  992. {
  993. return php_apache_flag_handler_ex(cmd, conf->ini_settings, arg1, arg2, PHP_INI_PERDIR);
  994. }
  995. /* }}} */
  996. /* {{{ php_apache_admin_flag_handler
  997. */
  998. static CONST_PREFIX char *php_apache_admin_flag_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1, char *arg2)
  999. {
  1000. return php_apache_flag_handler_ex(cmd, conf->ini_settings, arg1, arg2, PHP_INI_SYSTEM);
  1001. }
  1002. /* }}} */
  1003. /* {{{ int php_xbithack_handler(request_rec * r)
  1004. */
  1005. static int php_xbithack_handler(request_rec * r)
  1006. {
  1007. php_per_dir_config *conf;
  1008. TSRMLS_FETCH();
  1009. if (!(r->finfo.st_mode & S_IXUSR)) {
  1010. r->allowed |= (1 << METHODS) - 1;
  1011. return DECLINED;
  1012. }
  1013. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php4_module);
  1014. if (conf) {
  1015. zend_hash_apply((HashTable *) conf->ini_settings, (apply_func_t) php_apache_alter_ini_entries TSRMLS_CC);
  1016. }
  1017. if(!AP(xbithack)) {
  1018. r->allowed |= (1 << METHODS) - 1;
  1019. return DECLINED;
  1020. }
  1021. return send_parsed_php(r);
  1022. }
  1023. /* }}} */
  1024. /* {{{ apache_php_module_shutdown_wrapper
  1025. */
  1026. static void apache_php_module_shutdown_wrapper(void)
  1027. {
  1028. apache_php_initialized = 0;
  1029. apache_sapi_module.shutdown(&apache_sapi_module);
  1030. #if MODULE_MAGIC_NUMBER >= 19970728
  1031. /* This function is only called on server exit if the apache API
  1032. * child_exit handler exists, so shutdown globally
  1033. */
  1034. sapi_shutdown();
  1035. #endif
  1036. #ifdef ZTS
  1037. tsrm_shutdown();
  1038. #endif
  1039. }
  1040. /* }}} */
  1041. #if MODULE_MAGIC_NUMBER >= 19970728
  1042. /* {{{ php_child_exit_handler
  1043. */
  1044. static void php_child_exit_handler(server_rec *s, pool *p)
  1045. {
  1046. /* apache_php_initialized = 0; */
  1047. apache_sapi_module.shutdown(&apache_sapi_module);
  1048. #ifdef ZTS
  1049. tsrm_shutdown();
  1050. #endif
  1051. }
  1052. /* }}} */
  1053. #endif
  1054. /* {{{ void php_init_handler(server_rec *s, pool *p)
  1055. */
  1056. static void php_init_handler(server_rec *s, pool *p)
  1057. {
  1058. register_cleanup(p, NULL, (void (*)(void *))apache_php_module_shutdown_wrapper, (void (*)(void *))php_module_shutdown_for_exec);
  1059. if (!apache_php_initialized) {
  1060. apache_php_initialized = 1;
  1061. #ifdef ZTS
  1062. tsrm_startup(1, 1, 0, NULL);
  1063. #endif
  1064. sapi_startup(&apache_sapi_module);
  1065. php_apache_startup(&apache_sapi_module);
  1066. }
  1067. #if MODULE_MAGIC_NUMBER >= 19980527
  1068. {
  1069. TSRMLS_FETCH();
  1070. if (PG(expose_php)) {
  1071. ap_add_version_component("PHP/" PHP_VERSION);
  1072. }
  1073. }
  1074. #endif
  1075. }
  1076. /* }}} */
  1077. static int php_run_hook(php_handler *handler, request_rec *r)
  1078. {
  1079. zval *ret = NULL;
  1080. php_per_dir_config *conf;
  1081. TSRMLS_FETCH();
  1082. if(!AP(apache_config_loaded)) {
  1083. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php4_module);
  1084. if (conf)
  1085. zend_hash_apply((HashTable *)conf->ini_settings, (apply_func_t) php_apache_alter_ini_entries TSRMLS_CC);
  1086. AP(apache_config_loaded) = 1;
  1087. }
  1088. if (!handler->name)
  1089. return DECLINED;
  1090. hard_timeout("send", r);
  1091. SG(server_context) = r;
  1092. php_save_umask();
  1093. if (!AP(setup_env)) {
  1094. add_common_vars(r);
  1095. add_cgi_vars(r);
  1096. AP(setup_env) = 1;
  1097. }
  1098. init_request_info(TSRMLS_C);
  1099. apache_php_module_hook(r, handler, &ret TSRMLS_CC);
  1100. php_restore_umask();
  1101. kill_timeout(r);
  1102. if (ret) {
  1103. convert_to_long(ret);
  1104. return Z_LVAL_P(ret);
  1105. }
  1106. return HTTP_INTERNAL_SERVER_ERROR;
  1107. }
  1108. static int php_uri_translation(request_rec *r)
  1109. {
  1110. php_per_server_config *conf;
  1111. AP(current_hook) = AP_URI_TRANS;
  1112. conf = (php_per_server_config *) get_module_config(r->server->module_config, &php4_module);
  1113. return sapi_stack_apply_with_argument_stop_if_equals(&conf->uri_handlers,
  1114. ZEND_STACK_APPLY_BOTTOMUP,
  1115. (int (*)(void *element, void *)) php_run_hook, r, OK);
  1116. }
  1117. static int php_header_hook(request_rec *r)
  1118. {
  1119. php_per_dir_config *conf;
  1120. AP(current_hook) = AP_HEADER_PARSE;
  1121. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php4_module);
  1122. return sapi_stack_apply_with_argument_stop_if_http_error(&conf->headers_handlers,
  1123. ZEND_STACK_APPLY_BOTTOMUP,
  1124. (int (*)(void *element, void *)) php_run_hook, r);
  1125. }
  1126. static int php_auth_hook(request_rec *r)
  1127. {
  1128. php_per_dir_config *conf;
  1129. AP(current_hook) = AP_AUTHENTICATION;
  1130. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php4_module);
  1131. return sapi_stack_apply_with_argument_stop_if_equals(&conf->auth_handlers,
  1132. ZEND_STACK_APPLY_BOTTOMUP,
  1133. (int (*)(void *element, void *)) php_run_hook, r, OK);
  1134. }
  1135. static int php_access_hook(request_rec *r)
  1136. {
  1137. php_per_dir_config *conf;
  1138. AP(current_hook) = AP_ACCESS_CONTROL;
  1139. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php4_module);
  1140. return sapi_stack_apply_with_argument_stop_if_http_error(&conf->access_handlers,
  1141. ZEND_STACK_APPLY_BOTTOMUP,
  1142. (int (*)(void *element, void *)) php_run_hook, r);
  1143. }
  1144. static int php_type_hook(request_rec *r)
  1145. {
  1146. php_per_dir_config *conf;
  1147. AP(current_hook) = AP_TYPE_CHECKING;
  1148. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php4_module);
  1149. return sapi_stack_apply_with_argument_stop_if_equals(&conf->type_handlers,
  1150. ZEND_STACK_APPLY_BOTTOMUP,
  1151. (int (*)(void *element, void *)) php_run_hook,
  1152. r, OK);
  1153. }
  1154. static int php_fixup_hook(request_rec *r)
  1155. {
  1156. php_per_dir_config *conf;
  1157. AP(current_hook) = AP_FIXUP;
  1158. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php4_module);
  1159. return sapi_stack_apply_with_argument_stop_if_http_error(&conf->fixup_handlers,
  1160. ZEND_STACK_APPLY_BOTTOMUP,
  1161. (int (*)(void *element, void *)) php_run_hook,
  1162. r);
  1163. }
  1164. static int php_logger_hook(request_rec *r)
  1165. {
  1166. php_per_dir_config *conf;
  1167. AP(current_hook) = AP_LOGGING;
  1168. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php4_module);
  1169. return sapi_stack_apply_with_argument_stop_if_http_error(&conf->logger_handlers,
  1170. ZEND_STACK_APPLY_BOTTOMUP,
  1171. (int (*)(void *element, void *)) php_run_hook,
  1172. r);
  1173. }
  1174. static int php_post_read_hook(request_rec *r)
  1175. {
  1176. php_per_dir_config *conf;
  1177. php_per_server_config *svr;
  1178. AP(current_hook) = AP_POST_READ;
  1179. svr = get_module_config(r->server->module_config, &php4_module);
  1180. if(ap_is_initial_req(r)) {
  1181. sapi_stack_apply_with_argument_all(&svr->requires, ZEND_STACK_APPLY_BOTTOMUP, (int (*)(void *element, void *)) php_run_hook, r);
  1182. }
  1183. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php4_module);
  1184. return sapi_stack_apply_with_argument_stop_if_http_error(&conf->post_read_handlers,
  1185. ZEND_STACK_APPLY_BOTTOMUP,
  1186. (int (*)(void *element, void *)) php_run_hook, r);
  1187. }
  1188. static int php_response_handler(request_rec *r)
  1189. {
  1190. php_per_dir_config *conf;
  1191. AP(current_hook) = AP_RESPONSE;
  1192. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php4_module);
  1193. return sapi_stack_apply_with_argument_all(&conf->response_handlers, ZEND_STACK_APPLY_BOTTOMUP, (int (*)(void *element, void *)) php_run_hook, r);
  1194. }
  1195. /* {{{ handler_rec php_handlers[]
  1196. */
  1197. handler_rec php_handlers[] =
  1198. {
  1199. {"application/x-httpd-php", send_parsed_php},
  1200. {"application/x-httpd-php-source", send_parsed_php_source},
  1201. {"text/html", php_xbithack_handler},
  1202. {"php-script", php_response_handler},
  1203. {NULL}
  1204. };
  1205. /* }}} */
  1206. /* {{{ command_rec php_commands[]
  1207. */
  1208. command_rec php_commands[] =
  1209. {
  1210. {"php_value", php_apache_value_handler, NULL, OR_OPTIONS, TAKE2, "PHP Value Modifier"},
  1211. {"phpUriHandler", php_set_uri_handler, NULL, RSRC_CONF, TAKE1, "PHP Value Modifier"},
  1212. {"phpUriHandlerMethod", php_set_uri_handler_code, NULL, RSRC_CONF, TAKE1, "PHP Value Modifier"},
  1213. #if MODULE_MAGIC_NUMBER >= 19970103
  1214. {"phpHeaderHandler", php_set_header_handler, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1215. {"phpHeaderHandlerMethod", php_set_header_handler_code, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1216. #endif
  1217. {"phpAuthHandler", php_set_auth_handler, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1218. {"phpAuthHandlerMethod", php_set_auth_handler_code, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1219. {"phpAccessHandler", php_set_access_handler, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1220. {"phpAccessHandlerMethod", php_set_access_handler_code, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1221. {"phpTypeHandler", php_set_type_handler, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1222. {"phpTypeHandlerMethod", php_set_type_handler_code, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1223. {"phpFixupHandler", php_set_fixup_handler, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1224. {"phpFixupHandlerMethod", php_set_fixup_handler_code, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1225. {"phpLoggerHandler", php_set_logger_handler, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1226. {"phpLoggerHandlerMethod", php_set_logger_handler_code, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1227. #if MODULE_MAGIC_NUMBER >= 19970902
  1228. {"phpPostReadHandler", php_set_post_read_handler, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1229. {"phpPostReadHandlerMethod", php_set_post_read_handler_code, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1230. {"phpRequire", php_set_require, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1231. {"phpResponseHandler", php_set_response_handler, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1232. {"phpResponseHandlerMethod", php_set_response_handler_code, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1233. #endif
  1234. {"php_flag", php_apache_flag_handler, NULL, OR_OPTIONS, TAKE2, "PHP Flag Modifier"},
  1235. {"php_admin_value", php_apache_admin_value_handler, NULL, ACCESS_CONF|RSRC_CONF, TAKE2, "PHP Value Modifier (Admin)"},
  1236. {"php_admin_flag", php_apache_admin_flag_handler, NULL, ACCESS_CONF|RSRC_CONF, TAKE2, "PHP Flag Modifier (Admin)"},
  1237. {NULL}
  1238. };
  1239. /* }}} */
  1240. /* {{{ module MODULE_VAR_EXPORT php4_module
  1241. */
  1242. module MODULE_VAR_EXPORT php4_module =
  1243. {
  1244. STANDARD_MODULE_STUFF,
  1245. php_init_handler, /* initializer */
  1246. php_create_dir, /* per-directory config creator */
  1247. php_merge_dir, /* dir merger */
  1248. php_create_server, /* per-server config creator */
  1249. NULL, /* merge server config */
  1250. php_commands, /* command table */
  1251. php_handlers, /* handlers */
  1252. php_uri_translation, /* filename translation */
  1253. NULL, /* check_user_id */
  1254. php_auth_hook, /* check auth */
  1255. php_access_hook, /* check access */
  1256. php_type_hook, /* type_checker */
  1257. php_fixup_hook, /* fixups */
  1258. php_logger_hook /* logger */
  1259. #if MODULE_MAGIC_NUMBER >= 19970103
  1260. , php_header_hook /* header parser */
  1261. #endif
  1262. #if MODULE_MAGIC_NUMBER >= 19970719
  1263. , NULL /* child_init */
  1264. #endif
  1265. #if MODULE_MAGIC_NUMBER >= 19970728
  1266. , php_child_exit_handler /* child_exit */
  1267. #endif
  1268. #if MODULE_MAGIC_NUMBER >= 19970902
  1269. , php_post_read_hook /* post read-request */
  1270. #endif
  1271. };
  1272. /* }}} */
  1273. /*
  1274. * Local variables:
  1275. * tab-width: 4
  1276. * c-basic-offset: 4
  1277. * End:
  1278. * vim600: sw=4 ts=4 fdm=marker
  1279. * vim<600: sw=4 ts=4
  1280. */