<?php
defined('IN_HS') or exit('Access Denied');

$importdir = HS_ROOT.'/data/import/';

$submenu = array (
	array($LANG['manage_member_load_setting'], '?mod='.$mod.'&file='.$file.'&action=manage'),
	array($LANG['add_member_load_setting'], '?mod='.$mod.'&file='.$file.'&action=setting'),
);
$menu = adminmenu($LANG['member_data_load_manage'], $submenu);

switch($action)
{
	case 'import':
		include $importdir.$name.'.php';
		$setting = $settings[$name];

      if($setting['timelimit']) set_time_limit($setting['timelimit']);

			if($setting['database'] == 'mssql')
			{
				if(!extension_loaded('mssql')) showmessage($LANG['cannot_load_mssql_externsion']);
				include HS_ROOT.'/include/db_'.$setting['database'].'.class.php';

				$sqldb = new db_mssql;
				$sqldb->connect($setting['dbhost'], $setting['dbuser'], $setting['dbpw'], $setting['dbname']);
			}
			else
			{
				$sqldb = new db_mysql;
				$sqldb->connect($setting['dbhost'], $setting['dbuser'], $setting['dbpw'], $setting['dbname']);

			}


		$table = trim($setting['table']);
		$actserv = trim($setting['actserv']);

		$offset = isset($offset) ? $offset : 0;
		$number = $setting['number'];
    $limit = '';
		if($number) $limit = " LIMIT $offset,$number";

		$maxid = intval($setting['maxid']);

		if($offset == 0)
	  {
			if($sameserver) $sqldb->select_db($setting['dbname']);
			$r = $sqldb->get_one("SELECT count(*) AS total FROM $table");
			$total = $r['total'];
		}

		$result = $sqldb->query("SELECT * FROM $table ORDER BY idx ASC $limit");
		$importnum = $sqldb->num_rows($result);
		while($r = $sqldb->fetch_array($result))
		{
			$maxid = max($maxid, $r['idx']);
			extract($r);

			$memberexists = $db->get_one("SELECT userid FROM ".TABLE_MEMBER." WHERE username='$username'");
			if($memberexists) continue;
      $newmoney = $webmoney/10;
			$db->query("INSERT INTO ".TABLE_MEMBER." (username,password,email,groupid,money) VALUES('$username','$password','$email',6,'$newmoney')");
			$userid = $db->insert_id();
			$db->query("INSERT INTO ".TABLE_MEMBER_INFO." (userid,area) VALUES('$userid','$actserv')");
			$db->query("INSERT INTO bbs_members (uid,username,password,email,groupid) VALUES('$userid','$username','$password','$email',10)");
			$db->query("INSERT INTO bbs_memberfields (uid,avatar,avatarwidth,avatarheight) VALUES('$userid','images/avatars/noavatar.gif',100,100)");
			
		}
		$sqldb->free_result($result);
		$sqldb->close();

 		$name = $setting['name'];

    $finished = 0;
		if($number && ($importnum < $number))
	    {
			$finished = 1;
			$setting['maxid'] = $maxid;
			$setting['importtime'] = $PHP_TIME;
			array_save($setting , '$settings[\''.$name.'\']', $importdir.$name.'.php');
		}

		$newoffset = $offset + $number;

		$start = $offset+1;
		$end = $finished ? ($offset + $importnum) : $newoffset;

    $forward = $finished ? "?mod=$mod&file=$file&action=manage" : "?mod=$mod&file=$file&action=$action&name=$name&offset=$newoffset&total=$total";
    showmessage('<font color=red>请不要进行任何操作，等待自动导入完成！</font><br>共需要导入 '.$total.' 条数据。从 '.$start.' 到 '.$end.' 条导入完成。', $forward);

        break;
        
	case 'setting':

		if($dosubmit)
		{
			if(empty($setting['name']) || !preg_match('/^[a-zA-Z][0-9a-zA-Z]*$/i',$setting['name']))
			{
				showmessage($LANG['setting_name_not_null']);
			}
			
			$setting['edittime'] = $PHP_TIME;
			array_save($setting , '$settings[\''.$setting['name'].'\']', $importdir.$setting['name'].'.php');
			
			$forward = "?mod=$mod&file=$file&action=manage&channelid=$channelid";
			showmessage($LANG['data_load_setting_save_success'], $forward);
		}
		else
		{
			include HS_ROOT.'/config.inc.php';

			if(isset($name))
			{
				include $importdir.$name.'.php';
				$setting = $settings[$name];
			}
		$areas = array();
    $as = $db->query("SELECT id,servername FROM ".TABLE_GAMESERVER." ORDER BY id ASC");
    while($r = $db->fetch_array($as))
    {
     $areas[] =$r;
    }
		include admintpl('import_setting');
		}

	break;

	default:
		$files = glob($importdir.'*.php');
	    $settings = array();
		if(is_array($files))
		{
			foreach($files as $f)
			{
				require_once $f;
			}
		}
		include admintpl('import_manage');
}
?>