<?php
/*升级v2.0版数据库更新文件*/
define('IN_PHPUP',1);

//表前缀
function tname($table)
{
	global $dbname,$table_prefix;
	return '`'.$dbname.'`.`'.$table_prefix.$table.'`';
}
if(is_file("../data/config.inc.php"))
{
	
	include "../data/config.inc.php";

	$link=mysql_connect($dbhost,$dbuser,$dbpw);
	$error='';
	header("Content-type:text/html;charset=".$charset);
	mysql_query('use '.$dbname);
	mysql_query('set names '.$dbcharset);

    //$sql[]= "DROP TABLE  ".tname('adminlog')." , ".tname('appgood')." , ".tname('apption')." , ".tname('comment_group')." ,  ".tname('crons').", ".tname('disgoods')." , ".tname('favlist')." , ".tname('gifts')." , ".tname('guestbook')." , ".tname('invite')." , ".tname('link')." , ".tname('mail')." , ".tname('object')." ,  ".tname('objectmail')." , ".tname('order')." , ".tname('orderinfo')." , ".tname('question')." , ".tname('recharge')."  , ".tname('score')." ,".tname('share')." , ".tname('tongji')." ;";
    $sql[]="DROP TABLE ".tname('tempsite').";";
	$sql[]="CREATE TABLE IF NOT EXISTS ".tname('tempsite')." (`id` int(11) NOT NULL AUTO_INCREMENT,`subject` varchar(100) NOT NULL,`shortsubject` varchar(32) NOT NULL,`siteid` int(5) NOT NULL,`groupsite` varchar(32) NOT NULL,`url` varchar(100) NOT NULL,`thumb` varchar(200) NOT NULL,`oldprice` float NOT NULL,`nowprice` float NOT NULL,`discount` float NOT NULL,`lasttime` int(12) DEFAULT NULL,`starttime` int(11) DEFAULT NULL,`sortorder` int(3) NOT NULL,`cityid` char(32) NOT NULL,`keyword` varchar(50) NOT NULL, `updatetime` int(11) NOT NULL,`cityname` varchar(32) NOT NULL,`citysname` varchar(300) NOT NULL,`recommend` int(1) NOT NULL,`nowpeople` int(10) NOT NULL,`bizgroup` int(11) NOT NULL,`bizname` char(20) NOT NULL,`big_catelist` int(11) NOT NULL,`small_catelist` int(11) NOT NULL,`big_catelist_catename` char(50) NOT NULL,`small_catelist_catename` char(50) NOT NULL,`groupkey` char(32) NOT NULL,PRIMARY KEY (`id`),UNIQUE KEY `groupkey` (`groupkey`)) ENGINE=MyISAM  DEFAULT CHARSET=".$dbcharset." COMMENT='商品表，最主要的' AUTO_INCREMENT=1 ;";

	$sql[]="CREATE TABLE ".tname('bizgroup')." ( `id` int(11) NOT NULL AUTO_INCREMENT,`cityid` varchar(20) NOT NULL,`bizname` varchar(30) NOT NULL,PRIMARY KEY (`id`)) ENGINE=MyISAM  DEFAULT CHARSET=".$dbcharset." AUTO_INCREMENT=1 ;";
	$sql[]="ALTER TABLE ".tname('catelist')." ADD `parentid` INT NOT NULL ;";
	$sql[]="CREATE TABLE ".tname('dolog')." ( `id` int(11) NOT NULL AUTO_INCREMENT, `uid` int(11) NOT NULL,`username` char(32) NOT NULL COMMENT '用户名',`lastdo` char(200) NOT NULL COMMENT '最后操作',`lastip` char(32) NOT NULL COMMENT '操作ip',`lasttime` int(11) NOT NULL COMMENT '最后时间',PRIMARY KEY (`id`)) ENGINE=MyISAM  DEFAULT CHARSET=".$dbcharset." COMMENT='用户操作日志' AUTO_INCREMENT=1 ;";
	
	$sql[]="ALTER TABLE ".tname('groups')." ADD `bizgroup` int(11) NOT NULL ;";
	$sql[]="ALTER TABLE ".tname('groups')." ADD `bizname` varchar(20) NOT NULL ;";
	$sql[]="ALTER TABLE ".tname('groups')." CHANGE `grouptype` `big_catelist` INT( 3 ) NOT NULL;"; 
	$sql[]="ALTER TABLE ".tname('groups')." CHANGE `catename` `big_catelist_catename` VARCHAR( 32 )  NOT NULL;"; 
	$sql[]="ALTER TABLE ".tname('groups')." ADD `small_catelist` int(11) NOT NULL ;";
	$sql[]="ALTER TABLE ".tname('groups')." ADD `small_catelist_catename` varchar(50) NOT NULL;";
	$sql[]="ALTER TABLE ".tname('groups')." ADD `groupkey` CHAR( 32 ) NOT NULL ;";
	$sql[]="ALTER TABLE ".tname('groups')." ADD UNIQUE ( `groupkey` );";
	$sql[]="ALTER TABLE ".tname('groups')." ADD `citysname` VARCHAR( 300 ) NOT NULL AFTER `cityname` ;";

	$sql[]="ALTER TABLE ".tname('site')." DROP `trust` ;"; 
	$sql[]="ALTER TABLE ".tname('site')." ADD `gather_method` CHAR( 10 ) NULL default 0;";
	$sql[]="ALTER TABLE ".tname('site')." ADD `apikey` CHAR( 10 ) NOT NULL AFTER `siteapi` ;";
	$sql[]="ALTER TABLE ".tname('user')." DROP `sitename`, DROP `siteurl`, DROP `siteapi`, DROP `cityid`, DROP `connect_type`, DROP `intro`, DROP `all_point`, DROP `sharetime`, DROP `allowed`, DROP `spike_num`;";
	$sql[]="ALTER TABLE ".tname('user')." CHANGE `sex` `sex` TINYINT( 1 ) NOT NULL DEFAULT '0' ";

	$sql[]="CREATE TABLE ".tname('user_group')." ( `groupid` int(11) NOT NULL AUTO_INCREMENT,`groupname` varchar(20) NOT NULL,`groupkey` varchar(32) NOT NULL,PRIMARY KEY (`groupid`),UNIQUE KEY `groupkey` (`groupkey`)) ENGINE=MyISAM  DEFAULT CHARSET=".$dbcharset." AUTO_INCREMENT=1 ;";
	$sql[]="INSERT INTO ".tname('user_group')." (`groupid`, `groupname`, `groupkey`) VALUES (1, '管理员', 'adminuser'),(2, '商家', 'siteuser'),(3, '普通会员', 'normaluser');";

    $sql[]="CREATE TABLE ".tname('user_g_r')." (`roleid` text NOT NULL,`groupid` varchar(32) NOT NULL,PRIMARY KEY (`groupid`)) ENGINE=MyISAM DEFAULT CHARSET=".$dbcharset.";";
    $sql[]="INSERT INTO ".tname('user_g_r')." (`roleid`, `groupid`) VALUES('goodsmodify,usergroup,usergroupmodify,userrole,userrolemodify,user,roletogroup,address,usermodify,normaluser,cate,catemodify,giftmodify,gift,goods,complete,completemodify,cache,creategoods,auto,automodify,order,ordermodify,ordershow,guestbook,comment,guestbookmodify,chat,chatemodify,smslist,ordershowmodify,buylog,charge,chargemodify,usermoneymodify,moneylog,completelist,admin_ajax,admin_delete,setting,settingdata,deleteImg,delcache,link,article_cate,article,articlemodify,article_catemodify,ad,admodify,createcard,card,cardmodify,linkmodify', 'adminuser');";

	$sql[]="CREATE TABLE ".tname('user_role')." ( `roleid` int(11) NOT NULL AUTO_INCREMENT,  `rolename` varchar(32) NOT NULL, `rolekey` varchar(32) NOT NULL, `roletype` varchar(32) NOT NULL,PRIMARY KEY (`roleid`),UNIQUE KEY `rolekey` (`rolekey`)) ENGINE=MyISAM  DEFAULT CHARSET=".$dbcharset." AUTO_INCREMENT=1 ;";
	$sql[]="INSERT INTO ".tname('user_role')." (`roleid`, `rolename`, `rolekey`, `roletype`) VALUES(1, '添加团购', 'groupsmodify', '项目管理'),(2, '用户组管理', 'usergroup', '会员管理'),(3, '添加用户组', 'usergroupmodify', '会员管理'),(4, '用户角色管理', 'userrole', '会员管理'),(5, '添加用户角色', 'userrolemodify', '会员管理'),(6, '会员管理', 'user', '会员管理'),(7, '分配权限', 'roletogroup', '会员管理'),(8, '产品分类', 'cate', '产品管理'),(9, '添加分类', 'catemodify', '产品管理'),(10, '', '', '');";

	$sql[]="CREATE TABLE ".tname('othersite')." ( `id` int(11) NOT NULL AUTO_INCREMENT,`sitename` varchar(50) NOT NULL,`url` varchar(100) NOT NULL,`img` varchar(100) NOT NULL,`cate` int(11) NOT NULL,`istop` int(11) NOT NULL,`recommend` int(11) NOT NULL,`color` varchar(20) NOT NULL,`font` varchar(20) NOT NULL,`sort` int(11) NOT NULL DEFAULT '0',PRIMARY KEY (`id`)) ENGINE=MyISAM  DEFAULT CHARSET=".$dbcharset." AUTO_INCREMENT=1 ;";
	$sql[]="CREATE TABLE ".tname('link')." (`id` int(11) NOT NULL AUTO_INCREMENT,`title` varchar(32) NOT NULL,`url` varchar(100) NOT NULL,`decs` varchar(200) NOT NULL,`type` smallint(1) NOT NULL,`thumb` varchar(50) NOT NULL,`sortorder` int(4) NOT NULL,PRIMARY KEY (`id`)) ENGINE=MyISAM  DEFAULT CHARSET=".$dbcharset." AUTO_INCREMENT=1 ;";

	$sql[]="ALTER TABLE ".tname('site')." ADD `breaksuff` VARCHAR( 200 ) NOT NULL ;";
	$sql[]="ALTER TABLE ".tname('site')." ADD `limitcity` VARCHAR( 200 ) NOT NULL ;";
	$sql[]="ALTER TABLE ".tname('site')." ADD `limitnum` TINYINT( 5 ) NOT NULL ;";
	$sql[]="ALTER TABLE ".tname('site')." ADD `localapi` VARCHAR( 200 ) NULL AFTER `siteapi` ;";
	$sql[]='ALTER TABLE '.tname('groups').' ADD `temporder` INT NOT NULL;';
	$sql[]='ALTER TABLE '.tname('site').' CHANGE `level` `level` SMALLINT( 5 ) NOT NULL;';
	$sql[]='ALTER TABLE '.tname('othersite').' CHANGE `url` `url` VARCHAR( 200 );';
	$sql[]='CREATE TABLE IF NOT EXISTS '.tname('plugins').' (  `id` tinyint(4) NOT NULL AUTO_INCREMENT,  `pluginsname` varchar(300) NOT NULL,  `isok` tinyint(1) NOT NULL,  PRIMARY KEY (`id`)) ENGINE=MyISAM  DEFAULT CHARSET='.$dbcharset;

	foreach($sql as $v)
	{
		if(!mysql_query($v))
		{
			
			$error.="<p>".mysql_error()."</p>";
			continue;
		}
	}

	echo '升级完毕';
}
else
{
	echo "<script language='javascript'>alert('安装完成后升级');window.location='../install/index.php';</script>";
}

?>
