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.

1468 lines
43 KiB

21 years ago
21 years ago
23 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2005 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.0 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_0.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_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_headers_struct *sapi_headers TSRMLS_DC)
  276. {
  277. char *header_name, *header_content, *p;
  278. request_rec *r = (request_rec *) SG(server_context);
  279. header_name = sapi_header->header;
  280. header_content = p = strchr(header_name, ':');
  281. if (!p) {
  282. efree(sapi_header->header);
  283. return 0;
  284. }
  285. *p = 0;
  286. do {
  287. header_content++;
  288. } while (*header_content==' ');
  289. if (!strcasecmp(header_name, "Content-Type")) {
  290. r->content_type = pstrdup(r->pool, header_content);
  291. } else if (!strcasecmp(header_name, "Set-Cookie")) {
  292. table_add(r->headers_out, header_name, header_content);
  293. } else {
  294. table_set(r->headers_out, header_name, header_content);
  295. }
  296. *p = ':'; /* a well behaved header handler shouldn't change its original arguments */
  297. return SAPI_HEADER_ADD;
  298. }
  299. /* }}} */
  300. /* {{{ sapi_apache_send_headers
  301. */
  302. static int sapi_apache_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC)
  303. {
  304. if(SG(server_context) == NULL) { /* server_context is not here anymore */
  305. return SAPI_HEADER_SEND_FAILED;
  306. }
  307. ((request_rec *) SG(server_context))->status = SG(sapi_headers).http_response_code;
  308. /* check that we haven't sent headers already, we use our own
  309. * headers_sent since we may send headers at anytime
  310. */
  311. if(!AP(headers_sent)) {
  312. send_http_header((request_rec *) SG(server_context));
  313. AP(headers_sent) = 1;
  314. }
  315. return SAPI_HEADER_SENT_SUCCESSFULLY;
  316. }
  317. /* }}} */
  318. /* {{{ sapi_apache_register_server_variables
  319. */
  320. static void sapi_apache_register_server_variables(zval *track_vars_array TSRMLS_DC)
  321. {
  322. register int i;
  323. array_header *arr = table_elts(((request_rec *) SG(server_context))->subprocess_env);
  324. table_entry *elts = (table_entry *) arr->elts;
  325. zval **path_translated;
  326. HashTable *symbol_table;
  327. for (i = 0; i < arr->nelts; i++) {
  328. char *val;
  329. if (elts[i].val) {
  330. val = elts[i].val;
  331. } else {
  332. val = "";
  333. }
  334. php_register_variable(elts[i].key, val, track_vars_array TSRMLS_CC);
  335. }
  336. /* If PATH_TRANSLATED doesn't exist, copy it from SCRIPT_FILENAME */
  337. if (track_vars_array) {
  338. symbol_table = track_vars_array->value.ht;
  339. } else if (PG(register_globals)) {
  340. /* should never happen nowadays */
  341. symbol_table = EG(active_symbol_table);
  342. } else {
  343. symbol_table = NULL;
  344. }
  345. if (symbol_table
  346. && !zend_hash_exists(symbol_table, "PATH_TRANSLATED", sizeof("PATH_TRANSLATED"))
  347. && zend_hash_find(symbol_table, "SCRIPT_FILENAME", sizeof("SCRIPT_FILENAME"), (void **) &path_translated)==SUCCESS) {
  348. php_register_variable("PATH_TRANSLATED", Z_STRVAL_PP(path_translated), track_vars_array TSRMLS_CC);
  349. }
  350. php_register_variable("PHP_SELF", ((request_rec *) SG(server_context))->uri, track_vars_array TSRMLS_CC);
  351. }
  352. /* }}} */
  353. /* {{{ php_apache_startup
  354. */
  355. static int php_apache_startup(sapi_module_struct *sapi_module)
  356. {
  357. if (php_module_startup(sapi_module, &apache_module_entry, 1) == FAILURE) {
  358. return FAILURE;
  359. } else {
  360. return SUCCESS;
  361. }
  362. }
  363. /* }}} */
  364. /* {{{ php_apache_log_message
  365. */
  366. static void php_apache_log_message(char *message)
  367. {
  368. TSRMLS_FETCH();
  369. if (SG(server_context)) {
  370. #if MODULE_MAGIC_NUMBER >= 19970831
  371. aplog_error(NULL, 0, APLOG_ERR | APLOG_NOERRNO, ((request_rec *) SG(server_context))->server, "%s", message);
  372. #else
  373. log_error(message, ((request_rec *) SG(server_context))->server);
  374. #endif
  375. } else {
  376. fprintf(stderr, "%s", message);
  377. fprintf(stderr, "\n");
  378. }
  379. }
  380. /* }}} */
  381. /* {{{ php_apache_request_shutdown
  382. */
  383. static void php_apache_request_shutdown(void *dummy)
  384. {
  385. TSRMLS_FETCH();
  386. AP(current_hook) = AP_CLEANUP;
  387. php_output_set_status(0 TSRMLS_CC);
  388. SG(server_context) = NULL; /* The server context (request) is invalid by the time run_cleanups() is called */
  389. if(SG(sapi_started)) {
  390. php_request_shutdown(dummy);
  391. SG(sapi_started) = 0;
  392. }
  393. AP(in_request) = 0;
  394. if(AP(setup_env)) {
  395. AP(setup_env) = 0;
  396. }
  397. AP(current_hook) = AP_WAITING_FOR_REQUEST;
  398. AP(headers_sent) = 0;
  399. }
  400. /* }}} */
  401. /* {{{ php_apache_sapi_activate
  402. */
  403. static int php_apache_sapi_activate(TSRMLS_D)
  404. {
  405. request_rec *r = (request_rec *) SG(server_context);
  406. /*
  407. * For the Apache module version, this bit of code registers a cleanup
  408. * function that gets triggered when our request pool is destroyed.
  409. * We need this because at any point in our code we can be interrupted
  410. * and that may happen before we have had time to free our memory.
  411. * The php_request_shutdown function needs to free all outstanding allocated
  412. * memory.
  413. */
  414. block_alarms();
  415. register_cleanup(r->pool, NULL, php_apache_request_shutdown, php_request_shutdown_for_exec);
  416. AP(in_request)=1;
  417. unblock_alarms();
  418. /* Override the default headers_only value - sometimes "GET" requests should actually only
  419. * send headers.
  420. */
  421. SG(request_info).headers_only = r->header_only;
  422. return SUCCESS;
  423. }
  424. /* }}} */
  425. /* {{{ php_apache_get_stat
  426. */
  427. static struct stat *php_apache_get_stat(TSRMLS_D)
  428. {
  429. return &((request_rec *) SG(server_context))->finfo;
  430. }
  431. /* }}} */
  432. /* {{{ php_apache_getenv
  433. */
  434. static char *php_apache_getenv(char *name, size_t name_len TSRMLS_DC)
  435. {
  436. return (char *) table_get(((request_rec *) SG(server_context))->subprocess_env, name);
  437. }
  438. /* }}} */
  439. /* {{{ sapi_module_struct apache_sapi_module
  440. */
  441. static sapi_module_struct apache_sapi_module = {
  442. "apache", /* name */
  443. "Apache", /* pretty name */
  444. php_apache_startup, /* startup */
  445. php_module_shutdown_wrapper, /* shutdown */
  446. php_apache_sapi_activate, /* activate */
  447. NULL, /* deactivate */
  448. sapi_apache_ub_write, /* unbuffered write */
  449. sapi_apache_flush, /* flush */
  450. php_apache_get_stat, /* get uid */
  451. php_apache_getenv, /* getenv */
  452. php_error, /* error handler */
  453. sapi_apache_header_handler, /* header handler */
  454. sapi_apache_send_headers, /* send headers handler */
  455. NULL, /* send header handler */
  456. sapi_apache_read_post, /* read POST data */
  457. sapi_apache_read_cookies, /* read Cookies */
  458. sapi_apache_register_server_variables, /* register server variables */
  459. php_apache_log_message, /* Log message */
  460. NULL, /* Get request time */
  461. NULL, /* php.ini path override */
  462. #ifdef PHP_WIN32
  463. NULL,
  464. NULL,
  465. #else
  466. block_alarms, /* Block interruptions */
  467. unblock_alarms, /* Unblock interruptions */
  468. #endif
  469. NULL, /* default post reader */
  470. NULL, /* treat data */
  471. NULL, /* exe location */
  472. 0, /* ini ignore */
  473. NULL
  474. };
  475. /* }}} */
  476. /* {{{ php_restore_umask
  477. */
  478. static void php_restore_umask(void)
  479. {
  480. umask(saved_umask);
  481. }
  482. /* }}} */
  483. /* {{{ init_request_info
  484. */
  485. static void init_request_info(TSRMLS_D)
  486. {
  487. request_rec *r = ((request_rec *) SG(server_context));
  488. char *content_length = (char *) table_get(r->subprocess_env, "CONTENT_LENGTH");
  489. const char *authorization=NULL;
  490. char *tmp, *tmp_user;
  491. SG(request_info).query_string = r->args;
  492. SG(request_info).path_translated = r->filename;
  493. SG(request_info).request_uri = r->uri;
  494. SG(request_info).request_method = (char *)r->method;
  495. SG(request_info).proto_num = r->proto_num;
  496. SG(request_info).content_type = (char *) table_get(r->subprocess_env, "CONTENT_TYPE");
  497. SG(request_info).content_length = (content_length ? atoi(content_length) : 0);
  498. SG(sapi_headers).http_response_code = r->status;
  499. if (r->headers_in) {
  500. authorization = table_get(r->headers_in, "Authorization");
  501. }
  502. SG(request_info).auth_user = NULL;
  503. SG(request_info).auth_password = NULL;
  504. if (authorization && !auth_type(r)) {
  505. if (!strcasecmp(getword(r->pool, &authorization, ' '), "Basic")) {
  506. tmp = uudecode(r->pool, authorization);
  507. tmp_user = getword_nulls_nc(r->pool, &tmp, ':');
  508. if (tmp_user) {
  509. r->connection->user = pstrdup(r->connection->pool, tmp_user);
  510. r->connection->ap_auth_type = "Basic";
  511. SG(request_info).auth_user = estrdup(tmp_user);
  512. }
  513. if (tmp) {
  514. SG(request_info).auth_password = estrdup(tmp);
  515. }
  516. } else if (!strcasecmp(getword(r->pool, &authorization, ' '), "Digest")) {
  517. r->connection->ap_auth_type = "Digest";
  518. SG(request_info).auth_digest = estrdup(authorization);
  519. }
  520. }
  521. }
  522. /* }}} */
  523. /* {{{ php_apache_alter_ini_entries
  524. */
  525. static int php_apache_alter_ini_entries(php_per_dir_entry *per_dir_entry TSRMLS_DC)
  526. {
  527. 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);
  528. return 0;
  529. }
  530. /* }}} */
  531. /* {{{ php_apache_get_default_mimetype
  532. */
  533. static char *php_apache_get_default_mimetype(request_rec *r TSRMLS_DC)
  534. {
  535. char *mimetype;
  536. /* Assume output will be of the default MIME type. Individual
  537. scripts may change this later. */
  538. char *tmpmimetype;
  539. tmpmimetype = sapi_get_default_content_type(TSRMLS_C);
  540. mimetype = pstrdup(r->pool, tmpmimetype);
  541. efree(tmpmimetype);
  542. return mimetype;
  543. }
  544. /* }}} */
  545. /* {{{ send_php
  546. */
  547. static int send_php(request_rec *r, int display_source_mode, char *filename)
  548. {
  549. int retval;
  550. php_per_dir_config *per_dir_conf;
  551. TSRMLS_FETCH();
  552. if (AP(in_request)) {
  553. zend_file_handle fh;
  554. fh.filename = r->filename;
  555. fh.opened_path = NULL;
  556. fh.free_filename = 0;
  557. fh.type = ZEND_HANDLE_FILENAME;
  558. zend_execute_scripts(ZEND_INCLUDE TSRMLS_CC, NULL, 1, &fh);
  559. return OK;
  560. }
  561. zend_first_try {
  562. /* Make sure file exists */
  563. if (filename == NULL && r->finfo.st_mode == 0) {
  564. return DECLINED;
  565. }
  566. per_dir_conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php5_module);
  567. if (per_dir_conf) {
  568. zend_hash_apply((HashTable *) per_dir_conf->ini_settings, (apply_func_t) php_apache_alter_ini_entries TSRMLS_CC);
  569. }
  570. /* If PHP parser engine has been turned off with an "engine off"
  571. * directive, then decline to handle this request
  572. */
  573. if (!AP(engine)) {
  574. r->content_type = php_apache_get_default_mimetype(r TSRMLS_CC);
  575. r->allowed |= (1 << METHODS) - 1;
  576. zend_try {
  577. zend_ini_deactivate(TSRMLS_C);
  578. } zend_end_try();
  579. return DECLINED;
  580. }
  581. if (filename == NULL) {
  582. filename = r->filename;
  583. }
  584. /* Apache 1.2 has a more complex mechanism for reading POST data */
  585. #if MODULE_MAGIC_NUMBER > 19961007
  586. if ((retval = setup_client_block(r, REQUEST_CHUNKED_ERROR))) {
  587. zend_try {
  588. zend_ini_deactivate(TSRMLS_C);
  589. } zend_end_try();
  590. return retval;
  591. }
  592. #endif
  593. if (AP(last_modified)) {
  594. #if MODULE_MAGIC_NUMBER < 19970912
  595. if ((retval = set_last_modified(r, r->finfo.st_mtime))) {
  596. zend_try {
  597. zend_ini_deactivate(TSRMLS_C);
  598. } zend_end_try();
  599. return retval;
  600. }
  601. #else
  602. update_mtime (r, r->finfo.st_mtime);
  603. set_last_modified(r);
  604. set_etag(r);
  605. #endif
  606. }
  607. /* Assume output will be of the default MIME type. Individual
  608. scripts may change this later in the request. */
  609. r->content_type = php_apache_get_default_mimetype(r TSRMLS_CC);
  610. /* Init timeout */
  611. hard_timeout("send", r);
  612. SG(server_context) = r;
  613. php_save_umask();
  614. if(!AP(setup_env)) {
  615. AP(setup_env) = 1;
  616. add_common_vars(r);
  617. add_cgi_vars(r);
  618. }
  619. init_request_info(TSRMLS_C);
  620. apache_php_module_main(r, display_source_mode TSRMLS_CC);
  621. /* Done, restore umask, turn off timeout, close file and return */
  622. php_restore_umask();
  623. kill_timeout(r);
  624. } zend_end_try();
  625. return OK;
  626. }
  627. /* }}} */
  628. /* {{{ send_parsed_php
  629. */
  630. static int send_parsed_php(request_rec * r)
  631. {
  632. int result = send_php(r, 0, NULL);
  633. #if MEMORY_LIMIT
  634. {
  635. char *mem_usage;
  636. TSRMLS_FETCH();
  637. mem_usage = ap_psprintf(r->pool, "%u", AG(allocated_memory_peak));
  638. AG(allocated_memory_peak) = 0;
  639. ap_table_setn(r->notes, "mod_php_memory_usage", mem_usage);
  640. }
  641. #endif
  642. return result;
  643. }
  644. /* }}} */
  645. /* {{{ send_parsed_php_source
  646. */
  647. static int send_parsed_php_source(request_rec * r)
  648. {
  649. return send_php(r, 1, NULL);
  650. }
  651. /* }}} */
  652. /* {{{ destroy_per_dir_entry
  653. */
  654. static void destroy_per_dir_entry(php_per_dir_entry *per_dir_entry)
  655. {
  656. free(per_dir_entry->key);
  657. free(per_dir_entry->value);
  658. }
  659. /* }}} */
  660. /* {{{ copy_per_dir_entry
  661. */
  662. static void copy_per_dir_entry(php_per_dir_entry *per_dir_entry)
  663. {
  664. php_per_dir_entry tmp = *per_dir_entry;
  665. per_dir_entry->key = (char *) malloc(tmp.key_length+1);
  666. memcpy(per_dir_entry->key, tmp.key, tmp.key_length);
  667. per_dir_entry->key[per_dir_entry->key_length] = 0;
  668. per_dir_entry->value = (char *) malloc(tmp.value_length+1);
  669. memcpy(per_dir_entry->value, tmp.value, tmp.value_length);
  670. per_dir_entry->value[per_dir_entry->value_length] = 0;
  671. }
  672. /* }}} */
  673. /* {{{ should_overwrite_per_dir_entry;
  674. */
  675. 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)
  676. {
  677. php_per_dir_entry *new_per_dir_entry;
  678. if (zend_hash_find(target_ht, hash_key->arKey, hash_key->nKeyLength, (void **) &new_per_dir_entry)==FAILURE) {
  679. return 1; /* does not exist in dest, copy from source */
  680. }
  681. if (new_per_dir_entry->type==PHP_INI_SYSTEM
  682. && orig_per_dir_entry->type!=PHP_INI_SYSTEM) {
  683. return 1;
  684. } else {
  685. return 0;
  686. }
  687. }
  688. /* }}} */
  689. /* {{{ php_destroy_per_server_info
  690. */
  691. static void php_destroy_per_server_info(php_per_server_config *conf)
  692. {
  693. php_handler_stack_destroy(&conf->requires);
  694. php_handler_stack_destroy(&conf->uri_handlers);
  695. }
  696. /* }}} */
  697. /* {{{ php_destroy_per_dir_info
  698. */
  699. static void php_destroy_per_dir_info(php_per_dir_config *conf)
  700. {
  701. zend_hash_destroy(conf->ini_settings);
  702. php_handler_stack_destroy(&conf->response_handlers);
  703. php_handler_stack_destroy(&conf->auth_handlers);
  704. php_handler_stack_destroy(&conf->access_handlers);
  705. php_handler_stack_destroy(&conf->type_handlers);
  706. php_handler_stack_destroy(&conf->fixup_handlers);
  707. php_handler_stack_destroy(&conf->logger_handlers);
  708. php_handler_stack_destroy(&conf->post_read_handlers);
  709. php_handler_stack_destroy(&conf->headers_handlers);
  710. free(conf->ini_settings);
  711. }
  712. /* }}} */
  713. /* {{{ php_create_server
  714. */
  715. static void *php_create_server(pool *p, char *dummy)
  716. {
  717. php_per_server_config *conf;
  718. conf = (php_per_server_config *) malloc(sizeof(php_per_server_config));
  719. register_cleanup(p, (void *) conf, (void (*)(void *)) php_destroy_per_server_info, (void (*)(void *)) php_destroy_per_server_info);
  720. sapi_stack_init_ex(&conf->requires, 1);
  721. sapi_stack_init_ex(&conf->uri_handlers, 1);
  722. return conf;
  723. }
  724. /* }}} */
  725. /* {{{ php_create_dir
  726. */
  727. static void *php_create_dir(pool *p, char *dummy)
  728. {
  729. php_per_dir_config *conf;
  730. conf = (php_per_dir_config *) malloc(sizeof(php_per_dir_config));
  731. conf->ini_settings = (HashTable *) malloc(sizeof(HashTable));
  732. zend_hash_init_ex(conf->ini_settings, 5, NULL, (void (*)(void *)) destroy_per_dir_entry, 1, 0);
  733. sapi_stack_init_ex(&conf->response_handlers, 1);
  734. sapi_stack_init_ex(&conf->headers_handlers, 1);
  735. sapi_stack_init_ex(&conf->auth_handlers, 1);
  736. sapi_stack_init_ex(&conf->access_handlers, 1);
  737. sapi_stack_init_ex(&conf->type_handlers, 1);
  738. sapi_stack_init_ex(&conf->fixup_handlers, 1);
  739. sapi_stack_init_ex(&conf->logger_handlers, 1);
  740. sapi_stack_init_ex(&conf->post_read_handlers, 1);
  741. register_cleanup(p, (void *) conf, (void (*)(void *)) php_destroy_per_dir_info, (void (*)(void *)) php_destroy_per_dir_info);
  742. return conf;
  743. }
  744. /* }}} */
  745. /* {{{ php_merge_dir
  746. */
  747. static void *php_merge_dir(pool *p, void *basev, void *addv)
  748. {
  749. php_per_dir_config *a = (php_per_dir_config *) addv;
  750. php_per_dir_config *b = (php_per_dir_config *) basev;
  751. /* This function *must* return addv, and not modify basev */
  752. 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);
  753. a->headers_handlers = (a->headers_handlers.top)?a->headers_handlers:b->headers_handlers;
  754. a->auth_handlers = (a->auth_handlers.top)?a->auth_handlers:b->auth_handlers;
  755. a->access_handlers = (a->access_handlers.top)?a->access_handlers:b->access_handlers;
  756. a->type_handlers = (a->type_handlers.top)?a->type_handlers:b->type_handlers;
  757. a->fixup_handlers = (a->fixup_handlers.top)?a->fixup_handlers:b->fixup_handlers;
  758. a->logger_handlers = (a->logger_handlers.top)?a->logger_handlers:b->logger_handlers;
  759. a->post_read_handlers = (a->post_read_handlers.top)?a->post_read_handlers:b->post_read_handlers;
  760. a->response_handlers = (a->response_handlers.top)?a->response_handlers:b->response_handlers;
  761. return a;
  762. }
  763. /* }}} */
  764. /* {{{ php_apache_value_handler_ex
  765. */
  766. static CONST_PREFIX char *php_apache_value_handler_ex(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2, int mode)
  767. {
  768. php_per_dir_entry per_dir_entry;
  769. if (!apache_php_initialized) {
  770. apache_php_initialized = 1;
  771. #ifdef ZTS
  772. tsrm_startup(1, 1, 0, NULL);
  773. #endif
  774. sapi_startup(&apache_sapi_module);
  775. php_apache_startup(&apache_sapi_module);
  776. }
  777. per_dir_entry.type = mode;
  778. if (strcasecmp(arg2, "none") == 0) {
  779. arg2 = "";
  780. }
  781. per_dir_entry.key_length = strlen(arg1);
  782. per_dir_entry.value_length = strlen(arg2);
  783. per_dir_entry.key = (char *) malloc(per_dir_entry.key_length+1);
  784. memcpy(per_dir_entry.key, arg1, per_dir_entry.key_length);
  785. per_dir_entry.key[per_dir_entry.key_length] = 0;
  786. per_dir_entry.value = (char *) malloc(per_dir_entry.value_length+1);
  787. memcpy(per_dir_entry.value, arg2, per_dir_entry.value_length);
  788. per_dir_entry.value[per_dir_entry.value_length] = 0;
  789. zend_hash_update(conf, per_dir_entry.key, per_dir_entry.key_length, &per_dir_entry, sizeof(php_per_dir_entry), NULL);
  790. return NULL;
  791. }
  792. /* }}} */
  793. static CONST_PREFIX char *php_set_server_handler(server_rec *s, char *arg1, long handler_stage, long handler_type)
  794. {
  795. php_per_server_config *conf;
  796. php_handler *handler;
  797. handler = (php_handler *) malloc(sizeof(php_handler));
  798. handler->type = handler_type;
  799. handler->stage = handler_stage;
  800. handler->name = strdup(arg1);
  801. conf = get_module_config(s->module_config, &php5_module);
  802. switch(handler_stage) {
  803. case AP_URI_TRANS:
  804. sapi_stack_push(&conf->uri_handlers, handler);
  805. break;
  806. default:
  807. sapi_stack_push(&conf->requires, handler);
  808. break;
  809. }
  810. return NULL;
  811. }
  812. static CONST_PREFIX char *php_set_dir_handler(php_per_dir_config *conf, char *arg1, long handler_stage, long handler_type)
  813. {
  814. php_handler *handler;
  815. handler = (php_handler *) malloc(sizeof(php_handler));
  816. handler->type = handler_type;
  817. handler->stage = handler_stage;
  818. handler->name = strdup(arg1);
  819. switch(handler_stage) {
  820. case AP_POST_READ:
  821. sapi_stack_push(&conf->post_read_handlers, handler);
  822. break;
  823. case AP_HEADER_PARSE:
  824. sapi_stack_push(&conf->headers_handlers, handler);
  825. break;
  826. case AP_ACCESS_CONTROL:
  827. sapi_stack_push(&conf->access_handlers, handler);
  828. break;
  829. case AP_AUTHENTICATION:
  830. sapi_stack_push(&conf->auth_handlers, handler);
  831. break;
  832. case AP_AUTHORIZATION:
  833. break;
  834. case AP_TYPE_CHECKING:
  835. sapi_stack_push(&conf->type_handlers, handler);
  836. break;
  837. case AP_FIXUP:
  838. sapi_stack_push(&conf->fixup_handlers, handler);
  839. break;
  840. case AP_RESPONSE:
  841. sapi_stack_push(&conf->response_handlers, handler);
  842. break;
  843. case AP_LOGGING:
  844. sapi_stack_push(&conf->logger_handlers, handler);
  845. break;
  846. default:
  847. break;
  848. }
  849. return NULL;
  850. }
  851. /* {{{ php_set_uri_handler
  852. */
  853. static CONST_PREFIX char *php_set_uri_handler(cmd_parms *cmd, void *dummy, char *arg1)
  854. {
  855. return php_set_server_handler(cmd->server, arg1, AP_URI_TRANS, AP_HANDLER_TYPE_FILE);
  856. }
  857. /* }}} */
  858. /* {{{ php_set_uri_handler_code */
  859. static CONST_PREFIX char *php_set_uri_handler_code(cmd_parms *cmd, void *dummy, char *arg1)
  860. {
  861. return php_set_server_handler(cmd->server, arg1, AP_URI_TRANS, AP_HANDLER_TYPE_METHOD);
  862. }
  863. /* }}} */
  864. /* {{{ php_set_header_handler
  865. */
  866. static CONST_PREFIX char *php_set_header_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  867. {
  868. return php_set_dir_handler(conf, arg1, AP_HEADER_PARSE, AP_HANDLER_TYPE_FILE);
  869. }
  870. static CONST_PREFIX char *php_set_header_handler_code(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  871. {
  872. return php_set_dir_handler(conf, arg1, AP_HEADER_PARSE, AP_HANDLER_TYPE_METHOD);
  873. }
  874. /* }}} */
  875. /* {{{ php_set_auth_handler
  876. */
  877. static CONST_PREFIX char *php_set_auth_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  878. {
  879. return php_set_dir_handler(conf, arg1, AP_AUTHENTICATION, AP_HANDLER_TYPE_FILE);
  880. }
  881. static CONST_PREFIX char *php_set_auth_handler_code(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  882. {
  883. return php_set_dir_handler(conf, arg1, AP_AUTHENTICATION, AP_HANDLER_TYPE_METHOD);
  884. }
  885. /* }}} */
  886. /* {{{ php_set_access_handler
  887. */
  888. static CONST_PREFIX char *php_set_access_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  889. {
  890. return php_set_dir_handler(conf, arg1, AP_ACCESS_CONTROL, AP_HANDLER_TYPE_FILE);
  891. }
  892. static CONST_PREFIX char *php_set_access_handler_code(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  893. {
  894. return php_set_dir_handler(conf, arg1, AP_ACCESS_CONTROL, AP_HANDLER_TYPE_METHOD);
  895. }
  896. /* }}} */
  897. /* {{{ php_set_type_handler
  898. */
  899. static CONST_PREFIX char *php_set_type_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  900. {
  901. return php_set_dir_handler(conf, arg1, AP_TYPE_CHECKING, AP_HANDLER_TYPE_FILE);
  902. }
  903. static CONST_PREFIX char *php_set_type_handler_code(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  904. {
  905. return php_set_dir_handler(conf, arg1, AP_TYPE_CHECKING, AP_HANDLER_TYPE_METHOD);
  906. }
  907. /* }}} */
  908. /* {{{ php_set_fixup_handler
  909. */
  910. static CONST_PREFIX char *php_set_fixup_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  911. {
  912. return php_set_dir_handler(conf, arg1, AP_FIXUP, AP_HANDLER_TYPE_FILE);
  913. }
  914. static CONST_PREFIX char *php_set_fixup_handler_code(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  915. {
  916. return php_set_dir_handler(conf, arg1, AP_FIXUP, AP_HANDLER_TYPE_METHOD);
  917. }
  918. /* }}} */
  919. /* {{{ php_set_logger_handler
  920. */
  921. static CONST_PREFIX char *php_set_logger_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  922. {
  923. return php_set_dir_handler(conf, arg1, AP_LOGGING, AP_HANDLER_TYPE_FILE);
  924. }
  925. static CONST_PREFIX char *php_set_logger_handler_code(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  926. {
  927. return php_set_dir_handler(conf, arg1, AP_LOGGING, AP_HANDLER_TYPE_METHOD);
  928. }
  929. /* }}} */
  930. /* {{{ php_set_post_read_handler
  931. */
  932. static CONST_PREFIX char *php_set_post_read_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  933. {
  934. return php_set_dir_handler(conf, arg1, AP_POST_READ, AP_HANDLER_TYPE_FILE);
  935. }
  936. static CONST_PREFIX char *php_set_post_read_handler_code(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  937. {
  938. return php_set_dir_handler(conf, arg1, AP_POST_READ, AP_HANDLER_TYPE_METHOD);
  939. }
  940. /* }}} */
  941. /* {{{ php_set_require
  942. */
  943. static CONST_PREFIX char *php_set_require(cmd_parms *cmd, void *dummy, char *arg1)
  944. {
  945. return php_set_server_handler(cmd->server, arg1, 0, AP_HANDLER_TYPE_FILE);
  946. }
  947. /* }}} */
  948. /* {{{ php_set_response_handler
  949. */
  950. static CONST_PREFIX char *php_set_response_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  951. {
  952. return php_set_dir_handler(conf, arg1, AP_RESPONSE, AP_HANDLER_TYPE_FILE);
  953. }
  954. static CONST_PREFIX char *php_set_response_handler_code(cmd_parms *cmd, php_per_dir_config *conf, char *arg1)
  955. {
  956. return php_set_dir_handler(conf, arg1, AP_RESPONSE, AP_HANDLER_TYPE_METHOD);
  957. }
  958. /* }}} */
  959. /* {{{ php_apache_value_handler
  960. */
  961. static CONST_PREFIX char *php_apache_value_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1, char *arg2)
  962. {
  963. return php_apache_value_handler_ex(cmd, conf->ini_settings, arg1, arg2, PHP_INI_PERDIR);
  964. }
  965. /* }}} */
  966. /* {{{ php_apache_admin_value_handler
  967. */
  968. static CONST_PREFIX char *php_apache_admin_value_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1, char *arg2)
  969. {
  970. return php_apache_value_handler_ex(cmd, conf->ini_settings, arg1, arg2, PHP_INI_SYSTEM);
  971. }
  972. /* }}} */
  973. /* {{{ php_apache_flag_handler_ex
  974. */
  975. static CONST_PREFIX char *php_apache_flag_handler_ex(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2, int mode)
  976. {
  977. char bool_val[2];
  978. if (!strcasecmp(arg2, "On")) {
  979. bool_val[0] = '1';
  980. } else {
  981. bool_val[0] = '0';
  982. }
  983. bool_val[1] = 0;
  984. return php_apache_value_handler_ex(cmd, conf, arg1, bool_val, mode);
  985. }
  986. /* }}} */
  987. /* {{{ php_apache_flag_handler
  988. */
  989. static CONST_PREFIX char *php_apache_flag_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1, char *arg2)
  990. {
  991. return php_apache_flag_handler_ex(cmd, conf->ini_settings, arg1, arg2, PHP_INI_PERDIR);
  992. }
  993. /* }}} */
  994. /* {{{ php_apache_admin_flag_handler
  995. */
  996. static CONST_PREFIX char *php_apache_admin_flag_handler(cmd_parms *cmd, php_per_dir_config *conf, char *arg1, char *arg2)
  997. {
  998. return php_apache_flag_handler_ex(cmd, conf->ini_settings, arg1, arg2, PHP_INI_SYSTEM);
  999. }
  1000. /* }}} */
  1001. /* {{{ int php_xbithack_handler(request_rec * r)
  1002. */
  1003. static int php_xbithack_handler(request_rec * r)
  1004. {
  1005. php_per_dir_config *conf;
  1006. TSRMLS_FETCH();
  1007. if (!(r->finfo.st_mode & S_IXUSR)) {
  1008. r->allowed |= (1 << METHODS) - 1;
  1009. return DECLINED;
  1010. }
  1011. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php5_module);
  1012. if (conf) {
  1013. zend_hash_apply((HashTable *) conf->ini_settings, (apply_func_t) php_apache_alter_ini_entries TSRMLS_CC);
  1014. }
  1015. if(!AP(xbithack)) {
  1016. r->allowed |= (1 << METHODS) - 1;
  1017. zend_try {
  1018. zend_ini_deactivate(TSRMLS_C);
  1019. } zend_end_try();
  1020. return DECLINED;
  1021. }
  1022. return send_parsed_php(r);
  1023. }
  1024. /* }}} */
  1025. /* {{{ apache_php_module_shutdown_wrapper
  1026. */
  1027. static void apache_php_module_shutdown_wrapper(void)
  1028. {
  1029. apache_php_initialized = 0;
  1030. apache_sapi_module.shutdown(&apache_sapi_module);
  1031. #if MODULE_MAGIC_NUMBER >= 19970728
  1032. /* This function is only called on server exit if the apache API
  1033. * child_exit handler exists, so shutdown globally
  1034. */
  1035. sapi_shutdown();
  1036. #endif
  1037. #ifdef ZTS
  1038. tsrm_shutdown();
  1039. #endif
  1040. }
  1041. /* }}} */
  1042. #if MODULE_MAGIC_NUMBER >= 19970728
  1043. /* {{{ php_child_exit_handler
  1044. */
  1045. static void php_child_exit_handler(server_rec *s, pool *p)
  1046. {
  1047. /* apache_php_initialized = 0; */
  1048. apache_sapi_module.shutdown(&apache_sapi_module);
  1049. #ifdef ZTS
  1050. tsrm_shutdown();
  1051. #endif
  1052. }
  1053. /* }}} */
  1054. #endif
  1055. /* {{{ void php_init_handler(server_rec *s, pool *p)
  1056. */
  1057. static void php_init_handler(server_rec *s, pool *p)
  1058. {
  1059. register_cleanup(p, NULL, (void (*)(void *))apache_php_module_shutdown_wrapper, (void (*)(void *))php_module_shutdown_for_exec);
  1060. if (!apache_php_initialized) {
  1061. apache_php_initialized = 1;
  1062. #ifdef ZTS
  1063. tsrm_startup(1, 1, 0, NULL);
  1064. #endif
  1065. sapi_startup(&apache_sapi_module);
  1066. php_apache_startup(&apache_sapi_module);
  1067. }
  1068. #if MODULE_MAGIC_NUMBER >= 19980527
  1069. {
  1070. TSRMLS_FETCH();
  1071. if (PG(expose_php)) {
  1072. ap_add_version_component("PHP/" PHP_VERSION);
  1073. }
  1074. }
  1075. #endif
  1076. }
  1077. /* }}} */
  1078. static int php_run_hook(php_handler *handler, request_rec *r)
  1079. {
  1080. zval *ret = NULL;
  1081. php_per_dir_config *conf;
  1082. TSRMLS_FETCH();
  1083. if(!AP(apache_config_loaded)) {
  1084. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php5_module);
  1085. if (conf)
  1086. zend_hash_apply((HashTable *)conf->ini_settings, (apply_func_t) php_apache_alter_ini_entries TSRMLS_CC);
  1087. AP(apache_config_loaded) = 1;
  1088. }
  1089. if (!handler->name) {
  1090. return DECLINED;
  1091. }
  1092. php_save_umask();
  1093. if (!AP(setup_env)) {
  1094. AP(setup_env) = 1;
  1095. add_common_vars(r);
  1096. add_cgi_vars(r);
  1097. }
  1098. SG(server_context) = r;
  1099. init_request_info(TSRMLS_C);
  1100. apache_php_module_hook(r, handler, &ret TSRMLS_CC);
  1101. php_restore_umask();
  1102. kill_timeout(r);
  1103. if (ret) {
  1104. convert_to_long(ret);
  1105. return Z_LVAL_P(ret);
  1106. }
  1107. return HTTP_INTERNAL_SERVER_ERROR;
  1108. }
  1109. static int php_uri_translation(request_rec *r)
  1110. {
  1111. php_per_server_config *conf;
  1112. TSRMLS_FETCH();
  1113. AP(current_hook) = AP_URI_TRANS;
  1114. conf = (php_per_server_config *) get_module_config(r->server->module_config, &php5_module);
  1115. return sapi_stack_apply_with_argument_stop_if_equals(&conf->uri_handlers,
  1116. ZEND_STACK_APPLY_BOTTOMUP,
  1117. (int (*)(void *element, void *)) php_run_hook, r, OK);
  1118. }
  1119. static int php_header_hook(request_rec *r)
  1120. {
  1121. php_per_dir_config *conf;
  1122. TSRMLS_FETCH();
  1123. AP(current_hook) = AP_HEADER_PARSE;
  1124. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php5_module);
  1125. return sapi_stack_apply_with_argument_stop_if_http_error(&conf->headers_handlers,
  1126. ZEND_STACK_APPLY_BOTTOMUP,
  1127. (int (*)(void *element, void *)) php_run_hook, r);
  1128. }
  1129. static int php_auth_hook(request_rec *r)
  1130. {
  1131. php_per_dir_config *conf;
  1132. TSRMLS_FETCH();
  1133. AP(current_hook) = AP_AUTHENTICATION;
  1134. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php5_module);
  1135. return sapi_stack_apply_with_argument_stop_if_equals(&conf->auth_handlers,
  1136. ZEND_STACK_APPLY_BOTTOMUP,
  1137. (int (*)(void *element, void *)) php_run_hook, r, OK);
  1138. }
  1139. static int php_access_hook(request_rec *r)
  1140. {
  1141. php_per_dir_config *conf;
  1142. int status = DECLINED;
  1143. TSRMLS_FETCH();
  1144. AP(current_hook) = AP_ACCESS_CONTROL;
  1145. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php5_module);
  1146. status = sapi_stack_apply_with_argument_stop_if_http_error(&conf->access_handlers,
  1147. ZEND_STACK_APPLY_BOTTOMUP,
  1148. (int (*)(void *element, void *)) php_run_hook, r);
  1149. return status;
  1150. }
  1151. static int php_type_hook(request_rec *r)
  1152. {
  1153. php_per_dir_config *conf;
  1154. TSRMLS_FETCH();
  1155. AP(current_hook) = AP_TYPE_CHECKING;
  1156. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php5_module);
  1157. return sapi_stack_apply_with_argument_stop_if_equals(&conf->type_handlers,
  1158. ZEND_STACK_APPLY_BOTTOMUP,
  1159. (int (*)(void *element, void *)) php_run_hook,
  1160. r, OK);
  1161. }
  1162. static int php_fixup_hook(request_rec *r)
  1163. {
  1164. php_per_dir_config *conf;
  1165. TSRMLS_FETCH();
  1166. AP(current_hook) = AP_FIXUP;
  1167. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php5_module);
  1168. return sapi_stack_apply_with_argument_stop_if_http_error(&conf->fixup_handlers,
  1169. ZEND_STACK_APPLY_BOTTOMUP,
  1170. (int (*)(void *element, void *)) php_run_hook,
  1171. r);
  1172. }
  1173. static int php_logger_hook(request_rec *r)
  1174. {
  1175. php_per_dir_config *conf;
  1176. TSRMLS_FETCH();
  1177. AP(current_hook) = AP_LOGGING;
  1178. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php5_module);
  1179. return sapi_stack_apply_with_argument_stop_if_http_error(&conf->logger_handlers,
  1180. ZEND_STACK_APPLY_BOTTOMUP,
  1181. (int (*)(void *element, void *)) php_run_hook,
  1182. r);
  1183. }
  1184. static int php_post_read_hook(request_rec *r)
  1185. {
  1186. php_per_dir_config *conf;
  1187. php_per_server_config *svr;
  1188. TSRMLS_FETCH();
  1189. AP(current_hook) = AP_POST_READ;
  1190. svr = get_module_config(r->server->module_config, &php5_module);
  1191. if(ap_is_initial_req(r)) {
  1192. sapi_stack_apply_with_argument_all(&svr->requires, ZEND_STACK_APPLY_BOTTOMUP, (int (*)(void *element, void *)) php_run_hook, r);
  1193. }
  1194. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php5_module);
  1195. return sapi_stack_apply_with_argument_stop_if_http_error(&conf->post_read_handlers,
  1196. ZEND_STACK_APPLY_BOTTOMUP,
  1197. (int (*)(void *element, void *)) php_run_hook, r);
  1198. }
  1199. static int php_response_handler(request_rec *r)
  1200. {
  1201. php_per_dir_config *conf;
  1202. TSRMLS_FETCH();
  1203. AP(current_hook) = AP_RESPONSE;
  1204. conf = (php_per_dir_config *) get_module_config(r->per_dir_config, &php5_module);
  1205. return sapi_stack_apply_with_argument_all(&conf->response_handlers, ZEND_STACK_APPLY_BOTTOMUP, (int (*)(void *element, void *)) php_run_hook, r);
  1206. }
  1207. /* {{{ handler_rec php_handlers[]
  1208. */
  1209. handler_rec php_handlers[] =
  1210. {
  1211. {"application/x-httpd-php", send_parsed_php},
  1212. {"application/x-httpd-php-source", send_parsed_php_source},
  1213. {"text/html", php_xbithack_handler},
  1214. {"php-script", php_response_handler},
  1215. {NULL}
  1216. };
  1217. /* }}} */
  1218. /* {{{ command_rec php_commands[]
  1219. */
  1220. command_rec php_commands[] =
  1221. {
  1222. {"php_value", php_apache_value_handler, NULL, OR_OPTIONS, TAKE2, "PHP Value Modifier"},
  1223. {"phpUriHandler", php_set_uri_handler, NULL, RSRC_CONF, TAKE1, "PHP Value Modifier"},
  1224. {"phpUriHandlerMethod", php_set_uri_handler_code, NULL, RSRC_CONF, TAKE1, "PHP Value Modifier"},
  1225. #if MODULE_MAGIC_NUMBER >= 19970103
  1226. {"phpHeaderHandler", php_set_header_handler, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1227. {"phpHeaderHandlerMethod", php_set_header_handler_code, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1228. #endif
  1229. {"phpAuthHandler", php_set_auth_handler, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1230. {"phpAuthHandlerMethod", php_set_auth_handler_code, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1231. {"phpAccessHandler", php_set_access_handler, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1232. {"phpAccessHandlerMethod", php_set_access_handler_code, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1233. {"phpTypeHandler", php_set_type_handler, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1234. {"phpTypeHandlerMethod", php_set_type_handler_code, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1235. {"phpFixupHandler", php_set_fixup_handler, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1236. {"phpFixupHandlerMethod", php_set_fixup_handler_code, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1237. {"phpLoggerHandler", php_set_logger_handler, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1238. {"phpLoggerHandlerMethod", php_set_logger_handler_code, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1239. #if MODULE_MAGIC_NUMBER >= 19970902
  1240. {"phpPostReadHandler", php_set_post_read_handler, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1241. {"phpPostReadHandlerMethod", php_set_post_read_handler_code, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1242. {"phpRequire", php_set_require, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1243. {"phpResponseHandler", php_set_response_handler, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1244. {"phpResponseHandlerMethod", php_set_response_handler_code, NULL, OR_OPTIONS, TAKE1, "PHP Value Modifier"},
  1245. #endif
  1246. {"php_flag", php_apache_flag_handler, NULL, OR_OPTIONS, TAKE2, "PHP Flag Modifier"},
  1247. {"php_admin_value", php_apache_admin_value_handler, NULL, ACCESS_CONF|RSRC_CONF, TAKE2, "PHP Value Modifier (Admin)"},
  1248. {"php_admin_flag", php_apache_admin_flag_handler, NULL, ACCESS_CONF|RSRC_CONF, TAKE2, "PHP Flag Modifier (Admin)"},
  1249. {NULL}
  1250. };
  1251. /* }}} */
  1252. /* {{{ module MODULE_VAR_EXPORT php5_module
  1253. */
  1254. module MODULE_VAR_EXPORT php5_module =
  1255. {
  1256. STANDARD_MODULE_STUFF,
  1257. php_init_handler, /* initializer */
  1258. php_create_dir, /* per-directory config creator */
  1259. php_merge_dir, /* dir merger */
  1260. php_create_server, /* per-server config creator */
  1261. NULL, /* merge server config */
  1262. php_commands, /* command table */
  1263. php_handlers, /* handlers */
  1264. php_uri_translation, /* filename translation */
  1265. NULL, /* check_user_id */
  1266. php_auth_hook, /* check auth */
  1267. php_access_hook, /* check access */
  1268. php_type_hook, /* type_checker */
  1269. php_fixup_hook, /* fixups */
  1270. php_logger_hook /* logger */
  1271. #if MODULE_MAGIC_NUMBER >= 19970103
  1272. , php_header_hook /* header parser */
  1273. #endif
  1274. #if MODULE_MAGIC_NUMBER >= 19970719
  1275. , NULL /* child_init */
  1276. #endif
  1277. #if MODULE_MAGIC_NUMBER >= 19970728
  1278. , php_child_exit_handler /* child_exit */
  1279. #endif
  1280. #if MODULE_MAGIC_NUMBER >= 19970902
  1281. , php_post_read_hook /* post read-request */
  1282. #endif
  1283. };
  1284. /* }}} */
  1285. /*
  1286. * Local variables:
  1287. * tab-width: 4
  1288. * c-basic-offset: 4
  1289. * End:
  1290. * vim600: sw=4 ts=4 fdm=marker
  1291. * vim<600: sw=4 ts=4
  1292. */