php_embed.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 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/php_standard.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 TSRMLS_DC)
  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. extern EMBED_SAPI_API 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. ZEND_BEGIN_ARG_INFO(arginfo_dl, 0)
  119. ZEND_ARG_INFO(0, extension_filename)
  120. ZEND_END_ARG_INFO()
  121. /* }}} */
  122. static const zend_function_entry additional_functions[] = {
  123. ZEND_FE(dl, arginfo_dl)
  124. {NULL, NULL, NULL}
  125. };
  126. EMBED_SAPI_API int php_embed_init(int argc, char **argv PTSRMLS_DC)
  127. {
  128. zend_llist global_vars;
  129. #ifdef ZTS
  130. void ***tsrm_ls = NULL;
  131. #endif
  132. #ifdef HAVE_SIGNAL_H
  133. #if defined(SIGPIPE) && defined(SIG_IGN)
  134. signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so
  135. that sockets created via fsockopen()
  136. don't kill PHP if the remote site
  137. closes it. in apache|apxs mode apache
  138. does that for us! thies@thieso.net
  139. 20000419 */
  140. #endif
  141. #endif
  142. #ifdef ZTS
  143. tsrm_startup(1, 1, 0, NULL);
  144. tsrm_ls = ts_resource(0);
  145. *ptsrm_ls = tsrm_ls;
  146. #endif
  147. sapi_startup(&php_embed_module);
  148. #ifdef PHP_WIN32
  149. _fmode = _O_BINARY; /*sets default for file streams to binary */
  150. setmode(_fileno(stdin), O_BINARY); /* make the stdio mode be binary */
  151. setmode(_fileno(stdout), O_BINARY); /* make the stdio mode be binary */
  152. setmode(_fileno(stderr), O_BINARY); /* make the stdio mode be binary */
  153. #endif
  154. php_embed_module.ini_entries = malloc(sizeof(HARDCODED_INI));
  155. memcpy(php_embed_module.ini_entries, HARDCODED_INI, sizeof(HARDCODED_INI));
  156. php_embed_module.additional_functions = additional_functions;
  157. if (argv) {
  158. php_embed_module.executable_location = argv[0];
  159. }
  160. if (php_embed_module.startup(&php_embed_module)==FAILURE) {
  161. return FAILURE;
  162. }
  163. zend_llist_init(&global_vars, sizeof(char *), NULL, 0);
  164. /* Set some Embedded PHP defaults */
  165. SG(options) |= SAPI_OPTION_NO_CHDIR;
  166. SG(request_info).argc=argc;
  167. SG(request_info).argv=argv;
  168. if (php_request_startup(TSRMLS_C)==FAILURE) {
  169. php_module_shutdown(TSRMLS_C);
  170. return FAILURE;
  171. }
  172. SG(headers_sent) = 1;
  173. SG(request_info).no_headers = 1;
  174. php_register_variable("PHP_SELF", "-", NULL TSRMLS_CC);
  175. return SUCCESS;
  176. }
  177. EMBED_SAPI_API void php_embed_shutdown(TSRMLS_D)
  178. {
  179. php_request_shutdown((void *) 0);
  180. php_module_shutdown(TSRMLS_C);
  181. sapi_shutdown();
  182. #ifdef ZTS
  183. tsrm_shutdown();
  184. #endif
  185. if (php_embed_module.ini_entries) {
  186. free(php_embed_module.ini_entries);
  187. php_embed_module.ini_entries = NULL;
  188. }
  189. }
  190. /*
  191. * Local variables:
  192. * tab-width: 4
  193. * c-basic-offset: 4
  194. * End:
  195. * vim600: sw=4 ts=4 fdm=marker
  196. * vim<600: sw=4 ts=4
  197. */