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.

950 lines
25 KiB

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