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.

1465 lines
42 KiB

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