<?php
/*
秀影4.x升级到6.0类文件
*/

class upgrade{
	/*
	数据库连接
	*/
	private $DB;
	private $DB2;
	public function __construct(){
		$database = $GLOBALS['database'];
		Easy_Db::Connect($database);
		$this->DB = Easy_Db::getInstance();
	}
	public function connect($dbhost, $dbuser, $dbpass, $dbname, $dbcharset='GBK'){
		$connect['dbhost'] = $dbhost;
		$connect['dbuser'] = $dbuser;
		$connect['dbpass'] = $dbpass;
		$connect['dbname'] = $dbname;
		$connect['dbchar'] = $dbcharset;
		$connect['driver'] = 'MYSQL';
		$this->tblpre = $dbname.'.'.$_POST['tblpre'];
		Easy_Db::Connect($connect);
		$this->DB2 = Easy_Db::getInstance();
	}
	public function start(){
		$this->connect($_POST['dbhost'], $_POST['dbuser'], $_POST['dbpass'], $_POST['dbname'], $_POST['charset']);
		$all = get_class_methods(new self());
		$i =0 ;
		foreach ($all as $method){
			$i++;
			if ($i >3){
				$this->$method();
			}
		}
	}
	/*下面是升级4.x数据*/
	public function upClass(){
		$sql = 'SELECT * FROM '.$this->tblpre.'class';
		$data = null;
		echo '正在导入影片分类数据<br />';
		$this->DB->query('truncate table '.VODCMS_CATEGORY);
		foreach ($this->DB2->fetall($sql) as $row){
			$data['id'] = $row['id'];
			$data['category'] = $row['class'];
			$data['parentid'] = $row['classid'];
			$data['locked'] = $row['hidden'];
			$data['lable'] = $row['id'];
			$data['list_tpl'] = 'list.tpl.php';
			$data['movie_tpl '] = 'display.tpl.php';
			$this->DB->INSERT(CATEGORY, $data);
		}
		echo '完成导入影片分类数据<br />';
	}
	public function upAdmin(){
		$sql = 'SELECT * FROM '.$this->tblpre.'admin';
		$data = null;
		echo '正在导入管理员数据<br />';
		$this->DB->query('truncate table '.VODCMS_ADMIN);
		foreach ($this->DB2->fetall($sql) as $row){
			$data['username'] = $row['username'];
			$data['password'] = $row['password'];
			$data['realname'] = $row['realname'];
			$data['group'] = '超级管理员';
			$data['logintime'] = strtotime($row['logintime']);
			$data['lastime'] = strtotime($row['lastime']);
			$data['lastip'] = $row['ip'];
			$data['failed'] = 0;
			$data['locked'] = 0;
		}
		echo '导入管理员数据完成<br />';
	}
	public function upMovie(){
		$data = null;
		$sql = 'SELECT * FROM '.$this->tblpre.'movie';
		echo '正在导入影片数据...<br >';
		$this->DB->query('truncate table '.VODCMS_MOVIE);
		foreach ($this->DB2->fetall($sql) as $row){
			$data['movid'] = $row['id'];
			$data['title'] = $row['title'];
			$data['actor'] = $row['name'];
			$data['director'] = $row['director'];
			$data['pic'] = $row['pic'];
			$data['otherpic'] = $row['smallpic'];
			$data['sid'] = $row['serverip'];
			$data['player'] = 'vodplayer';
			$data['letter'] = $row['letter'];
			$data['content']= $row['content'];
			$data['area'] = $row['area'];
			$data['lookpoint'] = $row['money'];
			$data['downpoint'] = $row['money'];
			$data['lookgid'] = $row['looklevel'];
			$data['downgid'] = $row['downlevel'];
			$data['look'] = $row['look'];
			$data['down'] = $row['down'];
			$data['review'] = $row['review'];
			$data['addtime'] = strtotime($row['addtime']);
			$data['modifytime'] = strtotime($row['addtime']);
			$data['publish'] = $row['publish'];
			$data['click'] = $row['hits'];
			$data['language'] = $row['lang'];
			$data['mkey'] = $row['md5str'];
			$data['urlcount'] = $row['total'];
			$data['author'] = $row['author'];
			$data['md5str'] = substr($row['smv'], 8 ,16);
			$data['category'] = $this->getClassid($row['id'], $row['class']);
			$data['serialise'] = $row['lianzai'];
			$data['locked'] = $row['hidden'];
		}
		unset($data, $row);
		echo '导入影片数据完成<br >';
	}
	public function upUrl(){
		$data = null;
		$sql = 'SELECT * FROM '.$this->tblpre.'url';
		echo '正在导入影片地址数据...<br >';
		$this->DB->query('truncate table '.VODCMS_URL);
		foreach ($this->DB2->fetAll($sql) as $row){
			$data['urlid'] = $row['id'];
			$data['url'] = $row['url'];
			$data['movid'] = $row['movid'];
			$data['title'] = $row['title'];
			$data['sort'] = $row['sort'];
			$data['params'] = $row['p2pid'];
			$this->DB->insert(VODCMS_URL, $data);
		}
		echo '导入影片地址数据完成<br >';
		unset($row);
	}
	/*升级影片服务器*/
	public function upServer(){
		$data = null;
		$sql = 'SELECT * FROM '.$this->tblpre.'server';
		echo '正在导入服务器数据...<br >';
		$this->DB->query('truncate table '.VODCMS_SERVER);
		foreach ($this->DB2->fetAll($sql) as $row){
			$data['sid'] = $row['id'];
			$data['name'] = $row['title'];
			$data['helixserver'] = $row['helixurl'];
			$data['ftpserver'] = $row['ftpurl'];
			$data['parentid'] = $row['classid'];
			$data['maxnum'] = $row['maxnum'];
			$data['locked'] = $row['status'];
			$this->DB->insert(VODCMS_SERVER, $data);
			
		}
		echo '导入服务器数据完成<br >';
		unset($row);unset($data);
	}
	/*升级用户资料*/
	public function upmember(){
		$data = null;
		$this->DB->query('truncate table '.VODCMS_MEMBERS);
		$sql = 'SELECT * FROM '.$this->tblpre.'members';
		echo '正在导入会员数据...<br >';
		foreach ($this->DB2->fetAll($sql) as $row){
			$data['uid'] = $row['uid'];
			$data['username'] = $row['username'];
			$data['password'] = $row['password'];
			$data['email'] = $row['email'];
			$data['gender'] = $row['gender'];
			$data['regdate'] = $row['regdata'];
			$data['point'] = $row['money'];
			$data['usergroup'] = $row['gid'];
			$data['lifetime'] = $row['endtime'];
			$data['bindip'] = '';
			$data['usertype'] = $row['usertype'];
			$data['payment'] = $row['buynum'];
			$data['logins'] = $row['logins'];
			$data['lastime'] = $row['logintime'];
			$data['logintime'] = $row['logintime'];
			$data['plays'] = $row['looknum'];
			$data['playtime'] = strtotime($row['looktime']);
			$data['locked'] = 0;
			$this->DB->insert(VODCMS_MEMBERS, $data);
		}
		echo '导入会员数据完成<br >';
		unset($row);
	}
	public function upUserGroup(){
		$data = null;
		$sql = 'SELECT * FROM '.$this->tblpre.'usergroup';
		echo '正在导入用户组数据...<br >';
		$this->DB->query('truncate table '.VODCMS_USERGROUP);
		foreach ($this->DB2->fetAll($sql) as $row){
			$data['id'] = $row['id'];
			$data['title'] = $row['title'];
			$data['description'] = $row['content'];
			$data['flag'] = $row['id'];
			$data['repeat'] = $row['setcount'];
			$data['limitspeed'] = $row['maxdownspeed'];
			$data['downspeed'] = $row['maxdownspeed'];
			$data['maxip'] = $row['maxip'];
			$data['maxlook'] = $row['maxlook'];
			$data['maxdown'] = $row['maxlook'];
			$data['lockip'] = $row['lockip'];
			$data['limitdata'] = $row['lockmax'];
			$data['maxdata'] = $row['maxsize'];
			$this->DB->insert(VODCMS_USERGROUP, $data);
			
		}
		echo '导入用户组数据完成<br >';
		unset($row);
	}
	public function upNetBar(){
		$data = null;
		$sql = 'SELECT * FROM '.$this->tblpre.'netbar';
		echo '正在导入用户组数据...<br >';
		$this->DB->query('truncate table '.VODCMS_NETBAR);
		foreach ($this->DB2->fetAll($sql) as $row){
			$data['id'] = $row['id'];
			$data['name'] = $row['title'];
			$data['sip'] = sprintf('%u', ip2long($row['sip']));
			$data['eip'] = sprintf('%u', ip2long($row['eip']));
			$data['usergroup'] = 0;
			$data['maxnum'] = $row['maxnum'];
			$data['phone'] = $row['phone'];
			$data['pass'] = $row['password'];
			$data['username'] = $row['username'];
			$data['address'] = $row['address'];
			$data['lifetime'] = $row['endtime'];
			$data['addtime'] = $row['addtime'];
			$data['admin'] = 'admin';
			$data['locked'] = $row['ok'];
			$this->DB->insert(VODCMS_NETBAR, $data);
			
		}unset($data, $row);
		echo '导入用户组数据完成<br >';
	}
	/*电影卡升级*/
	public function upCar(){
		$data = null;
		$sql = 'SELECT * FROM '.$this->tblpre.'car';
		echo '正在导入影卡数据...<br >';
		$this->DB->query('truncate table '.VODCMS_CAR);
		foreach ($this->DB2->fetall($sql) as $row){
			$data['id'] = $row['id'];
			$data['carid'] = $row['carid'];
			$data['carpwd'] = $row['carpwd'];
			$data['usergroup'] = $row['groupid'];
			$data['usertype'] = $row['usertype'];
 			$data['money'] = $row['momey'];
			$data['usercount'] = $row['usercount'];
			$data['lifetime'] = $row['endtime'];
			$data['admin'] = $row['admin'];
			$data['addtime'] = $row['addtime'];
			$data['locked'] = 0;
			$this->DB->insert(VODCMS_CAR, $data);
			unset($data);
		}
		echo '导入影卡数据完成<br >';
	}
	public function getClassId($movid, $category){
		$sql = 'select id FROM '.$this->tblpre.'class WHERE class=\''.$category.'\'';
		$row = $this->DB2->getRow($sql);
		if ($row['id']){
			$this->DB->insert(VODCMS_MCLASSID, array('category'=> $row['id'], 'movid'=> $movid));
			return $row['id'];
		}else{
			return 0;
		}
	}
}?>