<?php
$version = "3.0";
/*
+--------------------------------------------------------+
| O-BLOG - PHP Blog System                               |
| Copyright (c) 2004 phpBlog.CN                          |
| Support : http://www.phpBlog.cn                        |
| Author : ShiShiRui (shishirui@163.com)                 |
|--------------------------------------------------------+
*/
error_reporting(7);

header("content-Type: text/html; charset=UTF-8");

// Functions & Variables 
$header = "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><title>O-BLOG $version 安装程序</title><style type=\"text/css\"><!--TABLE {font-family: Georgia, \"Times New Roman\", Times, serif;font-size: 14px;color: #000000;}.header {font-family: Georgia, \"Times New Roman\", Times, serif;font-size: 18px;font-weight: bold;color: #FFFFFF;background-color: #B50000;}--></style><style type=\"text/css\"><!--INPUT {font-family: Georgia, \"Times New Roman\", Times, serif;font-size: 14px;width: 260px;background-color: #B50000;color: #FFFFFF;font-weight: bold;height: 28px;}.itemtitle {font-size: 18px;}--></style></head><body bgcolor=\"#F3F3F3\"><form name=\"form1\" method=\"post\" action=\"?do=action\">  <table width=\"650\" border=\"0\" align=\"center\" cellpadding=\"10\" cellspacing=\"1\" bgcolor=\"#CCCCCC\">    <tr valign=\"top\" bgcolor=\"#666666\"><td height=\"80\" colspan=\"3\" bgcolor=\"#F3F3F3\" class=\"header\"><strong>O-BLOG $version 安装程序</strong>&nbsp;</td></tr><tr><td colspan=\"3\" bgcolor=\"#FFFFFF\">";

$footer = "</td></tr></table></form></body></html>";

function set_writeable($file) {
	if(is_writeable($file)) {
		echo"检测文件（夹）$file …… <strong>可写</strong><br>";
	} else {
		echo"检测文件（夹）$file …… <strong>不可写</strong><br>正在改变权限 …… ";
		if(@chmod($file,0777)) {
		echo"<strong>可写</strong><br>";
		} else {
		echo"<strong>失败,请手动更改此文件访问权限！</strong><br>";
		exit;
		}
	}
}

