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.

1011 lines
27 KiB

27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
Generalized server-API build process on UNIX. Each SAPI implementation now has its own directory under sapi/, just like extensions have theirs under ext/. To make the final targets appear in the main dir, the top-level Makefile includes sapi/NN/Makefile.inc from the selected sapi backend. This is a plan Makefile stub without any autoconf substitutions. Each SAPI backend also has its own config.m4 like extensions (read at the end of diversion 2) and config.h.stub files. Each SAPI backend has to contain: config.m4: just like for extensions, this file contains autoconf/automake directives that end up in the configure script. The only difference is that the sapi config.m4 files are read in diversion (output block) 2 instead of 3. The sapi config.m4 files should set two variables: PHP_SAPI (which sapi backend to choose) and SAPI_TARGET (the name of the resulting library or program, previously BINNAME). If they are not specified, they will default to "cgi" and "php", respectively. Makefile.inc: has to exist, has to define "INSTALL_IT" to the command used to install the final target (or ":" for no operation). It also has to define a plain Makefile rule (without autoconf substitutions) to build $(SAPI_TARGET) Makefile.am: just what you think. Make sure your target is called "libphpsapi_NNN.a", where NNN is the value of PHP_SAPI. Some testing and fixing probably remains. To make everything hang together, I've done some ugly tricks that I can imagine causing some problems. I've built and run the CGI version and built the Apache DSO.
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
25 years ago
26 years ago
26 years ago
26 years ago
23 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
24 years ago
27 years ago
27 years ago
27 years ago
21 years ago
27 years ago
26 years ago
27 years ago
25 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
25 years ago
27 years ago
27 years ago
23 years ago
27 years ago
27 years ago
27 years ago
24 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 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 through the world-wide-web at the following url: |
  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. #include "http_conf_globals.h"
  23. #ifdef NETWARE
  24. #define SIGPIPE SIGINT
  25. #endif
  26. #undef shutdown
  27. /* {{{ Prototypes
  28. */
  29. int apache_php_module_main(request_rec *r, int display_source_mode TSRMLS_DC);
  30. static void php_save_umask(void);
  31. static void php_restore_umask(void);
  32. static int sapi_apache_read_post(char *buffer, uint count_bytes TSRMLS_DC);
  33. static char *sapi_apache_read_cookies(TSRMLS_D);
  34. static int sapi_apache_header_handler(sapi_header_struct *sapi_header, sapi_headers_struct *sapi_headers TSRMLS_DC);
  35. static int sapi_apache_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC);
  36. static int send_php(request_rec *r, int display_source_mode, char *filename);
  37. static int send_parsed_php(request_rec * r);
  38. static int send_parsed_php_source(request_rec * r);
  39. static int php_xbithack_handler(request_rec * r);
  40. static void php_init_handler(server_rec *s, pool *p);
  41. /* }}} */
  42. #if MODULE_MAGIC_NUMBER >= 19970728
  43. static void php_child_exit_handler(server_rec *s, pool *p);
  44. #endif
  45. #if MODULE_MAGIC_NUMBER > 19961007
  46. #define CONST_PREFIX const
  47. #else
  48. #define CONST_PREFIX
  49. #endif
  50. static CONST_PREFIX char *php_apache_value_handler_ex(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2, int mode);
  51. static CONST_PREFIX char *php_apache_value_handler(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2);
  52. static CONST_PREFIX char *php_apache_admin_value_handler(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2);
  53. static CONST_PREFIX char *php_apache_flag_handler(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2);
  54. static CONST_PREFIX char *php_apache_flag_handler_ex(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2, int mode);
  55. static CONST_PREFIX char *php_apache_admin_flag_handler(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2);
  56. /* ### these should be defined in mod_php5.h or somewhere else */
  57. #define USE_PATH 1
  58. #define IGNORE_URL 2
  59. #define MAX_STATUS_LENGTH sizeof("xxxx LONGEST POSSIBLE STATUS DESCRIPTION")
  60. module MODULE_VAR_EXPORT php5_module;
  61. int saved_umask;
  62. static unsigned char apache_php_initialized;
  63. typedef struct _php_per_dir_entry {
  64. char *key;
  65. char *value;
  66. uint key_length;
  67. uint value_length;
  68. int type;
  69. } php_per_dir_entry;
  70. /* some systems are missing these from their header files */
  71. /* {{{ php_save_umask
  72. */
  73. static void php_save_umask(void)
  74. {
  75. saved_umask = umask(077);
  76. umask(saved_umask);
  77. }
  78. /* }}} */
  79. /* {{{ sapi_apache_ub_write
  80. */
  81. static int sapi_apache_ub_write(const char *str, uint str_length TSRMLS_DC)
  82. {
  83. int ret=0;
  84. if (SG(server_context)) {
  85. ret = rwrite(str, str_length, (request_rec *) SG(server_context));
  86. }
  87. if (ret != str_length) {
  88. php_handle_aborted_connection();
  89. }
  90. return ret;
  91. }
  92. /* }}} */
  93. /* {{{ sapi_apache_flush
  94. */
  95. static void sapi_apache_flush(void *server_context)
  96. {
  97. if (server_context) {
  98. #if MODULE_MAGIC_NUMBER > 19970110
  99. rflush((request_rec *) server_context);
  100. #else
  101. bflush((request_rec *) server_context->connection->client);
  102. #endif
  103. }
  104. }
  105. /* }}} */
  106. /* {{{ sapi_apache_read_post
  107. */
  108. static int sapi_apache_read_post(char *buffer, uint count_bytes TSRMLS_DC)
  109. {
  110. int total_read_bytes=0, read_bytes;
  111. request_rec *r = (request_rec *) SG(server_context);
  112. void (*handler)(int);
  113. /*
  114. * This handles the situation where the browser sends a Expect: 100-continue header
  115. * and needs to recieve confirmation from the server on whether or not it can send
  116. * the rest of the request. RFC 2616
  117. *
  118. */
  119. if (!SG(read_post_bytes) && !ap_should_client_block(r)) {
  120. return total_read_bytes;
  121. }
  122. handler = signal(SIGPIPE, SIG_IGN);
  123. while (total_read_bytes<count_bytes) {
  124. hard_timeout("Read POST information", r); /* start timeout timer */
  125. read_bytes = get_client_block(r, buffer+total_read_bytes, count_bytes-total_read_bytes);
  126. reset_timeout(r);
  127. if (read_bytes<=0) {
  128. break;
  129. }
  130. total_read_bytes += read_bytes;
  131. }
  132. signal(SIGPIPE, handler);
  133. return total_read_bytes;
  134. }
  135. /* }}} */
  136. /* {{{ sapi_apache_read_cookies
  137. */
  138. static char *sapi_apache_read_cookies(TSRMLS_D)
  139. {
  140. return (char *) table_get(((request_rec *) SG(server_context))->subprocess_env, "HTTP_COOKIE");
  141. }
  142. /* }}} */
  143. /* {{{ sapi_apache_header_handler
  144. */
  145. static int sapi_apache_header_handler(sapi_header_struct *sapi_header, sapi_headers_struct *sapi_headers TSRMLS_DC)
  146. {
  147. char *header_name, *header_content, *p;
  148. request_rec *r = (request_rec *) SG(server_context);
  149. if(!r) {
  150. efree(sapi_header->header);
  151. return 0;
  152. }
  153. header_name = sapi_header->header;
  154. header_content = p = strchr(header_name, ':');
  155. if (!p) {
  156. efree(sapi_header->header);
  157. return 0;
  158. }
  159. *p = 0;
  160. do {
  161. header_content++;
  162. } while (*header_content==' ');
  163. if (!strcasecmp(header_name, "Content-Type")) {
  164. r->content_type = pstrdup(r->pool, header_content);
  165. } else if (!strcasecmp(header_name, "Set-Cookie")) {
  166. table_add(r->headers_out, header_name, header_content);
  167. } else if (sapi_header->replace) {
  168. table_set(r->headers_out, header_name, header_content);
  169. } else {
  170. table_add(r->headers_out, header_name, header_content);
  171. }
  172. *p = ':'; /* a well behaved header handler shouldn't change its original arguments */
  173. return SAPI_HEADER_ADD;
  174. }
  175. /* }}} */
  176. /* {{{ sapi_apache_send_headers
  177. */
  178. static int sapi_apache_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC)
  179. {
  180. request_rec *r = SG(server_context);
  181. char *status_buf = NULL;
  182. const char *sline = SG(sapi_headers).http_status_line;
  183. int sline_len;
  184. if(r == NULL) { /* server_context is not here anymore */
  185. return SAPI_HEADER_SEND_FAILED;
  186. }
  187. r->status = SG(sapi_headers).http_response_code;
  188. /* httpd requires that r->status_line is set to the first digit of
  189. * the status-code: */
  190. if (sline && ((sline_len = strlen(sline)) > 12) && strncmp(sline, "HTTP/1.", 7) == 0 && sline[8] == ' ' && sline[12] == ' ') {
  191. if ((sline_len - 9) > MAX_STATUS_LENGTH) {
  192. status_buf = estrndup(sline + 9, MAX_STATUS_LENGTH);
  193. } else {
  194. status_buf = estrndup(sline + 9, sline_len - 9);
  195. }
  196. r->status_line = status_buf;
  197. }
  198. if(r->status==304) {
  199. send_error_response(r,0);
  200. } else {
  201. send_http_header(r);
  202. }
  203. if (status_buf) {
  204. efree(status_buf);
  205. }
  206. return SAPI_HEADER_SENT_SUCCESSFULLY;
  207. }
  208. /* }}} */
  209. /* {{{ sapi_apache_register_server_variables
  210. */
  211. static void sapi_apache_register_server_variables(zval *track_vars_array TSRMLS_DC)
  212. {
  213. register int i;
  214. array_header *arr = table_elts(((request_rec *) SG(server_context))->subprocess_env);
  215. table_entry *elts = (table_entry *) arr->elts;
  216. zval **path_translated;
  217. HashTable *symbol_table;
  218. for (i = 0; i < arr->nelts; i++) {
  219. char *val;
  220. int val_len, new_val_len;
  221. if (elts[i].val) {
  222. val = elts[i].val;
  223. } else {
  224. val = "";
  225. }
  226. val_len = strlen(val);
  227. if (sapi_module.input_filter(PARSE_SERVER, elts[i].key, &val, val_len, &new_val_len TSRMLS_CC)) {
  228. php_register_variable_safe(elts[i].key, val, new_val_len, track_vars_array TSRMLS_CC);
  229. }
  230. }
  231. /* If PATH_TRANSLATED doesn't exist, copy it from SCRIPT_FILENAME */
  232. if (track_vars_array) {
  233. symbol_table = track_vars_array->value.ht;
  234. } else {
  235. symbol_table = NULL;
  236. }
  237. if (symbol_table
  238. && !zend_hash_exists(symbol_table, "PATH_TRANSLATED", sizeof("PATH_TRANSLATED"))
  239. && zend_hash_find(symbol_table, "SCRIPT_FILENAME", sizeof("SCRIPT_FILENAME"), (void **) &path_translated)==SUCCESS) {
  240. php_register_variable("PATH_TRANSLATED", Z_STRVAL_PP(path_translated), track_vars_array TSRMLS_CC);
  241. }
  242. php_register_variable("PHP_SELF", ((request_rec *) SG(server_context))->uri, track_vars_array TSRMLS_CC);
  243. }
  244. /* }}} */
  245. /* {{{ php_apache_startup
  246. */
  247. static int php_apache_startup(sapi_module_struct *sapi_module)
  248. {
  249. if (php_module_startup(sapi_module, &apache_module_entry, 1) == FAILURE) {
  250. return FAILURE;
  251. } else {
  252. return SUCCESS;
  253. }
  254. }
  255. /* }}} */
  256. /* {{{ php_apache_log_message
  257. */
  258. static void php_apache_log_message(char *message)
  259. {
  260. TSRMLS_FETCH();
  261. if (SG(server_context)) {
  262. #if MODULE_MAGIC_NUMBER >= 19970831
  263. aplog_error(NULL, 0, APLOG_ERR | APLOG_NOERRNO, ((request_rec *) SG(server_context))->server, "%s", message);
  264. #else
  265. log_error(message, ((request_rec *) SG(server_context))->server);
  266. #endif
  267. } else {
  268. fprintf(stderr, "%s\n", message);
  269. }
  270. }
  271. /* }}} */
  272. /* {{{ php_apache_request_shutdown
  273. */
  274. static void php_apache_request_shutdown(void *dummy)
  275. {
  276. TSRMLS_FETCH();
  277. php_output_set_status(PHP_OUTPUT_DISABLED TSRMLS_CC);
  278. if (AP(in_request)) {
  279. AP(in_request) = 0;
  280. php_request_shutdown(dummy);
  281. }
  282. SG(server_context) = NULL;
  283. /*
  284. * The server context (request) is NOT invalid by the time
  285. * run_cleanups() is called
  286. */
  287. }
  288. /* }}} */
  289. /* {{{ php_apache_sapi_activate
  290. */
  291. static int php_apache_sapi_activate(TSRMLS_D)
  292. {
  293. request_rec *r = (request_rec *) SG(server_context);
  294. /*
  295. * For the Apache module version, this bit of code registers a cleanup
  296. * function that gets triggered when our request pool is destroyed.
  297. * We need this because at any point in our code we can be interrupted
  298. * and that may happen before we have had time to free our memory.
  299. * The php_request_shutdown function needs to free all outstanding allocated
  300. * memory.
  301. */
  302. block_alarms();
  303. register_cleanup(r->pool, NULL, php_apache_request_shutdown, php_request_shutdown_for_exec);
  304. AP(in_request)=1;
  305. unblock_alarms();
  306. /* Override the default headers_only value - sometimes "GET" requests should actually only
  307. * send headers.
  308. */
  309. SG(request_info).headers_only = r->header_only;
  310. return SUCCESS;
  311. }
  312. /* }}} */
  313. /* {{{ php_apache_get_stat
  314. */
  315. static struct stat *php_apache_get_stat(TSRMLS_D)
  316. {
  317. return &((request_rec *) SG(server_context))->finfo;
  318. }
  319. /* }}} */
  320. /* {{{ php_apache_getenv
  321. */
  322. static char *php_apache_getenv(char *name, size_t name_len TSRMLS_DC)
  323. {
  324. if (SG(server_context) == NULL) {
  325. return NULL;
  326. }
  327. return (char *) table_get(((request_rec *) SG(server_context))->subprocess_env, name);
  328. }
  329. /* }}} */
  330. /* {{{ sapi_apache_get_fd
  331. */
  332. static int sapi_apache_get_fd(int *nfd TSRMLS_DC)
  333. {
  334. #if PHP_APACHE_HAVE_CLIENT_FD
  335. request_rec *r = SG(server_context);
  336. int fd;
  337. fd = r->connection->client->fd;
  338. if (fd >= 0) {
  339. if (nfd) *nfd = fd;
  340. return SUCCESS;
  341. }
  342. #endif
  343. return FAILURE;
  344. }
  345. /* }}} */
  346. /* {{{ sapi_apache_force_http_10
  347. */
  348. static int sapi_apache_force_http_10(TSRMLS_D)
  349. {
  350. request_rec *r = SG(server_context);
  351. r->proto_num = HTTP_VERSION(1,0);
  352. return SUCCESS;
  353. }
  354. /* }}} */
  355. /* {{{ sapi_apache_get_target_uid
  356. */
  357. static int sapi_apache_get_target_uid(uid_t *obj TSRMLS_DC)
  358. {
  359. *obj = ap_user_id;
  360. return SUCCESS;
  361. }
  362. /* }}} */
  363. /* {{{ sapi_apache_get_target_gid
  364. */
  365. static int sapi_apache_get_target_gid(gid_t *obj TSRMLS_DC)
  366. {
  367. *obj = ap_group_id;
  368. return SUCCESS;
  369. }
  370. /* }}} */
  371. /* {{{ php_apache_get_request_time
  372. */
  373. static time_t php_apache_get_request_time(TSRMLS_D)
  374. {
  375. return ((request_rec *)SG(server_context))->request_time;
  376. }
  377. /* }}} */
  378. /* {{{ sapi_module_struct apache_sapi_module
  379. */
  380. static sapi_module_struct apache_sapi_module = {
  381. "apache", /* name */
  382. "Apache", /* pretty name */
  383. php_apache_startup, /* startup */
  384. php_module_shutdown_wrapper, /* shutdown */
  385. php_apache_sapi_activate, /* activate */
  386. NULL, /* deactivate */
  387. sapi_apache_ub_write, /* unbuffered write */
  388. sapi_apache_flush, /* flush */
  389. php_apache_get_stat, /* get uid */
  390. php_apache_getenv, /* getenv */
  391. php_error, /* error handler */
  392. sapi_apache_header_handler, /* header handler */
  393. sapi_apache_send_headers, /* send headers handler */
  394. NULL, /* send header handler */
  395. sapi_apache_read_post, /* read POST data */
  396. sapi_apache_read_cookies, /* read Cookies */
  397. sapi_apache_register_server_variables, /* register server variables */
  398. php_apache_log_message, /* Log message */
  399. php_apache_get_request_time, /* Get request time */
  400. NULL, /* php.ini path override */
  401. #ifdef PHP_WIN32
  402. NULL,
  403. NULL,
  404. #else
  405. block_alarms, /* Block interruptions */
  406. unblock_alarms, /* Unblock interruptions */
  407. #endif
  408. NULL, /* default post reader */
  409. NULL, /* treat data */
  410. NULL, /* exe location */
  411. 0, /* ini ignore */
  412. sapi_apache_get_fd,
  413. sapi_apache_force_http_10,
  414. sapi_apache_get_target_uid,
  415. sapi_apache_get_target_gid
  416. };
  417. /* }}} */
  418. /* {{{ php_restore_umask
  419. */
  420. static void php_restore_umask(void)
  421. {
  422. umask(saved_umask);
  423. }
  424. /* }}} */
  425. /* {{{ init_request_info
  426. */
  427. static void init_request_info(TSRMLS_D)
  428. {
  429. request_rec *r = ((request_rec *) SG(server_context));
  430. char *content_length = (char *) table_get(r->subprocess_env, "CONTENT_LENGTH");
  431. const char *authorization=NULL;
  432. char *tmp, *tmp_user;
  433. SG(request_info).query_string = r->args;
  434. SG(request_info).path_translated = r->filename;
  435. SG(request_info).request_uri = r->uri;
  436. SG(request_info).request_method = (char *)r->method;
  437. SG(request_info).content_type = (char *) table_get(r->subprocess_env, "CONTENT_TYPE");
  438. SG(request_info).content_length = (content_length ? atoi(content_length) : 0);
  439. SG(sapi_headers).http_response_code = r->status;
  440. SG(request_info).proto_num = r->proto_num;
  441. if (r->headers_in) {
  442. authorization = table_get(r->headers_in, "Authorization");
  443. }
  444. SG(request_info).auth_user = NULL;
  445. SG(request_info).auth_password = NULL;
  446. SG(request_info).auth_digest = NULL;
  447. if (authorization) {
  448. char *p = getword(r->pool, &authorization, ' ');
  449. if (!strcasecmp(p, "Basic")) {
  450. tmp = uudecode(r->pool, authorization);
  451. tmp_user = getword_nulls_nc(r->pool, &tmp, ':');
  452. if (tmp_user) {
  453. r->connection->user = pstrdup(r->connection->pool, tmp_user);
  454. r->connection->ap_auth_type = "Basic";
  455. SG(request_info).auth_user = estrdup(tmp_user);
  456. }
  457. if (tmp) {
  458. SG(request_info).auth_password = estrdup(tmp);
  459. }
  460. } else if (!strcasecmp(p, "Digest")) {
  461. r->connection->ap_auth_type = "Digest";
  462. SG(request_info).auth_digest = estrdup(authorization);
  463. }
  464. }
  465. }
  466. /* }}} */
  467. /* {{{ php_apache_alter_ini_entries
  468. */
  469. static int php_apache_alter_ini_entries(php_per_dir_entry *per_dir_entry TSRMLS_DC)
  470. {
  471. 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);
  472. return 0;
  473. }
  474. /* }}} */
  475. /* {{{ php_apache_get_default_mimetype
  476. */
  477. static char *php_apache_get_default_mimetype(request_rec *r TSRMLS_DC)
  478. {
  479. char *mimetype;
  480. /* Assume output will be of the default MIME type. Individual
  481. scripts may change this later. */
  482. char *tmpmimetype;
  483. tmpmimetype = sapi_get_default_content_type(TSRMLS_C);
  484. mimetype = pstrdup(r->pool, tmpmimetype);
  485. efree(tmpmimetype);
  486. return mimetype;
  487. }
  488. /* }}} */
  489. /* {{{ send_php
  490. */
  491. static int send_php(request_rec *r, int display_source_mode, char *filename)
  492. {
  493. int retval;
  494. HashTable *per_dir_conf;
  495. TSRMLS_FETCH();
  496. if (AP(in_request)) {
  497. zend_file_handle fh;
  498. fh.filename = r->filename;
  499. fh.opened_path = NULL;
  500. fh.free_filename = 0;
  501. fh.type = ZEND_HANDLE_FILENAME;
  502. zend_execute_scripts(ZEND_INCLUDE TSRMLS_CC, NULL, 1, &fh);
  503. return OK;
  504. }
  505. zend_first_try {
  506. /* Make sure file exists */
  507. if (filename == NULL && r->finfo.st_mode == 0) {
  508. return DECLINED;
  509. }
  510. per_dir_conf = (HashTable *) get_module_config(r->per_dir_config, &php5_module);
  511. if (per_dir_conf) {
  512. zend_hash_apply((HashTable *) per_dir_conf, (apply_func_t) php_apache_alter_ini_entries TSRMLS_CC);
  513. }
  514. /* If PHP parser engine has been turned off with an "engine off"
  515. * directive, then decline to handle this request
  516. */
  517. if (!AP(engine)) {
  518. r->content_type = php_apache_get_default_mimetype(r TSRMLS_CC);
  519. zend_try {
  520. zend_ini_deactivate(TSRMLS_C);
  521. } zend_end_try();
  522. return DECLINED;
  523. }
  524. if (filename == NULL) {
  525. filename = r->filename;
  526. }
  527. /* Apache 1.2 has a more complex mechanism for reading POST data */
  528. #if MODULE_MAGIC_NUMBER > 19961007
  529. if ((retval = setup_client_block(r, REQUEST_CHUNKED_DECHUNK))) {
  530. zend_try {
  531. zend_ini_deactivate(TSRMLS_C);
  532. } zend_end_try();
  533. return retval;
  534. }
  535. #endif
  536. if (AP(last_modified)) {
  537. #if MODULE_MAGIC_NUMBER < 19970912
  538. if ((retval = set_last_modified(r, r->finfo.st_mtime))) {
  539. zend_try {
  540. zend_ini_deactivate(TSRMLS_C);
  541. } zend_end_try();
  542. return retval;
  543. }
  544. #else
  545. update_mtime (r, r->finfo.st_mtime);
  546. set_last_modified(r);
  547. set_etag(r);
  548. #endif
  549. }
  550. /* Assume output will be of the default MIME type. Individual
  551. scripts may change this later in the request. */
  552. r->content_type = php_apache_get_default_mimetype(r TSRMLS_CC);
  553. /* Init timeout */
  554. hard_timeout("send", r);
  555. SG(server_context) = r;
  556. php_save_umask();
  557. add_common_vars(r);
  558. add_cgi_vars(r);
  559. init_request_info(TSRMLS_C);
  560. apache_php_module_main(r, display_source_mode TSRMLS_CC);
  561. /* Done, restore umask, turn off timeout, close file and return */
  562. php_restore_umask();
  563. kill_timeout(r);
  564. } zend_end_try();
  565. return OK;
  566. }
  567. /* }}} */
  568. /* {{{ send_parsed_php
  569. */
  570. static int send_parsed_php(request_rec * r)
  571. {
  572. int result = send_php(r, 0, NULL);
  573. #if MEMORY_LIMIT
  574. {
  575. char *mem_usage;
  576. TSRMLS_FETCH();
  577. mem_usage = ap_psprintf(r->pool, "%u", zend_memory_peak_usage(1 TSRMLS_CC));
  578. ap_table_setn(r->notes, "mod_php_memory_usage", mem_usage);
  579. }
  580. #endif
  581. return result;
  582. }
  583. /* }}} */
  584. /* {{{ send_parsed_php_source
  585. */
  586. static int send_parsed_php_source(request_rec * r)
  587. {
  588. return send_php(r, 1, NULL);
  589. }
  590. /* }}} */
  591. /* {{{ destroy_per_dir_entry
  592. */
  593. static void destroy_per_dir_entry(php_per_dir_entry *per_dir_entry)
  594. {
  595. free(per_dir_entry->key);
  596. free(per_dir_entry->value);
  597. }
  598. /* }}} */
  599. /* {{{ copy_per_dir_entry
  600. */
  601. static void copy_per_dir_entry(php_per_dir_entry *per_dir_entry)
  602. {
  603. php_per_dir_entry tmp = *per_dir_entry;
  604. per_dir_entry->key = (char *) malloc(tmp.key_length+1);
  605. memcpy(per_dir_entry->key, tmp.key, tmp.key_length);
  606. per_dir_entry->key[per_dir_entry->key_length] = 0;
  607. per_dir_entry->value = (char *) malloc(tmp.value_length+1);
  608. memcpy(per_dir_entry->value, tmp.value, tmp.value_length);
  609. per_dir_entry->value[per_dir_entry->value_length] = 0;
  610. }
  611. /* }}} */
  612. /* {{{ should_overwrite_per_dir_entry
  613. */
  614. static zend_bool should_overwrite_per_dir_entry(HashTable *target_ht, php_per_dir_entry *new_per_dir_entry, zend_hash_key *hash_key, void *pData)
  615. {
  616. php_per_dir_entry *orig_per_dir_entry;
  617. if (zend_u_hash_find(target_ht, hash_key->type, hash_key->arKey, hash_key->nKeyLength, (void **) &orig_per_dir_entry)==FAILURE) {
  618. return 1; /* does not exist in dest, copy from source */
  619. }
  620. if (new_per_dir_entry->type==PHP_INI_SYSTEM
  621. && orig_per_dir_entry->type!=PHP_INI_SYSTEM) {
  622. return 1;
  623. } else {
  624. return 0;
  625. }
  626. }
  627. /* }}} */
  628. /* {{{ php_destroy_per_dir_info
  629. */
  630. static void php_destroy_per_dir_info(HashTable *per_dir_info)
  631. {
  632. zend_hash_destroy(per_dir_info);
  633. free(per_dir_info);
  634. }
  635. /* }}} */
  636. /* {{{ php_create_dir
  637. */
  638. static void *php_create_dir(pool *p, char *dummy)
  639. {
  640. HashTable *per_dir_info;
  641. per_dir_info = (HashTable *) malloc(sizeof(HashTable));
  642. zend_hash_init_ex(per_dir_info, 5, NULL, (void (*)(void *)) destroy_per_dir_entry, 1, 0);
  643. register_cleanup(p, (void *) per_dir_info, (void (*)(void *)) php_destroy_per_dir_info, (void (*)(void *)) zend_hash_destroy);
  644. return per_dir_info;
  645. }
  646. /* }}} */
  647. /* {{{ php_merge_dir
  648. */
  649. static void *php_merge_dir(pool *p, void *basev, void *addv)
  650. {
  651. /* This function *must* return addv, and not modify basev */
  652. zend_hash_merge_ex((HashTable *) addv, (HashTable *) basev, (copy_ctor_func_t) copy_per_dir_entry, sizeof(php_per_dir_entry), (merge_checker_func_t) should_overwrite_per_dir_entry, NULL);
  653. return addv;
  654. }
  655. /* }}} */
  656. /* {{{ php_apache_value_handler_ex
  657. */
  658. static CONST_PREFIX char *php_apache_value_handler_ex(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2, int mode)
  659. {
  660. php_per_dir_entry per_dir_entry;
  661. if (!apache_php_initialized) {
  662. apache_php_initialized = 1;
  663. #ifdef ZTS
  664. tsrm_startup(1, 1, 0, NULL);
  665. #endif
  666. sapi_startup(&apache_sapi_module);
  667. php_apache_startup(&apache_sapi_module);
  668. }
  669. per_dir_entry.type = mode;
  670. if (strcasecmp(arg2, "none") == 0) {
  671. arg2 = "";
  672. }
  673. per_dir_entry.key_length = strlen(arg1);
  674. per_dir_entry.value_length = strlen(arg2);
  675. per_dir_entry.key = (char *) malloc(per_dir_entry.key_length+1);
  676. memcpy(per_dir_entry.key, arg1, per_dir_entry.key_length);
  677. per_dir_entry.key[per_dir_entry.key_length] = 0;
  678. per_dir_entry.value = (char *) malloc(per_dir_entry.value_length+1);
  679. memcpy(per_dir_entry.value, arg2, per_dir_entry.value_length);
  680. per_dir_entry.value[per_dir_entry.value_length] = 0;
  681. zend_hash_update(conf, per_dir_entry.key, per_dir_entry.key_length, &per_dir_entry, sizeof(php_per_dir_entry), NULL);
  682. return NULL;
  683. }
  684. /* }}} */
  685. /* {{{ php_apache_value_handler
  686. */
  687. static CONST_PREFIX char *php_apache_value_handler(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2)
  688. {
  689. return php_apache_value_handler_ex(cmd, conf, arg1, arg2, PHP_INI_PERDIR);
  690. }
  691. /* }}} */
  692. /* {{{ php_apache_admin_value_handler
  693. */
  694. static CONST_PREFIX char *php_apache_admin_value_handler(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2)
  695. {
  696. return php_apache_value_handler_ex(cmd, conf, arg1, arg2, PHP_INI_SYSTEM);
  697. }
  698. /* }}} */
  699. /* {{{ php_apache_flag_handler_ex
  700. */
  701. static CONST_PREFIX char *php_apache_flag_handler_ex(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2, int mode)
  702. {
  703. char bool_val[2];
  704. if (!strcasecmp(arg2, "On") || (arg2[0] == '1' && arg2[1] == '\0')) {
  705. bool_val[0] = '1';
  706. } else {
  707. bool_val[0] = '0';
  708. }
  709. bool_val[1] = 0;
  710. return php_apache_value_handler_ex(cmd, conf, arg1, bool_val, mode);
  711. }
  712. /* }}} */
  713. /* {{{ php_apache_flag_handler
  714. */
  715. static CONST_PREFIX char *php_apache_flag_handler(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2)
  716. {
  717. return php_apache_flag_handler_ex(cmd, conf, arg1, arg2, PHP_INI_PERDIR);
  718. }
  719. /* }}} */
  720. /* {{{ php_apache_admin_flag_handler
  721. */
  722. static CONST_PREFIX char *php_apache_admin_flag_handler(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2)
  723. {
  724. return php_apache_flag_handler_ex(cmd, conf, arg1, arg2, PHP_INI_SYSTEM);
  725. }
  726. /* }}} */
  727. /* {{{ php_apache_phpini_set
  728. */
  729. static CONST_PREFIX char *php_apache_phpini_set(cmd_parms *cmd, HashTable *conf, char *arg)
  730. {
  731. if (apache_sapi_module.php_ini_path_override) {
  732. return "Only first PHPINIDir directive honored per configuration tree - subsequent ones ignored";
  733. }
  734. apache_sapi_module.php_ini_path_override = ap_server_root_relative(cmd->pool, arg);
  735. return NULL;
  736. }
  737. /* }}} */
  738. /* {{{ int php_xbithack_handler(request_rec * r)
  739. */
  740. static int php_xbithack_handler(request_rec * r)
  741. {
  742. HashTable *per_dir_conf;
  743. TSRMLS_FETCH();
  744. if (!(r->finfo.st_mode & S_IXUSR)) {
  745. return DECLINED;
  746. }
  747. per_dir_conf = (HashTable *) get_module_config(r->per_dir_config, &php5_module);
  748. if (per_dir_conf) {
  749. zend_hash_apply((HashTable *) per_dir_conf, (apply_func_t) php_apache_alter_ini_entries TSRMLS_CC);
  750. }
  751. if(!AP(xbithack)) {
  752. zend_try {
  753. zend_ini_deactivate(TSRMLS_C);
  754. } zend_end_try();
  755. return DECLINED;
  756. }
  757. return send_parsed_php(r);
  758. }
  759. /* }}} */
  760. /* {{{ apache_php_module_shutdown_wrapper
  761. */
  762. static void apache_php_module_shutdown_wrapper(void)
  763. {
  764. apache_php_initialized = 0;
  765. apache_sapi_module.shutdown(&apache_sapi_module);
  766. #if MODULE_MAGIC_NUMBER >= 19970728
  767. /* This function is only called on server exit if the apache API
  768. * child_exit handler exists, so shutdown globally
  769. */
  770. sapi_shutdown();
  771. #endif
  772. #ifdef ZTS
  773. tsrm_shutdown();
  774. #endif
  775. }
  776. /* }}} */
  777. #if MODULE_MAGIC_NUMBER >= 19970728
  778. /* {{{ php_child_exit_handler
  779. */
  780. static void php_child_exit_handler(server_rec *s, pool *p)
  781. {
  782. /* apache_php_initialized = 0; */
  783. apache_sapi_module.shutdown(&apache_sapi_module);
  784. #ifdef ZTS
  785. tsrm_shutdown();
  786. #endif
  787. }
  788. /* }}} */
  789. #endif
  790. /* {{{ void php_init_handler(server_rec *s, pool *p)
  791. */
  792. static void php_init_handler(server_rec *s, pool *p)
  793. {
  794. register_cleanup(p, NULL, (void (*)(void *))apache_php_module_shutdown_wrapper, (void (*)(void *))php_module_shutdown_for_exec);
  795. if (!apache_php_initialized) {
  796. apache_php_initialized = 1;
  797. #ifdef ZTS
  798. tsrm_startup(1, 1, 0, NULL);
  799. #endif
  800. sapi_startup(&apache_sapi_module);
  801. php_apache_startup(&apache_sapi_module);
  802. }
  803. #if MODULE_MAGIC_NUMBER >= 19980527
  804. {
  805. TSRMLS_FETCH();
  806. if (PG(expose_php)) {
  807. ap_add_version_component("PHP/" PHP_VERSION);
  808. }
  809. }
  810. #endif
  811. }
  812. /* }}} */
  813. /* {{{ handler_rec php_handlers[]
  814. */
  815. handler_rec php_handlers[] =
  816. {
  817. {"application/x-httpd-php", send_parsed_php},
  818. {"application/x-httpd-php-source", send_parsed_php_source},
  819. {"text/html", php_xbithack_handler},
  820. {NULL}
  821. };
  822. /* }}} */
  823. /* {{{ command_rec php_commands[]
  824. */
  825. command_rec php_commands[] =
  826. {
  827. {"php_value", php_apache_value_handler, NULL, OR_OPTIONS, TAKE2, "PHP Value Modifier"},
  828. {"php_flag", php_apache_flag_handler, NULL, OR_OPTIONS, TAKE2, "PHP Flag Modifier"},
  829. {"php_admin_value", php_apache_admin_value_handler, NULL, ACCESS_CONF|RSRC_CONF, TAKE2, "PHP Value Modifier (Admin)"},
  830. {"php_admin_flag", php_apache_admin_flag_handler, NULL, ACCESS_CONF|RSRC_CONF, TAKE2, "PHP Flag Modifier (Admin)"},
  831. {"PHPINIDir", php_apache_phpini_set, NULL, RSRC_CONF, TAKE1, "Directory containing the php.ini file"},
  832. {NULL}
  833. };
  834. /* }}} */
  835. /* {{{ odule MODULE_VAR_EXPORT php5_module
  836. */
  837. module MODULE_VAR_EXPORT php5_module =
  838. {
  839. STANDARD_MODULE_STUFF,
  840. php_init_handler, /* initializer */
  841. php_create_dir, /* per-directory config creator */
  842. php_merge_dir, /* dir merger */
  843. NULL, /* per-server config creator */
  844. NULL, /* merge server config */
  845. php_commands, /* command table */
  846. php_handlers, /* handlers */
  847. NULL, /* filename translation */
  848. NULL, /* check_user_id */
  849. NULL, /* check auth */
  850. NULL, /* check access */
  851. NULL, /* type_checker */
  852. NULL, /* fixups */
  853. NULL /* logger */
  854. #if MODULE_MAGIC_NUMBER >= 19970103
  855. , NULL /* header parser */
  856. #endif
  857. #if MODULE_MAGIC_NUMBER >= 19970719
  858. , NULL /* child_init */
  859. #endif
  860. #if MODULE_MAGIC_NUMBER >= 19970728
  861. , php_child_exit_handler /* child_exit */
  862. #endif
  863. #if MODULE_MAGIC_NUMBER >= 19970902
  864. , NULL /* post read-request */
  865. #endif
  866. };
  867. /* }}} */
  868. /*
  869. * Local variables:
  870. * tab-width: 4
  871. * c-basic-offset: 4
  872. * End:
  873. * vim600: sw=4 ts=4 fdm=marker
  874. * vim<600: sw=4 ts=4
  875. */