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.

225 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. STANDARD_SAPI_MODULE_PROPERTIES
  105. };
  106. /* }}} */
  107. int php_embed_init(int argc, char **argv PTSRMLS_DC)
  108. {
  109. zend_llist global_vars;
  110. #ifdef ZTS
  111. zend_compiler_globals *compiler_globals;
  112. zend_executor_globals *executor_globals;
  113. php_core_globals *core_globals;
  114. sapi_globals_struct *sapi_globals;
  115. void ***tsrm_ls;
  116. #endif
  117. #ifdef HAVE_SIGNAL_H
  118. #if defined(SIGPIPE) && defined(SIG_IGN)
  119. signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so
  120. that sockets created via fsockopen()
  121. don't kill PHP if the remote site
  122. closes it. in apache|apxs mode apache
  123. does that for us! thies@thieso.net
  124. 20000419 */
  125. #endif
  126. #endif
  127. #ifdef PHP_WIN32
  128. _fmode = _O_BINARY; /*sets default for file streams to binary */
  129. setmode(_fileno(stdin), O_BINARY); /* make the stdio mode be binary */
  130. setmode(_fileno(stdout), O_BINARY); /* make the stdio mode be binary */
  131. setmode(_fileno(stderr), O_BINARY); /* make the stdio mode be binary */
  132. #endif
  133. #ifdef ZTS
  134. tsrm_startup(1, 1, 0, NULL);
  135. #endif
  136. #ifdef ZTS
  137. compiler_globals = ts_resource(compiler_globals_id);
  138. executor_globals = ts_resource(executor_globals_id);
  139. core_globals = ts_resource(core_globals_id);
  140. sapi_globals = ts_resource(sapi_globals_id);
  141. tsrm_ls = ts_resource(0);
  142. *ptsrm_ls = tsrm_ls;
  143. #endif
  144. sapi_startup(&php_embed_module);
  145. if (php_embed_module.startup(&php_embed_module)==FAILURE) {
  146. return FAILURE;
  147. }
  148. if (argv) {
  149. php_embed_module.executable_location = argv[0];
  150. }
  151. zend_llist_init(&global_vars, sizeof(char *), NULL, 0);
  152. /* Set some Embedded PHP defaults */
  153. SG(options) |= SAPI_OPTION_NO_CHDIR;
  154. zend_alter_ini_entry("register_argc_argv", 19, "1", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
  155. zend_alter_ini_entry("html_errors", 12, "0", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
  156. zend_alter_ini_entry("implicit_flush", 15, "1", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
  157. zend_alter_ini_entry("max_execution_time", 19, "0", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_ACTIVATE);
  158. SG(request_info).argc=argc;
  159. SG(request_info).argv=argv;
  160. if (php_request_startup(TSRMLS_C)==FAILURE) {
  161. php_module_shutdown(TSRMLS_C);
  162. return FAILURE;
  163. }
  164. SG(headers_sent) = 1;
  165. SG(request_info).no_headers = 1;
  166. php_register_variable("PHP_SELF", "-", NULL TSRMLS_CC);
  167. return SUCCESS;
  168. }
  169. void php_embed_shutdown(TSRMLS_D)
  170. {
  171. php_request_shutdown((void *) 0);
  172. php_module_shutdown(TSRMLS_C);
  173. #ifdef ZTS
  174. tsrm_shutdown();
  175. #endif
  176. }
  177. /*
  178. * Local variables:
  179. * tab-width: 4
  180. * c-basic-offset: 4
  181. * End:
  182. * vim600: sw=4 ts=4 fdm=marker
  183. * vim<600: sw=4 ts=4
  184. */