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