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.

898 lines
24 KiB

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