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.

1492 lines
44 KiB

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