<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
require_once(dirname(__FILE__)."/include/config_base.php");
if($cfg_version!='5_0_1_UTF8')
{
	ShowMsg("本升级程序仅适用于版本号为 5_0_1_UTF8 的程序到 v_5_1_UTF8 的升级！贵站的版本号为：<font color='red'> {$cfg_version} </font>，请在论坛咨询适合使用的升级程序！","javascript:;");
	exit();
}
if(empty($step)) $step = 0;
if($step==2)
{
	$dsql = new DedeSql(false);
	$upSqlpublic = "
	ALTER TABLE `#@__addonsoft` Add `downloads` INT(11) NOT NULL default '0';##
	ALTER TABLE `#@__addonsoft` ADD INDEX ( `downloads` );##
	DROP TABLE IF EXISTS `#@__task`;##
	INSERT INTO `#@__plus`(`plusname`, `menustring`, `writer`, `isshow`, `filelist`) VALUES ('百度新闻', '<m:item name=''百度新闻'' link=''baidunews.php'' rank=''plus_百度新闻'' target=''main'' />', '织梦团队', 1, 'baidunew.php,baidunew.htm');##
  INSERT INTO `#@__plus`(`plusname`, `menustring`, `writer`, `isshow`, `filelist`) VALUES ('互动模块JS调用', '<m:item name=''互动模块JS调用'' link=''plus_js.php'' rank=''plus_互动模块JS调用'' target=''main'' />', '织梦团队', 1, 'plus_js.php');##
  update `#@__sysconfig` set `aid`='2', `group`='8' where varname='cfg_basehost';##
	update `#@__sysconfig` set `aid`='3', `group`='8' where varname='cfg_cmspath';##
	update `#@__sysconfig` set `aid`='5', `group`='8' where varname='cfg_cookie_encode';##
	update `#@__sysconfig` set `aid`='4', `group`='8' where varname='cfg_indexurl';##
	update `#@__sysconfig` set `aid`='1', `group`='8' where varname='cfg_webname';##
	update `#@__sysconfig` set `aid`='8', `group`='8' where varname='cfg_adminemail';##
	update `#@__sysconfig` set `aid`='4', `group`='8' where varname='cfg_indexname';##
	update `#@__sysconfig` set `aid`='9', `group`='5' where varname='cfg_backup_dir';##
	update `#@__sysconfig` set `aid`='13', `group`='5' where varname='cfg_html_editor';##
	update `#@__sysconfig` set `aid`='5', `group`='8' where varname='cfg_arcdir';##
	update `#@__sysconfig` set `aid`='11', `group`='2' where varname='cfg_medias_dir';##
	update `#@__sysconfig` set `aid`='12', `group`='2' where varname='cfg_ddimg_width';##
	update `#@__sysconfig` set `aid`='13', `group`='2' where varname='cfg_ddimg_height';##
	update `#@__sysconfig` set `aid`='14', `group`='2' where varname='cfg_album_width';##
	update `#@__sysconfig` set `aid`='15', `group`='2' where varname='cfg_imgtype';##
	update `#@__sysconfig` set `aid`='16', `group`='2' where varname='cfg_softtype';##
	update `#@__sysconfig` set `aid`='17', `group`='2' where varname='cfg_mediatype';##
	update `#@__sysconfig` set `aid`='9', `group`='5' where varname='cfg_specnote';##
	update `#@__sysconfig` set `aid`='12', `group`='8' where varname='cfg_list_symbol';##
	update `#@__sysconfig` set `aid`='20', `group`='4' where varname='cfg_notallowstr';##
	update `#@__sysconfig` set `aid`='14', `group`='8' where varname='cfg_album_width';##
	update `#@__sysconfig` set `aid`='14', `group`='8' where varname='cfg_album_width';##
	REPLACE INTO `#@__sysconfig` (`aid`, `varname`, `info`, `value`, `type`, `group`) VALUES
	(709, 'cfg_safeurl', '安全的引用地址(外部JS调用时可用到,多个用回车区别)', 'dedecms.com', 'string', 4),
	(5, 'cfg_keywords', 'Meta 关键词', '织梦内容管理系统', 'string', 8),
	(6, 'cfg_description', 'Meta 描述', '织梦内容管理系统是国内知名的用PHP+MySQL开发的CMS系统', 'string', 8),
	(9, 'cfg_beian', '备案号', '', 'string', 8),
	(10, 'cfg_updateperi', '百度新闻xml更新时间 （单位：分钟）', '15', 'string', 5),
	(11, 'cfg_baidunews_limit', '百度新闻xml更新新闻数量 最大100', '100', 'string', 5);
	";
	$upSql40 = "
	##
	CREATE TABLE `#@__task` (
	  `id` smallint(6) NOT NULL default '1',
	  `usermtools` smallint(6) NOT NULL default '1',
	  `rmpwd` varchar(32) NOT NULL,
	  `tasks` varchar(100) NOT NULL,
	  `typeid` int(11) NOT NULL default '0',
	  `startid` int(11) NOT NULL default '0',
	  `endid` int(11) NOT NULL default '0',
	  `nodes` varchar(250) NOT NULL,
	  `dotime` varchar(10) default '00:00:00',
	  `degree` varchar(10) NOT NULL,
	  UNIQUE KEY `id` (`id`)
	) TYPE=MyISAM;
	";
	$upSql41 = "
	##
	CREATE TABLE `#@__task` (
	  `id` smallint(6) NOT NULL default '1',
	  `usermtools` smallint(6) NOT NULL default '1',
	  `rmpwd` varchar(32) NOT NULL,
	  `tasks` varchar(100) NOT NULL,
	  `typeid` int(11) NOT NULL default '0',
	  `startid` int(11) NOT NULL default '0',
	  `endid` int(11) NOT NULL default '0',
	  `nodes` varchar(250) NOT NULL,
	  `dotime` varchar(10) default '00:00:00',
	  `degree` varchar(10) NOT NULL,
	  UNIQUE KEY `id` (`id`)
	) ENGINE=MyISAM DEFAULT CHARSET=#~lang~#;
	";
	$mysql_version = $dsql->GetVersion();
  $mysql_versions = explode(".",trim($mysql_version));
  $mysql_version = $mysql_versions[0].".".$mysql_versions[1];
  if($mysql_version < 4.1) $upsql = $upSqlpublic.$upSql40;
  else $upsql = $upSqlpublic.str_replace('#~lang~#',$cfg_db_language,$upSql41);
  
  $upsqls = explode('##',$upsql);
  $errnum = 0;
  
  foreach($upsqls as $upsql){
  	$upsql = trim($upsql);
  	if(!$dsql->ExecuteNoneQuery($upsql)){
  		 echo "执行 {$upsql} 失败！<hr>\r\n";
  		 $errnum++;
  	}
  }
	$dsql->Close();
	$fp = fopen("include/config_base.php","r");
	$qstr = fread($fp,filesize("include/config_base.php"));
	fclose($fp);
	$qstr = str_replace('5_0_1_UTF8','v_5_1_UTF8',$qstr);
	
	$fp = fopen("include/config_base.php","w");
	$qstr = fwrite($fp,$qstr);
	fclose($fp);
	
	if($errnum==0) ShowMsg("完成升级，请确保 install.php 是否已经删除，如果尚没删除请手动删除！","javascript:;");
	else ShowMsg("完成升级但有 {$errnum} 个SQL出错，请自行分析原因或把错误提交给到官方论坛！","javascript:;");
	@unlink('install.php');
	exit();
}
else{
	ShowMsg("<a href='install.php?step=2'>本程序适用于DedeCms V5.01 UTF8 to 5.1 UTF8升级，<br /><br />如无误请点击进入下一步操作&gt;&gt;</a>","javascript:;");
	exit();
}
?>