function querySQL()
{
	global $mysql_prefix,$version;
	
	$sql = <<<END

DROP TABLE IF EXISTS `obprefix_admin`;

CREATE TABLE IF NOT EXISTS `obprefix_admin` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `username` varchar(100) NOT NULL,
  `password` varchar(40) NOT NULL,
  `nickname` varchar(100) NOT NULL,
  `email` varchar(100) NOT NULL,
  `homepage` varchar(200) NOT NULL,
  `qq` varchar(20) NOT NULL,
  `msn` varchar(100) NOT NULL,
  `icq` varchar(100) NOT NULL,
  `remark` mediumtext NOT NULL,
  `auth` mediumtext NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

INSERT INTO `obprefix_admin` VALUES ('1', 'longbill', 'ad41c06229e2a3f05cf6096511e69de0', 'longbill', 'longbill.cn@gmail.com', 'http://www.longbill.cn', '', '', '123456789', '哈哈', 'config,doconfig,phpinfo,addBlog,doaddBlog,editBlog,modBlog,domodBlog,delBlog,buildBlog,dosomeBlog,remarkManager,dosomeRemark,delRemark,checkRemark,banRemark,modRemark,domodRemark,addSort,doaddSort,editSort,delSort,modSort,domodSort,dosomeSort,addLink,doaddLink,editLink,delLink,modLink,domodLink,dosomeLink,bak,dobak,import,optimize,repair,dooptimize,dorepair,rssImport,doRssImport,rssImportSort,rssExport,doRssExport,actlog,cleanActlog,userlog,cleanUserlog,password,updatepassword,guestbook,modgb,domodgb,replygb,doreplygb,delgb,dosomeGb,banned,dobanned,addNote,doaddNote,editNote,viewNote,modNote,domodNote,delNote,uploadManager,bakManager,delFile,rebuild,dobuild,upload,doupload,addUser,doaddUser,editUser,modUser,domodUser,delUser,editTrackback,modTrackback,domodTrackback,delTrackback,dosomeTrackback,selectTemplate,editTemplate,saveTemplate,logout,runsql,dorunsql,addAutolink,editAutolink,doaddAutolink,updateAutolink,delAutolink,ads,savead,delad,cleanCache');


DROP TABLE IF EXISTS `obprefix_adminlog`;

CREATE TABLE IF NOT EXISTS `obprefix_adminlog` (
  `id` int(15) unsigned NOT NULL auto_increment,
  `action` varchar(50) NOT NULL,
  `script` varchar(255) NOT NULL,
  `date` varchar(20) NOT NULL,
  `ip` varchar(20) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `obprefix_blog`;

CREATE TABLE IF NOT EXISTS `obprefix_blog` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `date` varchar(20) NOT NULL,
  `title` varchar(250) NOT NULL,
  `subtitle` varchar(250) NOT NULL,
  `content` mediumtext NOT NULL,
  `trackbackurl` varchar(250) NOT NULL,
  `filename` varchar(250) NOT NULL,
  `author` varchar(100) NOT NULL default 'unknow',
  `classid` int(10) unsigned NOT NULL default '1',
  `top` tinyint(3) unsigned NOT NULL default '0',
  `allow_remark` tinyint(3) unsigned NOT NULL default '1',
  `allow_face` tinyint(3) unsigned NOT NULL default '1',
  `draft` tinyint(3) unsigned NOT NULL default '0',
  `viewcount` int(10) unsigned NOT NULL default '1',
  `ishtml` tinyint(3) unsigned NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `id` (`id`),
  KEY `classid` (`classid`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `obprefix_ads`;

CREATE TABLE IF NOT EXISTS  `obprefix_ads` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`tag` VARCHAR( 100 )  NOT NULL ,
`description` VARCHAR( 200 )  NULL ,
`content` TEXT NULL ,
`tag_open` TINYINT NULL default '1',
 PRIMARY KEY (`id` )
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `obprefix_ads` VALUES ('1', 'tongji', '网站统计代码', '','1');
INSERT INTO `obprefix_ads` VALUES ('2', 'beian', '网站备案信息', '暂未备案','1');

DROP TABLE IF EXISTS `obprefix_class`;

CREATE TABLE IF NOT EXISTS `obprefix_class` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `classname` varchar(250) NOT NULL,
  `showorder` int(10) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

INSERT INTO `obprefix_class` VALUES ('1', '默认分类', '1');



DROP TABLE IF EXISTS `obprefix_config`;

CREATE TABLE IF NOT EXISTS `obprefix_config` (
  `blogname` varchar(250) NOT NULL,
  `template` varchar(100) NOT NULL default 'default',
  `blogdescribe` varchar(250) NOT NULL,
  `blogurl` varchar(200) NOT NULL,
  `extraname` varchar(10) NOT NULL default 'html',
  `index_show_number` smallint(5) unsigned NOT NULL default '10',
  `lastblog` smallint(5) unsigned NOT NULL default '10',
  `gb_show_num` smallint(5) unsigned NOT NULL default '10',
  `checkremark` tinyint(3) unsigned NOT NULL default '0',
  `makehtml` tinyint(3) unsigned NOT NULL default '1',
  `fullarticle` tinyint(3) unsigned NOT NULL default '0',
  `keep_page_way` varchar(20) NOT NULL default 'month',
  `archive_folder` varchar(100) NOT NULL default 'archives',
  `max_gb_char` int(10) unsigned NOT NULL default '6000',
  `date_format` varchar(100) NOT NULL default 'F j, Y, g:i a',
  `date_format_remark` varchar(100) NOT NULL default 'y-m-d H:m:s',
  `date_format_gb` varchar(100) NOT NULL default 'y-m-d H:m:s',
  `max_image_width` smallint(6) NOT NULL default '480',
  `banned_username` mediumtext NOT NULL,
  `banned_word` mediumtext NOT NULL,
  `banned_ip` mediumtext NOT NULL,
  `post_gb_time` int(10) unsigned NOT NULL default '10',
  `verify_code` tinyint(3) unsigned NOT NULL default '0',
  `search_time` int(10) unsigned NOT NULL default '30',
  `close_blog` tinyint(3) unsigned NOT NULL default '0',
  `show_viewcount` tinyint(3) unsigned NOT NULL default '1',
  `lastblog_cut_char` smallint(5) unsigned NOT NULL default '20',
  `servertimezone` varchar(10) NOT NULL default '+8',
  `clienttimezone` varchar(10) NOT NULL default '+8',
  `close_reason` mediumtext NOT NULL,
  `superadmin` varchar(255) NOT NULL,
  `private_code` varchar(200) NOT NULL,
  `allow_trackback` tinyint(3) unsigned NOT NULL default '1',
  `desc_remark` tinyint(3) unsigned NOT NULL default '0',
  `editor` varchar(100) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `obprefix_config` VALUES ('O-blog $version', 'longbillblue', '..........', '', 'html', '10', '10', '10', '0', '1', '0', 'month', 'archives', '6000', 'F j, Y, g:i a', 'y-m-d H:m:s', 'y-m-d H:m:s', '440', 'admin', '', '', '10', '0', '60', '0', '1', '20', '+8', '+8', '本Blog因不可抗拒因素暂时关闭！', 'longbill','','','','ubb_oblog');

DROP TABLE IF EXISTS `obprefix_guestbook`;

CREATE TABLE IF NOT EXISTS `obprefix_guestbook` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `date` varchar(20) NOT NULL,
  `username` varchar(100) NOT NULL default 'Guest',
  `email` varchar(100) NULL,
  `ip` varchar(20) NULL,
  `content` mediumtext NULL,
  `reply` mediumtext NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

INSERT INTO `obprefix_guestbook` VALUES ('2', '1141450387', 'longbill', 'longbill.cn@gmail.com', '169.254.205.12', '测试留言', '');

DROP TABLE IF EXISTS `obprefix_link`;

CREATE TABLE IF NOT EXISTS `obprefix_link` (
  `id` int(5) unsigned NOT NULL auto_increment,
  `sitename` varchar(200) NULL,
  `linkurl` varchar(100) NULL,
  `alt` mediumtext NULL,
  `showorder` int(10) unsigned NOT NULL default '0',
  `linkhidden` tinyint(3) unsigned NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

INSERT INTO `obprefix_link` VALUES ('1', 'O-blog', 'http://www.phpBlog.cn', 'O-blog', '1', '0');
INSERT INTO `obprefix_link` VALUES ('2', 'PHPBo', 'http://www.phpbo.com', 'O-blog $version  演示站点', '2', '0');
INSERT INTO `obprefix_link` VALUES ('3', 'Longbill\'s Blog', 'http://www.longbill.cn', 'O-blog $version 开发人员', '3', '0');

DROP TABLE IF EXISTS `obprefix_loginlog`;

CREATE TABLE IF NOT EXISTS `obprefix_loginlog` (
  `id` int(15) unsigned NOT NULL auto_increment,
  `username` varchar(100) NOT NULL,
  `date` varchar(20) NOT NULL,
  `ip` varchar(20) NULL,
  `result` tinyint(3) unsigned NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

INSERT INTO `obprefix_loginlog` VALUES ('1', 'longbill', '1186930480', '222.211.239.215', '1');

DROP TABLE IF EXISTS `obprefix_note`;

CREATE TABLE IF NOT EXISTS `obprefix_note` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `date` varchar(20) NOT NULL,
  `title` varchar(250) NULL,
  `content` mediumtext NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

INSERT INTO `obprefix_note` VALUES ('1', '1142917992', '测试', '测试测试');

DROP TABLE IF EXISTS `obprefix_remark`;

CREATE TABLE IF NOT EXISTS `obprefix_remark` (
  `id` int(15) unsigned NOT NULL auto_increment,
  `username` varchar(100) NOT NULL default 'Guest',
  `email` varchar(100) NULL,
  `content` mediumtext NULL,
  `date` varchar(20) NOT NULL,
  `ip` varchar(20) NULL,
  `inblog` int(10) unsigned NOT NULL default '1',
  `ischeck` tinyint(3) unsigned NOT NULL default '1',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `obprefix_trackback`;

CREATE TABLE IF NOT EXISTS `obprefix_trackback` (
  `trackbackid` int(10) unsigned NOT NULL auto_increment,
  `adddate` varchar(50) NOT NULL,
  `title` varchar(250) NULL,
  `url` varchar(250) NOT NULL,
  `excerpt` mediumtext NULL,
  `blogname` varchar(200) NULL,
  `inblog` int(10) unsigned NOT NULL default '1',
  PRIMARY KEY  (`trackbackid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

END;

	$sql = str_replace('obprefix_',$mysql_prefix,$sql);
	if(mysql_get_server_info() < '5.0')
	{
		$sql = str_replace('ENGINE=MyISAM','TYPE=MyISAM',$sql);
	}
	$sql = str_replace("\r","\n",$sql);
	$arr = explode(";\n",$sql);
	foreach($arr as $val)
	{
		$val = trim(str_replace("\n","",$val));
		if (!$val) continue;
		if(!mysql_query($val))
		{
			die("导入数据失败:<br>query:<br>".$val);
		}
	}
	Return true;
}

function writeAdmin() {
	global $s_add,$s_name,$s_user,$s_pass,$s_prefix;
	$fp = fopen("admin/mysql.php","w") or die("无法打开文件");
	$write_data = "<?php"
				."\n"
				."//mysql.php"
				."\n\n"
				."/* Mysql Server */\n"
				."\$mysql_add\t\t\t= "
				."'".$s_add."';"
				."\n\n"
				."/* Mysql User */\n"
				."\$mysql_user\t\t\t= "
				."'".$s_user."';"
				."\n\n"
				."/* Mysql Password */\n"
				."\$mysql_pass\t\t\t= "
				."'".$s_pass."';"
				."\n\n"
				."/* Mysql Database Name */\n"
				."\$mysql_dbname\t\t= "
				."'".$s_name."';"
				."\n\n"
				."/* Mysql Database Table Prefix */\n"
				."\$mysql_prefix\t\t= "
				."'".$s_prefix."';"
				."\n\n?>";
	if(!fwrite($fp, $write_data)) {
		die("文件写入失败！");
	}
}

function updateAdmin() {
	global $admin_user,$admin_nickname,$admin_pass,$blogurl,$mysql_prefix;
	$admin_pass = md5($admin_pass);
	$adminsql[] = "UPDATE `".$mysql_prefix."admin` SET username= '".$admin_user."',nickname= '".$admin_nickname."', password= '".$admin_pass."'";
	$adminsql[] = "UPDATE `".$mysql_prefix."config` SET blogurl= '".$blogurl."'";
	$adminsql[] = "UPDATE`".$mysql_prefix."config` SET superadmin='".$admin_user."'";
	foreach($adminsql as $key=>$val) {
		if(!mysql_query($val)) {
			die("设定管理员出错！");
		}
	}
	echo "设定管理员 …… 成功";
}

if(isset($_GET['do']) && $_GET['do'] == 'action') {
	echo $header;
	echo "<b>检测文件目录是否可写：</b>";
	echo "<hr size=1>";

	set_writeable("./cache");
	set_writeable("./archives");
	set_writeable("./uploadfiles");
	set_writeable("./bak");
	set_writeable("./admin/mysql.php");
	set_writeable("./");
	
	$s_add			= trim($_POST['s_add']);
	$s_name			= trim($_POST['s_name']);
	$s_user			= trim($_POST['s_user']);
	$s_pass			= trim($_POST['s_pass']);
	$s_prefix		= trim($_POST['s_prefix']);
	$blogurl		= trim($_POST['blogurl']);
	$admin_user		= trim($_POST['admin_user']);
	$admin_nickname	= trim($_POST['admin_nickname']);
	$admin_pass		= trim($_POST['admin_pass']);
	$mysql_prefix = $s_prefix;

	echo "<hr size=1>";
	$conn = @mysql_connect($s_add,$s_user,$s_pass) or die("<br><b><font color=red>Could not connect: " . mysql_error()."</font></b>");
	$charset = "utf8";
	if(mysql_get_server_info() > '4.1' && $charset)
	{
		mysql_query("SET character_set_connection=$charset, character_set_results=$charset, character_set_client=binary");
	}
	if(mysql_get_server_info() > '5.0')
	{
		mysql_query("SET sql_mode=''");
	}

	if(@mysql_query("CREATE DATABASE ".$s_name)) {
		echo "数据库建立成功 …… 成功<br>";
	}
	mysql_select_db($s_name) or die("<br><b><font color=red>Can not select database!</font></b>");
	writeAdmin();
	echo "建立数据表 …… ";
	if(querySQL()) {
		echo "成功";
	}
	echo "<hr size=1>";
	updateAdmin();

	echo "<hr size=1>";
	die("恭喜您！安装成功！<br>现在请删除 <b>install.php</b> ,您就可以使用了！".$footer);
	echo $footer;
}

?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>O-BLOG <?php echo $version;?> 安装程序</title>
<style type="text/css">
<!--
TABLE {
	font-family: Georgia, "Times New Roman", Times, serif;
	font-size: 14px;
	color: #000000;
}
.header {
	font-family: Georgia, "Times New Roman", Times, serif;
	font-size: 18px;
	font-weight: bold;
	color: #FFFFFF;
	background-color: #B50000;
}
-->
</style>
<style type="text/css">
<!--
INPUT {
	font-family: Georgia, "Times New Roman", Times, serif;
	font-size: 14px;
	width: 260px;
	background-color: #B50000;
	color: #FFFFFF;
	font-weight: bold;
	height: 28px;
}
.itemtitle {
	font-size: 18px;
}
-->
</style>
</head>

<body bgcolor="#F3F3F3">
<form name="form1" method="post" action="?do=action">
  <table width="650" border="0" align="center" cellpadding="10" cellspacing="1" bgcolor="#CCCCCC">
    <tr valign="top" bgcolor="#666666"> 
      <td height="80" colspan="3" bgcolor="#F3F3F3" class="header"><strong>O-BLOG <?php echo $version;?> 安装程序</strong>&nbsp;</td>
    </tr>
    <tr> 
      <td colspan="3" bgcolor="#FFFFFF">
            <table width="100%" border="0" cellspacing="0" cellpadding="4">
          <tr class="itemtitle"> 
            <td width="36%"><strong>一些说明</strong> </td>
            <td width="64%">&nbsp;</td>
          </tr>
          <tr> 
            <td colspan="2">欢迎使用 O-BLOG，这是一个免费的 BLOG 程序，您可以随意修改和传播。<br>
              如果您在使用中遇到问题，发信给我：<a href="mailto:shirui@gmail.com">mailto:shirui@gmail.com</a></td>
          </tr>
          <tr> 
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
          <tr class="itemtitle"> 
            <td><strong>设置文件夹权限</strong></td>
            <td>&nbsp;</td>
          </tr>
          <tr> 
            <td colspan="2">在linux系统下运行本安装程序时，请用ftp工具连接到服务器，将下列文件的属性设置为可写(777)。</td>
          </tr>
          <tr> 
            <td colspan="2"><ul>
                <li>　./cache</li>
                <li>　./archives</li>
                <li>　./admin/mysql.php</li>
                <li>　./uploadfiles</li>
                <li>　./bak</li>
				<li>　./templates 及此文件夹下的所有文件(夹)</li>
				<li>　./</li>
              </ul></td>
          </tr>
          <tr> 
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
          <tr class="itemtitle"> 
            <td><strong>数据库设置</strong></td>
            <td>&nbsp;</td>
          </tr>
          <tr> 
            <td>数据库地址：</td>
            <td><input name="s_add" type="text" id="s_add2" value="localhost"></td>
          </tr>
          <tr> 
            <td>数据库名称：</td>
            <td><input name="s_name" type="text" id="s_name2" value=""></td>
          </tr>
          <tr> 
            <td height="26">数据库用户名：</td>
            <td><input name="s_user" type="text" id="s_user3" value="root"></td>
          </tr>
          <tr> 
            <td>数据库密码：</td>
            <td><input name="s_pass" type="password" id="s_pass3"></td>
          </tr>
          <tr>
            <td>数据表前缀：</td>
            <td><input name="s_prefix" type="text" id="s_pass4" value="ob3_"></td>
          </tr>
          <tr> 
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
          <tr class="itemtitle"> 
            <td><strong>参数设置</strong></td>
            <td>&nbsp;</td>
          </tr>
          <tr> 
            <td>BLOG的URL地址：</td>
            <td><input name="blogurl" type="text" id="blogurl2" value="<?php echo "http://".$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']).'/'; ?>"></td>
          </tr>
          <tr> 
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
          <tr class="itemtitle"> 
            <td><strong>管理员设置</strong></td>
            <td>&nbsp;</td>
          </tr>
          <tr> 
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
          <tr> 
            <td>用户名：</td>
            <td><input name="admin_user" type="text" id="admin_user2"></td>
          </tr>
		  <tr> 
            <td>昵称：</td>
            <td><input name="admin_nickname" type="text" id="admin_nickname2"></td>
          </tr>
          <tr> 
            <td>密码：</td>
            <td><input name="admin_pass" type="password" id="admin_pass2"></td>
          </tr>
          <tr> 
            <td>重复密码：</td>
            <td><input name="admin_pass" type="password" id="admin_pass22" onblur="if (this.value != document.getElementById('admin_pass2').value) {alert('两次输入密码不一致!');return false;}" /></td>
          </tr>
          <tr> 
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
          <tr align="center"> 
            <td colspan="2"> <input type="submit" name="Submit" value="填好了！开始安装！"> 
              <input type="reset" name="Submit2" value="重置"> </td>
          </tr>
        </table>
          </li>
        </ul>
        </td>
    </tr>
  </table>
</form>
</body>
</html>