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.

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