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.

227 lines
5.8 KiB

24 years ago
19 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. | Author: Edin Kadribasic <edink@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #include "php_embed.h"
  20. #ifdef PHP_WIN32
  21. #include <io.h>
  22. #include <fcntl.h>
  23. #endif
  24. const char HARDCODED_INI[] =
  25. "html_errors=0\n"
  26. "register_argc_argv=1\n"
  27. "implicit_flush=1\n"
  28. "output_buffering=0\n"
  29. "max_execution_time=0\n"
  30. "max_input_time=-1\n\0";
  31. static char* php_embed_read_cookies(TSRMLS_D)
  32. {
  33. return NULL;
  34. }
  35. static int php_embed_deactivate(TSRMLS_D)
  36. {
  37. fflush(stdout);
  38. return SUCCESS;
  39. }
  40. static inline size_t php_embed_single_write(const char *str, uint str_length)
  41. {
  42. #ifdef PHP_WRITE_STDOUT
  43. long ret;
  44. ret = write(STDOUT_FILENO, str, str_length);
  45. if (ret <= 0) return 0;
  46. return ret;
  47. #else
  48. size_t ret;
  49. ret = fwrite(str, 1, MIN(str_length, 16384), stdout);
  50. return ret;
  51. #endif
  52. }
  53. static int php_embed_ub_write(const char *str, uint str_length TSRMLS_DC)
  54. {
  55. const char *ptr = str;
  56. uint remaining = str_length;
  57. size_t ret;
  58. while (remaining > 0) {
  59. ret = php_embed_single_write(ptr, remaining);
  60. if (!ret) {
  61. php_handle_aborted_connection();
  62. }
  63. ptr += ret;
  64. remaining -= ret;
  65. }
  66. return str_length;
  67. }
  68. static void php_embed_flush(void *server_context)
  69. {
  70. if (fflush(stdout)==EOF) {
  71. php_handle_aborted_connection();
  72. }
  73. }
  74. static void php_embed_send_header(sapi_header_struct *sapi_header, void *server_context TSRMLS_DC)
  75. {
  76. }
  77. static void php_embed_log_message(char *message)
  78. {
  79. fprintf (stderr, "%s\n", message);
  80. }
  81. static void php_embed_register_variables(zval *track_vars_array TSRMLS_DC)
  82. {
  83. php_import_environment_variables(track_vars_array TSRMLS_CC);
  84. }
  85. static int php_embed_startup(sapi_module_struct *sapi_module)
  86. {
  87. if (php_module_startup(sapi_module, NULL, 0)==FAILURE) {
  88. return FAILURE;
  89. }
  90. return SUCCESS;
  91. }
  92. sapi_module_struct php_embed_module = {
  93. "embed", /* name */
  94. "PHP Embedded Library", /* pretty name */
  95. php_embed_startup, /* startup */
  96. php_module_shutdown_wrapper, /* shutdown */
  97. NULL, /* activate */
  98. php_embed_deactivate, /* deactivate */
  99. php_embed_ub_write, /* unbuffered write */
  100. php_embed_flush, /* flush */
  101. NULL, /* get uid */
  102. NULL, /* getenv */
  103. php_error, /* error handler */
  104. NULL, /* header handler */
  105. NULL, /* send headers handler */
  106. php_embed_send_header, /* send header handler */
  107. NULL, /* read POST data */
  108. php_embed_read_cookies, /* read Cookies */
  109. php_embed_register_variables, /* register server variables */
  110. php_embed_log_message, /* Log message */
  111. NULL, /* Get request time */
  112. NULL, /* Child terminate */
  113. STANDARD_SAPI_MODULE_PROPERTIES
  114. };
  115. /* }}} */
  116. int php_embed_init(int argc, char **argv PTSRMLS_DC)
  117. {
  118. zend_llist global_vars;
  119. #ifdef ZTS
  120. void ***tsrm_ls = NULL;
  121. #endif
  122. #ifdef HAVE_SIGNAL_H
  123. #if defined(SIGPIPE) && defined(SIG_IGN)
  124. signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so
  125. that sockets created via fsockopen()
  126. don't kill PHP if the remote site
  127. closes it. in apache|apxs mode apache
  128. does that for us! thies@thieso.net
  129. 20000419 */
  130. #endif
  131. #endif
  132. #ifdef ZTS
  133. tsrm_startup(1, 1, 0, NULL);
  134. tsrm_ls = ts_resource(0);
  135. *ptsrm_ls = tsrm_ls;
  136. #endif
  137. sapi_startup(&php_embed_module);
  138. #ifdef PHP_WIN32
  139. _fmode = _O_BINARY; /*sets default for file streams to binary */
  140. setmode(_fileno(stdin), O_BINARY); /* make the stdio mode be binary */
  141. setmode(_fileno(stdout), O_BINARY); /* make the stdio mode be binary */
  142. setmode(_fileno(stderr), O_BINARY); /* make the stdio mode be binary */
  143. #endif
  144. php_embed_module.ini_entries = malloc(sizeof(HARDCODED_INI));
  145. memcpy(php_embed_module.ini_entries, HARDCODED_INI, sizeof(HARDCODED_INI));
  146. if (argv) {
  147. php_embed_module.executable_location = argv[0];
  148. }
  149. if (php_embed_module.startup(&php_embed_module)==FAILURE) {
  150. return FAILURE;
  151. }
  152. zend_llist_init(&global_vars, sizeof(char *), NULL, 0);
  153. /* Set some Embedded PHP defaults */
  154. SG(options) |= SAPI_OPTION_NO_CHDIR;
  155. SG(request_info).argc=argc;
  156. SG(request_info).argv=argv;
  157. if (php_request_startup(TSRMLS_C)==FAILURE) {
  158. php_module_shutdown(TSRMLS_C);
  159. return FAILURE;
  160. }
  161. SG(headers_sent) = 1;
  162. SG(request_info).no_headers = 1;
  163. php_register_variable("PHP_SELF", "-", NULL TSRMLS_CC);
  164. return SUCCESS;
  165. }
  166. void php_embed_shutdown(TSRMLS_D)
  167. {
  168. php_request_shutdown((void *) 0);
  169. php_module_shutdown(TSRMLS_C);
  170. sapi_shutdown();
  171. #ifdef ZTS
  172. tsrm_shutdown();
  173. #endif
  174. if (php_embed_module.ini_entries) {
  175. free(php_embed_module.ini_entries);
  176. php_embed_module.ini_entries = NULL;
  177. }
  178. }
  179. /*
  180. * Local variables:
  181. * tab-width: 4
  182. * c-basic-offset: 4
  183. * End:
  184. * vim600: sw=4 ts=4 fdm=marker
  185. * vim<600: sw=4 ts=4
  186. */