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.

1013 lines
27 KiB

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