<?php
if(!@include('./includes/common.inc.php')) {
	exit('请将本文件移到程序根目录再运行!');
}
require_once "./includes/cache.func.php";
function splitsql($sql){
	$sql = str_replace("\r", "\n", $sql);
	$ret = array();
	$num = 0;
	$queriesarray = explode(";", trim($sql));
	unset($sql);
	foreach($queriesarray as $query) {
		$queries = explode("\n", trim($query));
		foreach($queries as $query) {
			$ret[$num] .= $query[0] == "#" ? NULL : $query;
		}
		$num++;
	}
	return($ret);
}
if($system_version['homesite'] <> 'pic.71116.com'){
	exit('非官方下载的原版程序和修改过的程序不允许进行升级!');
}
if($system_version['version'] < '1.25'){
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>美女图片 Cms 升级程序 v1.24 to v1.25</title>
<style type="text/css">
<!--
body,td {font-family: Geneva, Arial, Helvetica, sans-serif, Tahoma;font-size: 12px;color:#1876b7;}
.login {font-family: Geneva, Arial, Helvetica, sans-serif, Tahoma;font-size: 12px;height: 16px;border-top-width: 1px;border-right-width: 1px;border-bottom-width: 1px;border-left-width: 1px;border-top-color: #FFFFFF;border-right-color: #EEEEEE;border-bottom-color: #EEEEEE;border-left-color: #FFFFFF;background-color: #f2f2f2;}
input,textarea {font-family: Geneva, Arial, Helvetica, sans-serif, Tahoma;font-size: 12px;border-top-width: 1px;border-right-width: 1px;border-bottom-width: 1px;border-left-width: 1px;border-top-color: #FFFFFF;border-right-color: #EEEEEE;border-bottom-color: #EEEEEE;border-left-color: #FFFFFF;background-color: #f2f2f2;}
a {color: #1876b7;text-decoration: none;}
a:hover {color: #000000;text-decoration: underline;}
.smalltxt{ font: 11px Arial, Tahoma; }
Input,select,textarea{ font: 11px Tahoma, Verdana; color: #000000; font-weight: normal; background-color: #F8F8F8; }
form{ margin: 0; padding: 0;}
select{ font: 11px Arial, Tahoma; color: #000000; font-weight: normal; background-color: #F8F8F8; }
.nav{ font: 12px Tahoma, Verdana; color: #000000; font-weight: bold; }
.nav a{ color: #000000;}
.header{ font: 12px Tahoma, Verdana; color: #1876B7; font-weight: bold;background-color: #DAEEFA;background-image:url(images/header.gif);}
.header a{ color: #1876b7; }
.category{ font: 11px Arial, Tahoma; color: #000000; background-color: #EEF6F9; }
.tableborder{ background: #DCEEF6; border: 1px solid #98D5EC }
 .smalltxt{ font: 11px Arial, Tahoma; }
.bold{ font-weight: bold; }
.altbg1{ background: #F8F8F8; }
.altbg2{ background: #FFFFFF; }
.maintable{ width: 98%; background-color: #FFFFFF; }
.menu {color:#FFFFFF;}
.menu a{color:#FFFFFF;font-weight:bold;}
.menu a:hover{color:#CCFFFF;}
-->
</style>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table border="0" cellspacing="1" cellpadding="3" width="680" align="center" class="tableborder">
<tr><td class="header">
<center>
美女图片 Cms 升级程序 v1.24 to v1.25
</center>
</td>
</tr>
<? 
	if(empty($step)) {
?>
<tr><td bgcolor="#ffffff">
<center>
<br>
<br>
你是否现在开始进行系统升级?
<input type=button value="是" class="button" onclick="location.href='updatev125.php?step=update';">
<input type=button value="否" class="button" onclick="location.href='updatev125.php?step=update_exit';">&emsp;&emsp;<BR>
<br><br>&nbsp;&nbsp;<font color=red><b>注意事项:</b></font>安装后请务必删除“updatev125.php”!
</center>
</td>
</tr>
</table>
<?
	} elseif($step=='update') {
?>
<tr><td bgcolor="#ffffff" align="left">
<form name="form1" method="post" action="updatev125.php">
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;升级注意事项:<br><br>
<li>升级程序会新建数据表：<?=$tablepre?>tags 在数据表：<?=$tablepre?>pic 新建 ding字段 以防万一请先备份所有数据</li><br><br>
<li>如果数据表名存在时，还要进行安装，则点击强制升级。<font color=red>注：如果升级的数据表名存在的话，请谨慎操作！！！</font></li><br><br>
<li>请选择安装方式：<input class="radio" type="radio" name="step" value="doupdate" checked> 默认升级
<input class="radio" type="radio" name="step" value="coerce"> 强制升级(会删除同名数据表，请谨慎使用该选项)</li><br><br>
<li><font color=red>升级完成后,必定要删除“updatev125.php”</font></li><br><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;点击按钮开始升级&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" value="开始升级" class="button">
</form>
</td></tr></table>
<?
	} elseif($step=='update_exit') {
		header("Location:/");
		mexit();
	} elseif($step=='doupdate' or $step == 'coerce') {
	$tablearr = array($tablepre.'tags');
	$query = $db->query("show tables");
	while($row = $db->fetch_row($query)) {
		if(in_array($row[0], $tablearr)) {
			if($step == "coerce"){
				$db->query("DROP TABLE IF EXISTS `".$row[0]."`", 1);
			} else {
				echo "数据表名存在，不能进行升级，如果还要升级请选择强制升级<br><a href='javascript:history.back()'>[ 点击返回 ]</a>";
				mexit();
			}
		}
	}
	$pluscharset = "gbk";
	$pluscharstr = $pluscharset ? " CHARACTER SET ".$pluscharset : "";
	$query = "
CREATE TABLE {$tablepre}tags (
  id mediumint(8) NOT NULL auto_increment,
  keyword varchar(100) NOT NULL default '',
  tags varchar(100) NOT NULL default '',
  count smallint(3) NOT NULL default '0',
  weekct smallint(3) NOT NULL default '0',
  weekup date NOT NULL default '0000-00-00',
  monthct smallint(3) NOT NULL default '0',
  monthup date NOT NULL default '0000-00-00',
  result smallint(3) NOT NULL default '0',
  sotime date NOT NULL default '0000-00-00',
  PRIMARY KEY  (id)
) TYPE=MyISAM{$pluscharstr};
ALTER TABLE {$tablepre}pic ADD ding mediumint(8) unsigned NOT NULL default '0';
ALTER TABLE {$tablepre}caiji CHANGE z11 z11 VARCHAR( 250 ) $pluscharstr  NOT NULL;
ALTER TABLE {$tablepre}caiji CHANGE z12 z12 VARCHAR( 250 ) $pluscharstr  NOT NULL;
ALTER TABLE {$tablepre}caiji CHANGE z21 z21 VARCHAR( 250 ) $pluscharstr  NOT NULL;
ALTER TABLE {$tablepre}caiji CHANGE z22 z22 VARCHAR( 250 ) $pluscharstr  NOT NULL;
ALTER TABLE {$tablepre}caiji CHANGE z31 z31 VARCHAR( 250 ) $pluscharstr  NOT NULL;
ALTER TABLE {$tablepre}caiji CHANGE z32 z32 VARCHAR( 250 ) $pluscharstr  NOT NULL;
";
$sqlquery = splitsql($query);
foreach($sqlquery as $sql) {
	if(trim($sql) != '') {
		$db->query(stripslashes($sql));
		if($sqlerror = $db->error()) {
			break;
		}
	}
}
?>
<tr><td bgcolor="#ffffff">
<center>
数据库升级完成!<br>
请上传 update 目录内的文件覆盖完成升级！<br>
<br>
<br>
<a href=/admin>点击这里进入后台</a>
<br><br>
</center>
</td></tr>
</table>
<?
	}
} else {
echo "您当前的版本是：V ".$system_version['version']."与升级版本不符合。";
}
	
?>