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.

241 lines
6.2 KiB

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