2002-09-29 10:45:25 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2004-01-08 16:18:22 +08:00
|
|
|
| PHP Version 5 |
|
2002-09-29 10:45:25 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2015-01-15 23:26:37 +08:00
|
|
|
| Copyright (c) 1997-2015 The PHP Group |
|
2002-09-29 10:45:25 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2006-01-01 20:51:34 +08:00
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
2002-09-29 10:45:25 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-11 04:04:29 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
2006-01-01 20:51:34 +08:00
|
|
|
| http://www.php.net/license/3_01.txt |
|
2002-09-29 10:45:25 +08:00
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
2002-09-29 18:00:07 +08:00
|
|
|
| Author: Edin Kadribasic <edink@php.net> |
|
2002-09-29 10:45:25 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#include "php_embed.h"
|
2009-01-05 04:18:57 +08:00
|
|
|
#include "ext/standard/php_standard.h"
|
2002-09-29 10:45:25 +08:00
|
|
|
|
|
|
|
#ifdef PHP_WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
2008-03-26 06:05:22 +08:00
|
|
|
const char HARDCODED_INI[] =
|
|
|
|
"html_errors=0\n"
|
|
|
|
"register_argc_argv=1\n"
|
|
|
|
"implicit_flush=1\n"
|
|
|
|
"output_buffering=0\n"
|
|
|
|
"max_execution_time=0\n"
|
|
|
|
"max_input_time=-1\n\0";
|
2007-08-03 07:45:28 +08:00
|
|
|
|
2002-09-29 10:45:25 +08:00
|
|
|
static char* php_embed_read_cookies(TSRMLS_D)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int php_embed_deactivate(TSRMLS_D)
|
|
|
|
{
|
|
|
|
fflush(stdout);
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline size_t php_embed_single_write(const char *str, uint str_length)
|
|
|
|
{
|
|
|
|
#ifdef PHP_WRITE_STDOUT
|
|
|
|
long ret;
|
|
|
|
|
|
|
|
ret = write(STDOUT_FILENO, str, str_length);
|
|
|
|
if (ret <= 0) return 0;
|
|
|
|
return ret;
|
|
|
|
#else
|
|
|
|
size_t ret;
|
|
|
|
|
|
|
|
ret = fwrite(str, 1, MIN(str_length, 16384), stdout);
|
|
|
|
return ret;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int php_embed_ub_write(const char *str, uint str_length TSRMLS_DC)
|
|
|
|
{
|
|
|
|
const char *ptr = str;
|
|
|
|
uint remaining = str_length;
|
|
|
|
size_t ret;
|
|
|
|
|
|
|
|
while (remaining > 0) {
|
|
|
|
ret = php_embed_single_write(ptr, remaining);
|
|
|
|
if (!ret) {
|
|
|
|
php_handle_aborted_connection();
|
|
|
|
}
|
|
|
|
ptr += ret;
|
|
|
|
remaining -= ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return str_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void php_embed_flush(void *server_context)
|
|
|
|
{
|
|
|
|
if (fflush(stdout)==EOF) {
|
|
|
|
php_handle_aborted_connection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void php_embed_send_header(sapi_header_struct *sapi_header, void *server_context TSRMLS_DC)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-08-18 21:34:40 +08:00
|
|
|
static void php_embed_log_message(char *message TSRMLS_DC)
|
2002-09-29 10:45:25 +08:00
|
|
|
{
|
|
|
|
fprintf (stderr, "%s\n", message);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void php_embed_register_variables(zval *track_vars_array TSRMLS_DC)
|
|
|
|
{
|
|
|
|
php_import_environment_variables(track_vars_array TSRMLS_CC);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int php_embed_startup(sapi_module_struct *sapi_module)
|
|
|
|
{
|
|
|
|
if (php_module_startup(sapi_module, NULL, 0)==FAILURE) {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2009-07-31 04:20:56 +08:00
|
|
|
extern EMBED_SAPI_API sapi_module_struct php_embed_module = {
|
2003-01-29 23:41:42 +08:00
|
|
|
"embed", /* name */
|
2002-09-29 10:45:25 +08:00
|
|
|
"PHP Embedded Library", /* pretty name */
|
|
|
|
|
|
|
|
php_embed_startup, /* startup */
|
|
|
|
php_module_shutdown_wrapper, /* shutdown */
|
|
|
|
|
|
|
|
NULL, /* activate */
|
|
|
|
php_embed_deactivate, /* deactivate */
|
|
|
|
|
|
|
|
php_embed_ub_write, /* unbuffered write */
|
|
|
|
php_embed_flush, /* flush */
|
|
|
|
NULL, /* get uid */
|
|
|
|
NULL, /* getenv */
|
|
|
|
|
|
|
|
php_error, /* error handler */
|
|
|
|
|
|
|
|
NULL, /* header handler */
|
|
|
|
NULL, /* send headers handler */
|
|
|
|
php_embed_send_header, /* send header handler */
|
|
|
|
|
|
|
|
NULL, /* read POST data */
|
|
|
|
php_embed_read_cookies, /* read Cookies */
|
|
|
|
|
|
|
|
php_embed_register_variables, /* register server variables */
|
|
|
|
php_embed_log_message, /* Log message */
|
2004-12-21 03:33:41 +08:00
|
|
|
NULL, /* Get request time */
|
2008-03-19 06:23:21 +08:00
|
|
|
NULL, /* Child terminate */
|
2002-09-29 10:45:25 +08:00
|
|
|
|
|
|
|
STANDARD_SAPI_MODULE_PROPERTIES
|
|
|
|
};
|
|
|
|
/* }}} */
|
|
|
|
|
2008-12-31 22:27:09 +08:00
|
|
|
/* {{{ arginfo ext/standard/dl.c */
|
|
|
|
ZEND_BEGIN_ARG_INFO(arginfo_dl, 0)
|
|
|
|
ZEND_ARG_INFO(0, extension_filename)
|
|
|
|
ZEND_END_ARG_INFO()
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
static const zend_function_entry additional_functions[] = {
|
|
|
|
ZEND_FE(dl, arginfo_dl)
|
|
|
|
{NULL, NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2009-07-31 04:20:56 +08:00
|
|
|
EMBED_SAPI_API int php_embed_init(int argc, char **argv PTSRMLS_DC)
|
2002-09-29 10:45:25 +08:00
|
|
|
{
|
|
|
|
zend_llist global_vars;
|
|
|
|
#ifdef ZTS
|
2007-08-09 05:11:53 +08:00
|
|
|
void ***tsrm_ls = NULL;
|
2002-09-29 10:45:25 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SIGNAL_H
|
|
|
|
#if defined(SIGPIPE) && defined(SIG_IGN)
|
|
|
|
signal(SIGPIPE, SIG_IGN); /* ignore SIGPIPE in standalone mode so
|
|
|
|
that sockets created via fsockopen()
|
|
|
|
don't kill PHP if the remote site
|
|
|
|
closes it. in apache|apxs mode apache
|
|
|
|
does that for us! thies@thieso.net
|
|
|
|
20000419 */
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ZTS
|
|
|
|
tsrm_startup(1, 1, 0, NULL);
|
2007-08-09 05:57:30 +08:00
|
|
|
tsrm_ls = ts_resource(0);
|
|
|
|
*ptsrm_ls = tsrm_ls;
|
2002-09-29 10:45:25 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
sapi_startup(&php_embed_module);
|
|
|
|
|
2008-03-26 06:05:22 +08:00
|
|
|
#ifdef PHP_WIN32
|
|
|
|
_fmode = _O_BINARY; /*sets default for file streams to binary */
|
|
|
|
setmode(_fileno(stdin), O_BINARY); /* make the stdio mode be binary */
|
|
|
|
setmode(_fileno(stdout), O_BINARY); /* make the stdio mode be binary */
|
|
|
|
setmode(_fileno(stderr), O_BINARY); /* make the stdio mode be binary */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
php_embed_module.ini_entries = malloc(sizeof(HARDCODED_INI));
|
|
|
|
memcpy(php_embed_module.ini_entries, HARDCODED_INI, sizeof(HARDCODED_INI));
|
|
|
|
|
2008-12-31 22:27:09 +08:00
|
|
|
php_embed_module.additional_functions = additional_functions;
|
|
|
|
|
2007-08-09 05:06:28 +08:00
|
|
|
if (argv) {
|
|
|
|
php_embed_module.executable_location = argv[0];
|
|
|
|
}
|
|
|
|
|
2003-01-28 21:54:42 +08:00
|
|
|
if (php_embed_module.startup(&php_embed_module)==FAILURE) {
|
2002-09-29 10:45:25 +08:00
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
zend_llist_init(&global_vars, sizeof(char *), NULL, 0);
|
|
|
|
|
|
|
|
/* Set some Embedded PHP defaults */
|
|
|
|
SG(options) |= SAPI_OPTION_NO_CHDIR;
|
|
|
|
SG(request_info).argc=argc;
|
|
|
|
SG(request_info).argv=argv;
|
|
|
|
|
|
|
|
if (php_request_startup(TSRMLS_C)==FAILURE) {
|
|
|
|
php_module_shutdown(TSRMLS_C);
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
SG(headers_sent) = 1;
|
|
|
|
SG(request_info).no_headers = 1;
|
|
|
|
php_register_variable("PHP_SELF", "-", NULL TSRMLS_CC);
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2009-07-31 04:20:56 +08:00
|
|
|
EMBED_SAPI_API void php_embed_shutdown(TSRMLS_D)
|
2002-09-29 10:45:25 +08:00
|
|
|
{
|
|
|
|
php_request_shutdown((void *) 0);
|
|
|
|
php_module_shutdown(TSRMLS_C);
|
2005-01-26 05:33:58 +08:00
|
|
|
sapi_shutdown();
|
2002-09-29 10:45:25 +08:00
|
|
|
#ifdef ZTS
|
|
|
|
tsrm_shutdown();
|
|
|
|
#endif
|
2007-08-03 07:45:28 +08:00
|
|
|
if (php_embed_module.ini_entries) {
|
|
|
|
free(php_embed_module.ini_entries);
|
|
|
|
php_embed_module.ini_entries = NULL;
|
|
|
|
}
|
2002-09-29 10:45:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
|
|
|
* vim600: sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: sw=4 ts=4
|
|
|
|
*/
|