<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
	<title>PBDigg升级程序</title>
</head>

<body>
<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);

set_magic_quotes_runtime(0);
define('PBDIGG_ROOT',substr(dirname(__FILE__), 0, -6));
$PHP_SELF = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
$PB_URL = 'http://'.$_SERVER['HTTP_HOST'].dirname($PHP_SELF).'/';
if (file_exists('update.lock'))
{
	exit('PBDigg 升级程序已锁定。如要重新升级，请删除本目录的 update.lock 文件！');
}

$action = $_GET['action'];
$step = $_GET['step'];
$start = (int)$_GET['start'];
$percount = 500;//每次处理会员数

//UCenter 数据库信息
$uc_host = 'localhost';//数据库服务器，一般为localhost
$uc_name = 'ucenter';//数据库名称
$uc_username = 'root';//数据库用户名
$uc_password = '';//数据库密码
$uc_prefix = 'uc_';//表前缀

if(!$action)
{
	echo '本程序用于整合PBDIGG用户信息到 UCenter 用户管理中心。<br /><br />';
	echo '<strong><font color="red">强烈建议您升级之前备份数据库资料!</font></strong><br /><br /><br />';
	echo '<a href="'.$PB_URL.'uc.php?action=upgrade&step=1">点这里开始升级</a>';
}
else
{
	require_once(PBDIGG_ROOT.'data/sql.inc.php');
	require_once(PBDIGG_ROOT.'include/MySQL.class.php');
	$DB = new MySQL($db_host, $db_username, $db_password, $db_name, $db_pconnect);
	$UCDB = new MySQL($uc_host, $uc_username, $uc_password, $uc_name);
	if ($step == '1')
	{
		$maxuid = getmaxuid();
		$DB->db_query("SELECT * FROM {$db_name}.{$db_prefix}members LIMIT $start, $percount");
		if (!$DB->db_num())
		{
			echo '<script>location.href="'.$PB_URL.'uc.php?action=update&step=2";</script>';exit;
		}
		$rs = $DB->getResource();
		while($data = $DB->db_fetch_array())
		{
			$salt = rand(100000, 999999);
			$password = md5($data['password'].$salt);
			$data['username'] = addslashes($data['username']);
			$olduid = $data['uid'];
			$lastuid = $data['uid'] += $maxuid;
			$queryuc = $UCDB->db_fetch_one_array("SELECT COUNT(*) num FROM {$uc_name}.{$uc_prefix}members WHERE username = '".addslashes($data['username'])."'");
			if(!$queryuc['num'])
			{
				$UCDB->db_query("INSERT LOW_PRIORITY INTO {$uc_name}.{$uc_prefix}members SET uid = '".$data['uid']."', username = '".addslashes($data['username'])."', password = '$password', email = '".addslashes($data['email'])."', regip = '".$data['regip']."', regdate = '".$data['regdate']."', salt = '$salt'", 0);
				$UCDB->db_query("INSERT LOW_PRIORITY INTO {$uc_name}.{$uc_prefix}memberfields SET uid = '".$data['uid']."'", 0);
				$DB->db_query("UPDATE {$db_name}.{$db_prefix}memberdata SET ucuid = ".$data['uid']." WHERE uid = $olduid LIMIT 1", 0);
			}
			else
			{
				PWriteFile(PBDIGG_ROOT.'update/uc_rename.txt', $data['username']."\r\n");
			}
			$DB->setResource($rs);
		}
		$UCDB->db_query("ALTER TABLE {$uc_name}.{$uc_prefix}members AUTO_INCREMENT = ".($lastuid + 1));
		echo $start.' - '.$percount.' 会员导入完成，请耐心等待...';
		echo '<script>location.href="'.$PB_URL.'uc.php?action=update&step=1&start='.($start+$percount).'";</script>';exit;
	}
	elseif ($step == '2')
	{
		@fopen('update.lock', 'w');
		@fclose($fp);
		echo '<script>alert("恭喜您，升级成功！");self.close();</script>';
	}
}
function getmaxuid()
{
	global $UCDB, $uc_prefix;
	$data = $UCDB->db_fetch_one_array("SHOW CREATE TABLE {$uc_prefix}members");
	$data = $data['Create Table'];
	if(preg_match('/AUTO_INCREMENT=(\d+?)[\s|$]/i', $data, $a))
	{
		return $a[1] - 1;
	}
	else
	{
		return 0;
	}
}
function PWriteFile($filename, $content, $mode = 'ab')
{
	if (strpos($filename, '..') !== FALSE)
	{
		return FALSE;
	}
	$path = dirname($filename);
	if (!is_dir($path))
	{
		if (!mkdir($path, 0777))
		{
			return FALSE;
		}
	}
	$fp = @ fopen($filename, $mode);
	if ($fp)
	{
		flock($fp, LOCK_EX);
		fwrite($fp, $content);
		fclose($fp);
		@ chmod($filename, 0777);
		return TRUE;
	}
	return FALSE;
}
?>
</body>
</html>