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.

931 lines
25 KiB

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