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.

789 lines
21 KiB

28 years ago
27 years ago
28 years ago
28 years ago
27 years ago
27 years ago
28 years ago
28 years ago
28 years ago
28 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
28 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
28 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
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
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 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
28 years ago
28 years ago
28 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
28 years ago
28 years ago
27 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP version 4.0 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.01 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_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. #define NO_REGEX_EXTRA_H
  22. #ifdef WIN32
  23. #include <winsock2.h>
  24. #include <stddef.h>
  25. #endif
  26. #include "zend.h"
  27. #include "php.h"
  28. #include "php_variables.h"
  29. #include "SAPI.h"
  30. #include "httpd.h"
  31. #include "http_config.h"
  32. #if MODULE_MAGIC_NUMBER > 19980712
  33. # include "ap_compat.h"
  34. #else
  35. # if MODULE_MAGIC_NUMBER > 19980324
  36. # include "compat.h"
  37. # endif
  38. #endif
  39. #include "http_core.h"
  40. #include "http_main.h"
  41. #include "http_protocol.h"
  42. #include "http_request.h"
  43. #include "http_log.h"
  44. #include "php_ini.h"
  45. #include "php_globals.h"
  46. #include "SAPI.h"
  47. #include "main.h"
  48. #include "zend_compile.h"
  49. #include "zend_execute.h"
  50. #include "zend_highlight.h"
  51. #include "zend_indent.h"
  52. #include "ext/standard/php_standard.h"
  53. #include "util_script.h"
  54. #include "mod_php4.h"
  55. #if HAVE_MOD_DAV
  56. # include "mod_dav.h"
  57. #endif
  58. int apache_php_module_main(request_rec *r, int fd, int display_source_mode CLS_DC ELS_DC PLS_DC SLS_DC);
  59. void php_save_umask(void);
  60. void php_restore_umask(void);
  61. int sapi_apache_read_post(char *buffer, uint count_bytes SLS_DC);
  62. char *sapi_apache_read_cookies(SLS_D);
  63. int sapi_apache_header_handler(sapi_header_struct *sapi_header, sapi_headers_struct *sapi_headers SLS_DC);
  64. int sapi_apache_send_headers(sapi_headers_struct *sapi_headers SLS_DC);
  65. int send_php(request_rec *r, int display_source_mode, char *filename);
  66. int send_parsed_php(request_rec * r);
  67. int send_parsed_php_source(request_rec * r);
  68. int php_xbithack_handler(request_rec * r);
  69. void php_init_handler(server_rec *s, pool *p);
  70. #if MODULE_MAGIC_NUMBER > 19961007
  71. #define CONST_PREFIX const
  72. #else
  73. #define CONST_PREFIX
  74. #endif
  75. CONST_PREFIX char *php_apache_value_handler_ex(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2, int mode);
  76. CONST_PREFIX char *php_apache_value_handler(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2);
  77. CONST_PREFIX char *php_apache_admin_value_handler(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2);
  78. CONST_PREFIX char *php_apache_flag_handler(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2);
  79. CONST_PREFIX char *php_apache_flag_handler_ex(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2, int mode);
  80. CONST_PREFIX char *php_apache_admin_flag_handler(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2);
  81. /* ### these should be defined in mod_php4.h or somewhere else */
  82. #define USE_PATH 1
  83. #define IGNORE_URL 2
  84. module MODULE_VAR_EXPORT php4_module;
  85. int saved_umask;
  86. static unsigned char apache_php_initialized;
  87. typedef struct _php_per_dir_entry {
  88. char *key;
  89. char *value;
  90. uint key_length;
  91. uint value_length;
  92. int type;
  93. } php_per_dir_entry;
  94. /* handled apropriately in apache_php_module_main */
  95. /* popenf isn't working on Windows, use open instead
  96. #if WIN32|WINNT
  97. # ifdef popenf
  98. # undef popenf
  99. # endif
  100. # define popenf(p,n,f,m) open((n),(f),(m))
  101. # ifdef pclosef
  102. # undef pclosef
  103. # endif
  104. # define pclosef(p,f) close(f)
  105. #endif
  106. */
  107. php_apache_info_struct php_apache_info; /* active config */
  108. /* some systems are missing these from their header files */
  109. void php_save_umask(void)
  110. {
  111. saved_umask = umask(077);
  112. umask(saved_umask);
  113. }
  114. static int sapi_apache_ub_write(const char *str, uint str_length)
  115. {
  116. int ret;
  117. SLS_FETCH();
  118. PLS_FETCH();
  119. if (SG(server_context)) {
  120. ret = rwrite(str, str_length, (request_rec *) SG(server_context));
  121. } else {
  122. ret = fwrite(str, 1, str_length, stdout);
  123. }
  124. if(ret != str_length) {
  125. PG(connection_status) = PHP_CONNECTION_ABORTED;
  126. if (!PG(ignore_user_abort)) {
  127. zend_bailout();
  128. }
  129. }
  130. return ret;
  131. }
  132. static void sapi_apache_flush(void *server_context)
  133. {
  134. if (server_context) {
  135. #if MODULE_MAGIC_NUMBER > 19970110
  136. rflush((request_rec *) server_context);
  137. #else
  138. bflush((request_rec *) server_context->connection->client);
  139. #endif
  140. }
  141. }
  142. int sapi_apache_read_post(char *buffer, uint count_bytes SLS_DC)
  143. {
  144. uint total_read_bytes=0, read_bytes;
  145. request_rec *r = (request_rec *) SG(server_context);
  146. void (*handler)(int);
  147. handler = signal(SIGPIPE, SIG_IGN);
  148. while (total_read_bytes<count_bytes) {
  149. hard_timeout("Read POST information", r); /* start timeout timer */
  150. read_bytes = get_client_block(r, buffer+total_read_bytes, count_bytes-total_read_bytes);
  151. reset_timeout(r);
  152. if (read_bytes<=0) {
  153. break;
  154. }
  155. total_read_bytes += read_bytes;
  156. }
  157. signal(SIGPIPE, handler);
  158. return total_read_bytes;
  159. }
  160. char *sapi_apache_read_cookies(SLS_D)
  161. {
  162. return (char *) table_get(((request_rec *) SG(server_context))->subprocess_env, "HTTP_COOKIE");
  163. }
  164. int sapi_apache_header_handler(sapi_header_struct *sapi_header, sapi_headers_struct *sapi_headers SLS_DC)
  165. {
  166. char *header_name, *header_content, *p;
  167. request_rec *r = (request_rec *) SG(server_context);
  168. header_name = sapi_header->header;
  169. header_content = p = strchr(header_name, ':');
  170. if (!p) {
  171. return 0;
  172. }
  173. *p = 0;
  174. do {
  175. header_content++;
  176. } while (*header_content==' ');
  177. if (!strcasecmp(header_name, "Content-Type")) {
  178. r->content_type = pstrdup(r->pool, header_content);
  179. } else if (!strcasecmp(header_name, "Set-Cookie")) {
  180. table_add(r->headers_out, header_name, header_content);
  181. } else {
  182. table_set(r->headers_out, header_name, header_content);
  183. }
  184. *p = ':'; /* a well behaved header handler shouldn't change its original arguments */
  185. efree(sapi_header->header);
  186. return 0; /* don't use the default SAPI mechanism, Apache duplicates this functionality */
  187. }
  188. int sapi_apache_send_headers(sapi_headers_struct *sapi_headers SLS_DC)
  189. {
  190. ((request_rec *) SG(server_context))->status = SG(sapi_headers).http_response_code;
  191. send_http_header((request_rec *) SG(server_context));
  192. return SAPI_HEADER_SENT_SUCCESSFULLY;
  193. }
  194. static void sapi_apache_register_server_variables(zval *track_vars_array ELS_DC SLS_DC PLS_DC)
  195. {
  196. register int i;
  197. array_header *arr = table_elts(((request_rec *) SG(server_context))->subprocess_env);
  198. table_entry *elts = (table_entry *) arr->elts;
  199. char *script_filename=NULL;
  200. for (i = 0; i < arr->nelts; i++) {
  201. char *val;
  202. if (elts[i].val) {
  203. val = elts[i].val;
  204. if (!strcmp(elts[i].key, "SCRIPT_FILENAME")) {
  205. script_filename = val;
  206. }
  207. } else {
  208. val = empty_string;
  209. }
  210. php_register_variable(elts[i].key, val, NULL ELS_CC PLS_CC);
  211. }
  212. /* insert special variables */
  213. if (script_filename) {
  214. php_register_variable("PATH_TRANSLATED", script_filename, track_vars_array ELS_CC PLS_CC);
  215. }
  216. php_register_variable("PHP_SELF", ((request_rec *) SG(server_context))->uri, track_vars_array ELS_CC PLS_CC);
  217. }
  218. static int php_apache_startup(sapi_module_struct *sapi_module)
  219. {
  220. if(php_module_startup(sapi_module) == FAILURE
  221. || zend_startup_module(&apache_module_entry) == FAILURE) {
  222. return FAILURE;
  223. } else {
  224. return SUCCESS;
  225. }
  226. }
  227. static void php_apache_log_message(char *message)
  228. {
  229. SLS_FETCH();
  230. if (SG(server_context)) {
  231. #if MODULE_MAGIC_NUMBER >= 19970831
  232. aplog_error(NULL, 0, APLOG_ERR | APLOG_NOERRNO, ((request_rec *) SG(server_context))->server, "%s", message);
  233. #else
  234. log_error(message, ((requset_rec *) SG(server_context))->server);
  235. #endif
  236. } else {
  237. fprintf(stderr, message);
  238. fprintf(stderr, "\n");
  239. }
  240. }
  241. static int php_apache_sapi_activate(SLS_D)
  242. {
  243. /*
  244. * For the Apache module version, this bit of code registers a cleanup
  245. * function that gets triggered when our request pool is destroyed.
  246. * We need this because at any point in our code we can be interrupted
  247. * and that may happen before we have had time to free our memory.
  248. * The php_request_shutdown function needs to free all outstanding allocated
  249. * memory.
  250. */
  251. block_alarms();
  252. register_cleanup(((request_rec *) SG(server_context))->pool, NULL, php_request_shutdown, php_request_shutdown_for_exec);
  253. unblock_alarms();
  254. return SUCCESS;
  255. }
  256. static struct stat *php_apache_get_stat(SLS_D)
  257. {
  258. return &((request_rec *) SG(server_context))->finfo;
  259. }
  260. static char *php_apache_getenv(char *name, int name_len SLS_DC)
  261. {
  262. return (char *) table_get(((request_rec *) SG(server_context))->subprocess_env, name);
  263. }
  264. static sapi_module_struct sapi_module = {
  265. "Apache", /* name */
  266. php_apache_startup, /* startup */
  267. php_module_shutdown_wrapper, /* shutdown */
  268. php_apache_sapi_activate, /* activate */
  269. NULL, /* deactivate */
  270. sapi_apache_ub_write, /* unbuffered write */
  271. sapi_apache_flush, /* flush */
  272. php_apache_get_stat, /* get uid */
  273. php_apache_getenv, /* getenv */
  274. php_error, /* error handler */
  275. sapi_apache_header_handler, /* header handler */
  276. sapi_apache_send_headers, /* send headers handler */
  277. NULL, /* send header handler */
  278. sapi_apache_read_post, /* read POST data */
  279. sapi_apache_read_cookies, /* read Cookies */
  280. sapi_apache_register_server_variables, /* register server variables */
  281. php_apache_log_message, /* Log message */
  282. #ifdef PHP_WIN32
  283. NULL,
  284. NULL,
  285. #else
  286. block_alarms, /* Block interruptions */
  287. unblock_alarms, /* Unblock interruptions */
  288. #endif
  289. STANDARD_SAPI_MODULE_PROPERTIES
  290. };
  291. void php_restore_umask(void)
  292. {
  293. umask(saved_umask);
  294. }
  295. static void init_request_info(SLS_D)
  296. {
  297. request_rec *r = ((request_rec *) SG(server_context));
  298. char *content_length = (char *) table_get(r->subprocess_env, "CONTENT_LENGTH");
  299. const char *authorization=NULL;
  300. char *tmp;
  301. SG(request_info).query_string = r->args;
  302. SG(request_info).path_translated = r->filename;
  303. SG(request_info).request_uri = r->uri;
  304. SG(request_info).request_method = (char *)r->method;
  305. SG(request_info).content_type = (char *) table_get(r->subprocess_env, "CONTENT_TYPE");
  306. SG(request_info).content_length = (content_length ? atoi(content_length) : 0);
  307. if (r->headers_in) {
  308. authorization = table_get(r->headers_in, "Authorization");
  309. }
  310. if (authorization
  311. /* && !auth_type(r) */
  312. && !strcmp(getword(r->pool, &authorization, ' '), "Basic")) {
  313. tmp = uudecode(r->pool, authorization);
  314. SG(request_info).auth_user = getword_nulls_nc(r->pool, &tmp, ':');
  315. if (SG(request_info).auth_user) {
  316. SG(request_info).auth_user = estrdup(SG(request_info).auth_user);
  317. }
  318. SG(request_info).auth_password = tmp;
  319. if (SG(request_info).auth_password) {
  320. SG(request_info).auth_password = estrdup(SG(request_info).auth_password);
  321. }
  322. } else {
  323. SG(request_info).auth_user = NULL;
  324. SG(request_info).auth_password = NULL;
  325. }
  326. }
  327. static int php_apache_alter_ini_entries(php_per_dir_entry *per_dir_entry)
  328. {
  329. php_alter_ini_entry(per_dir_entry->key, per_dir_entry->key_length+1, per_dir_entry->value, per_dir_entry->value_length+1, per_dir_entry->type, PHP_INI_STAGE_ACTIVATE);
  330. return 0;
  331. }
  332. static char *php_apache_get_default_mimetype(request_rec *r SLS_DC)
  333. {
  334. char *mimetype;
  335. if (SG(default_mimetype) || SG(default_charset)) {
  336. /* Assume output will be of the default MIME type. Individual
  337. scripts may change this later. */
  338. char *tmpmimetype;
  339. tmpmimetype = sapi_get_default_content_type(SLS_C);
  340. mimetype = pstrdup(r->pool, tmpmimetype);
  341. efree(tmpmimetype);
  342. } else {
  343. mimetype = SAPI_DEFAULT_MIMETYPE "; charset=" SAPI_DEFAULT_CHARSET;
  344. }
  345. return mimetype;
  346. }
  347. int send_php(request_rec *r, int display_source_mode, char *filename)
  348. {
  349. int fd, retval;
  350. HashTable *per_dir_conf;
  351. SLS_FETCH();
  352. ELS_FETCH();
  353. CLS_FETCH();
  354. PLS_FETCH();
  355. if (setjmp(EG(bailout))!=0) {
  356. return OK;
  357. }
  358. per_dir_conf = (HashTable *) get_module_config(r->per_dir_config, &php4_module);
  359. zend_hash_apply((HashTable *) per_dir_conf, (int (*)(void *)) php_apache_alter_ini_entries);
  360. /* We don't accept OPTIONS requests, but take everything else */
  361. if (r->method_number == M_OPTIONS) {
  362. r->allowed |= (1 << METHODS) - 1;
  363. return DECLINED;
  364. }
  365. /* Make sure file exists */
  366. if (filename == NULL && r->finfo.st_mode == 0) {
  367. return NOT_FOUND;
  368. }
  369. /* If PHP parser engine has been turned off with an "engine off"
  370. * directive, then decline to handle this request
  371. */
  372. if (!php_apache_info.engine) {
  373. r->content_type = php_apache_get_default_mimetype(r SLS_CC);
  374. r->allowed |= (1 << METHODS) - 1;
  375. return DECLINED;
  376. }
  377. if (filename == NULL) {
  378. filename = r->filename;
  379. }
  380. /* Open the file */
  381. if ((fd = popenf(r->pool, filename, O_RDONLY, 0)) == -1) {
  382. log_reason("file permissions deny server access", filename, r);
  383. return FORBIDDEN;
  384. }
  385. /* Apache 1.2 has a more complex mechanism for reading POST data */
  386. #if MODULE_MAGIC_NUMBER > 19961007
  387. if ((retval = setup_client_block(r, REQUEST_CHUNKED_ERROR)))
  388. return retval;
  389. #endif
  390. if (php_apache_info.last_modified) {
  391. #if MODULE_MAGIC_NUMBER < 19970912
  392. if ((retval = set_last_modified(r, r->finfo.st_mtime))) {
  393. return retval;
  394. }
  395. #else
  396. update_mtime (r, r->finfo.st_mtime);
  397. set_last_modified(r);
  398. set_etag(r);
  399. #endif
  400. }
  401. /* Assume output will be of the default MIME type. Individual
  402. scripts may change this later in the request. */
  403. r->content_type = php_apache_get_default_mimetype(r SLS_CC);
  404. /* Init timeout */
  405. hard_timeout("send", r);
  406. SG(server_context) = r;
  407. php_save_umask();
  408. V_CHDIR_FILE(filename);
  409. add_common_vars(r);
  410. add_cgi_vars(r);
  411. init_request_info(SLS_C);
  412. apache_php_module_main(r, fd, display_source_mode CLS_CC ELS_CC PLS_CC SLS_CC);
  413. /* Done, restore umask, turn off timeout, close file and return */
  414. php_restore_umask();
  415. kill_timeout(r);
  416. pclosef(r->pool, fd);
  417. return OK;
  418. }
  419. int send_parsed_php(request_rec * r)
  420. {
  421. return send_php(r, 0, NULL);
  422. }
  423. int send_parsed_php_source(request_rec * r)
  424. {
  425. return send_php(r, 1, NULL);
  426. }
  427. static void destroy_per_dir_entry(php_per_dir_entry *per_dir_entry)
  428. {
  429. free(per_dir_entry->key);
  430. free(per_dir_entry->value);
  431. }
  432. static void copy_per_dir_entry(php_per_dir_entry *per_dir_entry)
  433. {
  434. php_per_dir_entry tmp = *per_dir_entry;
  435. per_dir_entry->key = (char *) malloc(tmp.key_length+1);
  436. memcpy(per_dir_entry->key, tmp.key, tmp.key_length);
  437. per_dir_entry->key[per_dir_entry->key_length] = 0;
  438. per_dir_entry->value = (char *) malloc(tmp.value_length+1);
  439. memcpy(per_dir_entry->value, tmp.value, tmp.value_length);
  440. per_dir_entry->value[per_dir_entry->value_length] = 0;
  441. }
  442. static void php_destroy_per_dir_info(HashTable *per_dir_info)
  443. {
  444. zend_hash_destroy(per_dir_info);
  445. free(per_dir_info);
  446. }
  447. static void *php_create_dir(pool *p, char *dummy)
  448. {
  449. HashTable *per_dir_info;
  450. per_dir_info = (HashTable *) malloc(sizeof(HashTable));
  451. zend_hash_init(per_dir_info, 5, NULL, (void (*)(void *)) destroy_per_dir_entry, 1);
  452. register_cleanup(p, (void *) per_dir_info, (void (*)(void *)) php_destroy_per_dir_info, (void (*)(void *)) zend_hash_destroy);
  453. return per_dir_info;
  454. }
  455. static void *php_merge_dir(pool *p, void *basev, void *addv)
  456. {
  457. php_per_dir_entry tmp;
  458. zend_hash_merge((HashTable *) addv, (HashTable *) basev, (void (*)(void *)) copy_per_dir_entry, &tmp, sizeof(php_per_dir_entry), 0);
  459. return addv;
  460. }
  461. CONST_PREFIX char *php_apache_value_handler_ex(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2, int mode)
  462. {
  463. php_per_dir_entry per_dir_entry;
  464. if (!apache_php_initialized) {
  465. apache_php_initialized = 1;
  466. #ifdef ZTS
  467. tsrm_startup(1, 1, 0);
  468. #endif
  469. sapi_startup(&sapi_module);
  470. php_apache_startup(&sapi_module);
  471. }
  472. per_dir_entry.type = mode;
  473. if (strcasecmp(arg2, "none") == 0) {
  474. arg2 = "";
  475. }
  476. per_dir_entry.key_length = strlen(arg1);
  477. per_dir_entry.value_length = strlen(arg2);
  478. per_dir_entry.key = (char *) malloc(per_dir_entry.key_length+1);
  479. memcpy(per_dir_entry.key, arg1, per_dir_entry.key_length);
  480. per_dir_entry.key[per_dir_entry.key_length] = 0;
  481. per_dir_entry.value = (char *) malloc(per_dir_entry.value_length+1);
  482. memcpy(per_dir_entry.value, arg2, per_dir_entry.value_length);
  483. per_dir_entry.value[per_dir_entry.value_length] = 0;
  484. zend_hash_update((HashTable *) conf, per_dir_entry.key, per_dir_entry.key_length, &per_dir_entry, sizeof(php_per_dir_entry), NULL);
  485. return NULL;
  486. }
  487. CONST_PREFIX char *php_apache_value_handler(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2)
  488. {
  489. return php_apache_value_handler_ex(cmd, conf, arg1, arg2, PHP_INI_PERDIR);
  490. }
  491. CONST_PREFIX char *php_apache_admin_value_handler(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2)
  492. {
  493. return php_apache_value_handler_ex(cmd, conf, arg1, arg2, PHP_INI_SYSTEM);
  494. }
  495. CONST_PREFIX char *php_apache_flag_handler_ex(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2, int mode)
  496. {
  497. char bool_val[2];
  498. if (!strcasecmp(arg2, "On")) {
  499. bool_val[0] = '1';
  500. } else {
  501. bool_val[0] = '0';
  502. }
  503. bool_val[1] = 0;
  504. return php_apache_value_handler_ex(cmd, conf, arg1, bool_val, mode);
  505. }
  506. CONST_PREFIX char *php_apache_flag_handler(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2)
  507. {
  508. return php_apache_flag_handler_ex(cmd, conf, arg1, arg2, PHP_INI_PERDIR);
  509. }
  510. CONST_PREFIX char *php_apache_admin_flag_handler(cmd_parms *cmd, HashTable *conf, char *arg1, char *arg2)
  511. {
  512. return php_apache_flag_handler_ex(cmd, conf, arg1, arg2, PHP_INI_SYSTEM);
  513. }
  514. int php_xbithack_handler(request_rec * r)
  515. {
  516. php_apache_info_struct *conf;
  517. conf = (php_apache_info_struct *) get_module_config(r->per_dir_config, &php4_module);
  518. if (!(r->finfo.st_mode & S_IXUSR)) {
  519. r->allowed |= (1 << METHODS) - 1;
  520. return DECLINED;
  521. }
  522. if (conf->xbithack == 0) {
  523. r->allowed |= (1 << METHODS) - 1;
  524. return DECLINED;
  525. }
  526. return send_parsed_php(r);
  527. }
  528. static void apache_php_module_shutdown_wrapper(void)
  529. {
  530. apache_php_initialized = 0;
  531. sapi_module.shutdown(&sapi_module);
  532. #ifdef ZTS
  533. tsrm_shutdown();
  534. #endif
  535. }
  536. void php_init_handler(server_rec *s, pool *p)
  537. {
  538. register_cleanup(p, NULL, (void (*)(void *))apache_php_module_shutdown_wrapper, (void (*)(void *))php_module_shutdown_for_exec);
  539. if (!apache_php_initialized) {
  540. apache_php_initialized = 1;
  541. #ifdef ZTS
  542. tsrm_startup(1, 1, 0);
  543. #endif
  544. sapi_startup(&sapi_module);
  545. php_apache_startup(&sapi_module);
  546. }
  547. #if MODULE_MAGIC_NUMBER >= 19980527
  548. {
  549. PLS_FETCH();
  550. if (PG(expose_php)) {
  551. ap_add_version_component("PHP/" PHP_VERSION);
  552. }
  553. }
  554. #endif
  555. }
  556. #if HAVE_MOD_DAV
  557. extern int phpdav_mkcol_test_handler(request_rec *r);
  558. extern int phpdav_mkcol_create_handler(request_rec *r);
  559. /* conf is being read twice (both here and in send_php()) */
  560. int send_parsed_php_dav_script(request_rec *r)
  561. {
  562. php_apache_info_struct *conf;
  563. conf = (php_apache_info_struct *) get_module_config(r->per_dir_config,
  564. &php4_module);
  565. return send_php(r, 0, 0, conf->dav_script);
  566. }
  567. static int php_type_checker(request_rec *r)
  568. {
  569. php_apache_info_struct *conf;
  570. conf = (php_apache_info_struct *)get_module_config(r->per_dir_config,
  571. &php4_module);
  572. /* If DAV support is enabled, use mod_dav's type checker. */
  573. if (conf->dav_script) {
  574. dav_api_set_request_handler(r, send_parsed_php_dav_script);
  575. dav_api_set_mkcol_handlers(r, phpdav_mkcol_test_handler,
  576. phpdav_mkcol_create_handler);
  577. /* leave the rest of the request to mod_dav */
  578. return dav_api_type_checker(r);
  579. }
  580. return DECLINED;
  581. }
  582. #else /* HAVE_MOD_DAV */
  583. # define php_type_checker NULL
  584. #endif /* HAVE_MOD_DAV */
  585. handler_rec php_handlers[] =
  586. {
  587. {"application/x-httpd-php", send_parsed_php},
  588. {"application/x-httpd-php-source", send_parsed_php_source},
  589. {"text/html", php_xbithack_handler},
  590. {NULL}
  591. };
  592. command_rec php_commands[] =
  593. {
  594. {"php_value", php_apache_value_handler, NULL, OR_OPTIONS, TAKE2, "PHP Value Modifier"},
  595. {"php_flag", php_apache_flag_handler, NULL, OR_OPTIONS, TAKE2, "PHP Flag Modifier"},
  596. {"php_admin_value", php_apache_admin_value_handler, NULL, ACCESS_CONF|RSRC_CONF, TAKE2, "PHP Value Modifier (Admin)"},
  597. {"php_admin_flag", php_apache_admin_flag_handler, NULL, ACCESS_CONF|RSRC_CONF, TAKE2, "PHP Flag Modifier (Admin)"},
  598. {NULL}
  599. };
  600. module MODULE_VAR_EXPORT php4_module =
  601. {
  602. STANDARD_MODULE_STUFF,
  603. php_init_handler, /* initializer */
  604. php_create_dir, /* per-directory config creator */
  605. php_merge_dir, /* dir merger */
  606. NULL, /* per-server config creator */
  607. NULL, /* merge server config */
  608. php_commands, /* command table */
  609. php_handlers, /* handlers */
  610. NULL, /* filename translation */
  611. NULL, /* check_user_id */
  612. NULL, /* check auth */
  613. NULL, /* check access */
  614. php_type_checker, /* type_checker */
  615. NULL, /* fixups */
  616. NULL /* logger */
  617. #if MODULE_MAGIC_NUMBER >= 19970103
  618. ,NULL /* header parser */
  619. #endif
  620. #if MODULE_MAGIC_NUMBER >= 19970719
  621. ,NULL /* child_init */
  622. #endif
  623. #if MODULE_MAGIC_NUMBER >= 19970728
  624. ,NULL /* child_exit */
  625. #endif
  626. #if MODULE_MAGIC_NUMBER >= 19970902
  627. ,NULL /* post read-request */
  628. #endif
  629. };
  630. /*
  631. * Local variables:
  632. * tab-width: 4
  633. * c-basic-offset: 4
  634. * End:
  635. */