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
6.1 KiB

24 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2004 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.0 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_0.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. static char* php_embed_read_cookies(TSRMLS_D)
  25. {
  26. return NULL;
  27. }
  28. static int php_embed_deactivate(TSRMLS_D)
  29. {
  30. fflush(stdout);
  31. return SUCCESS;
  32. }
  33. static inline size_t php_embed_single_write(const char *str, uint str_length)
  34. {
  35. #ifdef PHP_WRITE_STDOUT
  36. long ret;
  37. ret = write(STDOUT_FILENO, str, str_length);
  38. if (ret <= 0) return 0;
  39. return ret;
  40. #else
  41. size_t ret;
  42. ret = fwrite(str, 1, MIN(str_length, 16384), stdout);
  43. return ret;
  44. #endif
  45. }
  46. static int php_embed_ub_write(const char *str, uint str_length TSRMLS_DC)
  47. {
  48. const char *ptr = str;
  49. uint remaining = str_length;
  50. size_t ret;
  51. while (remaining > 0) {
  52. ret = php_embed_single_write(ptr, remaining);
  53. if (!ret) {
  54. php_handle_aborted_connection();
  55. }
  56. ptr += ret;
  57. remaining -= ret;
  58. }
  59. return str_length;
  60. }
  61. static void php_embed_flush(void *server_context)
  62. {
  63. if (fflush(stdout)==EOF) {
  64. php_handle_aborted_connection();
  65. }
  66. }
  67. static void php_embed_send_header(sapi_header_struct *sapi_header, void *server_context TSRMLS_DC)
  68. {
  69. }
  70. static void php_embed_log_message(char *message)
  71. {
  72. fprintf (stderr, "%s\n", message);
  73. }
  74. static void php_embed_register_variables(zval *track_vars_array TSRMLS_DC)
  75. {
  76. php_import_environment_variables(track_vars_array TSRMLS_CC);
  77. }
  78. static int php_embed_startup(sapi_module_struct *sapi_module)
  79. {
  80. if (php_module_startup(sapi_module, NULL, 0)==FAILURE) {
  81. return FAILURE;
  82. }
  83. return SUCCESS;
  84. }
  85. sapi_module_struct php_embed_module = {
  86. "embed", /* name */
  87. "PHP Embedded Library", /* pretty name */
  88. php_embed_startup, /* startup */
  89. php_module_shutdown_wrapper, /* shutdown */
  90. NULL, /* activate */
  91. php_embed_deactivate, /* deactivate */
  92. php_embed_ub_write, /* unbuffered write */
  93. php_embed_flush, /* flush */
  94. NULL, /* get uid */
  95. NULL, /* getenv */
  96. php_error, /* error handler */
  97. NULL, /* header handler */
  98. NULL, /* send headers handler */
  99. php_embed_send_header, /* send header handler */
  100. NULL, /* read POST data */
  101. php_embed_read_cookies, /* read Cookies */
  102. php_embed_register_variables, /* register server variables */
  103. php_embed_log_message, /* Log message */
  104. NULL, /* Get request time */
  105. STANDARD_SAPI_MODULE_PROPERTIES
  106. };
  107. /* }}} */
  108. int php_embed_init(int argc, char **argv PTSRMLS_DC)
  109. {
  110. zend_llist global_vars;
  111. #ifdef ZTS
  112. zend_compiler_globals *compiler_globals;
  113. zend_executor_globals *executor_globals;
  114. php_core_globals *core_globals;
  115. sapi_globals_struct *sapi_globals;
  116. void ***tsrm_ls;
  117. #endif
  118. #ifdef HAVE_SIGNAL_H
  119. #if defined(SIGPIPE) && defined(SIG_IGN)
  120. signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so
  121. that sockets created via fsockopen()
  122. don't kill PHP if the remote site
  123. closes it. in apache|apxs mode apache
  124. does that for us! thies@thieso.net
  125. 20000419 */
  126. #endif
  127. #endif
  128. #ifdef PHP_WIN32
  129. _fmode = _O_BINARY; /*sets default for file streams to binary */
  130. setmode(_fileno(stdin), O_BINARY); /* make the stdio mode be binary */
  131. setmode(_fileno(stdout), O_BINARY); /* make the stdio mode be binary */
  132. setmode(_fileno(stderr), O_BINARY); /* make the stdio mode be binary */
  133. #endif
  134. #ifdef ZTS
  135. tsrm_startup(1, 1, 0, NULL);
  136. #endif
  137. #ifdef ZTS
  138. compiler_globals = ts_resource(compiler_globals_id);
  139. executor_globals = ts_resource(executor_globals_id);
  140. core_globals = ts_resource(core_globals_id);
  141. sapi_globals = ts_resource(sapi_globals_id);
  142. tsrm_ls = ts_resource(0);
  143. *ptsrm_ls = tsrm_ls;
  144. #endif
  145. sapi_startup(&php_embed_module);
  146. if (php_embed_module.startup(&php_embed_module)==FAILURE) {
  147. return FAILURE;
  148. }
  149. if (argv) {
  150. php_embed_module.executable_location = argv[0];
  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. zend_alter_ini_entry("register_argc_argv", 19, "1", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
  156. zend_alter_ini_entry("html_errors", 12, "0", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
  157. zend_alter_ini_entry("implicit_flush", 15, "1", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
  158. zend_alter_ini_entry("max_execution_time", 19, "0", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
  159. SG(request_info).argc=argc;
  160. SG(request_info).argv=argv;
  161. if (php_request_startup(TSRMLS_C)==FAILURE) {
  162. php_module_shutdown(TSRMLS_C);
  163. return FAILURE;
  164. }
  165. SG(headers_sent) = 1;
  166. SG(request_info).no_headers = 1;
  167. php_register_variable("PHP_SELF", "-", NULL TSRMLS_CC);
  168. return SUCCESS;
  169. }
  170. void php_embed_shutdown(TSRMLS_D)
  171. {
  172. php_request_shutdown((void *) 0);
  173. php_module_shutdown(TSRMLS_C);
  174. sapi_shutdown();
  175. #ifdef ZTS
  176. tsrm_shutdown();
  177. #endif
  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. */