windows warnings fixes and formatting for mod_php.c

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@2652 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Michael Jerris 2006-09-12 03:20:56 +00:00
parent 941d97f8a2
commit fb4c59351a

View File

@ -32,7 +32,6 @@
* *
*/ */
#ifndef _REENTRANT #ifndef _REENTRANT
#define _REENTRANT #define _REENTRANT
#endif #endif
@ -41,15 +40,25 @@
#define _GNU_SOURCE #define _GNU_SOURCE
#endif #endif
#include <sapi/embed/php_embed.h> #ifdef _MSC_VER
//disable warnings for malformed header files
#ifdef ZTS #pragma warning(push)
zend_compiler_globals *compiler_globals; #pragma warning(disable:4127 4311 4133 4244 4201)
zend_executor_globals *executor_globals; #define _USE_32BIT_TIME_T 1
php_core_globals *core_globals;
sapi_globals_struct *sapi_globals;
#endif #endif
#include <sapi/embed/php_embed.h>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#ifdef ZTS
zend_compiler_globals *compiler_globals;
zend_executor_globals *executor_globals;
php_core_globals *core_globals;
sapi_globals_struct *sapi_globals;
#endif
#include <switch.h> #include <switch.h>
@ -57,127 +66,124 @@ const char modname[] = "mod_php";
static int sapi_mod_php_ub_write(const char *str, unsigned int str_length TSRMLS_DC) static int sapi_mod_php_ub_write(const char *str, unsigned int str_length TSRMLS_DC)
{ {
char buffer[4096];
FILE *fp = fopen("mod_php.log", "a"); unsigned int i;
fwrite(str, str_length, sizeof(char), fp); int j = 0;
fclose(fp); FILE *fp = fopen("mod_php.log", "a");
fwrite(str, str_length, sizeof(char), fp);
fclose(fp);
char buffer[4096]; for(i = 0; i < str_length; i++) {
int i, j = 0; buffer[j++] = str[i];
for(i = 0; i < str_length; i++) { if(str[i] == 10) { /* new line */
buffer[j++] = str[i]; buffer[j] = 0;
if(str[i] == 10) { /* new line */ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "%s", buffer);
buffer[j] = 0; j = 0;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "%s", buffer); }
j = 0; else if(str[i] == 0) { /* null character */
} switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "%s", buffer);
else if(str[i] == 0) { /* null character */ j = 0;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "%s", buffer); }
j = 0; if(j == 4095) { /* don't overfill buffer */
} buffer[j] = 0;
if(j == 4095) { /* don't overfill buffer */ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "%s", buffer);
buffer[j] = 0; j = 0;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "%s", buffer); }
j = 0; }
} if(j) { /* stuff left over */
} buffer[j] = 0;
if(j) { /* stuff left over */ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "%s", buffer);
buffer[j] = 0; }
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "%s", buffer); return str_length;
}
return str_length;
} }
void mod_php_error_handler(int type, const char *error_filename, const uint error_lineno, const char *format, va_list args) void mod_php_error_handler(int type, const char *error_filename, const uint error_lineno, const char *format, va_list args)
{ {
char *buffer; char *buffer;
int buffer_len; int buffer_len;
TSRMLS_FETCH(); TSRMLS_FETCH();
buffer_len = vspprintf(&buffer, PG(log_errors_max_len), format, args); buffer_len = vspprintf(&buffer, PG(log_errors_max_len), format, args);
if((EG(error_reporting) & type || (type & E_CORE)) && (PG(log_errors) || PG(display_errors))) { if((EG(error_reporting) & type || (type & E_CORE)) && (PG(log_errors) || PG(display_errors))) {
char *error_type_str; char *error_type_str;
switch (type) { switch (type) {
case E_ERROR: case E_ERROR:
case E_CORE_ERROR: case E_CORE_ERROR:
case E_COMPILE_ERROR: case E_COMPILE_ERROR:
case E_USER_ERROR: case E_USER_ERROR:
error_type_str = "Fatal error"; error_type_str = "Fatal error";
break; break;
case E_WARNING: case E_WARNING:
case E_CORE_WARNING: case E_CORE_WARNING:
case E_COMPILE_WARNING: case E_COMPILE_WARNING:
case E_USER_WARNING: case E_USER_WARNING:
error_type_str = "Warning"; error_type_str = "Warning";
break; break;
case E_PARSE: case E_PARSE:
error_type_str = "Parse error"; error_type_str = "Parse error";
break; break;
case E_NOTICE: case E_NOTICE:
case E_USER_NOTICE: case E_USER_NOTICE:
error_type_str = "Notice"; error_type_str = "Notice";
break; break;
default: default:
error_type_str = "Unknown error"; error_type_str = "Unknown error";
break; break;
} }
if(PG(log_errors)) { if(PG(log_errors)) {
char *log_buffer; char *log_buffer;
spprintf(&log_buffer, 0, "PHP %s: %s in %s on line %d", error_type_str, buffer, error_filename, error_lineno); spprintf(&log_buffer, 0, "PHP %s: %s in %s on line %d", error_type_str, buffer, error_filename, error_lineno);
php_log_err(log_buffer TSRMLS_CC); php_log_err(log_buffer TSRMLS_CC);
efree(log_buffer); efree(log_buffer);
} }
if(PG(display_errors)) { if(PG(display_errors)) {
char *prepend_string = INI_STR("error_prepend_string"); char *prepend_string = INI_STR("error_prepend_string");
char *append_string = INI_STR("error_append_string"); char *append_string = INI_STR("error_append_string");
char *error_format = "%s\n%s: %s in %s on line %d\n%s"; char *error_format = "%s\n%s: %s in %s on line %d\n%s";
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, error_format, STR_PRINT(prepend_string), error_type_str, buffer, error_filename, error_lineno, STR_PRINT(append_string)); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, error_format, STR_PRINT(prepend_string), error_type_str, buffer, error_filename, error_lineno, STR_PRINT(append_string));
} }
} }
// Bail out if we can't recover // Bail out if we can't recover
switch(type) { switch(type) {
case E_CORE_ERROR: case E_CORE_ERROR:
case E_ERROR: case E_ERROR:
//case E_PARSE: the parser would return 1 (failure), we can bail out nicely //case E_PARSE: the parser would return 1 (failure), we can bail out nicely
case E_COMPILE_ERROR: case E_COMPILE_ERROR:
case E_USER_ERROR: case E_USER_ERROR:
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "\nPHP: %s exiting\n", error_filename); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "\nPHP: %s exiting\n", error_filename);
EG(exit_status) = 255; EG(exit_status) = 255;
#if MEMORY_LIMIT #if MEMORY_LIMIT
// restore memory limit // restore memory limit
AG(memory_limit) = PG(memory_limit); AG(memory_limit) = PG(memory_limit);
#endif #endif
efree(buffer); efree(buffer);
zend_bailout(); zend_bailout();
return; return;
}
} // Log if necessary
if(PG(track_errors) && EG(active_symbol_table)) {
pval *tmp;
// Log if necessary ALLOC_ZVAL(tmp);
if(PG(track_errors) && EG(active_symbol_table)) { INIT_PZVAL(tmp);
pval *tmp; Z_STRVAL_P(tmp) = (char *) estrndup(buffer, buffer_len);
Z_STRLEN_P(tmp) = buffer_len;
ALLOC_ZVAL(tmp); Z_TYPE_P(tmp) = IS_STRING;
INIT_PZVAL(tmp); zend_hash_update(EG(active_symbol_table), "php_errormsg", sizeof("php_errormsg"), (void **) & tmp, sizeof(pval *), NULL);
Z_STRVAL_P(tmp) = (char *) estrndup(buffer, buffer_len); }
Z_STRLEN_P(tmp) = buffer_len; efree(buffer);
Z_TYPE_P(tmp) = IS_STRING;
zend_hash_update(EG(active_symbol_table), "php_errormsg", sizeof("php_errormsg"), (void **) & tmp, sizeof(pval *), NULL);
}
efree(buffer);
} }
static void mod_php_log_message(char *message) static void mod_php_log_message(char *message)
{ {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "%s\n", message); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "%s\n", message);
} }
typedef void (*sapi_error_function_t)(int type, const char *error_msg, ...); typedef void (*sapi_error_function_t)(int type, const char *error_msg, ...);
@ -186,67 +192,66 @@ typedef void (*sapi_error_function_t)(int type, const char *error_msg, ...);
static void php_function(switch_core_session_t *session, char *data) static void php_function(switch_core_session_t *session, char *data)
{ {
char *uuid = switch_core_session_get_uuid(session); char *uuid = switch_core_session_get_uuid(session);
uint32_t ulen = strlen(uuid); size_t ulen = strlen(uuid);
uint32_t len = strlen((char *) data) + ulen + 2; size_t len = strlen((char *) data) + ulen + 2;
char *mydata = switch_core_session_alloc(session, len); char *mydata = switch_core_session_alloc(session, len);
int argc, retval; int argc, retval;
char *argv[5]; char *argv[5];
char php_code[1024]; char php_code[1024];
void*** tsrm_ls = NULL; void*** tsrm_ls = NULL;
zend_file_handle script;
zval *php_uuid;
snprintf(mydata, len, "%s %s", uuid, data); snprintf(mydata, len, "%s %s", uuid, data);
argc = switch_separate_string(mydata, ' ',argv,(sizeof(argv) / sizeof(argv[0]))); argc = switch_separate_string(mydata, ' ',argv,(sizeof(argv) / sizeof(argv[0])));
sprintf(php_code, "uuid=\"%s\"; include(\"%s\");\n", argv[0], argv[1]);
sprintf(php_code, "uuid=\"%s\"; include(\"%s\");\n", argv[0], argv[1]);
//sprintf(php_code, "include('%s');", argv[1]);
sprintf(php_code, "%s %s", data, uuid); sprintf(php_code, "%s %s", data, uuid);
zend_file_handle script; script.type = ZEND_HANDLE_FP;
script.type = ZEND_HANDLE_FP; script.filename = data;
script.filename = data; script.opened_path = NULL;
script.opened_path = NULL; script.free_filename = 0;
script.free_filename = 0; script.handle.fp = fopen(script.filename, "rb");
script.handle.fp = fopen(script.filename, "rb");
// Initialize PHPs CORE // Initialize PHPs CORE
php_embed_init(argc, argv, &tsrm_ls); php_embed_init(argc, argv, &tsrm_ls);
// Return All of the DEBUG crap to the console and/or a log file // Return All of the DEBUG crap to the console and/or a log file
php_embed_module.ub_write = sapi_mod_php_ub_write; php_embed_module.ub_write = sapi_mod_php_ub_write;
php_embed_module.log_message = mod_php_log_message; php_embed_module.log_message = mod_php_log_message;
php_embed_module.sapi_error = (sapi_error_function_t)mod_php_error_handler; php_embed_module.sapi_error = (sapi_error_function_t)mod_php_error_handler;
// Let the nice people know we are about to start their script // Let the nice people know we are about to start their script
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Starting Script %s\n",data); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Starting Script %s\n",data);
// Force $uuid and $session to exist in PHPs memory space // Force $uuid and $session to exist in PHPs memory space
zval *php_uuid;
MAKE_STD_ZVAL(php_uuid); MAKE_STD_ZVAL(php_uuid);
//MAKE_STD_ZVAL(php_session);
//php_uuid->type = IS_STRING;
//php_uuid->value.str.len = strlen(uuid);
//php_uuid->value.str.val = estrdup(uuid);
ZVAL_STRING(php_uuid, uuid , 1);
//ZVAL_STRING(php_session, session , 1);
ZEND_SET_SYMBOL(&EG(symbol_table), "uuid", php_uuid);
//ZEND_SET_SYMBOL(&EG(active_symbol_table), "session", php_session);
// Force Some INI entries weather the user likes it or not #ifdef _MSC_VER
//zend_alter_ini_entry("register_globals", strlen("register_globals")+1, "1", 1, PHP_INI_SYSTEM, PHP_INI_STAGE_RUNTIME); //disable warnings for malformed macros from header files
#pragma warning(push)
#pragma warning(disable:4127 4267)
#endif
ZVAL_STRING(php_uuid, uuid , 1);
ZEND_SET_SYMBOL(&EG(symbol_table), "uuid", php_uuid);
#ifdef _MSC_VER
#pragma warning(pop)
#endif
// Execute the bloody script // Execute the bloody script
retval = php_execute_script(&script TSRMLS_CC); retval = php_execute_script(&script TSRMLS_CC);
// Clean up after PHP and such // Clean up after PHP and such
php_embed_shutdown(tsrm_ls); php_embed_shutdown(tsrm_ls);
// Return back to the Dialplan // Return back to the Dialplan
// Buh bye now! // Buh bye now!
} }
@ -270,12 +275,10 @@ static switch_loadable_module_interface_t php_module_interface = {
SWITCH_MOD_DECLARE(switch_status_t) switch_module_load(const switch_loadable_module_interface_t **module_interface, char *filename) SWITCH_MOD_DECLARE(switch_status_t) switch_module_load(const switch_loadable_module_interface_t **module_interface, char *filename)
{ {
void*** tsrm_ls = NULL; void*** tsrm_ls = NULL;
/* connect my internal structure to the blank pointer passed to me */ /* connect my internal structure to the blank pointer passed to me */
*module_interface = &php_module_interface; *module_interface = &php_module_interface;
//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Hello World!\n");
#ifdef ZTS #ifdef ZTS
tsrm_startup(1, 1, 0, NULL); tsrm_startup(1, 1, 0, NULL);
@ -286,8 +289,8 @@ void*** tsrm_ls = NULL;
tsrm_ls = ts_resource(0); tsrm_ls = ts_resource(0);
#endif #endif
/* indicate that the module should continue to be loaded */ /* indicate that the module should continue to be loaded */
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
/* /*
@ -297,13 +300,10 @@ void*** tsrm_ls = NULL;
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
//If it exists, this is called in it's own thread when the module-load completes //If it exists, this is called in it's own thread when the module-load completes
SWITCH_MOD_DECLARE(switch_status) switch_module_shutdown(void) SWITCH_MOD_DECLARE(switch_status) switch_module_shutdown(void)
{ {
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
*/ */