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.

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