php_core_lib.cpp

来自「wxWidgets写的电驴」· C++ 代码 · 共 1,791 行 · 第 1/4 页

CPP
1,791
字号
		result->type = PHP_VAL_INT;		result->int_val = obj->nFilePriority;	} else if ( strcmp(prop_name, "prio_auto") == 0 ) {		result->type = PHP_VAL_BOOL;		result->int_val = obj->bFileAutoPriority;	} else {		php_report_error(PHP_ERROR, "'SharedFile' property [%s] is unknown", prop_name);	}}void amule_search_file_prop_get(void *ptr, char *prop_name, PHP_VALUE_NODE *result){	if ( !ptr ) {		value_value_free(result);		return;	}	SearchFile *obj = (SearchFile *)ptr;	if ( strcmp(prop_name, "name") == 0 ) {		result->type = PHP_VAL_STRING;		result->str_val = strdup((const char *)unicode2UTF8(obj->sFileName));	} else if ( strcmp(prop_name, "short_name") == 0 ) {		result->type = PHP_VAL_STRING;		wxString short_name(obj->sFileName.Length() > 60 ? (obj->sFileName.Left(60) + (wxT(" ..."))) : obj->sFileName);		result->str_val = strdup((const char *)unicode2UTF8(short_name));	} else if ( strcmp(prop_name, "hash") == 0 ) {		result->type = PHP_VAL_STRING;		result->str_val = strdup((const char *)unicode2UTF8(obj->sHash));	} else if ( strcmp(prop_name, "size") == 0 ) {		result->type = PHP_VAL_INT;		result->int_val = obj->lFileSize;	} else if ( strcmp(prop_name, "sources") == 0 ) {		result->type = PHP_VAL_INT;		result->int_val = obj->lSourceCount;	} else if ( strcmp(prop_name, "present") == 0 ) {		result->type = PHP_VAL_BOOL;		result->int_val = obj->bPresent;	} else {		php_report_error(PHP_ERROR, "'SearchFile' property [%s] is unknown", prop_name);	}}#elsevoid amule_fake_prop_get(void *obj, char *prop_name, PHP_VALUE_NODE *result){	if ( !strcmp(prop_name, "name") || !strcmp(prop_name, "hash") ) {		result->type = PHP_VAL_STRING;		result->str_val = strdup("some_str");	} else {		result->type = PHP_VAL_INT;		result->int_val = 10;	}}void amule_download_file_prop_get(void *obj, char *prop_name, PHP_VALUE_NODE *result){	amule_fake_prop_get(obj, prop_name, result);}void amule_upload_file_prop_get(void *obj, char *prop_name, PHP_VALUE_NODE *result){	amule_fake_prop_get(obj, prop_name, result);}void amule_server_prop_get(void *obj, char *prop_name, PHP_VALUE_NODE *result){	amule_fake_prop_get(obj, prop_name, result);}void amule_shared_file_prop_get(void *obj, char *prop_name, PHP_VALUE_NODE *result){	amule_fake_prop_get(obj, prop_name, result);}void amule_search_file_prop_get(void *obj, char *prop_name, PHP_VALUE_NODE *result){	amule_fake_prop_get(obj, prop_name, result);}#endifPHP_BLTIN_FUNC_DEF core_lib_funcs[] = {	{		"var_dump", 		1,		php_native_var_dump,	},	{		"strlen",		1, php_native_strlen,	},	{		"count",		1, php_native_count,	},	{		"isset",		1, php_native_isset,	},	{		"usort",		2,		php_native_usort,	},	{		"amule_load_vars",		1, php_native_load_amule_vars,	},	{		"amule_get_stats",		0, php_get_amule_stats,	},	{		"amule_get_categories",		0, php_get_amule_categories,	},	{		"amule_get_options",		0, php_get_amule_options,	},	{		"amule_set_options",		1, php_set_amule_options,	},	{		"amule_do_server_cmd",		3,		php_native_server_cmd,	},	{		"amule_do_add_server_cmd",		3,		php_native_add_server_cmd,	},	{		"amule_do_download_cmd",		3,		php_native_download_file_cmd,	},	{		"amule_do_shared_cmd",		3,		php_native_shared_file_cmd,	},	{		"amule_do_reload_shared_cmd",		0, php_native_reload_shared_file_cmd,	},	{		"amule_do_search_download_cmd",		2,		php_native_search_download_cmd,	},	{		"amule_do_search_start_cmd",		7,		php_native_search_start_cmd,	},	{		"amule_do_ed2k_download_cmd",		2,		php_native_ed2k_download_cmd,	},	{		"amule_get_log",		1, php_get_log,	},	{		"amule_get_serverinfo",		1, php_get_serverinfo,	},	{ 0, 0, 0, },};void php_init_core_lib(){	// load function definitions	PHP_BLTIN_FUNC_DEF *curr_def = core_lib_funcs;	while ( curr_def->name ) {		php_add_native_func(curr_def);		curr_def++;	}	// load object definitions	php_add_native_class("AmuleDownloadFile", amule_download_file_prop_get);	php_add_native_class("AmuleUploadFile", amule_upload_file_prop_get);	php_add_native_class("AmuleServer", amule_server_prop_get);	php_add_native_class("AmuleSharedFile", amule_shared_file_prop_get);	php_add_native_class("AmuleSearchFile", amule_search_file_prop_get);}//// lexer has no include file//extern "C"void php_set_input_buffer(char *buf, int len);CPhPLibContext::CPhPLibContext(CWebServerBase *server, const char *file){	g_curr_context = this;#ifndef PHP_STANDALONE_EN	m_server = server;#endif	php_engine_init();	yyin = fopen(file, "r");	if ( !yyin ) {		return;	}	yyparse();		m_syn_tree_top = g_syn_tree_top;	m_global_scope = g_global_scope;}CPhPLibContext::CPhPLibContext(CWebServerBase *server, char *php_buf, int len){	g_curr_context = this;#ifndef PHP_STANDALONE_EN	m_server = server;#endif	php_engine_init();	m_global_scope = g_global_scope;	php_set_input_buffer(php_buf, len);	yyparse();		m_syn_tree_top = g_syn_tree_top;}CPhPLibContext::~CPhPLibContext(){	SetContext();	php_engine_free();}void CPhPLibContext::SetContext(){	g_syn_tree_top = m_syn_tree_top;	g_global_scope = m_global_scope;}void CPhPLibContext::Execute(CWriteStrBuffer *buf){	m_curr_str_buffer = buf;		PHP_VALUE_NODE val;	php_execute(g_syn_tree_top, &val);}CPhPLibContext *CPhPLibContext::g_curr_context = 0;/* * For simplicity and performance sake, this function can * only handle limited-length printf's. In should be NOT be used * for string concatenation like printf("xyz %s %s", s1, s2). *  * Engine will call Print for "print" and "echo" */void CPhPLibContext::Printf(const char *str, ...){	va_list args;        	va_start(args, str);	if ( !g_curr_context || !g_curr_context->m_curr_str_buffer ) {		vprintf(str, args);	} else {		char buf[4096];		vsnprintf(buf, sizeof(buf), str, args);		g_curr_context->m_curr_str_buffer->Write(buf);	}	va_end(args);}void CPhPLibContext::Print(const char *str){	if ( !g_curr_context || !g_curr_context->m_curr_str_buffer ) {		printf("%s", str);	} else {		g_curr_context->m_curr_str_buffer->Write(str);	}}CPhpFilter::CPhpFilter(CWebServerBase *server, CSession *sess,			const char *file, CWriteStrBuffer *buff){	FILE *f = fopen(file, "r");	if ( !f ) {		return;	}	if ( fseek(f, 0, SEEK_END) != 0 ) {		return;	}	int size = ftell(f);	char *buf = new char [size+1];	rewind(f);	fread(buf, 1, size, f);	buf[size] = 0;	fclose(f);	char *scan_ptr = buf;	char *curr_code_end = buf;	while ( strlen(scan_ptr) ) {		scan_ptr = strstr(scan_ptr, "<?php");		if ( !scan_ptr ) {			buff->Write(curr_code_end);			break;		}		if ( scan_ptr != curr_code_end ) {			buff->Write(curr_code_end, scan_ptr - curr_code_end);		}		curr_code_end = strstr(scan_ptr, "?>");		if ( !curr_code_end ) {			break;		}		curr_code_end += 2; // include "?>" in buffer		int len = curr_code_end - scan_ptr;		yydebug = 0;		CPhPLibContext *context = new CPhPLibContext(server, scan_ptr, len);#ifndef PHP_STANDALONE_EN		load_session_vars("HTTP_GET_VARS", sess->m_get_vars);		load_session_vars("_SESSION", sess->m_vars);#endif		context->Execute(buff);#ifndef PHP_STANDALONE_EN		save_session_vars(sess->m_vars);#endif			delete context;				scan_ptr = curr_code_end;	}#ifndef PHP_STANDALONE_EN	sess->m_get_vars.clear();#endif		delete [] buf;}/* * String buffer: almost same as regular 'string' class, but, * without reallocation when full. Instead, new buffer is * allocated, and added to list */CWriteStrBuffer::CWriteStrBuffer(){	m_alloc_size = 1024;	m_total_length = 0;		AllocBuf();}CWriteStrBuffer::~CWriteStrBuffer(){	for(std::list<char *>::iterator i = m_buf_list.begin(); i != m_buf_list.end(); i++) {		delete [] *i;	}	delete [] m_curr_buf;}void CWriteStrBuffer::AllocBuf(){	m_curr_buf = new char [m_alloc_size];	m_buf_ptr = m_curr_buf;	m_curr_buf_left = m_alloc_size;}void CWriteStrBuffer::Write(const char *s, int len){	if ( len == -1 ) {		len = strlen(s);	}	m_total_length += len;		while ( len ) {		if ( (len + 1) <= m_curr_buf_left ) {			strncpy(m_buf_ptr, s, len);			m_buf_ptr += len;			m_curr_buf_left -= len;			len = 0;		} else {			memcpy(m_buf_ptr, s, m_curr_buf_left);			int rem_len = len - m_curr_buf_left;			s += m_curr_buf_left;									len = rem_len;			m_buf_list.push_back(m_curr_buf);			AllocBuf();		}	}}void CWriteStrBuffer::CopyAll(char *dst_buffer){	char *curr_ptr = dst_buffer;	int rem_size = m_total_length;	for(std::list<char *>::iterator i = m_buf_list.begin(); i != m_buf_list.end(); i++) {		memcpy(curr_ptr, *i, m_alloc_size);		rem_size -= m_alloc_size;		curr_ptr += m_alloc_size;	}	if ( rem_size ) {		memcpy(curr_ptr, m_curr_buf, rem_size);	}	*(curr_ptr + rem_size) = 0;}void load_session_vars(char *target, std::map<std::string, std::string> &varmap){	PHP_EXP_NODE *sess_vars_exp_node = get_var_node(target);	PHP_VAR_NODE *sess_vars = sess_vars_exp_node->var_si_node->var;	// i'm not building exp tree, node not needed	delete sess_vars_exp_node;	cast_value_array(&sess_vars->value);	for(std::map<std::string, std::string>::iterator i = varmap.begin(); i != varmap.end(); i++) {		PHP_VAR_NODE *curr_var = array_get_by_str_key(&sess_vars->value, i->first);		PHP_VALUE_NODE val;		val.type = PHP_VAL_STRING;		val.str_val = (char *)i->second.c_str();		value_value_assign(&curr_var->value, &val);	}}void save_session_vars(std::map<std::string, std::string> &varmap){	PHP_EXP_NODE *sess_vars_exp_node = get_var_node("_SESSION");	PHP_VAR_NODE *sess_vars = sess_vars_exp_node->var_si_node->var;	delete sess_vars_exp_node;	if ( sess_vars->value.type != PHP_VAL_ARRAY ) {		return;	}	for(int i = 0; i < array_get_size(&sess_vars->value); i++) {		std::string s = array_get_ith_key(&sess_vars->value, i);		PHP_VAR_NODE *var = array_get_by_str_key(&sess_vars->value, s);		cast_value_str(&var->value);		varmap[s] = var->value.str_val;	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?