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.

1498 lines
44 KiB

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