php_core_lib.cpp

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

CPP
1,791
字号
//// This file is part of the aMule Project.// Copyright (c) 2003-2006 aMule Team ( admin@amule.org / http://www.amule.org )// Copyright (C) 2005-2006Froenchenko Leonid ( lfroen@amule.org )//// Any parts of this program derived from the xMule, lMule or eMule project,// or contributed by third-party developers are copyrighted by their// respective authors.//// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the// GNU General Public License for more details.// // You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA//#include <cstdio>#include <cstring>#include <cassert>#include <cstdarg>#include <list>#include <vector>#include <map>#include <string>#include <algorithm>#ifndef PHP_STANDALONE_EN	#include "WebServer.h"	#include <ec/ECSpecialTags.h>#endif#include "php_syntree.h"#include "php_core_lib.h"/* * Built-in php functions. Those are both library and core internals. *  * I'm not going event to get near to what Zend provide, but * at least base things must be here *//* * Print info about variable: php var_dump() */void php_var_dump(PHP_VALUE_NODE *node, int ident, int ref){	for(int i = 0; i < ident;i++) {		printf("\t");	}	if ( ref ) printf("&");	switch(node->type) {		case PHP_VAL_BOOL: printf("bool(%s)\n", node->int_val ? "true" : "false"); break;		case PHP_VAL_INT: printf("int(%lld)\n", node->int_val); break;		case PHP_VAL_FLOAT: printf("float(%f)\n", node->float_val); break;		case PHP_VAL_STRING: printf("string(%d) \"%s\"\n", strlen(node->str_val), node->str_val); break;		case PHP_VAL_OBJECT: printf("Object(%s)\n", node->obj_val.class_name); break;		case PHP_VAL_ARRAY: {			int arr_size = array_get_size(node);			printf("array(%d) {\n", arr_size);			for(int i = 0; i < arr_size;i++) {				const std::string &curr_key = array_get_ith_key(node, i);				PHP_VAR_NODE *curr_val = array_get_by_str_key(node, curr_key);				printf("\t[%s]=>\n", curr_key.c_str());				php_var_dump(&curr_val->value, ident+1, curr_val->ref_count > 1);			}			for(int i = 0; i < ident;i++) {				printf("\t");			}			printf("}\n");			break;		}		case PHP_VAL_NONE: printf("NULL\n"); break;		case PHP_VAL_VAR_NODE:		case PHP_VAL_INT_DATA: assert(0); break;	}}void php_native_var_dump(PHP_VALUE_NODE *){	PHP_SCOPE_ITEM *si = get_scope_item(g_current_scope, "__param_0");	if ( si ) {		assert((si->type == PHP_SCOPE_VAR)||(si->type == PHP_SCOPE_PARAM));		php_var_dump(&si->var->value, 0, 0);	} else {		php_report_error(PHP_ERROR, "Invalid or missing argument");	}}/* * Sorting stl-way requires operator ">" */class SortElem {	public:		SortElem() {}		SortElem(PHP_VAR_NODE *p) { obj = p; }		PHP_VAR_NODE *obj;		static PHP_SYN_FUNC_DECL_NODE *callback;				friend bool operator<(const SortElem &o1, const SortElem &o2);};PHP_SYN_FUNC_DECL_NODE *SortElem::callback = 0;bool operator<(const SortElem &o1, const SortElem &o2){	PHP_VALUE_NODE result;	value_value_assign(&SortElem::callback->params[0].si_var->var->value, &o1.obj->value);	value_value_assign(&SortElem::callback->params[1].si_var->var->value, &o2.obj->value);		switch_push_scope_table((PHP_SCOPE_TABLE_TYPE *)SortElem::callback->scope);	//	// params passed by-value, all & notations ignored	//	result.type = PHP_VAL_NONE;	php_execute(SortElem::callback->code, &result);	cast_value_dnum(&result);	//	// restore stack, free arg list	//	switch_pop_scope_table(0);	value_value_free(&SortElem::callback->params[0].si_var->var->value);	value_value_free(&SortElem::callback->params[1].si_var->var->value);		return result.int_val;}void php_native_usort(PHP_VALUE_NODE *){	PHP_SCOPE_ITEM *si = get_scope_item(g_current_scope, "__param_0");	if ( !si || (si->var->value.type != PHP_VAL_ARRAY)) {		php_report_error(PHP_ERROR, "Invalid or missing argument (array)");		return;	}	PHP_VAR_NODE *array = si->var;	si = get_scope_item(g_current_scope, "__param_1");	if ( !si || (si->var->value.type != PHP_VAL_STRING)) {		php_report_error(PHP_ERROR, "Invalid or missing argument (func name)");		return;	}	char *cmp_func_name = si->var->value.str_val;	si = get_scope_item(g_global_scope, cmp_func_name);	if ( !si || (si->type != PHP_SCOPE_FUNC)) {		php_report_error(PHP_ERROR, "Compare function [%s] not found", cmp_func_name);		return;	}	PHP_SYN_FUNC_DECL_NODE *func_decl = si->func->func_decl;	//	// usort invalidates keys, and sorts values	//	PHP_ARRAY_TYPE *arr_obj = (PHP_ARRAY_TYPE *)array->value.ptr_val;	//	// create vector of values	//	if ( arr_obj->array.size() == 0 ) {		php_report_error(PHP_WARNING, "Sorting array of size 0");		return;	}	std::list<SortElem> sort_list;	for(PHP_ARRAY_ITER_TYPE i = arr_obj->array.begin(); i != arr_obj->array.end(); i++) {		sort_list.push_back(SortElem(i->second));	}	SortElem::callback = func_decl;	sort_list.sort();	arr_obj->array.clear();	arr_obj->sorted_keys.clear();	unsigned int key = 0;	for(std::list<SortElem>::iterator i = sort_list.begin(); i != sort_list.end(); i++) {		array_add_to_int_key(&array->value, key++, i->obj);	}}void php_native_shared_file_cmd(PHP_VALUE_NODE *){	PHP_SCOPE_ITEM *si = get_scope_item(g_current_scope, "__param_0");	if ( !si || (si->var->value.type != PHP_VAL_STRING)) {		php_report_error(PHP_ERROR, "Invalid or missing argument 1");		return;	}	char *str_hash = si->var->value.str_val;		si = get_scope_item(g_current_scope, "__param_1");	if ( !si || (si->var->value.type != PHP_VAL_STRING)) {		php_report_error(PHP_ERROR, "Invalid or missing argument 2");		return;	}	char *cmd_name = si->var->value.str_val;	si = get_scope_item(g_current_scope, "__param_2");	PHP_VAR_NODE *opt_param = si ? si->var : 0;#ifndef PHP_STANDALONE_EN	if ( !strcmp(cmd_name, "prio") && !opt_param ) {		php_report_error(PHP_ERROR, "Command 'prio' need 3-rd argument");		return;	}	CPhPLibContext::g_curr_context->WebServer()->Send_SharedFile_Cmd(wxString(char2unicode(str_hash)),		wxString(char2unicode(cmd_name)),		opt_param ? opt_param->value.int_val : 0);#else	printf("php_native_shared_file_cmd: hash=%s cmd=%s\n", str_hash, cmd_name);#endif}void php_native_reload_shared_file_cmd(PHP_VALUE_NODE *){#ifndef PHP_STANDALONE_EN	CPhPLibContext::g_curr_context->WebServer()->Send_ReloadSharedFile_Cmd();#else	printf("php_native_reload_shared_file_cmd\n");#endif}/* *  * Usage: php_native_download_file_cmd($file_hash, "command", $optional_arg) *  */void php_native_download_file_cmd(PHP_VALUE_NODE *){	PHP_SCOPE_ITEM *si = get_scope_item(g_current_scope, "__param_0");	if ( !si || (si->var->value.type != PHP_VAL_STRING)) {		php_report_error(PHP_ERROR, "Invalid or missing argument 1");		return;	}	char *str_hash = si->var->value.str_val;		si = get_scope_item(g_current_scope, "__param_1");	if ( !si || (si->var->value.type != PHP_VAL_STRING)) {		php_report_error(PHP_ERROR, "Invalid or missing argument 2");		return;	}	char *cmd_name = si->var->value.str_val;	si = get_scope_item(g_current_scope, "__param_2");	PHP_VAR_NODE *opt_param = si ? si->var : 0;#ifndef PHP_STANDALONE_EN	if ( (!strcmp(cmd_name, "prio") || !strcmp(cmd_name, "setcat")) && !opt_param ) {		php_report_error(PHP_ERROR, "Commands 'prio' and 'setcat' needs 3-rd argument");		return;	}			CPhPLibContext::g_curr_context->WebServer()->Send_DownloadFile_Cmd(wxString(char2unicode(str_hash)),		wxString(char2unicode(cmd_name)),		opt_param ? opt_param->value.int_val : 0);#else	printf("php_native_download_file_cmd: hash=%s cmd=%s\n", str_hash, cmd_name);#endif}/* * Usage amule_kad_connect($bootstrap_ip, $bootstrap_port) */void php_native_kad_connect(PHP_VALUE_NODE *){	PHP_SCOPE_ITEM *si = get_scope_item(g_current_scope, "__param_0");	if ( !si ) {		php_report_error(PHP_ERROR, "Missing or bad argument 1: $bootstrap_ip_addr");		return;	}	cast_value_dnum(&si->var->value);	unsigned int ipaddr = si->var->value.int_val;	si = get_scope_item(g_current_scope, "__param_1");	if ( !si ) {		php_report_error(PHP_ERROR, "Missing or bad argument 2: $bootstrap_ip_port");		return;	}	cast_value_dnum(&si->var->value);	unsigned int ipport = si->var->value.int_val;#ifndef PHP_STANDALONE_EN	CECPacket req(EC_OP_KAD_START);	req.AddTag(CECTag(EC_TAG_SERVER_ADDRESS, EC_IPv4_t(ipaddr, ipport)));	CPhPLibContext::g_curr_context->WebServer()->Send_Discard_V2_Request(&req);#else	printf("php_native_kad_connect: ip=%08x port=%d\n", ipaddr, ipport);#endif}void php_native_kad_disconnect(PHP_VALUE_NODE *){#ifndef PHP_STANDALONE_EN	CECPacket req(EC_OP_KAD_STOP);	CPhPLibContext::g_curr_context->WebServer()->Send_Discard_V2_Request(&req);#else	printf("php_native_kad_disconnect\n");#endif}/* * Usage amule_add_server_cmd($server_addr, $server_port, $server_name); */void php_native_add_server_cmd(PHP_VALUE_NODE *){	PHP_SCOPE_ITEM *si = get_scope_item(g_current_scope, "__param_0");	if ( !si || (si->var->value.type != PHP_VAL_STRING) ) {		php_report_error(PHP_ERROR, "Missing or bad argument 1: $server_addr");		return;	}	char *addr = si->var->value.str_val;		si = get_scope_item(g_current_scope, "__param_1");	if ( !si ) {		php_report_error(PHP_ERROR, "Missing argument 2: $server_port");		return;	}	cast_value_dnum(&si->var->value);	int port = si->var->value.int_val;	si = get_scope_item(g_current_scope, "__param_2");	if ( !si || (si->var->value.type != PHP_VAL_STRING) ) {		php_report_error(PHP_ERROR, "Invalid or missing argument 3: $server_name");		return;	}	char *name = si->var->value.str_val;#ifndef PHP_STANDALONE_EN	CPhPLibContext::g_curr_context->WebServer()->Send_AddServer_Cmd(wxString(char2unicode(addr)),		wxString::Format(_("%d"), port), wxString(char2unicode(name)));#else	printf("php_native_add_server_cmd: addr=%s port=%04d name=%s\n", addr, port, name);#endif}/* * Usage amule_server_cmd($server_ip, $server_port, "command"); */void php_native_server_cmd(PHP_VALUE_NODE *){	PHP_SCOPE_ITEM *si = get_scope_item(g_current_scope, "__param_0");	if ( !si ) {		php_report_error(PHP_ERROR, "Missing argument 1: $server_ip");		return;	}	cast_value_dnum(&si->var->value);	int ip = si->var->value.int_val;		si = get_scope_item(g_current_scope, "__param_1");	if ( !si ) {		php_report_error(PHP_ERROR, "Missing argument 2: $server_port");		return;	}	cast_value_dnum(&si->var->value);	int port = si->var->value.int_val;	si = get_scope_item(g_current_scope, "__param_2");	if ( !si || (si->var->value.type != PHP_VAL_STRING)) {		php_report_error(PHP_ERROR, "Invalid or missing argument 3: $command");		return;	}	char *cmd = si->var->value.str_val;#ifndef PHP_STANDALONE_EN	CPhPLibContext::g_curr_context->WebServer()->Send_Server_Cmd(ip, port, wxString(char2unicode(cmd)));#else	printf("php_native_server_cmd: ip=%08x port=%04d cmd=%s\n", ip, port, cmd);#endif}/* * Query amule status. Return hash containing stats values */void php_get_amule_stats(PHP_VALUE_NODE *result){#ifndef PHP_STANDALONE_EN	CECPacket stat_req(EC_OP_STAT_REQ, EC_DETAIL_FULL);	const CECPacket *stats = CPhPLibContext::g_curr_context->WebServer()->webInterface->SendRecvMsg_v2(&stat_req);	if (!stats) {		return ;	}	CEC_ConnState_Tag *tag = (CEC_ConnState_Tag *)stats->GetTagByName(EC_TAG_CONNSTATE);	if (!tag) {		return ;	}		cast_value_array(result);	PHP_VAR_NODE *id = array_get_by_str_key(result, "id");	cast_value_dnum(&id->value);	id->value.int_val = tag->GetEd2kId();	const CECTag *server = tag->GetTagByName(EC_TAG_SERVER);	if ( server ) {		PHP_VAR_NODE *srv_ip = array_get_by_str_key(result, "serv_addr");		value_value_free(&srv_ip->value);		srv_ip->value.type = PHP_VAL_STRING;		srv_ip->value.str_val =strdup(unicode2UTF8(server->GetIPv4Data().StringIP()));		const CECTag *sname = server->GetTagByName(EC_TAG_SERVER_NAME);		if ( sname ) {			PHP_VAR_NODE *srv_name = array_get_by_str_key(result, "serv_name");			value_value_free(&srv_name->value);			srv_name->value.type = PHP_VAL_STRING;			srv_name->value.str_val = strdup(unicode2UTF8(sname->GetStringData()));		}				const CECTag *susers = server->GetTagByName(EC_TAG_SERVER_USERS);		if ( susers ) {			PHP_VAR_NODE *srv_users = array_get_by_str_key(result, "serv_users");			value_value_free(&srv_users->value);			srv_users->value.type = PHP_VAL_INT;			srv_users->value.int_val = susers->GetInt32Data();		}				}	// kademlia	PHP_VAR_NODE *kad = array_get_by_str_key(result, "kad_connected");	value_value_free(&kad->value);	kad->value.type = PHP_VAL_BOOL;	if ( tag->IsConnectedKademlia() ) {		kad->value.int_val = 1;		PHP_VAR_NODE *kad_fwl = array_get_by_str_key(result, "kad_firewalled");		kad_fwl->value.type = PHP_VAL_BOOL;		kad_fwl->value.int_val = tag->IsKadFirewalled();	} else {		kad->value.int_val = 0;	}	// traffic stats	PHP_VAR_NODE *speed;	speed = array_get_by_str_key(result, "speed_up");	value_value_free(&speed->value);	speed->value.type = PHP_VAL_INT;	speed->value.int_val = stats->GetTagByName(EC_TAG_STATS_UL_SPEED)->GetInt32Data();		speed = array_get_by_str_key(result, "speed_down");	value_value_free(&speed->value);	speed->value.type = PHP_VAL_INT;	speed->value.int_val = stats->GetTagByName(EC_TAG_STATS_DL_SPEED)->GetInt32Data();

⌨️ 快捷键说明

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