php_embed.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 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. #include "php_embed.h"
  19. #include "ext/standard/php_standard.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. #if defined(PHP_WIN32) && defined(ZTS)
  32. ZEND_TSRMLS_CACHE_DEFINE()
  33. #endif
  34. static char* php_embed_read_cookies(void)
  35. {
  36. return NULL;
  37. }
  38. static int php_embed_deactivate(void)
  39. {
  40. fflush(stdout);
  41. return SUCCESS;
  42. }
  43. static inline size_t php_embed_single_write(const char *str, size_t str_length)
  44. {
  45. #ifdef PHP_WRITE_STDOUT
  46. zend_long ret;
  47. ret = write(STDOUT_FILENO, str, str_length);
  48. if (ret <= 0) return 0;
  49. return ret;
  50. #else
  51. size_t ret;
  52. ret = fwrite(str, 1, MIN(str_length, 16384), stdout);
  53. return ret;
  54. #endif
  55. }
  56. static size_t php_embed_ub_write(const char *str, size_t str_length)
  57. {
  58. const char *ptr = str;
  59. size_t remaining = str_length;
  60. size_t ret;
  61. while (remaining > 0) {
  62. ret = php_embed_single_write(ptr, remaining);
  63. if (!ret) {
  64. php_handle_aborted_connection();
  65. }
  66. ptr += ret;
  67. remaining -= ret;
  68. }
  69. return str_length;
  70. }
  71. static void php_embed_flush(void *server_context)
  72. {
  73. if (fflush(stdout)==EOF) {
  74. php_handle_aborted_connection();
  75. }
  76. }
  77. static void php_embed_send_header(sapi_header_struct *sapi_header, void *server_context)
  78. {
  79. }
  80. static void php_embed_log_message(char *message, int syslog_type_int)
  81. {
  82. fprintf (stderr, "%s\n", message);
  83. }
  84. static void php_embed_register_variables(zval *track_vars_array)
  85. {
  86. php_import_environment_variables(track_vars_array);
  87. }
  88. static int php_embed_startup(sapi_module_struct *sapi_module)
  89. {
  90. if (php_module_startup(sapi_module, NULL, 0)==FAILURE) {
  91. return FAILURE;
  92. }
  93. return SUCCESS;
  94. }
  95. EMBED_SAPI_API sapi_module_struct php_embed_module = {
  96. "embed", /* name */
  97. "PHP Embedded Library", /* pretty name */
  98. php_embed_startup, /* startup */
  99. php_module_shutdown_wrapper, /* shutdown */
  100. NULL, /* activate */
  101. php_embed_deactivate, /* deactivate */
  102. php_embed_ub_write, /* unbuffered write */
  103. php_embed_flush, /* flush */
  104. NULL, /* get uid */
  105. NULL, /* getenv */
  106. php_error, /* error handler */
  107. NULL, /* header handler */
  108. NULL, /* send headers handler */
  109. php_embed_send_header, /* send header handler */
  110. NULL, /* read POST data */
  111. php_embed_read_cookies, /* read Cookies */
  112. php_embed_register_variables, /* register server variables */
  113. php_embed_log_message, /* Log message */
  114. NULL, /* Get request time */
  115. NULL, /* Child terminate */
  116. STANDARD_SAPI_MODULE_PROPERTIES
  117. };
  118. /* }}} */
  119. /* {{{ arginfo ext/standard/dl.c */
  120. ZEND_BEGIN_ARG_INFO(arginfo_dl, 0)
  121. ZEND_ARG_INFO(0, extension_filename)
  122. ZEND_END_ARG_INFO()
  123. /* }}} */
  124. static const zend_function_entry additional_functions[] = {
  125. ZEND_FE(dl, arginfo_dl)
  126. {NULL, NULL, NULL}
  127. };
  128. EMBED_SAPI_API int php_embed_init(int argc, char **argv)
  129. {
  130. zend_llist global_vars;
  131. #ifdef HAVE_SIGNAL_H
  132. #if defined(SIGPIPE) && defined(SIG_IGN)
  133. signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so
  134. that sockets created via fsockopen()
  135. don't kill PHP if the remote site
  136. closes it. in apache|apxs mode apache
  137. does that for us! thies@thieso.net
  138. 20000419 */
  139. #endif
  140. #endif
  141. #ifdef ZTS
  142. tsrm_startup(1, 1, 0, NULL);
  143. (void)ts_resource(0);
  144. ZEND_TSRMLS_CACHE_UPDATE();
  145. #endif
  146. zend_signal_startup();
  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()==FAILURE) {
  169. php_module_shutdown();
  170. return FAILURE;
  171. }
  172. SG(headers_sent) = 1;
  173. SG(request_info).no_headers = 1;
  174. php_register_variable("PHP_SELF", "-", NULL);
  175. return SUCCESS;
  176. }
  177. EMBED_SAPI_API void php_embed_shutdown(void)
  178. {
  179. php_request_shutdown((void *) 0);
  180. php_module_shutdown();
  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. */