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.

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