⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 php_amule_lib.cpp

📁 电驴的MAC源代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//// This file is part of the aMule Project.// Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )// Copyright (C) 2005-2008 Froenchenko 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 <string> // Do_not_auto_remove (g++-4.0.1)#include <sys/types.h>#include <regex.h>#include "config.h"#include "WebServer.h"#include <ec/cpp/ECSpecialTags.h>#include "php_syntree.h"#include "php_core_lib.h"#include <wx/datetime.h>/* * Built-in php functions. Those are amule-specific funcions, accessing EC and internal * datastructre *  */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;	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);}void php_native_reload_shared_file_cmd(PHP_VALUE_NODE *){	CPhPLibContext::g_curr_context->WebServer()->Send_ReloadSharedFile_Cmd();}/* *  * 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;	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);}/* * 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;	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);}void php_native_kad_disconnect(PHP_VALUE_NODE *){	CECPacket req(EC_OP_KAD_STOP);	CPhPLibContext::g_curr_context->WebServer()->Send_Discard_V2_Request(&req);}/* * 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;	CPhPLibContext::g_curr_context->WebServer()->Send_AddServer_Cmd(wxString(char2unicode(addr)),		wxString::Format(wxT("%d"), port), wxString(char2unicode(name)));}/* * 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);	uint32_t 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;	CPhPLibContext::g_curr_context->WebServer()->Send_Server_Cmd(ip, port, wxString(char2unicode(cmd)));}/* * Query amule status. Return hash containing stats values */void php_get_amule_stats(PHP_VALUE_NODE *result){	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->GetInt();		}				}	// 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)->GetInt();		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)->GetInt();	speed = array_get_by_str_key(result, "speed_limit_up");	value_value_free(&speed->value);	speed->value.type = PHP_VAL_INT;	speed->value.int_val = stats->GetTagByName(EC_TAG_STATS_UL_SPEED_LIMIT)->GetInt();		speed = array_get_by_str_key(result, "speed_limit_down");	value_value_free(&speed->value);	speed->value.type = PHP_VAL_INT;	speed->value.int_val = stats->GetTagByName(EC_TAG_STATS_DL_SPEED_LIMIT)->GetInt();	delete stats;}void php_get_amule_categories(PHP_VALUE_NODE *result){	cast_value_array(result);	CECPacket req(EC_OP_GET_PREFERENCES);	req.AddTag(CECTag(EC_TAG_SELECT_PREFS, (uint32)EC_PREFS_CATEGORIES));	const CECPacket *reply = CPhPLibContext::g_curr_context->WebServer()->webInterface->SendRecvMsg_v2(&req);	if ( !reply ) {		return ;	}	const CECTag *cats_tag = reply->GetTagCount() ? reply->GetTagByIndex(0) : 0;	if ( cats_tag && cats_tag->GetTagCount() ) {		for (int i = 0; i < cats_tag->GetTagCount(); i++) {			const CECTag *tag = cats_tag->GetTagByIndex(i);			const CECTag *categoryTitle = tag->GetTagByName(EC_TAG_CATEGORY_TITLE);			PHP_VAR_NODE *cat = array_get_by_int_key(result, i);			value_value_free(&cat->value);			cat->value.type = PHP_VAL_STRING;			cat->value.str_val = strdup(unicode2UTF8(categoryTitle->GetStringData()));		}	} else {		PHP_VAR_NODE *cat = array_get_by_int_key(result, 0);		value_value_free(&cat->value);		cat->value.type = PHP_VAL_STRING;		cat->value.str_val = strdup("all");	}	delete reply;}typedef struct {	const char *php_name;	ECTagNames tagname;	int opsize;} PHP_2_EC_OPT_DEF;PHP_2_EC_OPT_DEF g_connection_opt_defs[] = {	{ "max_line_up_cap", EC_TAG_CONN_UL_CAP, 4}, { "max_line_down_cap", EC_TAG_CONN_DL_CAP, 4},	{ "max_up_limit", EC_TAG_CONN_MAX_UL, 2}, { "max_down_limit", EC_TAG_CONN_MAX_DL, 2},	{ "slot_alloc", EC_TAG_CONN_SLOT_ALLOCATION, 2},	{ "tcp_port", EC_TAG_CONN_TCP_PORT, 2}, { "udp_port", EC_TAG_CONN_UDP_PORT, 2}, 	{ "udp_dis", EC_TAG_CONN_UDP_DISABLE, 0},	{ "max_file_src", EC_TAG_CONN_MAX_FILE_SOURCES, 2},	{ "max_conn_total", EC_TAG_CONN_MAX_CONN, 2}, 	{ "autoconn_en", EC_TAG_CONN_AUTOCONNECT, 0}, { "reconn_en", EC_TAG_CONN_RECONNECT, 0},	{0, (ECTagNames)0, 0}};PHP_2_EC_OPT_DEF g_file_opt_defs[] = {	{ "ich_en", EC_TAG_FILES_ICH_ENABLED, 0},	{ "aich_trust", EC_TAG_FILES_AICH_TRUST, 0},	{ "new_files_paused", EC_TAG_FILES_NEW_PAUSED, 0},	{ "new_files_auto_dl_prio", EC_TAG_FILES_NEW_AUTO_DL_PRIO, 0},	{ "preview_prio", EC_TAG_FILES_PREVIEW_PRIO, 0},	{ "new_files_auto_ul_prio", EC_TAG_FILES_NEW_AUTO_UL_PRIO, 0},	{ "upload_full_chunks", EC_TAG_FILES_UL_FULL_CHUNKS, 0},	{ "first_last_chunks_prio", EC_TAG_FILES_PREVIEW_PRIO, 0},	{ "start_next_paused", EC_TAG_FILES_START_NEXT_PAUSED, 0},	{ "resume_same_cat", EC_TAG_FILES_RESUME_SAME_CAT, 0},	{ "save_sources", EC_TAG_FILES_SAVE_SOURCES, 0},	{ "extract_metadata", EC_TAG_FILES_EXTRACT_METADATA, 0},	{ "alloc_full", EC_TAG_FILES_ALLOC_FULL_SIZE, 0},	{ "check_free_space", EC_TAG_FILES_CHECK_FREE_SPACE, 0},	{ "min_free_space", EC_TAG_FILES_MIN_FREE_SPACE, 4},	{0, (ECTagNames)0, 0}};PHP_2_EC_OPT_DEF g_webserver_opt_defs[] = {	{ "use_gzip", EC_TAG_WEBSERVER_USEGZIP, 0},	{ "autorefresh_time", EC_TAG_WEBSERVER_REFRESH, 4},	{0, (ECTagNames)0, 0}};PHP_2_EC_OPT_DEF g_coretweak_opt_defs[] = {	{ "max_conn_5sec", EC_TAG_CORETW_MAX_CONN_PER_FIVE, 2},	{0, (ECTagNames)0, 0}};void set_array_int_val(PHP_VALUE_NODE *array, std::string arrkey, int value){	PHP_VAR_NODE *key = array_get_by_str_key(array, arrkey);	PHP_VALUE_NODE intval;	intval.type = PHP_VAL_INT;	intval.int_val = value;	value_value_assign(&key->value, &intval);}void ec_tag_2_php(const CECTag *cattag, PHP_2_EC_OPT_DEF *opts, PHP_VAR_NODE *catvar){	for(PHP_2_EC_OPT_DEF *def = opts; def->php_name; def++) {		int val;		switch(def->opsize) {			case 0: val = cattag->GetTagByName(def->tagname) ? 1 : 0; break;			case 1: val = cattag->GetTagByNameSafe(def->tagname)->GetInt(); break;			case 2: val = cattag->GetTagByNameSafe(def->tagname)->GetInt(); break;			case 4: val = cattag->GetTagByNameSafe(def->tagname)->GetInt(); break;			default: val = -1;

⌨️ 快捷键说明

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